annotate DEPENDENCIES/generic/include/boost/random/lognormal_distribution.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 /* boost random/lognormal_distribution.hpp header file
Chris@16 2 *
Chris@16 3 * Copyright Jens Maurer 2000-2001
Chris@16 4 * Copyright Steven Watanabe 2011
Chris@16 5 * Distributed under the Boost Software License, Version 1.0. (See
Chris@16 6 * accompanying file LICENSE_1_0.txt or copy at
Chris@16 7 * http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8 *
Chris@16 9 * See http://www.boost.org for most recent version including documentation.
Chris@16 10 *
Chris@101 11 * $Id$
Chris@16 12 *
Chris@16 13 * Revision history
Chris@16 14 * 2001-02-18 moved to individual header files
Chris@16 15 */
Chris@16 16
Chris@16 17 #ifndef BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
Chris@16 18 #define BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
Chris@16 19
Chris@16 20 #include <boost/config/no_tr1/cmath.hpp> // std::exp, std::sqrt
Chris@16 21 #include <cassert>
Chris@16 22 #include <iosfwd>
Chris@16 23 #include <istream>
Chris@16 24 #include <boost/limits.hpp>
Chris@16 25 #include <boost/random/detail/config.hpp>
Chris@16 26 #include <boost/random/detail/operators.hpp>
Chris@16 27 #include <boost/random/normal_distribution.hpp>
Chris@16 28
Chris@16 29 namespace boost {
Chris@16 30 namespace random {
Chris@16 31
Chris@16 32 /**
Chris@16 33 * Instantiations of class template lognormal_distribution model a
Chris@16 34 * \random_distribution. Such a distribution produces random numbers
Chris@16 35 * with \f$\displaystyle p(x) = \frac{1}{x s \sqrt{2\pi}} e^{\frac{-\left(\log(x)-m\right)^2}{2s^2}}\f$
Chris@16 36 * for x > 0.
Chris@16 37 *
Chris@16 38 * @xmlwarning
Chris@16 39 * This distribution has been updated to match the C++ standard.
Chris@16 40 * Its behavior has changed from the original
Chris@16 41 * boost::lognormal_distribution. A backwards compatible
Chris@16 42 * version is provided in namespace boost.
Chris@16 43 * @endxmlwarning
Chris@16 44 */
Chris@16 45 template<class RealType = double>
Chris@16 46 class lognormal_distribution
Chris@16 47 {
Chris@16 48 public:
Chris@16 49 typedef typename normal_distribution<RealType>::input_type input_type;
Chris@16 50 typedef RealType result_type;
Chris@16 51
Chris@16 52 class param_type
Chris@16 53 {
Chris@16 54 public:
Chris@16 55
Chris@16 56 typedef lognormal_distribution distribution_type;
Chris@16 57
Chris@16 58 /** Constructs the parameters of a lognormal_distribution. */
Chris@16 59 explicit param_type(RealType m_arg = RealType(0.0),
Chris@16 60 RealType s_arg = RealType(1.0))
Chris@16 61 : _m(m_arg), _s(s_arg) {}
Chris@16 62
Chris@16 63 /** Returns the "m" parameter of the distribution. */
Chris@16 64 RealType m() const { return _m; }
Chris@16 65
Chris@16 66 /** Returns the "s" parameter of the distribution. */
Chris@16 67 RealType s() const { return _s; }
Chris@16 68
Chris@16 69 /** Writes the parameters to a std::ostream. */
Chris@16 70 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
Chris@16 71 {
Chris@16 72 os << parm._m << " " << parm._s;
Chris@16 73 return os;
Chris@16 74 }
Chris@16 75
Chris@16 76 /** Reads the parameters from a std::istream. */
Chris@16 77 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
Chris@16 78 {
Chris@16 79 is >> parm._m >> std::ws >> parm._s;
Chris@16 80 return is;
Chris@16 81 }
Chris@16 82
Chris@16 83 /** Returns true if the two sets of parameters are equal. */
Chris@16 84 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
Chris@16 85 { return lhs._m == rhs._m && lhs._s == rhs._s; }
Chris@16 86
Chris@16 87 /** Returns true if the two sets of parameters are different. */
Chris@16 88 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
Chris@16 89
Chris@16 90 private:
Chris@16 91 RealType _m;
Chris@16 92 RealType _s;
Chris@16 93 };
Chris@16 94
Chris@16 95 /**
Chris@16 96 * Constructs a lognormal_distribution. @c m and @c s are the
Chris@16 97 * parameters of the distribution.
Chris@16 98 */
Chris@16 99 explicit lognormal_distribution(RealType m_arg = RealType(0.0),
Chris@16 100 RealType s_arg = RealType(1.0))
Chris@16 101 : _normal(m_arg, s_arg) {}
Chris@16 102
Chris@16 103 /**
Chris@16 104 * Constructs a lognormal_distribution from its parameters.
Chris@16 105 */
Chris@16 106 explicit lognormal_distribution(const param_type& parm)
Chris@16 107 : _normal(parm.m(), parm.s()) {}
Chris@16 108
Chris@16 109 // compiler-generated copy ctor and assignment operator are fine
Chris@16 110
Chris@16 111 /** Returns the m parameter of the distribution. */
Chris@16 112 RealType m() const { return _normal.mean(); }
Chris@16 113 /** Returns the s parameter of the distribution. */
Chris@16 114 RealType s() const { return _normal.sigma(); }
Chris@16 115
Chris@16 116 /** Returns the smallest value that the distribution can produce. */
Chris@16 117 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
Chris@16 118 { return RealType(0); }
Chris@16 119 /** Returns the largest value that the distribution can produce. */
Chris@16 120 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
Chris@16 121 { return (std::numeric_limits<RealType>::infinity)(); }
Chris@16 122
Chris@16 123 /** Returns the parameters of the distribution. */
Chris@16 124 param_type param() const { return param_type(m(), s()); }
Chris@16 125 /** Sets the parameters of the distribution. */
Chris@16 126 void param(const param_type& parm)
Chris@16 127 {
Chris@16 128 typedef normal_distribution<RealType> normal_type;
Chris@16 129 typename normal_type::param_type normal_param(parm.m(), parm.s());
Chris@16 130 _normal.param(normal_param);
Chris@16 131 }
Chris@16 132
Chris@16 133 /**
Chris@16 134 * Effects: Subsequent uses of the distribution do not depend
Chris@16 135 * on values produced by any engine prior to invoking reset.
Chris@16 136 */
Chris@16 137 void reset() { _normal.reset(); }
Chris@16 138
Chris@16 139 /**
Chris@16 140 * Returns a random variate distributed according to the
Chris@16 141 * lognormal distribution.
Chris@16 142 */
Chris@16 143 template<class Engine>
Chris@16 144 result_type operator()(Engine& eng)
Chris@16 145 {
Chris@16 146 using std::exp;
Chris@16 147 return exp(_normal(eng));
Chris@16 148 }
Chris@16 149
Chris@16 150 /**
Chris@16 151 * Returns a random variate distributed according to the
Chris@16 152 * lognormal distribution with parameters specified by param.
Chris@16 153 */
Chris@16 154 template<class Engine>
Chris@16 155 result_type operator()(Engine& eng, const param_type& parm)
Chris@16 156 { return lognormal_distribution(parm)(eng); }
Chris@16 157
Chris@16 158 /** Writes the distribution to a @c std::ostream. */
Chris@16 159 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, lognormal_distribution, ld)
Chris@16 160 {
Chris@16 161 os << ld._normal;
Chris@16 162 return os;
Chris@16 163 }
Chris@16 164
Chris@16 165 /** Reads the distribution from a @c std::istream. */
Chris@16 166 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, lognormal_distribution, ld)
Chris@16 167 {
Chris@16 168 is >> ld._normal;
Chris@16 169 return is;
Chris@16 170 }
Chris@16 171
Chris@16 172 /**
Chris@16 173 * Returns true if the two distributions will produce identical
Chris@16 174 * sequences of values given equal generators.
Chris@16 175 */
Chris@16 176 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(lognormal_distribution, lhs, rhs)
Chris@16 177 { return lhs._normal == rhs._normal; }
Chris@16 178
Chris@16 179 /**
Chris@16 180 * Returns true if the two distributions may produce different
Chris@16 181 * sequences of values given equal generators.
Chris@16 182 */
Chris@16 183 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(lognormal_distribution)
Chris@16 184
Chris@16 185 private:
Chris@16 186 normal_distribution<result_type> _normal;
Chris@16 187 };
Chris@16 188
Chris@16 189 } // namespace random
Chris@16 190
Chris@16 191 /// \cond show_deprecated
Chris@16 192
Chris@16 193 /**
Chris@16 194 * Provided for backwards compatibility. This class is
Chris@16 195 * deprecated. It provides the old behavior of lognormal_distribution with
Chris@16 196 * \f$\displaystyle p(x) = \frac{1}{x \sigma_N \sqrt{2\pi}} e^{\frac{-\left(\log(x)-\mu_N\right)^2}{2\sigma_N^2}}\f$
Chris@16 197 * for x > 0, where \f$\displaystyle \mu_N = \log\left(\frac{\mu^2}{\sqrt{\sigma^2 + \mu^2}}\right)\f$ and
Chris@16 198 * \f$\displaystyle \sigma_N = \sqrt{\log\left(1 + \frac{\sigma^2}{\mu^2}\right)}\f$.
Chris@16 199 */
Chris@16 200 template<class RealType = double>
Chris@16 201 class lognormal_distribution
Chris@16 202 {
Chris@16 203 public:
Chris@16 204 typedef typename normal_distribution<RealType>::input_type input_type;
Chris@16 205 typedef RealType result_type;
Chris@16 206
Chris@16 207 lognormal_distribution(RealType mean_arg = RealType(1.0),
Chris@16 208 RealType sigma_arg = RealType(1.0))
Chris@16 209 : _mean(mean_arg), _sigma(sigma_arg)
Chris@16 210 {
Chris@16 211 init();
Chris@16 212 }
Chris@16 213 RealType mean() const { return _mean; }
Chris@16 214 RealType sigma() const { return _sigma; }
Chris@16 215 void reset() { _normal.reset(); }
Chris@16 216 template<class Engine>
Chris@16 217 RealType operator()(Engine& eng)
Chris@16 218 {
Chris@16 219 using std::exp;
Chris@16 220 return exp(_normal(eng) * _nsigma + _nmean);
Chris@16 221 }
Chris@16 222 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, lognormal_distribution, ld)
Chris@16 223 {
Chris@16 224 os << ld._normal << " " << ld._mean << " " << ld._sigma;
Chris@16 225 return os;
Chris@16 226 }
Chris@16 227 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, lognormal_distribution, ld)
Chris@16 228 {
Chris@16 229 is >> ld._normal >> std::ws >> ld._mean >> std::ws >> ld._sigma;
Chris@16 230 ld.init();
Chris@16 231 return is;
Chris@16 232 }
Chris@16 233 private:
Chris@16 234 /// \cond show_private
Chris@16 235 void init()
Chris@16 236 {
Chris@16 237 using std::log;
Chris@16 238 using std::sqrt;
Chris@16 239 _nmean = log(_mean*_mean/sqrt(_sigma*_sigma + _mean*_mean));
Chris@16 240 _nsigma = sqrt(log(_sigma*_sigma/_mean/_mean+result_type(1)));
Chris@16 241 }
Chris@16 242 RealType _mean;
Chris@16 243 RealType _sigma;
Chris@16 244 RealType _nmean;
Chris@16 245 RealType _nsigma;
Chris@16 246 normal_distribution<RealType> _normal;
Chris@16 247 /// \endcond
Chris@16 248 };
Chris@16 249
Chris@16 250 /// \endcond
Chris@16 251
Chris@16 252 } // namespace boost
Chris@16 253
Chris@16 254 #endif // BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP