non_central_chi_squared_distribution.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* boost random/non_central_chi_squared_distribution.hpp header file
  2. *
  3. * Copyright Thijs van den Berg 2014
  4. *
  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$
  12. */
  13. #ifndef BOOST_RANDOM_NON_CENTRAL_CHI_SQUARED_DISTRIBUTION_HPP
  14. #define BOOST_RANDOM_NON_CENTRAL_CHI_SQUARED_DISTRIBUTION_HPP
  15. #include <boost/config/no_tr1/cmath.hpp>
  16. #include <iosfwd>
  17. #include <istream>
  18. #include <boost/limits.hpp>
  19. #include <boost/random/detail/config.hpp>
  20. #include <boost/random/detail/operators.hpp>
  21. #include <boost/random/uniform_real_distribution.hpp>
  22. #include <boost/random/normal_distribution.hpp>
  23. #include <boost/random/chi_squared_distribution.hpp>
  24. #include <boost/random/poisson_distribution.hpp>
  25. namespace boost {
  26. namespace random {
  27. /**
  28. * The noncentral chi-squared distribution is a real valued distribution with
  29. * two parameter, @c k and @c lambda. The distribution produces values > 0.
  30. *
  31. * This is the distribution of the sum of squares of k Normal distributed
  32. * variates each with variance one and \f$\lambda\f$ the sum of squares of the
  33. * normal means.
  34. *
  35. * The distribution function is
  36. * \f$\displaystyle P(x) = \frac{1}{2} e^{-(x+\lambda)/2} \left( \frac{x}{\lambda} \right)^{k/4-1/2} I_{k/2-1}( \sqrt{\lambda x} )\f$.
  37. * where \f$\displaystyle I_\nu(z)\f$ is a modified Bessel function of the
  38. * first kind.
  39. *
  40. * The algorithm is taken from
  41. *
  42. * @blockquote
  43. * "Monte Carlo Methods in Financial Engineering", Paul Glasserman,
  44. * 2003, XIII, 596 p, Stochastic Modelling and Applied Probability, Vol. 53,
  45. * ISBN 978-0-387-21617-1, p 124, Fig. 3.5.
  46. * @endblockquote
  47. */
  48. template <typename RealType = double>
  49. class non_central_chi_squared_distribution {
  50. public:
  51. typedef RealType result_type;
  52. typedef RealType input_type;
  53. class param_type {
  54. public:
  55. typedef non_central_chi_squared_distribution distribution_type;
  56. /**
  57. * Constructs the parameters of a non_central_chi_squared_distribution.
  58. * @c k and @c lambda are the parameter of the distribution.
  59. *
  60. * Requires: k > 0 && lambda > 0
  61. */
  62. explicit
  63. param_type(RealType k_arg = RealType(1), RealType lambda_arg = RealType(1))
  64. : _k(k_arg), _lambda(lambda_arg)
  65. {
  66. BOOST_ASSERT(k_arg > RealType(0));
  67. BOOST_ASSERT(lambda_arg > RealType(0));
  68. }
  69. /** Returns the @c k parameter of the distribution */
  70. RealType k() const { return _k; }
  71. /** Returns the @c lambda parameter of the distribution */
  72. RealType lambda() const { return _lambda; }
  73. /** Writes the parameters of the distribution to a @c std::ostream. */
  74. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
  75. {
  76. os << parm._k << ' ' << parm._lambda;
  77. return os;
  78. }
  79. /** Reads the parameters of the distribution from a @c std::istream. */
  80. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
  81. {
  82. is >> parm._k >> std::ws >> parm._lambda;
  83. return is;
  84. }
  85. /** Returns true if the parameters have the same values. */
  86. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
  87. { return lhs._k == rhs._k && lhs._lambda == rhs._lambda; }
  88. /** Returns true if the parameters have different values. */
  89. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
  90. private:
  91. RealType _k;
  92. RealType _lambda;
  93. };
  94. /**
  95. * Construct a @c non_central_chi_squared_distribution object. @c k and
  96. * @c lambda are the parameter of the distribution.
  97. *
  98. * Requires: k > 0 && lambda > 0
  99. */
  100. explicit
  101. non_central_chi_squared_distribution(RealType k_arg = RealType(1), RealType lambda_arg = RealType(1))
  102. : _param(k_arg, lambda_arg)
  103. {
  104. BOOST_ASSERT(k_arg > RealType(0));
  105. BOOST_ASSERT(lambda_arg > RealType(0));
  106. }
  107. /**
  108. * Construct a @c non_central_chi_squared_distribution object from the parameter.
  109. */
  110. explicit
  111. non_central_chi_squared_distribution(const param_type& parm)
  112. : _param( parm )
  113. { }
  114. /**
  115. * Returns a random variate distributed according to the
  116. * non central chi squared distribution specified by @c param.
  117. */
  118. template<typename URNG>
  119. RealType operator()(URNG& eng, const param_type& parm) const
  120. { return non_central_chi_squared_distribution(parm)(eng); }
  121. /**
  122. * Returns a random variate distributed according to the
  123. * non central chi squared distribution.
  124. */
  125. template<typename URNG>
  126. RealType operator()(URNG& eng)
  127. {
  128. using std::sqrt;
  129. if (_param.k() > 1) {
  130. boost::random::normal_distribution<RealType> n_dist;
  131. boost::random::chi_squared_distribution<RealType> c_dist(_param.k() - RealType(1));
  132. RealType _z = n_dist(eng);
  133. RealType _x = c_dist(eng);
  134. RealType term1 = _z + sqrt(_param.lambda());
  135. return term1*term1 + _x;
  136. }
  137. else {
  138. boost::random::poisson_distribution<> p_dist(_param.lambda()/RealType(2));
  139. boost::random::poisson_distribution<>::result_type _p = p_dist(eng);
  140. boost::random::chi_squared_distribution<RealType> c_dist(_param.k() + RealType(2)*_p);
  141. return c_dist(eng);
  142. }
  143. }
  144. /** Returns the @c k parameter of the distribution. */
  145. RealType k() const { return _param.k(); }
  146. /** Returns the @c lambda parameter of the distribution. */
  147. RealType lambda() const { return _param.lambda(); }
  148. /** Returns the parameters of the distribution. */
  149. param_type param() const { return _param; }
  150. /** Sets parameters of the distribution. */
  151. void param(const param_type& parm) { _param = parm; }
  152. /** Resets the distribution, so that subsequent uses does not depend on values already produced by it.*/
  153. void reset() {}
  154. /** Returns the smallest value that the distribution can produce. */
  155. RealType min BOOST_PREVENT_MACRO_SUBSTITUTION() const
  156. { return RealType(0); }
  157. /** Returns the largest value that the distribution can produce. */
  158. RealType max BOOST_PREVENT_MACRO_SUBSTITUTION() const
  159. { return (std::numeric_limits<RealType>::infinity)(); }
  160. /** Writes the parameters of the distribution to a @c std::ostream. */
  161. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, non_central_chi_squared_distribution, dist)
  162. {
  163. os << dist.param();
  164. return os;
  165. }
  166. /** reads the parameters of the distribution from a @c std::istream. */
  167. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, non_central_chi_squared_distribution, dist)
  168. {
  169. param_type parm;
  170. if(is >> parm) {
  171. dist.param(parm);
  172. }
  173. return is;
  174. }
  175. /** Returns true if two distributions have the same parameters and produce
  176. the same sequence of random numbers given equal generators.*/
  177. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(non_central_chi_squared_distribution, lhs, rhs)
  178. { return lhs.param() == rhs.param(); }
  179. /** Returns true if two distributions have different parameters and/or can produce
  180. different sequences of random numbers given equal generators.*/
  181. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(non_central_chi_squared_distribution)
  182. private:
  183. /// @cond show_private
  184. param_type _param;
  185. /// @endcond
  186. };
  187. } // namespace random
  188. } // namespace boost
  189. #endif