student_t_distribution.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* boost random/student_t_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$
  11. */
  12. #ifndef BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP
  13. #define BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP
  14. #include <boost/config/no_tr1/cmath.hpp>
  15. #include <iosfwd>
  16. #include <boost/config.hpp>
  17. #include <boost/limits.hpp>
  18. #include <boost/random/detail/operators.hpp>
  19. #include <boost/random/chi_squared_distribution.hpp>
  20. #include <boost/random/normal_distribution.hpp>
  21. namespace boost {
  22. namespace random {
  23. /**
  24. * The Student t distribution is a real valued distribution with one
  25. * parameter n, the number of degrees of freedom.
  26. *
  27. * It has \f$\displaystyle p(x) =
  28. * \frac{1}{\sqrt{n\pi}}
  29. * \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
  30. * \left(1+\frac{x^2}{n}\right)^{-(n+1)/2}
  31. * \f$.
  32. */
  33. template<class RealType = double>
  34. class student_t_distribution {
  35. public:
  36. typedef RealType result_type;
  37. typedef RealType input_type;
  38. class param_type {
  39. public:
  40. typedef student_t_distribution distribution_type;
  41. /**
  42. * Constructs a @c param_type with "n" degrees of freedom.
  43. *
  44. * Requires: n > 0
  45. */
  46. explicit param_type(RealType n_arg = RealType(1.0))
  47. : _n(n_arg)
  48. {}
  49. /** Returns the number of degrees of freedom of the distribution. */
  50. RealType n() const { return _n; }
  51. /** Writes a @c param_type to a @c std::ostream. */
  52. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
  53. { os << parm._n; return os; }
  54. /** Reads a @c param_type from a @c std::istream. */
  55. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
  56. { is >> parm._n; return is; }
  57. /** Returns true if the two sets of parameters are the same. */
  58. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
  59. { return lhs._n == rhs._n; }
  60. /** Returns true if the two sets of parameters are the different. */
  61. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
  62. private:
  63. RealType _n;
  64. };
  65. /**
  66. * Constructs an @c student_t_distribution with "n" degrees of freedom.
  67. *
  68. * Requires: n > 0
  69. */
  70. explicit student_t_distribution(RealType n_arg = RealType(1.0))
  71. : _normal(), _chi_squared(n_arg)
  72. {}
  73. /** Constructs an @c student_t_distribution from its parameters. */
  74. explicit student_t_distribution(const param_type& parm)
  75. : _normal(), _chi_squared(parm.n())
  76. {}
  77. /**
  78. * Returns a random variate distributed according to the
  79. * Student t distribution.
  80. */
  81. template<class URNG>
  82. RealType operator()(URNG& urng)
  83. {
  84. using std::sqrt;
  85. return _normal(urng) / sqrt(_chi_squared(urng) / n());
  86. }
  87. /**
  88. * Returns a random variate distributed accordint to the Student
  89. * t distribution with parameters specified by @c param.
  90. */
  91. template<class URNG>
  92. RealType operator()(URNG& urng, const param_type& parm) const
  93. {
  94. return student_t_distribution(parm)(urng);
  95. }
  96. /** Returns the number of degrees of freedom. */
  97. RealType n() const { return _chi_squared.n(); }
  98. /** Returns the smallest value that the distribution can produce. */
  99. RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
  100. { return -std::numeric_limits<RealType>::infinity(); }
  101. /** Returns the largest value that the distribution can produce. */
  102. RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
  103. { return std::numeric_limits<RealType>::infinity(); }
  104. /** Returns the parameters of the distribution. */
  105. param_type param() const { return param_type(n()); }
  106. /** Sets the parameters of the distribution. */
  107. void param(const param_type& parm)
  108. {
  109. typedef chi_squared_distribution<RealType> chi_squared_type;
  110. typename chi_squared_type::param_type chi_squared_param(parm.n());
  111. _chi_squared.param(chi_squared_param);
  112. }
  113. /**
  114. * Effects: Subsequent uses of the distribution do not depend
  115. * on values produced by any engine prior to invoking reset.
  116. */
  117. void reset()
  118. {
  119. _normal.reset();
  120. _chi_squared.reset();
  121. }
  122. /** Writes a @c student_t_distribution to a @c std::ostream. */
  123. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, student_t_distribution, td)
  124. {
  125. os << td.param();
  126. return os;
  127. }
  128. /** Reads a @c student_t_distribution from a @c std::istream. */
  129. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, student_t_distribution, td)
  130. {
  131. param_type parm;
  132. if(is >> parm) {
  133. td.param(parm);
  134. }
  135. return is;
  136. }
  137. /**
  138. * Returns true if the two instances of @c student_t_distribution will
  139. * return identical sequences of values given equal generators.
  140. */
  141. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(student_t_distribution, lhs, rhs)
  142. { return lhs._normal == rhs._normal && lhs._chi_squared == rhs._chi_squared; }
  143. /**
  144. * Returns true if the two instances of @c student_t_distribution will
  145. * return different sequences of values given equal generators.
  146. */
  147. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(student_t_distribution)
  148. private:
  149. normal_distribution<RealType> _normal;
  150. chi_squared_distribution<RealType> _chi_squared;
  151. };
  152. } // namespace random
  153. } // namespace boost
  154. #endif // BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP