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