📄 test_nc_chi_squared.cpp
字号:
test_spot( static_cast<RealType>(1), // degrees of freedom static_cast<RealType>(1), // non centrality static_cast<RealType>(0.00016), // Chi Squared statistic static_cast<RealType>(0.6121428929881423e-2), // Probability of result (CDF), P static_cast<RealType>(1-0.6121428929881423e-2), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(1), // degrees of freedom static_cast<RealType>(1), // non centrality static_cast<RealType>(0.00393), // Chi Squared statistic static_cast<RealType>(0.3033814229753780e-1), // Probability of result (CDF), P static_cast<RealType>(1-0.3033814229753780e-1), // Q = 1 - P tolerance); RealType tol2 = boost::math::tools::epsilon<RealType>() * 5 * 100; // 5 eps as a percentage boost::math::non_central_chi_squared_distribution<RealType> dist(static_cast<RealType>(8), static_cast<RealType>(12)); RealType x = 7; using namespace std; // ADL of std names. // mean: BOOST_CHECK_CLOSE( mean(dist) , static_cast<RealType>(8+12), tol2); // variance: BOOST_CHECK_CLOSE( variance(dist) , static_cast<RealType>(64), tol2); // std deviation: BOOST_CHECK_CLOSE( standard_deviation(dist) , static_cast<RealType>(8), tol2); // hazard: BOOST_CHECK_CLOSE( hazard(dist, x) , pdf(dist, x) / cdf(complement(dist, x)), tol2); // cumulative hazard: BOOST_CHECK_CLOSE( chf(dist, x) , -log(cdf(complement(dist, x))), tol2); // coefficient_of_variation: BOOST_CHECK_CLOSE( coefficient_of_variation(dist) , standard_deviation(dist) / mean(dist), tol2); // mode: BOOST_CHECK_CLOSE( mode(dist) , static_cast<RealType>(17.184201184730857030170788677340294070728990862663L), sqrt(tolerance * 500)); BOOST_CHECK_CLOSE( median(dist), quantile( boost::math::non_central_chi_squared_distribution<RealType>( static_cast<RealType>(8), static_cast<RealType>(12)), static_cast<RealType>(0.5)), static_cast<RealType>(tol2)); // skewness: BOOST_CHECK_CLOSE( skewness(dist) , static_cast<RealType>(0.6875), tol2); // kurtosis: BOOST_CHECK_CLOSE( kurtosis(dist) , static_cast<RealType>(3.65625), tol2); // kurtosis excess: BOOST_CHECK_CLOSE( kurtosis_excess(dist) , static_cast<RealType>(0.65625), tol2);} // template <class RealType>void test_spots(RealType)template <class T>T nccs_cdf(T df, T nc, T x){ return cdf(boost::math::non_central_chi_squared_distribution<T>(df, nc), x);}template <class T>T nccs_ccdf(T df, T nc, T x){ return cdf(complement(boost::math::non_central_chi_squared_distribution<T>(df, nc), x));}template <typename T>void do_test_nc_chi_squared(T& data, const char* type_name, const char* test){ typedef typename T::value_type row_type; typedef typename row_type::value_type value_type; std::cout << "Testing: " << test << std::endl; value_type (*fp1)(value_type, value_type, value_type) = nccs_cdf; boost::math::tools::test_result<value_type> result; result = boost::math::tools::test( data, bind_func(fp1, 0, 1, 2), extract_result(3)); handle_test_result(result, data[result.worst()], result.worst(), type_name, "CDF", test); fp1 = nccs_ccdf; result = boost::math::tools::test( data, bind_func(fp1, 0, 1, 2), extract_result(4)); handle_test_result(result, data[result.worst()], result.worst(), type_name, "CCDF", test);#ifdef TEST_OTHER fp1 = other::nccs_cdf; result = boost::math::tools::test( data, bind_func(fp1, 0, 1, 2), extract_result(3)); handle_test_result(result, data[result.worst()], result.worst(), type_name, "other::CDF", test);#endif std::cout << std::endl;}template <typename T>void quantile_sanity_check(T& data, const char* type_name, const char* test){ typedef typename T::value_type row_type; typedef typename row_type::value_type value_type; // // Tests with type real_concept take rather too long to run, so // for now we'll disable them: // if(!boost::is_floating_point<value_type>::value) return; std::cout << "Testing: " << type_name << " quantile sanity check, with tests " << test << std::endl; // // These sanity checks test for a round trip accuracy of one half // of the bits in T, unless T is type float, in which case we check // for just one decimal digit. The problem here is the sensitivity // of the functions, not their accuracy. This test data was generated // for the forward functions, which means that when it is used as // the input to the inverses then it is necessarily inexact. This rounding // of the input is what makes the data unsuitable for use as an accuracy check, // and also demonstrates that you can't in general round-trip these functions. // It is however a useful sanity check. // value_type precision = static_cast<value_type>(ldexp(1.0, 1-boost::math::policies::digits<value_type, boost::math::policies::policy<> >()/2)) * 100; if(boost::math::policies::digits<value_type, boost::math::policies::policy<> >() < 50) precision = 1; // 1% or two decimal digits, all we can hope for when the input is truncated to float for(unsigned i = 0; i < data.size(); ++i) { if(data[i][3] == 0) { BOOST_CHECK(0 == quantile(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), data[i][3])); } else if(data[i][3] < 0.9999f) { value_type p = quantile(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), data[i][3]); value_type pt = data[i][2]; BOOST_CHECK_CLOSE_EX(pt, p, precision, i); } if(data[i][4] == 0) { BOOST_CHECK(0 == quantile(complement(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), data[i][3]))); } else if(data[i][4] < 0.9999f) { value_type p = quantile(complement(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), data[i][4])); value_type pt = data[i][2]; BOOST_CHECK_CLOSE_EX(pt, p, precision, i); } if(boost::math::tools::digits<value_type>() > 50) { // // Sanity check mode, the accuracy of // the mode is at *best* the square root of the accuracy of the PDF: // try{ value_type m = mode(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1])); value_type p = pdf(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), m); 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); 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); } catch(const boost::math::evaluation_error& ) {} // // Sanity check degrees-of-freedom finder, don't bother at float // precision though as there's not enough data in the probability // values to get back to the correct degrees of freedom or // non-cenrality parameter: // try{ if((data[i][3] < 0.99) && (data[i][3] != 0)) { BOOST_CHECK_CLOSE_EX( boost::math::non_central_chi_squared_distribution<value_type>::find_degrees_of_freedom(data[i][1], data[i][2], data[i][3]), data[i][0], precision, i); BOOST_CHECK_CLOSE_EX( boost::math::non_central_chi_squared_distribution<value_type>::find_non_centrality(data[i][0], data[i][2], data[i][3]), data[i][1], precision, i); } if((data[i][4] < 0.99) && (data[i][4] != 0)) { BOOST_CHECK_CLOSE_EX( 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])), data[i][0], precision, i); BOOST_CHECK_CLOSE_EX( boost::math::non_central_chi_squared_distribution<value_type>::find_non_centrality(boost::math::complement(data[i][0], data[i][2], data[i][4])), data[i][1], precision, i); } } catch(const std::exception& e) { BOOST_ERROR(e.what()); } } }}template <typename T>void test_accuracy(T, const char* type_name){#include "nccs.ipp" do_test_nc_chi_squared(nccs, type_name, "Non Central Chi Squared, medium parameters"); quantile_sanity_check(nccs, type_name, "Non Central Chi Squared, medium parameters");#include "nccs_big.ipp" do_test_nc_chi_squared(nccs_big, type_name, "Non Central Chi Squared, large parameters"); quantile_sanity_check(nccs_big, type_name, "Non Central Chi Squared, large parameters");}int test_main(int, char* []){ BOOST_MATH_CONTROL_FP; // Basic sanity-check spot values. expected_results(); // (Parameter value, arbitrarily zero, only communicates the floating point type).#ifdef TEST_FLOAT test_spots(0.0F); // Test float.#endif#ifdef TEST_DOUBLE test_spots(0.0); // Test double.#endif#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS#ifdef TEST_LDOUBLE test_spots(0.0L); // Test long double.#endif#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))#ifdef TEST_REAL_CONCEPT test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.#endif#endif#endif#ifdef TEST_FLOAT test_accuracy(0.0F, "float"); // Test float.#endif#ifdef TEST_DOUBLE test_accuracy(0.0, "double"); // Test double.#endif#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS#ifdef TEST_LDOUBLE test_accuracy(0.0L, "long double"); // Test long double.#endif#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))#ifdef TEST_REAL_CONCEPT test_accuracy(boost::math::concepts::real_concept(0.), "real_concept"); // Test real concept.#endif#endif#endif return 0;} // int test_main(int, char* [])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -