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