normal_distribution.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* boost random/normal_distribution.hpp header file
  2. *
  3. * Copyright Jens Maurer 2000-2001
  4. * Copyright Steven Watanabe 2010-2011
  5. * Distributed under the Boost Software License, Version 1.0. (See
  6. * accompanying file LICENSE_1_0.txt or copy at
  7. * http://www.boost.org/LICENSE_1_0.txt)
  8. *
  9. * See http://www.boost.org for most recent version including documentation.
  10. *
  11. * $Id: normal_distribution.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
  12. *
  13. * Revision history
  14. * 2001-02-18 moved to individual header files
  15. */
  16. #ifndef BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
  17. #define BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
  18. #include <boost/config/no_tr1/cmath.hpp>
  19. #include <istream>
  20. #include <iosfwd>
  21. #include <boost/assert.hpp>
  22. #include <boost/limits.hpp>
  23. #include <boost/static_assert.hpp>
  24. #include <boost/random/detail/config.hpp>
  25. #include <boost/random/detail/operators.hpp>
  26. #include <boost/random/uniform_01.hpp>
  27. namespace boost {
  28. namespace random {
  29. // deterministic Box-Muller method, uses trigonometric functions
  30. /**
  31. * Instantiations of class template normal_distribution model a
  32. * \random_distribution. Such a distribution produces random numbers
  33. * @c x distributed with probability density function
  34. * \f$\displaystyle p(x) =
  35. * \frac{1}{\sqrt{2\pi\sigma}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}
  36. * \f$,
  37. * where mean and sigma are the parameters of the distribution.
  38. */
  39. template<class RealType = double>
  40. class normal_distribution
  41. {
  42. public:
  43. typedef RealType input_type;
  44. typedef RealType result_type;
  45. class param_type {
  46. public:
  47. typedef normal_distribution distribution_type;
  48. /**
  49. * Constructs a @c param_type with a given mean and
  50. * standard deviation.
  51. *
  52. * Requires: sigma >= 0
  53. */
  54. explicit param_type(RealType mean_arg = RealType(0.0),
  55. RealType sigma_arg = RealType(1.0))
  56. : _mean(mean_arg),
  57. _sigma(sigma_arg)
  58. {}
  59. /** Returns the mean of the distribution. */
  60. RealType mean() const { return _mean; }
  61. /** Returns the standand deviation of the distribution. */
  62. RealType sigma() const { return _sigma; }
  63. /** Writes a @c param_type to a @c std::ostream. */
  64. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
  65. { os << parm._mean << " " << parm._sigma ; return os; }
  66. /** Reads a @c param_type from a @c std::istream. */
  67. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
  68. { is >> parm._mean >> std::ws >> parm._sigma; return is; }
  69. /** Returns true if the two sets of parameters are the same. */
  70. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
  71. { return lhs._mean == rhs._mean && lhs._sigma == rhs._sigma; }
  72. /** Returns true if the two sets of parameters are the different. */
  73. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
  74. private:
  75. RealType _mean;
  76. RealType _sigma;
  77. };
  78. /**
  79. * Constructs a @c normal_distribution object. @c mean and @c sigma are
  80. * the parameters for the distribution.
  81. *
  82. * Requires: sigma >= 0
  83. */
  84. explicit normal_distribution(const RealType& mean_arg = RealType(0.0),
  85. const RealType& sigma_arg = RealType(1.0))
  86. : _mean(mean_arg), _sigma(sigma_arg),
  87. _r1(0), _r2(0), _cached_rho(0), _valid(false)
  88. {
  89. BOOST_ASSERT(_sigma >= RealType(0));
  90. }
  91. /**
  92. * Constructs a @c normal_distribution object from its parameters.
  93. */
  94. explicit normal_distribution(const param_type& parm)
  95. : _mean(parm.mean()), _sigma(parm.sigma()),
  96. _r1(0), _r2(0), _cached_rho(0), _valid(false)
  97. {}
  98. /** Returns the mean of the distribution. */
  99. RealType mean() const { return _mean; }
  100. /** Returns the standard deviation of the distribution. */
  101. RealType sigma() const { return _sigma; }
  102. /** Returns the smallest value that the distribution can produce. */
  103. RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
  104. { return -std::numeric_limits<RealType>::infinity(); }
  105. /** Returns the largest value that the distribution can produce. */
  106. RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
  107. { return std::numeric_limits<RealType>::infinity(); }
  108. /** Returns the parameters of the distribution. */
  109. param_type param() const { return param_type(_mean, _sigma); }
  110. /** Sets the parameters of the distribution. */
  111. void param(const param_type& parm)
  112. {
  113. _mean = parm.mean();
  114. _sigma = parm.sigma();
  115. _valid = false;
  116. }
  117. /**
  118. * Effects: Subsequent uses of the distribution do not depend
  119. * on values produced by any engine prior to invoking reset.
  120. */
  121. void reset() { _valid = false; }
  122. /** Returns a normal variate. */
  123. template<class Engine>
  124. result_type operator()(Engine& eng)
  125. {
  126. using std::sqrt;
  127. using std::log;
  128. using std::sin;
  129. using std::cos;
  130. if(!_valid) {
  131. _r1 = boost::uniform_01<RealType>()(eng);
  132. _r2 = boost::uniform_01<RealType>()(eng);
  133. _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2));
  134. _valid = true;
  135. } else {
  136. _valid = false;
  137. }
  138. // Can we have a boost::mathconst please?
  139. const result_type pi = result_type(3.14159265358979323846);
  140. return _cached_rho * (_valid ?
  141. cos(result_type(2)*pi*_r1) :
  142. sin(result_type(2)*pi*_r1))
  143. * _sigma + _mean;
  144. }
  145. /** Returns a normal variate with parameters specified by @c param. */
  146. template<class URNG>
  147. result_type operator()(URNG& urng, const param_type& parm)
  148. {
  149. return normal_distribution(parm)(urng);
  150. }
  151. /** Writes a @c normal_distribution to a @c std::ostream. */
  152. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, normal_distribution, nd)
  153. {
  154. os << nd._mean << " " << nd._sigma << " "
  155. << nd._valid << " " << nd._cached_rho << " " << nd._r1;
  156. return os;
  157. }
  158. /** Reads a @c normal_distribution from a @c std::istream. */
  159. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, normal_distribution, nd)
  160. {
  161. is >> std::ws >> nd._mean >> std::ws >> nd._sigma
  162. >> std::ws >> nd._valid >> std::ws >> nd._cached_rho
  163. >> std::ws >> nd._r1;
  164. return is;
  165. }
  166. /**
  167. * Returns true if the two instances of @c normal_distribution will
  168. * return identical sequences of values given equal generators.
  169. */
  170. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(normal_distribution, lhs, rhs)
  171. {
  172. return lhs._mean == rhs._mean && lhs._sigma == rhs._sigma
  173. && lhs._valid == rhs._valid
  174. && (!lhs._valid || (lhs._r1 == rhs._r1 && lhs._r2 == rhs._r2));
  175. }
  176. /**
  177. * Returns true if the two instances of @c normal_distribution will
  178. * return different sequences of values given equal generators.
  179. */
  180. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(normal_distribution)
  181. private:
  182. RealType _mean, _sigma;
  183. RealType _r1, _r2, _cached_rho;
  184. bool _valid;
  185. };
  186. } // namespace random
  187. using random::normal_distribution;
  188. } // namespace boost
  189. #endif // BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP