1 /* boost random/lognormal_distribution.hpp header file
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)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: lognormal_distribution.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
19 #include <boost/config/no_tr1/cmath.hpp> // std::exp, std::sqrt
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>
27 #ifdef BOOST_NO_STDC_NAMESPACE
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)
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$.
50 template<class RealType = double>
51 class lognormal_distribution
54 typedef typename normal_distribution<RealType>::input_type input_type;
55 typedef RealType result_type;
57 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
58 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
62 * Constructs a lognormal_distribution. @c mean and @c sigma are the
63 * mean and standard deviation of the lognormal distribution.
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)
69 assert(_mean > result_type(0));
73 // compiler-generated copy ctor and assignment operator are fine
75 RealType mean() const { return _mean; }
76 RealType sigma() const { return _sigma; }
77 void reset() { _normal.reset(); }
79 template<class Engine>
80 result_type operator()(Engine& eng)
82 #ifndef BOOST_NO_STDC_NAMESPACE
83 // allow for Koenig lookup
86 return exp(_normal(eng) * _nsigma + _nmean);
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)
94 os << ld._normal << " " << ld._mean << " " << ld._sigma;
98 template<class CharT, class Traits>
99 friend std::basic_istream<CharT,Traits>&
100 operator>>(std::basic_istream<CharT,Traits>& is, lognormal_distribution& ld)
102 is >> std::ws >> ld._normal >> std::ws >> ld._mean >> std::ws >> ld._sigma;
110 /// \cond hide_private_members
113 #ifndef BOOST_NO_STDC_NAMESPACE
114 // allow for Koenig lookup
115 using std::exp; using std::log; using std::sqrt;
117 _nmean = log(_mean*_mean/sqrt(_sigma*_sigma + _mean*_mean));
118 _nsigma = sqrt(log(_sigma*_sigma/_mean/_mean+result_type(1)));
122 RealType _mean, _sigma;
123 RealType _nmean, _nsigma;
124 normal_distribution<result_type> _normal;
129 #endif // BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP