Chris@16
|
1 /* boost random/fisher_f_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_FISHER_F_DISTRIBUTION_HPP
|
Chris@16
|
14 #define BOOST_RANDOM_FISHER_F_DISTRIBUTION_HPP
|
Chris@16
|
15
|
Chris@16
|
16 #include <iosfwd>
|
Chris@16
|
17 #include <istream>
|
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
|
Chris@16
|
23 namespace boost {
|
Chris@16
|
24 namespace random {
|
Chris@16
|
25
|
Chris@16
|
26 /**
|
Chris@16
|
27 * The Fisher F distribution is a real valued distribution with two
|
Chris@16
|
28 * parameters m and n.
|
Chris@16
|
29 *
|
Chris@16
|
30 * It has \f$\displaystyle p(x) =
|
Chris@16
|
31 * \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
|
Chris@16
|
32 * \left(\frac{m}{n}\right)^{m/2}
|
Chris@16
|
33 * x^{(m/2)-1} \left(1+\frac{mx}{n}\right)^{-(m+n)/2}
|
Chris@16
|
34 * \f$.
|
Chris@16
|
35 */
|
Chris@16
|
36 template<class RealType = double>
|
Chris@16
|
37 class fisher_f_distribution {
|
Chris@16
|
38 public:
|
Chris@16
|
39 typedef RealType result_type;
|
Chris@16
|
40 typedef RealType input_type;
|
Chris@16
|
41
|
Chris@16
|
42 class param_type {
|
Chris@16
|
43 public:
|
Chris@16
|
44 typedef fisher_f_distribution distribution_type;
|
Chris@16
|
45
|
Chris@16
|
46 /**
|
Chris@16
|
47 * Constructs a @c param_type from the "m" and "n" parameters
|
Chris@16
|
48 * of the distribution.
|
Chris@16
|
49 *
|
Chris@16
|
50 * Requires: m > 0 and n > 0
|
Chris@16
|
51 */
|
Chris@16
|
52 explicit param_type(RealType m_arg = RealType(1.0),
|
Chris@16
|
53 RealType n_arg = RealType(1.0))
|
Chris@16
|
54 : _m(m_arg), _n(n_arg)
|
Chris@16
|
55 {}
|
Chris@16
|
56
|
Chris@16
|
57 /** Returns the "m" parameter of the distribtuion. */
|
Chris@16
|
58 RealType m() const { return _m; }
|
Chris@16
|
59 /** Returns the "n" parameter of the distribution. */
|
Chris@16
|
60 RealType n() const { return _n; }
|
Chris@16
|
61
|
Chris@16
|
62 /** Writes a @c param_type to a @c std::ostream. */
|
Chris@16
|
63 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
|
Chris@16
|
64 { os << parm._m << ' ' << parm._n; return os; }
|
Chris@16
|
65
|
Chris@16
|
66 /** Reads a @c param_type from a @c std::istream. */
|
Chris@16
|
67 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
|
Chris@16
|
68 { is >> parm._m >> std::ws >> parm._n; return is; }
|
Chris@16
|
69
|
Chris@16
|
70 /** Returns true if the two sets of parameters are the same. */
|
Chris@16
|
71 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
|
Chris@16
|
72 { return lhs._m == rhs._m && lhs._n == rhs._n; }
|
Chris@16
|
73
|
Chris@16
|
74 /** Returns true if the two sets of parameters are the different. */
|
Chris@16
|
75 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
|
Chris@16
|
76
|
Chris@16
|
77 private:
|
Chris@16
|
78 RealType _m;
|
Chris@16
|
79 RealType _n;
|
Chris@16
|
80 };
|
Chris@16
|
81
|
Chris@16
|
82 /**
|
Chris@16
|
83 * Constructs a @c fisher_f_distribution from its "m" and "n" parameters.
|
Chris@16
|
84 *
|
Chris@16
|
85 * Requires: m > 0 and n > 0
|
Chris@16
|
86 */
|
Chris@16
|
87 explicit fisher_f_distribution(RealType m_arg = RealType(1.0),
|
Chris@16
|
88 RealType n_arg = RealType(1.0))
|
Chris@16
|
89 : _impl_m(m_arg), _impl_n(n_arg)
|
Chris@16
|
90 {}
|
Chris@16
|
91 /** Constructs an @c fisher_f_distribution from its parameters. */
|
Chris@16
|
92 explicit fisher_f_distribution(const param_type& parm)
|
Chris@16
|
93 : _impl_m(parm.m()), _impl_n(parm.n())
|
Chris@16
|
94 {}
|
Chris@16
|
95
|
Chris@16
|
96 /**
|
Chris@16
|
97 * Returns a random variate distributed according to the
|
Chris@16
|
98 * F distribution.
|
Chris@16
|
99 */
|
Chris@16
|
100 template<class URNG>
|
Chris@16
|
101 RealType operator()(URNG& urng)
|
Chris@16
|
102 {
|
Chris@16
|
103 return (_impl_m(urng) * n()) / (_impl_n(urng) * m());
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 /**
|
Chris@16
|
107 * Returns a random variate distributed according to the
|
Chris@16
|
108 * F distribution with parameters specified by @c param.
|
Chris@16
|
109 */
|
Chris@16
|
110 template<class URNG>
|
Chris@16
|
111 RealType operator()(URNG& urng, const param_type& parm) const
|
Chris@16
|
112 {
|
Chris@16
|
113 return fisher_f_distribution(parm)(urng);
|
Chris@16
|
114 }
|
Chris@16
|
115
|
Chris@16
|
116 /** Returns the "m" parameter of the distribution. */
|
Chris@16
|
117 RealType m() const { return _impl_m.n(); }
|
Chris@16
|
118 /** Returns the "n" parameter of the distribution. */
|
Chris@16
|
119 RealType n() const { return _impl_n.n(); }
|
Chris@16
|
120
|
Chris@16
|
121 /** Returns the smallest value that the distribution can produce. */
|
Chris@16
|
122 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
|
Chris@16
|
123 /** Returns the largest value that the distribution can produce. */
|
Chris@16
|
124 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
|
Chris@16
|
125 { return std::numeric_limits<RealType>::infinity(); }
|
Chris@16
|
126
|
Chris@16
|
127 /** Returns the parameters of the distribution. */
|
Chris@16
|
128 param_type param() const { return param_type(m(), n()); }
|
Chris@16
|
129 /** Sets the parameters of the distribution. */
|
Chris@16
|
130 void param(const param_type& parm)
|
Chris@16
|
131 {
|
Chris@16
|
132 typedef chi_squared_distribution<RealType> impl_type;
|
Chris@16
|
133 typename impl_type::param_type m_param(parm.m());
|
Chris@16
|
134 _impl_m.param(m_param);
|
Chris@16
|
135 typename impl_type::param_type n_param(parm.n());
|
Chris@16
|
136 _impl_n.param(n_param);
|
Chris@16
|
137 }
|
Chris@16
|
138
|
Chris@16
|
139 /**
|
Chris@16
|
140 * Effects: Subsequent uses of the distribution do not depend
|
Chris@16
|
141 * on values produced by any engine prior to invoking reset.
|
Chris@16
|
142 */
|
Chris@16
|
143 void reset() { }
|
Chris@16
|
144
|
Chris@16
|
145 /** Writes an @c fisher_f_distribution to a @c std::ostream. */
|
Chris@16
|
146 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, fisher_f_distribution, fd)
|
Chris@16
|
147 {
|
Chris@16
|
148 os << fd.param();
|
Chris@16
|
149 return os;
|
Chris@16
|
150 }
|
Chris@16
|
151
|
Chris@16
|
152 /** Reads an @c fisher_f_distribution from a @c std::istream. */
|
Chris@16
|
153 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, fisher_f_distribution, fd)
|
Chris@16
|
154 {
|
Chris@16
|
155 param_type parm;
|
Chris@16
|
156 if(is >> parm) {
|
Chris@16
|
157 fd.param(parm);
|
Chris@16
|
158 }
|
Chris@16
|
159 return is;
|
Chris@16
|
160 }
|
Chris@16
|
161
|
Chris@16
|
162 /**
|
Chris@16
|
163 * Returns true if the two instances of @c fisher_f_distribution will
|
Chris@16
|
164 * return identical sequences of values given equal generators.
|
Chris@16
|
165 */
|
Chris@16
|
166 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(fisher_f_distribution, lhs, rhs)
|
Chris@16
|
167 { return lhs._impl_m == rhs._impl_m && lhs._impl_n == rhs._impl_n; }
|
Chris@16
|
168
|
Chris@16
|
169 /**
|
Chris@16
|
170 * Returns true if the two instances of @c fisher_f_distribution will
|
Chris@16
|
171 * return different sequences of values given equal generators.
|
Chris@16
|
172 */
|
Chris@16
|
173 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(fisher_f_distribution)
|
Chris@16
|
174
|
Chris@16
|
175 private:
|
Chris@16
|
176 chi_squared_distribution<RealType> _impl_m;
|
Chris@16
|
177 chi_squared_distribution<RealType> _impl_n;
|
Chris@16
|
178 };
|
Chris@16
|
179
|
Chris@16
|
180 } // namespace random
|
Chris@16
|
181 } // namespace boost
|
Chris@16
|
182
|
Chris@16
|
183 #endif // BOOST_RANDOM_EXTREME_VALUE_DISTRIBUTION_HPP
|