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