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