NegExponential.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Implements the negative exponential 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_NEGEXPONENTIAL_H
33 #define SHARK_RNG_NEGEXPONENTIAL_H
34 
35 
36 
37 
38 #include <boost/random/exponential_distribution.hpp>
39 
40 #include <cmath>
41 
42 namespace shark{
43 
44  ///\brief Implements the Negative exponential distribution.
45  ///
46  /// It's propability distribution is defined as
47  /// \f[ p(x) = e^{-\lambda x}\f]
48  /// Instead of lambda, we define the exponential distribution using the mean
49  /// \f[ \mu = 1.0/\lambda \f]
50  template<typename RngType = shark::DefaultRngType>
51  class NegExponential:public boost::variate_generator<RngType*,boost::exponential_distribution<> >
52  {
53  private:
54  typedef boost::variate_generator<RngType*,boost::exponential_distribution<> > Base;
55  public:
56 
57  NegExponential( RngType & rng, double mean = 0 )
58  :Base(&rng,boost::exponential_distribution<>(1.0/mean))
59  {}
60 
61  using Base::operator();
62 
63  ///\brief Draws a random number from the negative exponential distribution with the mean defined in the argument
64  ///
65  ///\param mean: the mean of the distribution from which the number is drawn
66  double operator()(double mean)
67  {
68  boost::exponential_distribution<> dist(1.0/mean);
69  return dist(Base::engine());
70  }
71 
72  ///\brief Returns the mean of the negative exponential distribution
73  double mean()const
74  {
75  return 1.0/Base::distribution().lambda();
76  }
77  ///\brief Sets the mean of the negative exponential distribution
78  ///
79  ///\param newMean the new Mean value
80  void mean(double newMean)
81  {
82  Base::distribution()=boost::exponential_distribution<>(1.0/newMean);
83  }
84 
85  double p(double x)
86  {
87  return x >= 0 ? Base::distribution().lambda() * exp(- Base::distribution().lambda() * x) : 0.;
88  }
89 
90  };
91 }
92 #endif
93 
94 
95 
96 
97 
98