Chris@16
|
1 // Copyright John Maddock 2006, 2007.
|
Chris@16
|
2 // Copyright Paul A. Bristow 2008, 2010.
|
Chris@16
|
3
|
Chris@16
|
4 // Use, modification and distribution are subject to the
|
Chris@16
|
5 // Boost Software License, Version 1.0.
|
Chris@16
|
6 // (See accompanying file LICENSE_1_0.txt
|
Chris@16
|
7 // or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8
|
Chris@16
|
9 #ifndef BOOST_MATH_DISTRIBUTIONS_CHI_SQUARED_HPP
|
Chris@16
|
10 #define BOOST_MATH_DISTRIBUTIONS_CHI_SQUARED_HPP
|
Chris@16
|
11
|
Chris@16
|
12 #include <boost/math/distributions/fwd.hpp>
|
Chris@16
|
13 #include <boost/math/special_functions/gamma.hpp> // for incomplete beta.
|
Chris@16
|
14 #include <boost/math/distributions/complement.hpp> // complements
|
Chris@16
|
15 #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
|
Chris@16
|
16 #include <boost/math/special_functions/fpclassify.hpp>
|
Chris@16
|
17
|
Chris@16
|
18 #include <utility>
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost{ namespace math{
|
Chris@16
|
21
|
Chris@16
|
22 template <class RealType = double, class Policy = policies::policy<> >
|
Chris@16
|
23 class chi_squared_distribution
|
Chris@16
|
24 {
|
Chris@16
|
25 public:
|
Chris@16
|
26 typedef RealType value_type;
|
Chris@16
|
27 typedef Policy policy_type;
|
Chris@16
|
28
|
Chris@16
|
29 chi_squared_distribution(RealType i) : m_df(i)
|
Chris@16
|
30 {
|
Chris@16
|
31 RealType result;
|
Chris@16
|
32 detail::check_df(
|
Chris@16
|
33 "boost::math::chi_squared_distribution<%1%>::chi_squared_distribution", m_df, &result, Policy());
|
Chris@16
|
34 } // chi_squared_distribution
|
Chris@16
|
35
|
Chris@16
|
36 RealType degrees_of_freedom()const
|
Chris@16
|
37 {
|
Chris@16
|
38 return m_df;
|
Chris@16
|
39 }
|
Chris@16
|
40
|
Chris@16
|
41 // Parameter estimation:
|
Chris@16
|
42 static RealType find_degrees_of_freedom(
|
Chris@16
|
43 RealType difference_from_variance,
|
Chris@16
|
44 RealType alpha,
|
Chris@16
|
45 RealType beta,
|
Chris@16
|
46 RealType variance,
|
Chris@16
|
47 RealType hint = 100);
|
Chris@16
|
48
|
Chris@16
|
49 private:
|
Chris@16
|
50 //
|
Chris@16
|
51 // Data member:
|
Chris@16
|
52 //
|
Chris@16
|
53 RealType m_df; // degrees of freedom is a positive real number.
|
Chris@16
|
54 }; // class chi_squared_distribution
|
Chris@16
|
55
|
Chris@16
|
56 typedef chi_squared_distribution<double> chi_squared;
|
Chris@16
|
57
|
Chris@16
|
58 template <class RealType, class Policy>
|
Chris@16
|
59 inline const std::pair<RealType, RealType> range(const chi_squared_distribution<RealType, Policy>& /*dist*/)
|
Chris@16
|
60 { // Range of permissible values for random variable x.
|
Chris@16
|
61 if (std::numeric_limits<RealType>::has_infinity)
|
Chris@16
|
62 {
|
Chris@16
|
63 return std::pair<RealType, RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()); // 0 to + infinity.
|
Chris@16
|
64 }
|
Chris@16
|
65 else
|
Chris@16
|
66 {
|
Chris@16
|
67 using boost::math::tools::max_value;
|
Chris@16
|
68 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + max.
|
Chris@16
|
69 }
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 template <class RealType, class Policy>
|
Chris@16
|
73 inline const std::pair<RealType, RealType> support(const chi_squared_distribution<RealType, Policy>& /*dist*/)
|
Chris@16
|
74 { // Range of supported values for random variable x.
|
Chris@16
|
75 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
|
Chris@16
|
76 return std::pair<RealType, RealType>(static_cast<RealType>(0), tools::max_value<RealType>()); // 0 to + infinity.
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 template <class RealType, class Policy>
|
Chris@16
|
80 RealType pdf(const chi_squared_distribution<RealType, Policy>& dist, const RealType& chi_square)
|
Chris@16
|
81 {
|
Chris@16
|
82 BOOST_MATH_STD_USING // for ADL of std functions
|
Chris@16
|
83 RealType degrees_of_freedom = dist.degrees_of_freedom();
|
Chris@16
|
84 // Error check:
|
Chris@16
|
85 RealType error_result;
|
Chris@16
|
86
|
Chris@16
|
87 static const char* function = "boost::math::pdf(const chi_squared_distribution<%1%>&, %1%)";
|
Chris@16
|
88
|
Chris@16
|
89 if(false == detail::check_df(
|
Chris@16
|
90 function, degrees_of_freedom, &error_result, Policy()))
|
Chris@16
|
91 return error_result;
|
Chris@16
|
92
|
Chris@16
|
93 if((chi_square < 0) || !(boost::math::isfinite)(chi_square))
|
Chris@16
|
94 {
|
Chris@16
|
95 return policies::raise_domain_error<RealType>(
|
Chris@16
|
96 function, "Chi Square parameter was %1%, but must be > 0 !", chi_square, Policy());
|
Chris@16
|
97 }
|
Chris@16
|
98
|
Chris@16
|
99 if(chi_square == 0)
|
Chris@16
|
100 {
|
Chris@16
|
101 // Handle special cases:
|
Chris@16
|
102 if(degrees_of_freedom < 2)
|
Chris@16
|
103 {
|
Chris@16
|
104 return policies::raise_overflow_error<RealType>(
|
Chris@16
|
105 function, 0, Policy());
|
Chris@16
|
106 }
|
Chris@16
|
107 else if(degrees_of_freedom == 2)
|
Chris@16
|
108 {
|
Chris@16
|
109 return 0.5f;
|
Chris@16
|
110 }
|
Chris@16
|
111 else
|
Chris@16
|
112 {
|
Chris@16
|
113 return 0;
|
Chris@16
|
114 }
|
Chris@16
|
115 }
|
Chris@16
|
116
|
Chris@16
|
117 return gamma_p_derivative(degrees_of_freedom / 2, chi_square / 2, Policy()) / 2;
|
Chris@16
|
118 } // pdf
|
Chris@16
|
119
|
Chris@16
|
120 template <class RealType, class Policy>
|
Chris@16
|
121 inline RealType cdf(const chi_squared_distribution<RealType, Policy>& dist, const RealType& chi_square)
|
Chris@16
|
122 {
|
Chris@16
|
123 RealType degrees_of_freedom = dist.degrees_of_freedom();
|
Chris@16
|
124 // Error check:
|
Chris@16
|
125 RealType error_result;
|
Chris@16
|
126 static const char* function = "boost::math::cdf(const chi_squared_distribution<%1%>&, %1%)";
|
Chris@16
|
127
|
Chris@16
|
128 if(false == detail::check_df(
|
Chris@16
|
129 function, degrees_of_freedom, &error_result, Policy()))
|
Chris@16
|
130 return error_result;
|
Chris@16
|
131
|
Chris@16
|
132 if((chi_square < 0) || !(boost::math::isfinite)(chi_square))
|
Chris@16
|
133 {
|
Chris@16
|
134 return policies::raise_domain_error<RealType>(
|
Chris@16
|
135 function, "Chi Square parameter was %1%, but must be > 0 !", chi_square, Policy());
|
Chris@16
|
136 }
|
Chris@16
|
137
|
Chris@16
|
138 return boost::math::gamma_p(degrees_of_freedom / 2, chi_square / 2, Policy());
|
Chris@16
|
139 } // cdf
|
Chris@16
|
140
|
Chris@16
|
141 template <class RealType, class Policy>
|
Chris@16
|
142 inline RealType quantile(const chi_squared_distribution<RealType, Policy>& dist, const RealType& p)
|
Chris@16
|
143 {
|
Chris@16
|
144 RealType degrees_of_freedom = dist.degrees_of_freedom();
|
Chris@16
|
145 static const char* function = "boost::math::quantile(const chi_squared_distribution<%1%>&, %1%)";
|
Chris@16
|
146 // Error check:
|
Chris@16
|
147 RealType error_result;
|
Chris@16
|
148 if(false ==
|
Chris@16
|
149 (
|
Chris@16
|
150 detail::check_df(function, degrees_of_freedom, &error_result, Policy())
|
Chris@16
|
151 && detail::check_probability(function, p, &error_result, Policy()))
|
Chris@16
|
152 )
|
Chris@16
|
153 return error_result;
|
Chris@16
|
154
|
Chris@16
|
155 return 2 * boost::math::gamma_p_inv(degrees_of_freedom / 2, p, Policy());
|
Chris@16
|
156 } // quantile
|
Chris@16
|
157
|
Chris@16
|
158 template <class RealType, class Policy>
|
Chris@16
|
159 inline RealType cdf(const complemented2_type<chi_squared_distribution<RealType, Policy>, RealType>& c)
|
Chris@16
|
160 {
|
Chris@16
|
161 RealType const& degrees_of_freedom = c.dist.degrees_of_freedom();
|
Chris@16
|
162 RealType const& chi_square = c.param;
|
Chris@16
|
163 static const char* function = "boost::math::cdf(const chi_squared_distribution<%1%>&, %1%)";
|
Chris@16
|
164 // Error check:
|
Chris@16
|
165 RealType error_result;
|
Chris@16
|
166 if(false == detail::check_df(
|
Chris@16
|
167 function, degrees_of_freedom, &error_result, Policy()))
|
Chris@16
|
168 return error_result;
|
Chris@16
|
169
|
Chris@16
|
170 if((chi_square < 0) || !(boost::math::isfinite)(chi_square))
|
Chris@16
|
171 {
|
Chris@16
|
172 return policies::raise_domain_error<RealType>(
|
Chris@16
|
173 function, "Chi Square parameter was %1%, but must be > 0 !", chi_square, Policy());
|
Chris@16
|
174 }
|
Chris@16
|
175
|
Chris@16
|
176 return boost::math::gamma_q(degrees_of_freedom / 2, chi_square / 2, Policy());
|
Chris@16
|
177 }
|
Chris@16
|
178
|
Chris@16
|
179 template <class RealType, class Policy>
|
Chris@16
|
180 inline RealType quantile(const complemented2_type<chi_squared_distribution<RealType, Policy>, RealType>& c)
|
Chris@16
|
181 {
|
Chris@16
|
182 RealType const& degrees_of_freedom = c.dist.degrees_of_freedom();
|
Chris@16
|
183 RealType const& q = c.param;
|
Chris@16
|
184 static const char* function = "boost::math::quantile(const chi_squared_distribution<%1%>&, %1%)";
|
Chris@16
|
185 // Error check:
|
Chris@16
|
186 RealType error_result;
|
Chris@16
|
187 if(false == (
|
Chris@16
|
188 detail::check_df(function, degrees_of_freedom, &error_result, Policy())
|
Chris@16
|
189 && detail::check_probability(function, q, &error_result, Policy()))
|
Chris@16
|
190 )
|
Chris@16
|
191 return error_result;
|
Chris@16
|
192
|
Chris@16
|
193 return 2 * boost::math::gamma_q_inv(degrees_of_freedom / 2, q, Policy());
|
Chris@16
|
194 }
|
Chris@16
|
195
|
Chris@16
|
196 template <class RealType, class Policy>
|
Chris@16
|
197 inline RealType mean(const chi_squared_distribution<RealType, Policy>& dist)
|
Chris@16
|
198 { // Mean of Chi-Squared distribution = v.
|
Chris@16
|
199 return dist.degrees_of_freedom();
|
Chris@16
|
200 } // mean
|
Chris@16
|
201
|
Chris@16
|
202 template <class RealType, class Policy>
|
Chris@16
|
203 inline RealType variance(const chi_squared_distribution<RealType, Policy>& dist)
|
Chris@16
|
204 { // Variance of Chi-Squared distribution = 2v.
|
Chris@16
|
205 return 2 * dist.degrees_of_freedom();
|
Chris@16
|
206 } // variance
|
Chris@16
|
207
|
Chris@16
|
208 template <class RealType, class Policy>
|
Chris@16
|
209 inline RealType mode(const chi_squared_distribution<RealType, Policy>& dist)
|
Chris@16
|
210 {
|
Chris@16
|
211 RealType df = dist.degrees_of_freedom();
|
Chris@16
|
212 static const char* function = "boost::math::mode(const chi_squared_distribution<%1%>&)";
|
Chris@16
|
213 // Most sources only define mode for df >= 2,
|
Chris@16
|
214 // but for 0 <= df <= 2, the pdf maximum actually occurs at random variate = 0;
|
Chris@16
|
215 // So one could extend the definition of mode thus:
|
Chris@16
|
216 //if(df < 0)
|
Chris@16
|
217 //{
|
Chris@16
|
218 // return policies::raise_domain_error<RealType>(
|
Chris@16
|
219 // function,
|
Chris@16
|
220 // "Chi-Squared distribution only has a mode for degrees of freedom >= 0, but got degrees of freedom = %1%.",
|
Chris@16
|
221 // df, Policy());
|
Chris@16
|
222 //}
|
Chris@16
|
223 //return (df <= 2) ? 0 : df - 2;
|
Chris@16
|
224
|
Chris@16
|
225 if(df < 2)
|
Chris@16
|
226 return policies::raise_domain_error<RealType>(
|
Chris@16
|
227 function,
|
Chris@16
|
228 "Chi-Squared distribution only has a mode for degrees of freedom >= 2, but got degrees of freedom = %1%.",
|
Chris@16
|
229 df, Policy());
|
Chris@16
|
230 return df - 2;
|
Chris@16
|
231 }
|
Chris@16
|
232
|
Chris@16
|
233 //template <class RealType, class Policy>
|
Chris@16
|
234 //inline RealType median(const chi_squared_distribution<RealType, Policy>& dist)
|
Chris@16
|
235 //{ // Median is given by Quantile[dist, 1/2]
|
Chris@16
|
236 // RealType df = dist.degrees_of_freedom();
|
Chris@16
|
237 // if(df <= 1)
|
Chris@16
|
238 // return tools::domain_error<RealType>(
|
Chris@16
|
239 // BOOST_CURRENT_FUNCTION,
|
Chris@16
|
240 // "The Chi-Squared distribution only has a mode for degrees of freedom >= 2, but got degrees of freedom = %1%.",
|
Chris@16
|
241 // df);
|
Chris@16
|
242 // return df - RealType(2)/3;
|
Chris@16
|
243 //}
|
Chris@16
|
244 // Now implemented via quantile(half) in derived accessors.
|
Chris@16
|
245
|
Chris@16
|
246 template <class RealType, class Policy>
|
Chris@16
|
247 inline RealType skewness(const chi_squared_distribution<RealType, Policy>& dist)
|
Chris@16
|
248 {
|
Chris@16
|
249 BOOST_MATH_STD_USING // For ADL
|
Chris@16
|
250 RealType df = dist.degrees_of_freedom();
|
Chris@16
|
251 return sqrt (8 / df); // == 2 * sqrt(2 / df);
|
Chris@16
|
252 }
|
Chris@16
|
253
|
Chris@16
|
254 template <class RealType, class Policy>
|
Chris@16
|
255 inline RealType kurtosis(const chi_squared_distribution<RealType, Policy>& dist)
|
Chris@16
|
256 {
|
Chris@16
|
257 RealType df = dist.degrees_of_freedom();
|
Chris@16
|
258 return 3 + 12 / df;
|
Chris@16
|
259 }
|
Chris@16
|
260
|
Chris@16
|
261 template <class RealType, class Policy>
|
Chris@16
|
262 inline RealType kurtosis_excess(const chi_squared_distribution<RealType, Policy>& dist)
|
Chris@16
|
263 {
|
Chris@16
|
264 RealType df = dist.degrees_of_freedom();
|
Chris@16
|
265 return 12 / df;
|
Chris@16
|
266 }
|
Chris@16
|
267
|
Chris@16
|
268 //
|
Chris@16
|
269 // Parameter estimation comes last:
|
Chris@16
|
270 //
|
Chris@16
|
271 namespace detail
|
Chris@16
|
272 {
|
Chris@16
|
273
|
Chris@16
|
274 template <class RealType, class Policy>
|
Chris@16
|
275 struct df_estimator
|
Chris@16
|
276 {
|
Chris@16
|
277 df_estimator(RealType a, RealType b, RealType variance, RealType delta)
|
Chris@16
|
278 : alpha(a), beta(b), ratio(delta/variance)
|
Chris@16
|
279 { // Constructor
|
Chris@16
|
280 }
|
Chris@16
|
281
|
Chris@16
|
282 RealType operator()(const RealType& df)
|
Chris@16
|
283 {
|
Chris@16
|
284 if(df <= tools::min_value<RealType>())
|
Chris@16
|
285 return 1;
|
Chris@16
|
286 chi_squared_distribution<RealType, Policy> cs(df);
|
Chris@16
|
287
|
Chris@16
|
288 RealType result;
|
Chris@16
|
289 if(ratio > 0)
|
Chris@16
|
290 {
|
Chris@16
|
291 RealType r = 1 + ratio;
|
Chris@16
|
292 result = cdf(cs, quantile(complement(cs, alpha)) / r) - beta;
|
Chris@16
|
293 }
|
Chris@16
|
294 else
|
Chris@16
|
295 { // ratio <= 0
|
Chris@16
|
296 RealType r = 1 + ratio;
|
Chris@16
|
297 result = cdf(complement(cs, quantile(cs, alpha) / r)) - beta;
|
Chris@16
|
298 }
|
Chris@16
|
299 return result;
|
Chris@16
|
300 }
|
Chris@16
|
301 private:
|
Chris@16
|
302 RealType alpha;
|
Chris@16
|
303 RealType beta;
|
Chris@16
|
304 RealType ratio; // Difference from variance / variance, so fractional.
|
Chris@16
|
305 };
|
Chris@16
|
306
|
Chris@16
|
307 } // namespace detail
|
Chris@16
|
308
|
Chris@16
|
309 template <class RealType, class Policy>
|
Chris@16
|
310 RealType chi_squared_distribution<RealType, Policy>::find_degrees_of_freedom(
|
Chris@16
|
311 RealType difference_from_variance,
|
Chris@16
|
312 RealType alpha,
|
Chris@16
|
313 RealType beta,
|
Chris@16
|
314 RealType variance,
|
Chris@16
|
315 RealType hint)
|
Chris@16
|
316 {
|
Chris@16
|
317 static const char* function = "boost::math::chi_squared_distribution<%1%>::find_degrees_of_freedom(%1%,%1%,%1%,%1%,%1%)";
|
Chris@16
|
318 // Check for domain errors:
|
Chris@16
|
319 RealType error_result;
|
Chris@16
|
320 if(false ==
|
Chris@16
|
321 detail::check_probability(function, alpha, &error_result, Policy())
|
Chris@16
|
322 && detail::check_probability(function, beta, &error_result, Policy()))
|
Chris@16
|
323 { // Either probability is outside 0 to 1.
|
Chris@16
|
324 return error_result;
|
Chris@16
|
325 }
|
Chris@16
|
326
|
Chris@16
|
327 if(hint <= 0)
|
Chris@16
|
328 { // No hint given, so guess df = 1.
|
Chris@16
|
329 hint = 1;
|
Chris@16
|
330 }
|
Chris@16
|
331
|
Chris@16
|
332 detail::df_estimator<RealType, Policy> f(alpha, beta, variance, difference_from_variance);
|
Chris@16
|
333 tools::eps_tolerance<RealType> tol(policies::digits<RealType, Policy>());
|
Chris@16
|
334 boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
|
Chris@16
|
335 std::pair<RealType, RealType> r =
|
Chris@16
|
336 tools::bracket_and_solve_root(f, hint, RealType(2), false, tol, max_iter, Policy());
|
Chris@16
|
337 RealType result = r.first + (r.second - r.first) / 2;
|
Chris@16
|
338 if(max_iter >= policies::get_max_root_iterations<Policy>())
|
Chris@16
|
339 {
|
Chris@16
|
340 policies::raise_evaluation_error<RealType>(function, "Unable to locate solution in a reasonable time:"
|
Chris@16
|
341 " either there is no answer to how many degrees of freedom are required"
|
Chris@16
|
342 " or the answer is infinite. Current best guess is %1%", result, Policy());
|
Chris@16
|
343 }
|
Chris@16
|
344 return result;
|
Chris@16
|
345 }
|
Chris@16
|
346
|
Chris@16
|
347 } // namespace math
|
Chris@16
|
348 } // namespace boost
|
Chris@16
|
349
|
Chris@16
|
350 // This include must be at the end, *after* the accessors
|
Chris@16
|
351 // for this distribution have been defined, in order to
|
Chris@16
|
352 // keep compilers that support two-phase lookup happy.
|
Chris@16
|
353 #include <boost/math/distributions/detail/derived_accessors.hpp>
|
Chris@16
|
354
|
Chris@16
|
355 #endif // BOOST_MATH_DISTRIBUTIONS_CHI_SQUARED_HPP
|