Poisson.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Implements a poisson 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_POISSON_H
33 #define SHARK_RNG_POISSON_H
34 
35 #include <shark/Rng/Rng.h>
36 
37 #include <boost/random.hpp>
38 #include <boost/random/poisson_distribution.hpp>
39 
40 namespace shark {
41 
42  /**
43  * \brief Implements a Poisson distribution with parameter mean.
44  */
45  template<typename RngType = shark::DefaultRngType>
46  class Poisson : public boost::variate_generator< RngType*,boost::poisson_distribution<> > {
47  private:
48  typedef boost::variate_generator< RngType*,boost::poisson_distribution<> > Base;
49 
50  public:
51 
52  /**
53  * \brief C'tor taking parameter mean as argument. Associates the
54  * distribution with supplied RNG.
55  *
56  */
57  Poisson( RngType & rng, double mean = 0.01 )
58  :Base(&rng,boost::poisson_distribution<>(mean))
59  {}
60 
61  /**
62  * \brief Injects the default sampling operator.
63  */
64  using Base::operator();
65 
66  /**
67  * \brief Adjusts the parameter mean and samples the distribution.
68  * \param [in] mean The new mean.
69  */
70  double operator()(double mean)
71  {
72  boost::poisson_distribution<> dist(mean);
73  return dist(Base::engine());
74  }
75 
76  /**
77  * \brief Accesses the parameter mean.
78  */
79  double mean()const
80  {
81  return Base::distribution().mean();
82  }
83 
84  /**
85  * \brief Adjusts the parameter mean.
86  */
87  void mean(double newMean)
88  {
89  Base::distribution()=boost::poisson_distribution<>(newMean);
90  }
91 
92  /**
93  * \brief Calculates the probability of x >= 0.
94  */
95  double p(double x)const
96  {
97  if(x >= 0.0)
98  return std::pow(mean(), x) * std::exp(-mean()) / boost::math::factorial<double>(std::size_t(x));
99  else
100  return 0;
101  }
102 
103  };
104 }
105 #endif