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)
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"
16 #include "handle_test_result.hpp"
17 #include "table_type.hpp"
22 #define BOOST_CHECK_CLOSE_EX(a, b, prec, i) \
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)\
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;\
35 #define BOOST_CHECK_EX(a, i) \
37 unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
39 if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
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;\
48 template <class RealType>
49 RealType naive_pdf(RealType v, RealType lam, RealType x)
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);
59 prefix *= lam / (2 * i);
60 term = prefix * pdf(boost::math::chi_squared_distribution<RealType>(v + 2 * i), x);
65 return sum * exp(-lam / 2);
68 template <class RealType>
70 RealType df, // Degrees of freedom
71 RealType ncp, // non-centrality param
72 RealType cs, // Chi Square statistic
74 RealType Q, // Complement of CDF
75 RealType tol) // Test tolerance
77 boost::math::non_central_chi_squared_distribution<RealType> dist(df, ncp);
79 cdf(dist, cs), P, tol);
80 #ifndef BOOST_NO_EXCEPTIONS
83 pdf(dist, cs), naive_pdf(dist.degrees_of_freedom(), ncp, cs), tol * 150);
85 catch(const std::overflow_error&)
89 if((P < 0.99) && (Q < 0.99))
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:
96 cdf(complement(dist, cs)), Q, tol);
98 quantile(dist, P), cs, tol * 10);
100 quantile(complement(dist, Q)), cs, tol * 10);
102 dist.find_degrees_of_freedom(ncp, cs, P), df, tol * 10);
104 dist.find_degrees_of_freedom(boost::math::complement(ncp, cs, Q)), df, tol * 10);
106 dist.find_non_centrality(df, cs, P), ncp, tol * 10);
108 dist.find_non_centrality(boost::math::complement(df, cs, Q)), ncp, tol * 10);
112 template <class RealType> // Any floating-point type RealType.
113 void test_spots(RealType)
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;
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.
124 if(boost::math::tools::digits<RealType>() < 50)
126 if(boost::is_floating_point<RealType>::value != 1)
127 tolerance *= 20; // real_concept special functions are less accurate
129 std::cout << "Tolerance = " << tolerance << "%." << std::endl;
131 using boost::math::chi_squared_distribution;
132 using ::boost::math::chi_squared;
133 using ::boost::math::cdf;
134 using ::boost::math::pdf;
136 // Test against the data from Table 6 of:
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.
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
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
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
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
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
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
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
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
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
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
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));
218 using namespace std; // ADL of std names.
222 , static_cast<RealType>(8 + 12), tol2);
226 , static_cast<RealType>(64), tol2);
229 standard_deviation(dist)
230 , static_cast<RealType>(8), tol2);
234 , pdf(dist, x) / cdf(complement(dist, x)), tol2);
235 // cumulative hazard:
238 , -log(cdf(complement(dist, x))), tol2);
239 // coefficient_of_variation:
241 coefficient_of_variation(dist)
242 , standard_deviation(dist) / mean(dist), tol2);
246 , static_cast<RealType>(17.184201184730857030170788677340294070728990862663L), sqrt(tolerance * 500));
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));
257 , static_cast<RealType>(0.6875), tol2);
261 , static_cast<RealType>(3.65625), tol2);
264 kurtosis_excess(dist)
265 , static_cast<RealType>(0.65625), tol2);
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);
275 } // template <class RealType>void test_spots(RealType)
278 T nccs_cdf(T df, T nc, T x)
280 return cdf(boost::math::non_central_chi_squared_distribution<T>(df, nc), x);
284 T nccs_ccdf(T df, T nc, T x)
286 return cdf(complement(boost::math::non_central_chi_squared_distribution<T>(df, nc), x));
289 template <typename Real, typename T>
290 void do_test_nc_chi_squared(T& data, const char* type_name, const char* test)
292 typedef typename T::value_type row_type;
293 typedef Real value_type;
295 std::cout << "Testing: " << test << std::endl;
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;
300 value_type(*fp1)(value_type, value_type, value_type) = nccs_cdf;
302 boost::math::tools::test_result<value_type> result;
304 #if !(defined(ERROR_REPORTING_MODE) && !defined(NC_CHI_SQUARED_CDF_FUNCTION_TO_TEST))
305 result = boost::math::tools::test_hetero<Real>(
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);
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;
318 result = boost::math::tools::test_hetero<Real>(
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);
325 std::cout << std::endl;
329 template <typename Real, typename T>
330 void quantile_sanity_check(T& data, const char* type_name, const char* test)
332 #ifndef ERROR_REPORTING_MODE
333 typedef typename T::value_type row_type;
334 typedef Real value_type;
337 // Tests with type real_concept take rather too long to run, so
338 // for now we'll disable them:
340 if(!boost::is_floating_point<value_type>::value)
343 std::cout << "Testing: " << type_name << " quantile sanity check, with tests " << test << std::endl;
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.
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
360 for(unsigned i = 0; i < data.size(); ++i)
362 if(Real(data[i][3]) == 0)
364 BOOST_CHECK(0 == quantile(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), data[i][3]));
366 else if(data[i][3] < 0.9999f)
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);
374 BOOST_CHECK(0 == quantile(complement(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), data[i][3])));
376 else if(data[i][4] < 0.9999f)
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);
382 if(boost::math::tools::digits<value_type>() > 50)
385 // Sanity check mode, the accuracy of
386 // the mode is at *best* the square root of the accuracy of the PDF:
388 #ifndef BOOST_NO_EXCEPTIONS
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);
395 catch(const boost::math::evaluation_error&) {}
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:
403 #ifndef BOOST_NO_EXCEPTIONS
406 if((data[i][3] < 0.99) && (data[i][3] != 0))
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);
415 if((data[i][4] < 0.99) && (data[i][4] != 0))
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);
424 #ifndef BOOST_NO_EXCEPTIONS
426 catch(const std::exception& e)
428 BOOST_ERROR(e.what());
436 template <typename T>
437 void test_accuracy(T, const char* type_name)
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");
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");