]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/test/test_nc_chi_squared.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / math / test / test_nc_chi_squared.hpp
1 // (C) Copyright John Maddock 2007.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
7 #include <boost/math/concepts/real_concept.hpp>
8 #define BOOST_TEST_MAIN
9 #include <boost/test/unit_test.hpp>
10 #include <boost/test/floating_point_comparison.hpp>
11 #include <boost/math/distributions/non_central_chi_squared.hpp>
12 #include <boost/type_traits/is_floating_point.hpp>
13 #include <boost/array.hpp>
14 #include "functor.hpp"
15
16 #include "handle_test_result.hpp"
17 #include "table_type.hpp"
18
19 #include <iostream>
20 #include <iomanip>
21
22 #define BOOST_CHECK_CLOSE_EX(a, b, prec, i) \
23 {\
24 unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
25 BOOST_CHECK_CLOSE(a, b, prec); \
26 if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
27 {\
28 std::cerr << "Failure was at row " << i << std::endl;\
29 std::cerr << std::setprecision(35); \
30 std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
31 std::cerr << " , " << data[i][3] << " , " << data[i][4] << " } " << std::endl;\
32 }\
33 }
34
35 #define BOOST_CHECK_EX(a, i) \
36 {\
37 unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
38 BOOST_CHECK(a); \
39 if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
40 {\
41 std::cerr << "Failure was at row " << i << std::endl;\
42 std::cerr << std::setprecision(35); \
43 std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
44 std::cerr << " , " << data[i][3] << " , " << data[i][4] << " } " << std::endl;\
45 }\
46 }
47
48 template <class RealType>
49 RealType naive_pdf(RealType v, RealType lam, RealType x)
50 {
51 // Formula direct from
52 // http://mathworld.wolfram.com/NoncentralChi-SquaredDistribution.html
53 // with no simplification:
54 RealType sum, term, prefix(1);
55 RealType eps = boost::math::tools::epsilon<RealType>();
56 term = sum = pdf(boost::math::chi_squared_distribution<RealType>(v), x);
57 for(int i = 1;; ++i)
58 {
59 prefix *= lam / (2 * i);
60 term = prefix * pdf(boost::math::chi_squared_distribution<RealType>(v + 2 * i), x);
61 sum += term;
62 if(term / sum < eps)
63 break;
64 }
65 return sum * exp(-lam / 2);
66 }
67
68 template <class RealType>
69 void test_spot(
70 RealType df, // Degrees of freedom
71 RealType ncp, // non-centrality param
72 RealType cs, // Chi Square statistic
73 RealType P, // CDF
74 RealType Q, // Complement of CDF
75 RealType tol) // Test tolerance
76 {
77 boost::math::non_central_chi_squared_distribution<RealType> dist(df, ncp);
78 BOOST_CHECK_CLOSE(
79 cdf(dist, cs), P, tol);
80 #ifndef BOOST_NO_EXCEPTIONS
81 try{
82 BOOST_CHECK_CLOSE(
83 pdf(dist, cs), naive_pdf(dist.degrees_of_freedom(), ncp, cs), tol * 150);
84 }
85 catch(const std::overflow_error&)
86 {
87 }
88 #endif
89 if((P < 0.99) && (Q < 0.99))
90 {
91 //
92 // We can only check this if P is not too close to 1,
93 // so that we can guarantee Q is reasonably free of error:
94 //
95 BOOST_CHECK_CLOSE(
96 cdf(complement(dist, cs)), Q, tol);
97 BOOST_CHECK_CLOSE(
98 quantile(dist, P), cs, tol * 10);
99 BOOST_CHECK_CLOSE(
100 quantile(complement(dist, Q)), cs, tol * 10);
101 BOOST_CHECK_CLOSE(
102 dist.find_degrees_of_freedom(ncp, cs, P), df, tol * 10);
103 BOOST_CHECK_CLOSE(
104 dist.find_degrees_of_freedom(boost::math::complement(ncp, cs, Q)), df, tol * 10);
105 BOOST_CHECK_CLOSE(
106 dist.find_non_centrality(df, cs, P), ncp, tol * 10);
107 BOOST_CHECK_CLOSE(
108 dist.find_non_centrality(boost::math::complement(df, cs, Q)), ncp, tol * 10);
109 }
110 }
111
112 template <class RealType> // Any floating-point type RealType.
113 void test_spots(RealType)
114 {
115 #ifndef ERROR_REPORTING_MODE
116 RealType tolerance = (std::max)(
117 boost::math::tools::epsilon<RealType>(),
118 (RealType)boost::math::tools::epsilon<double>() * 5) * 150;
119 //
120 // At float precision we need to up the tolerance, since
121 // the input values are rounded off to inexact quantities
122 // the results get thrown off by a noticeable amount.
123 //
124 if(boost::math::tools::digits<RealType>() < 50)
125 tolerance *= 50;
126 if(boost::is_floating_point<RealType>::value != 1)
127 tolerance *= 20; // real_concept special functions are less accurate
128
129 std::cout << "Tolerance = " << tolerance << "%." << std::endl;
130
131 using boost::math::chi_squared_distribution;
132 using ::boost::math::chi_squared;
133 using ::boost::math::cdf;
134 using ::boost::math::pdf;
135 //
136 // Test against the data from Table 6 of:
137 //
138 // "Self-Validating Computations of Probabilities for Selected
139 // Central and Noncentral Univariate Probability Functions."
140 // Morgan C. Wang; William J. Kennedy
141 // Journal of the American Statistical Association,
142 // Vol. 89, No. 427. (Sep., 1994), pp. 878-887.
143 //
144 test_spot(
145 static_cast<RealType>(1), // degrees of freedom
146 static_cast<RealType>(6), // non centrality
147 static_cast<RealType>(0.00393), // Chi Squared statistic
148 static_cast<RealType>(0.2498463724258039e-2), // Probability of result (CDF), P
149 static_cast<RealType>(1 - 0.2498463724258039e-2), // Q = 1 - P
150 tolerance);
151 test_spot(
152 static_cast<RealType>(5), // degrees of freedom
153 static_cast<RealType>(1), // non centrality
154 static_cast<RealType>(9.23636), // Chi Squared statistic
155 static_cast<RealType>(0.8272918751175548), // Probability of result (CDF), P
156 static_cast<RealType>(1 - 0.8272918751175548), // Q = 1 - P
157 tolerance);
158 test_spot(
159 static_cast<RealType>(11), // degrees of freedom
160 static_cast<RealType>(21), // non centrality
161 static_cast<RealType>(24.72497), // Chi Squared statistic
162 static_cast<RealType>(0.2539481822183126), // Probability of result (CDF), P
163 static_cast<RealType>(1 - 0.2539481822183126), // Q = 1 - P
164 tolerance);
165 test_spot(
166 static_cast<RealType>(31), // degrees of freedom
167 static_cast<RealType>(6), // non centrality
168 static_cast<RealType>(44.98534), // Chi Squared statistic
169 static_cast<RealType>(0.8125198785064969), // Probability of result (CDF), P
170 static_cast<RealType>(1 - 0.8125198785064969), // Q = 1 - P
171 tolerance);
172 test_spot(
173 static_cast<RealType>(51), // degrees of freedom
174 static_cast<RealType>(1), // non centrality
175 static_cast<RealType>(38.56038), // Chi Squared statistic
176 static_cast<RealType>(0.8519497361859118e-1), // Probability of result (CDF), P
177 static_cast<RealType>(1 - 0.8519497361859118e-1), // Q = 1 - P
178 tolerance * 2);
179 test_spot(
180 static_cast<RealType>(100), // degrees of freedom
181 static_cast<RealType>(16), // non centrality
182 static_cast<RealType>(82.35814), // Chi Squared statistic
183 static_cast<RealType>(0.1184348822747824e-1), // Probability of result (CDF), P
184 static_cast<RealType>(1 - 0.1184348822747824e-1), // Q = 1 - P
185 tolerance);
186 test_spot(
187 static_cast<RealType>(300), // degrees of freedom
188 static_cast<RealType>(16), // non centrality
189 static_cast<RealType>(331.78852), // Chi Squared statistic
190 static_cast<RealType>(0.7355956710306709), // Probability of result (CDF), P
191 static_cast<RealType>(1 - 0.7355956710306709), // Q = 1 - P
192 tolerance);
193 test_spot(
194 static_cast<RealType>(500), // degrees of freedom
195 static_cast<RealType>(21), // non centrality
196 static_cast<RealType>(459.92612), // Chi Squared statistic
197 static_cast<RealType>(0.2797023600800060e-1), // Probability of result (CDF), P
198 static_cast<RealType>(1 - 0.2797023600800060e-1), // Q = 1 - P
199 tolerance);
200 test_spot(
201 static_cast<RealType>(1), // degrees of freedom
202 static_cast<RealType>(1), // non centrality
203 static_cast<RealType>(0.00016), // Chi Squared statistic
204 static_cast<RealType>(0.6121428929881423e-2), // Probability of result (CDF), P
205 static_cast<RealType>(1 - 0.6121428929881423e-2), // Q = 1 - P
206 tolerance);
207 test_spot(
208 static_cast<RealType>(1), // degrees of freedom
209 static_cast<RealType>(1), // non centrality
210 static_cast<RealType>(0.00393), // Chi Squared statistic
211 static_cast<RealType>(0.3033814229753780e-1), // Probability of result (CDF), P
212 static_cast<RealType>(1 - 0.3033814229753780e-1), // Q = 1 - P
213 tolerance);
214
215 RealType tol2 = boost::math::tools::epsilon<RealType>() * 5 * 100; // 5 eps as a percentage
216 boost::math::non_central_chi_squared_distribution<RealType> dist(static_cast<RealType>(8), static_cast<RealType>(12));
217 RealType x = 7;
218 using namespace std; // ADL of std names.
219 // mean:
220 BOOST_CHECK_CLOSE(
221 mean(dist)
222 , static_cast<RealType>(8 + 12), tol2);
223 // variance:
224 BOOST_CHECK_CLOSE(
225 variance(dist)
226 , static_cast<RealType>(64), tol2);
227 // std deviation:
228 BOOST_CHECK_CLOSE(
229 standard_deviation(dist)
230 , static_cast<RealType>(8), tol2);
231 // hazard:
232 BOOST_CHECK_CLOSE(
233 hazard(dist, x)
234 , pdf(dist, x) / cdf(complement(dist, x)), tol2);
235 // cumulative hazard:
236 BOOST_CHECK_CLOSE(
237 chf(dist, x)
238 , -log(cdf(complement(dist, x))), tol2);
239 // coefficient_of_variation:
240 BOOST_CHECK_CLOSE(
241 coefficient_of_variation(dist)
242 , standard_deviation(dist) / mean(dist), tol2);
243 // mode:
244 BOOST_CHECK_CLOSE(
245 mode(dist)
246 , static_cast<RealType>(17.184201184730857030170788677340294070728990862663L), sqrt(tolerance * 500));
247 BOOST_CHECK_CLOSE(
248 median(dist),
249 quantile(
250 boost::math::non_central_chi_squared_distribution<RealType>(
251 static_cast<RealType>(8),
252 static_cast<RealType>(12)),
253 static_cast<RealType>(0.5)), static_cast<RealType>(tol2));
254 // skewness:
255 BOOST_CHECK_CLOSE(
256 skewness(dist)
257 , static_cast<RealType>(0.6875), tol2);
258 // kurtosis:
259 BOOST_CHECK_CLOSE(
260 kurtosis(dist)
261 , static_cast<RealType>(3.65625), tol2);
262 // kurtosis excess:
263 BOOST_CHECK_CLOSE(
264 kurtosis_excess(dist)
265 , static_cast<RealType>(0.65625), tol2);
266
267 // Error handling checks:
268 check_out_of_range<boost::math::non_central_chi_squared_distribution<RealType> >(1, 1);
269 BOOST_MATH_CHECK_THROW(pdf(boost::math::non_central_chi_squared_distribution<RealType>(0, 1), 0), std::domain_error);
270 BOOST_MATH_CHECK_THROW(pdf(boost::math::non_central_chi_squared_distribution<RealType>(-1, 1), 0), std::domain_error);
271 BOOST_MATH_CHECK_THROW(pdf(boost::math::non_central_chi_squared_distribution<RealType>(1, -1), 0), std::domain_error);
272 BOOST_MATH_CHECK_THROW(quantile(boost::math::non_central_chi_squared_distribution<RealType>(1, 1), -1), std::domain_error);
273 BOOST_MATH_CHECK_THROW(quantile(boost::math::non_central_chi_squared_distribution<RealType>(1, 1), 2), std::domain_error);
274 #endif
275 } // template <class RealType>void test_spots(RealType)
276
277 template <class T>
278 T nccs_cdf(T df, T nc, T x)
279 {
280 return cdf(boost::math::non_central_chi_squared_distribution<T>(df, nc), x);
281 }
282
283 template <class T>
284 T nccs_ccdf(T df, T nc, T x)
285 {
286 return cdf(complement(boost::math::non_central_chi_squared_distribution<T>(df, nc), x));
287 }
288
289 template <typename Real, typename T>
290 void do_test_nc_chi_squared(T& data, const char* type_name, const char* test)
291 {
292 typedef typename T::value_type row_type;
293 typedef Real value_type;
294
295 std::cout << "Testing: " << test << std::endl;
296
297 #ifdef NC_CHI_SQUARED_CDF_FUNCTION_TO_TEST
298 value_type(*fp1)(value_type, value_type, value_type) = NC_CHI_SQUARED_CDF_FUNCTION_TO_TEST;
299 #else
300 value_type(*fp1)(value_type, value_type, value_type) = nccs_cdf;
301 #endif
302 boost::math::tools::test_result<value_type> result;
303
304 #if !(defined(ERROR_REPORTING_MODE) && !defined(NC_CHI_SQUARED_CDF_FUNCTION_TO_TEST))
305 result = boost::math::tools::test_hetero<Real>(
306 data,
307 bind_func<Real>(fp1, 0, 1, 2),
308 extract_result<Real>(3));
309 handle_test_result(result, data[result.worst()], result.worst(),
310 type_name, "non central chi squared CDF", test);
311 #endif
312 #if !(defined(ERROR_REPORTING_MODE) && !defined(NC_CHI_SQUARED_CCDF_FUNCTION_TO_TEST))
313 #ifdef NC_CHI_SQUARED_CCDF_FUNCTION_TO_TEST
314 fp1 = NC_CHI_SQUARED_CCDF_FUNCTION_TO_TEST;
315 #else
316 fp1 = nccs_ccdf;
317 #endif
318 result = boost::math::tools::test_hetero<Real>(
319 data,
320 bind_func<Real>(fp1, 0, 1, 2),
321 extract_result<Real>(4));
322 handle_test_result(result, data[result.worst()], result.worst(),
323 type_name, "non central chi squared CDF complement", test);
324
325 std::cout << std::endl;
326 #endif
327 }
328
329 template <typename Real, typename T>
330 void quantile_sanity_check(T& data, const char* type_name, const char* test)
331 {
332 #ifndef ERROR_REPORTING_MODE
333 typedef typename T::value_type row_type;
334 typedef Real value_type;
335
336 //
337 // Tests with type real_concept take rather too long to run, so
338 // for now we'll disable them:
339 //
340 if(!boost::is_floating_point<value_type>::value)
341 return;
342
343 std::cout << "Testing: " << type_name << " quantile sanity check, with tests " << test << std::endl;
344
345 //
346 // These sanity checks test for a round trip accuracy of one half
347 // of the bits in T, unless T is type float, in which case we check
348 // for just one decimal digit. The problem here is the sensitivity
349 // of the functions, not their accuracy. This test data was generated
350 // for the forward functions, which means that when it is used as
351 // the input to the inverses then it is necessarily inexact. This rounding
352 // of the input is what makes the data unsuitable for use as an accuracy check,
353 // and also demonstrates that you can't in general round-trip these functions.
354 // It is however a useful sanity check.
355 //
356 value_type precision = static_cast<value_type>(ldexp(1.0, 1 - boost::math::policies::digits<value_type, boost::math::policies::policy<> >() / 2)) * 100;
357 if(boost::math::policies::digits<value_type, boost::math::policies::policy<> >() < 50)
358 precision = 1; // 1% or two decimal digits, all we can hope for when the input is truncated to float
359
360 for(unsigned i = 0; i < data.size(); ++i)
361 {
362 if(Real(data[i][3]) == 0)
363 {
364 BOOST_CHECK(0 == quantile(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), data[i][3]));
365 }
366 else if(data[i][3] < 0.9999f)
367 {
368 value_type p = quantile(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), data[i][3]);
369 value_type pt = data[i][2];
370 BOOST_CHECK_CLOSE_EX(pt, p, precision, i);
371 }
372 if(data[i][4] == 0)
373 {
374 BOOST_CHECK(0 == quantile(complement(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), data[i][3])));
375 }
376 else if(data[i][4] < 0.9999f)
377 {
378 value_type p = quantile(complement(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), data[i][4]));
379 value_type pt = data[i][2];
380 BOOST_CHECK_CLOSE_EX(pt, p, precision, i);
381 }
382 if(boost::math::tools::digits<value_type>() > 50)
383 {
384 //
385 // Sanity check mode, the accuracy of
386 // the mode is at *best* the square root of the accuracy of the PDF:
387 //
388 #ifndef BOOST_NO_EXCEPTIONS
389 try{
390 value_type m = mode(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]));
391 value_type p = pdf(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), m);
392 BOOST_CHECK_EX(pdf(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), m * (1 + sqrt(precision) * 50)) <= p, i);
393 BOOST_CHECK_EX(pdf(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), m * (1 - sqrt(precision)) * 50) <= p, i);
394 }
395 catch(const boost::math::evaluation_error&) {}
396 #endif
397 //
398 // Sanity check degrees-of-freedom finder, don't bother at float
399 // precision though as there's not enough data in the probability
400 // values to get back to the correct degrees of freedom or
401 // non-cenrality parameter:
402 //
403 #ifndef BOOST_NO_EXCEPTIONS
404 try{
405 #endif
406 if((data[i][3] < 0.99) && (data[i][3] != 0))
407 {
408 BOOST_CHECK_CLOSE_EX(
409 boost::math::non_central_chi_squared_distribution<value_type>::find_degrees_of_freedom(data[i][1], data[i][2], data[i][3]),
410 data[i][0], precision, i);
411 BOOST_CHECK_CLOSE_EX(
412 boost::math::non_central_chi_squared_distribution<value_type>::find_non_centrality(data[i][0], data[i][2], data[i][3]),
413 data[i][1], precision, i);
414 }
415 if((data[i][4] < 0.99) && (data[i][4] != 0))
416 {
417 BOOST_CHECK_CLOSE_EX(
418 boost::math::non_central_chi_squared_distribution<value_type>::find_degrees_of_freedom(boost::math::complement(data[i][1], data[i][2], data[i][4])),
419 data[i][0], precision, i);
420 BOOST_CHECK_CLOSE_EX(
421 boost::math::non_central_chi_squared_distribution<value_type>::find_non_centrality(boost::math::complement(data[i][0], data[i][2], data[i][4])),
422 data[i][1], precision, i);
423 }
424 #ifndef BOOST_NO_EXCEPTIONS
425 }
426 catch(const std::exception& e)
427 {
428 BOOST_ERROR(e.what());
429 }
430 #endif
431 }
432 }
433 #endif
434 }
435
436 template <typename T>
437 void test_accuracy(T, const char* type_name)
438 {
439 #include "nccs.ipp"
440 do_test_nc_chi_squared<T>(nccs, type_name, "Non Central Chi Squared, medium parameters");
441 quantile_sanity_check<T>(nccs, type_name, "Non Central Chi Squared, medium parameters");
442
443 #include "nccs_big.ipp"
444 do_test_nc_chi_squared<T>(nccs_big, type_name, "Non Central Chi Squared, large parameters");
445 quantile_sanity_check<T>(nccs_big, type_name, "Non Central Chi Squared, large parameters");
446 }
447