]> git.donarmstrong.com Git - rsem.git/blob - boost/random/lognormal_distribution.hpp
RSEM Source Codes
[rsem.git] / boost / random / lognormal_distribution.hpp
1 /* boost random/lognormal_distribution.hpp header file
2  *
3  * Copyright Jens Maurer 2000-2001
4  * Distributed under the Boost Software License, Version 1.0. (See
5  * accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  * See http://www.boost.org for most recent version including documentation.
9  *
10  * $Id: lognormal_distribution.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
11  *
12  * Revision history
13  *  2001-02-18  moved to individual header files
14  */
15
16 #ifndef BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
18
19 #include <boost/config/no_tr1/cmath.hpp>      // std::exp, std::sqrt
20 #include <cassert>
21 #include <iostream>
22 #include <boost/limits.hpp>
23 #include <boost/static_assert.hpp>
24 #include <boost/random/detail/config.hpp>
25 #include <boost/random/normal_distribution.hpp>
26
27 #ifdef BOOST_NO_STDC_NAMESPACE
28 namespace std {
29   using ::log;
30   using ::sqrt;
31 }
32 #endif
33
34 namespace boost {
35
36 #if defined(__GNUC__) && (__GNUC__ < 3)
37 // Special gcc workaround: gcc 2.95.x ignores using-declarations
38 // in template classes (confirmed by gcc author Martin v. Loewis)
39   using std::sqrt;
40   using std::exp;
41 #endif
42
43 /**
44  * Instantiations of class template lognormal_distribution model a
45  * \random_distribution. Such a distribution produces random numbers
46  * with \f$p(x) = \frac{1}{x \sigma_N \sqrt{2\pi}} e^{\frac{-\left(\log(x)-\mu_N\right)^2}{2\sigma_N^2}}\f$
47  * for x > 0, where \f$\mu_N = \log\left(\frac{\mu^2}{\sqrt{\sigma^2 + \mu^2}}\right)\f$ and
48  * \f$\sigma_N = \sqrt{\log\left(1 + \frac{\sigma^2}{\mu^2}\right)}\f$.
49  */
50 template<class RealType = double>
51 class lognormal_distribution
52 {
53 public:
54   typedef typename normal_distribution<RealType>::input_type input_type;
55   typedef RealType result_type;
56
57 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
58     BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
59 #endif
60
61   /**
62    * Constructs a lognormal_distribution. @c mean and @c sigma are the
63    * mean and standard deviation of the lognormal distribution.
64    */
65   explicit lognormal_distribution(result_type mean_arg = result_type(1),
66                                   result_type sigma_arg = result_type(1))
67     : _mean(mean_arg), _sigma(sigma_arg)
68   { 
69     assert(_mean > result_type(0));
70     init();
71   }
72
73   // compiler-generated copy ctor and assignment operator are fine
74
75   RealType mean() const { return _mean; }
76   RealType sigma() const { return _sigma; }
77   void reset() { _normal.reset(); }
78
79   template<class Engine>
80   result_type operator()(Engine& eng)
81   {
82 #ifndef BOOST_NO_STDC_NAMESPACE
83     // allow for Koenig lookup
84     using std::exp;
85 #endif
86     return exp(_normal(eng) * _nsigma + _nmean);
87   }
88
89 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
90   template<class CharT, class Traits>
91   friend std::basic_ostream<CharT,Traits>&
92   operator<<(std::basic_ostream<CharT,Traits>& os, const lognormal_distribution& ld)
93   {
94     os << ld._normal << " " << ld._mean << " " << ld._sigma;
95     return os;
96   }
97
98   template<class CharT, class Traits>
99   friend std::basic_istream<CharT,Traits>&
100   operator>>(std::basic_istream<CharT,Traits>& is, lognormal_distribution& ld)
101   {
102     is >> std::ws >> ld._normal >> std::ws >> ld._mean >> std::ws >> ld._sigma;
103     ld.init();
104     return is;
105   }
106 #endif
107
108 private:
109
110   /// \cond hide_private_members
111   void init()
112   {
113 #ifndef BOOST_NO_STDC_NAMESPACE
114     // allow for Koenig lookup
115     using std::exp; using std::log; using std::sqrt;
116 #endif
117     _nmean = log(_mean*_mean/sqrt(_sigma*_sigma + _mean*_mean));
118     _nsigma = sqrt(log(_sigma*_sigma/_mean/_mean+result_type(1)));
119   }
120   /// \endcond
121
122   RealType _mean, _sigma;
123   RealType _nmean, _nsigma;
124   normal_distribution<result_type> _normal;
125 };
126
127 } // namespace boost
128
129 #endif // BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP