Bernoulli.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Implements a Bernoulli distribution.
5  *
6  *
7  *
8  * \author O. Krause
9  * \date 2010-01-01
10  *
11  *
12  * \par Copyright 1995-2017 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://shark-ml.org/>
17  *
18  * Shark is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Lesser General Public License as published
20  * by the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * Shark is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 #ifndef SHARK_RNG_BERNOULLI_H
33 #define SHARK_RNG_BERNOULLI_H
34 
35 #include <shark/Rng/Rng.h>
36 
37 #include <boost/random.hpp>
38 
39 #include <cmath>
40 
41 namespace shark{
42 
43  /*!
44  * \brief This class simulates a "Bernoulli trial", which
45  * is like a coin toss.
46  *
47  * This class is a thin wrapper for the boost::bernoulli_distribution class.
48  * A bernoulli distribution simulates a generalized coin toss.
49  * A probability for the occurrence of the event (coin side)
50  * is defined. When using the equal probability of "0.5" for the
51  * occurrence and non-occurrence of the event (coin side), then the
52  * event (coin) is named "normal", otherwise it is named "abnormal".
53  *
54  * \author O.Krause
55  * \date 2010-01-01
56  *
57  * \par Changes:
58  * none
59  *
60  * \par Status:
61  * testing
62  *
63  */
64  template<typename RngType = shark::DefaultRngType>
65  class Bernoulli : public boost::variate_generator< RngType*,boost::bernoulli_distribution<> >
66  {
67  private:
68  typedef boost::variate_generator<RngType*,boost::bernoulli_distribution<> > Base;
69  public:
70  //! Creates a new Bernoulli random generator using the global generator from instance and
71  //! sets the probability for the occurrence of the event
72  //! to "prob".
73  /*
74  Bernoulli(double prob=0.5)
75  :Base(&Rng::globalRng,boost::bernoulli_distribution<>(prob))
76  {}*/
77 
78  //! Creates a new Bernoulli random generator instance by
79  //! using the pseudo random number generator "rng" for the determination
80  //! of random values and sets the probability for the occurrence
81  //! of the event to "prob".
82  Bernoulli( RngType & rng, double prob = 0.5 )
83  :Base(&rng,boost::bernoulli_distribution<>(prob))
84  {}
85  /*!
86  * \brief Returns a Bernoulli random number, i.e. a "true" or "false"
87  * marking the occurrence and non-occurrence of an event respectively,
88  * using the preset propability
89  *
90  * \return a bernoulli distributed number
91  */
92  using Base::operator();
93 
94  /*!
95  * \brief Returns a Bernoulli random number, i.e. a "true" or "false"
96  * marking the occurrence and non-occurrence of an event respectively,
97  * when the probability for the occurrence is "p".
98  *
99  * \return a bernoulli distributed number
100  */
101  bool operator()(double p)
102  {
103  boost::bernoulli_distribution<> dist(p);
104  return dist(Base::engine());
105  }
106  /*!
107  * \brief Returns the probability for the occurrence of an event.
108  *
109  * \return the probability for the occurrence of an event
110  */
111  double prob()const
112  {
113  return Base::distribution().p();
114  }
115  /*!
116  * \brief Sets the probability for the occurrence of an event to "newP".
117  *
118  * \param newP the new probability for the occurrence of an event
119  * \return none
120  */
121  void prob(double newP)
122  {
123  Base::distribution()=boost::bernoulli_distribution<>(newP);
124  }
125 
126  //! Returns the probability \f$p\f$ for the occurrence of an
127  //! event ("x = true") or \f$1 - p\f$ for the non-occurrence
128  //! ("x = false").
129  double p(bool x) const
130  {
131  return x ? prob() : 1 - prob();
132  }
133 
134  };
135 
136  ///\brief Flips a coin with probability of heads being pHeads by drawing random numbers from rng.
137  template<class RngType>
138  bool coinToss(RngType& rng, double pHeads){
139  Bernoulli<RngType> dist(rng, pHeads);
140  return dist();
141  }
142 }
143 #endif