1 /* boost random/student_t_distribution.hpp header file
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)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: student_t_distribution.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
13 #ifndef BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP
14 #define BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP
16 #include <boost/config/no_tr1/cmath.hpp>
18 #include <boost/config.hpp>
19 #include <boost/limits.hpp>
20 #include <boost/random/detail/operators.hpp>
21 #include <boost/random/chi_squared_distribution.hpp>
22 #include <boost/random/normal_distribution.hpp>
28 * The Student t distribution is a real valued distribution with one
29 * parameter n, the number of degrees of freedom.
31 * It has \f$\displaystyle p(x) =
32 * \frac{1}{\sqrt{n\pi}}
33 * \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
34 * \left(1+\frac{x^2}{n}\right)^{-(n+1)/2}
37 template<class RealType = double>
38 class student_t_distribution {
40 typedef RealType result_type;
41 typedef RealType input_type;
45 typedef student_t_distribution distribution_type;
48 * Constructs a @c param_type with "n" degrees of freedom.
52 explicit param_type(RealType n_arg = RealType(1.0))
56 /** Returns the number of degrees of freedom of the distribution. */
57 RealType n() const { return _n; }
59 /** Writes a @c param_type to a @c std::ostream. */
60 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
61 { os << parm._n; return os; }
63 /** Reads a @c param_type from a @c std::istream. */
64 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
65 { is >> parm._n; return is; }
67 /** Returns true if the two sets of parameters are the same. */
68 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
69 { return lhs._n == rhs._n; }
71 /** Returns true if the two sets of parameters are the different. */
72 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
79 * Constructs an @c student_t_distribution with "n" degrees of freedom.
83 explicit student_t_distribution(RealType n_arg = RealType(1.0))
84 : _normal(), _chi_squared(n_arg)
86 /** Constructs an @c student_t_distribution from its parameters. */
87 explicit student_t_distribution(const param_type& parm)
88 : _normal(), _chi_squared(parm.n())
92 * Returns a random variate distributed according to the
93 * Student t distribution.
96 RealType operator()(URNG& urng)
99 return _normal(urng) / sqrt(_chi_squared(urng) / n());
103 * Returns a random variate distributed accordint to the Student
104 * t distribution with parameters specified by @c param.
107 RealType operator()(URNG& urng, const param_type& parm) const
109 return student_t_distribution(parm)(urng);
112 /** Returns the number of degrees of freedom. */
113 RealType n() const { return _chi_squared.n(); }
115 /** Returns the smallest value that the distribution can produce. */
116 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
117 { return -std::numeric_limits<RealType>::infinity(); }
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(); }
122 /** Returns the parameters of the distribution. */
123 param_type param() const { return param_type(n()); }
124 /** Sets the parameters of the distribution. */
125 void param(const param_type& parm)
127 typedef chi_squared_distribution<RealType> chi_squared_type;
128 typename chi_squared_type::param_type chi_squared_param(parm.n());
129 _chi_squared.param(chi_squared_param);
133 * Effects: Subsequent uses of the distribution do not depend
134 * on values produced by any engine prior to invoking reset.
139 _chi_squared.reset();
142 /** Writes a @c student_t_distribution to a @c std::ostream. */
143 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, student_t_distribution, td)
149 /** Reads a @c student_t_distribution from a @c std::istream. */
150 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, student_t_distribution, td)
160 * Returns true if the two instances of @c student_t_distribution will
161 * return identical sequences of values given equal generators.
163 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(student_t_distribution, lhs, rhs)
164 { return lhs._normal == rhs._normal && lhs._chi_squared == rhs._chi_squared; }
167 * Returns true if the two instances of @c student_t_distribution will
168 * return different sequences of values given equal generators.
170 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(student_t_distribution)
173 normal_distribution<RealType> _normal;
174 chi_squared_distribution<RealType> _chi_squared;
177 } // namespace random
180 #endif // BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP