/Src/Dependencies/Boost/boost/random/chi_squared_distribution.hpp

http://hadesmem.googlecode.com/ · C++ Header · 209 lines · 119 code · 26 blank · 64 comment · 5 complexity · 79eebf135d637d16aa5948f38a9da97f MD5 · raw file

  1. /* boost random/chi_squared_distribution.hpp header file
  2. *
  3. * Copyright Steven Watanabe 2011
  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: chi_squared_distribution.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
  11. */
  12. #ifndef BOOST_RANDOM_CHI_SQUARED_DISTRIBUTION_HPP_INCLUDED
  13. #define BOOST_RANDOM_CHI_SQUARED_DISTRIBUTION_HPP_INCLUDED
  14. #include <iosfwd>
  15. #include <boost/limits.hpp>
  16. #include <boost/random/detail/config.hpp>
  17. #include <boost/random/gamma_distribution.hpp>
  18. namespace boost {
  19. namespace random {
  20. /**
  21. * The chi squared distribution is a real valued distribution with
  22. * one parameter, @c n. The distribution produces values > 0.
  23. *
  24. * The distribution function is
  25. * \f$\displaystyle P(x) = \frac{x^{(n/2)-1}e^{-x/2}}{\Gamma(n/2)2^{n/2}}\f$.
  26. */
  27. template<class RealType = double>
  28. class chi_squared_distribution {
  29. public:
  30. typedef RealType result_type;
  31. typedef RealType input_type;
  32. class param_type {
  33. public:
  34. typedef chi_squared_distribution distribution_type;
  35. /**
  36. * Construct a param_type object. @c n
  37. * is the parameter of the distribution.
  38. *
  39. * Requires: t >=0 && 0 <= p <= 1
  40. */
  41. explicit param_type(RealType n_arg = RealType(1))
  42. : _n(n_arg)
  43. {}
  44. /** Returns the @c n parameter of the distribution. */
  45. RealType n() const { return _n; }
  46. #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
  47. /** Writes the parameters of the distribution to a @c std::ostream. */
  48. template<class CharT, class Traits>
  49. friend std::basic_ostream<CharT,Traits>&
  50. operator<<(std::basic_ostream<CharT,Traits>& os,
  51. const param_type& parm)
  52. {
  53. os << parm._n;
  54. return os;
  55. }
  56. /** Reads the parameters of the distribution from a @c std::istream. */
  57. template<class CharT, class Traits>
  58. friend std::basic_istream<CharT,Traits>&
  59. operator>>(std::basic_istream<CharT,Traits>& is, param_type& parm)
  60. {
  61. is >> parm._n;
  62. return is;
  63. }
  64. #endif
  65. /** Returns true if the parameters have the same values. */
  66. friend bool operator==(const param_type& lhs, const param_type& rhs)
  67. {
  68. return lhs._n == rhs._n;
  69. }
  70. /** Returns true if the parameters have different values. */
  71. friend bool operator!=(const param_type& lhs, const param_type& rhs)
  72. {
  73. return !(lhs == rhs);
  74. }
  75. private:
  76. RealType _n;
  77. };
  78. /**
  79. * Construct a @c chi_squared_distribution object. @c n
  80. * is the parameter of the distribution.
  81. *
  82. * Requires: t >=0 && 0 <= p <= 1
  83. */
  84. explicit chi_squared_distribution(RealType n_arg = RealType(1))
  85. : _impl(n_arg / 2)
  86. {
  87. }
  88. /**
  89. * Construct an @c chi_squared_distribution object from the
  90. * parameters.
  91. */
  92. explicit chi_squared_distribution(const param_type& parm)
  93. : _impl(parm.n() / 2)
  94. {
  95. }
  96. /**
  97. * Returns a random variate distributed according to the
  98. * chi squared distribution.
  99. */
  100. template<class URNG>
  101. RealType operator()(URNG& urng)
  102. {
  103. return 2 * _impl(urng);
  104. }
  105. /**
  106. * Returns a random variate distributed according to the
  107. * chi squared distribution with parameters specified by @c param.
  108. */
  109. template<class URNG>
  110. RealType operator()(URNG& urng, const param_type& parm) const
  111. {
  112. return chi_squared_distribution(parm)(urng);
  113. }
  114. /** Returns the @c n parameter of the distribution. */
  115. RealType n() const { return 2 * _impl.alpha(); }
  116. /** Returns the smallest value that the distribution can produce. */
  117. RealType min BOOST_PREVENT_MACRO_SUBSTITUTION() const { return 0; }
  118. /** Returns the largest value that the distribution can produce. */
  119. RealType max BOOST_PREVENT_MACRO_SUBSTITUTION() const
  120. { return (std::numeric_limits<RealType>::infinity)(); }
  121. /** Returns the parameters of the distribution. */
  122. param_type param() const { return param_type(n()); }
  123. /** Sets parameters of the distribution. */
  124. void param(const param_type& parm)
  125. {
  126. typedef gamma_distribution<RealType> impl_type;
  127. typename impl_type::param_type impl_parm(parm.n() / 2);
  128. _impl.param(impl_parm);
  129. }
  130. /**
  131. * Effects: Subsequent uses of the distribution do not depend
  132. * on values produced by any engine prior to invoking reset.
  133. */
  134. void reset() { _impl.reset(); }
  135. #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
  136. /** Writes the parameters of the distribution to a @c std::ostream. */
  137. template<class CharT, class Traits>
  138. friend std::basic_ostream<CharT,Traits>&
  139. operator<<(std::basic_ostream<CharT,Traits>& os,
  140. const chi_squared_distribution& c2d)
  141. {
  142. os << c2d.param();
  143. return os;
  144. }
  145. /** Reads the parameters of the distribution from a @c std::istream. */
  146. template<class CharT, class Traits>
  147. friend std::basic_istream<CharT,Traits>&
  148. operator>>(std::basic_istream<CharT,Traits>& is,
  149. chi_squared_distribution& c2d)
  150. {
  151. c2d.read(is);
  152. return is;
  153. }
  154. #endif
  155. /** Returns true if the two distributions will produce the same
  156. sequence of values, given equal generators. */
  157. friend bool operator==(const chi_squared_distribution& lhs,
  158. const chi_squared_distribution& rhs)
  159. {
  160. return lhs._impl == rhs._impl;
  161. }
  162. /** Returns true if the two distributions could produce different
  163. sequences of values, given equal generators. */
  164. friend bool operator!=(const chi_squared_distribution& lhs,
  165. const chi_squared_distribution& rhs)
  166. {
  167. return !(lhs == rhs);
  168. }
  169. private:
  170. /// @cond show_private
  171. template<class CharT, class Traits>
  172. void read(std::basic_istream<CharT, Traits>& is) {
  173. param_type parm;
  174. if(is >> parm) {
  175. param(parm);
  176. }
  177. }
  178. gamma_distribution<RealType> _impl;
  179. /// @endcond
  180. };
  181. }
  182. }
  183. #endif