📄 test_chi_squared.cpp
字号:
void test_spots(RealType){ // Basic sanity checks, test data is to three decimal places only // so set tolerance to 0.001 expressed as a persentage. RealType tolerance = 0.001f * 100; cout << "Tolerance = " << tolerance << "%." << endl; using boost::math::chi_squared_distribution; using ::boost::math::chi_squared; using ::boost::math::cdf; using ::boost::math::pdf; for(unsigned i = 0; i < sizeof(upper_critical_values) / sizeof(upper_critical_values[0]); ++i) { test_spot( static_cast<RealType>(upper_critical_values[i][0]), // degrees of freedom static_cast<RealType>(upper_critical_values[i][1]), // Chi Squared statistic static_cast<RealType>(1 - q[0]), // Probability of result (CDF), P static_cast<RealType>(q[0]), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(upper_critical_values[i][0]), // degrees of freedom static_cast<RealType>(upper_critical_values[i][2]), // Chi Squared statistic static_cast<RealType>(1 - q[1]), // Probability of result (CDF), P static_cast<RealType>(q[1]), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(upper_critical_values[i][0]), // degrees of freedom static_cast<RealType>(upper_critical_values[i][3]), // Chi Squared statistic static_cast<RealType>(1 - q[2]), // Probability of result (CDF), P static_cast<RealType>(q[2]), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(upper_critical_values[i][0]), // degrees of freedom static_cast<RealType>(upper_critical_values[i][4]), // Chi Squared statistic static_cast<RealType>(1 - q[3]), // Probability of result (CDF), P static_cast<RealType>(q[3]), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(upper_critical_values[i][0]), // degrees of freedom static_cast<RealType>(upper_critical_values[i][5]), // Chi Squared statistic static_cast<RealType>(1 - q[4]), // Probability of result (CDF), P static_cast<RealType>(q[4]), // Q = 1 - P tolerance); } for(unsigned i = 0; i < sizeof(lower_critical_values) / sizeof(lower_critical_values[0]); ++i) { test_spot( static_cast<RealType>(lower_critical_values[i][0]), // degrees of freedom static_cast<RealType>(lower_critical_values[i][1]), // Chi Squared statistic static_cast<RealType>(q[0]), // Probability of result (CDF), P static_cast<RealType>(1 - q[0]), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(lower_critical_values[i][0]), // degrees of freedom static_cast<RealType>(lower_critical_values[i][2]), // Chi Squared statistic static_cast<RealType>(q[1]), // Probability of result (CDF), P static_cast<RealType>(1 - q[1]), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(lower_critical_values[i][0]), // degrees of freedom static_cast<RealType>(lower_critical_values[i][3]), // Chi Squared statistic static_cast<RealType>(q[2]), // Probability of result (CDF), P static_cast<RealType>(1 - q[2]), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(lower_critical_values[i][0]), // degrees of freedom static_cast<RealType>(lower_critical_values[i][4]), // Chi Squared statistic static_cast<RealType>(q[3]), // Probability of result (CDF), P static_cast<RealType>(1 - q[3]), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(lower_critical_values[i][0]), // degrees of freedom static_cast<RealType>(lower_critical_values[i][5]), // Chi Squared statistic static_cast<RealType>(q[4]), // Probability of result (CDF), P static_cast<RealType>(1 - q[4]), // Q = 1 - P tolerance); } RealType tol2 = boost::math::tools::epsilon<RealType>() * 5 * 100; // 5 eps as a percentage chi_squared_distribution<RealType> dist(static_cast<RealType>(8)); RealType x = 7; using namespace std; // ADL of std names. // mean: BOOST_CHECK_CLOSE( mean(dist) , static_cast<RealType>(8), tol2); // variance: BOOST_CHECK_CLOSE( variance(dist) , static_cast<RealType>(16), tol2); // std deviation: BOOST_CHECK_CLOSE( standard_deviation(dist) , static_cast<RealType>(4), 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>(6), tol2); BOOST_CHECK_CLOSE( median(dist), quantile( chi_squared_distribution<RealType>(static_cast<RealType>(8)), static_cast<RealType>(0.5)), static_cast<RealType>(tol2)); // skewness: BOOST_CHECK_CLOSE( skewness(dist) , static_cast<RealType>(1), tol2); // kurtosis: BOOST_CHECK_CLOSE( kurtosis(dist) , static_cast<RealType>(4.5), tol2); // kurtosis excess: BOOST_CHECK_CLOSE( kurtosis_excess(dist) , static_cast<RealType>(1.5), tol2); // special cases: BOOST_CHECK_THROW( pdf( chi_squared_distribution<RealType>(static_cast<RealType>(1)), static_cast<RealType>(0)), std::overflow_error ); BOOST_CHECK_EQUAL( pdf(chi_squared_distribution<RealType>(2), static_cast<RealType>(0)) , static_cast<RealType>(0.5f)); BOOST_CHECK_EQUAL( pdf(chi_squared_distribution<RealType>(3), static_cast<RealType>(0)) , static_cast<RealType>(0.0f)); BOOST_CHECK_EQUAL( cdf(chi_squared_distribution<RealType>(1), static_cast<RealType>(0)) , static_cast<RealType>(0.0f)); BOOST_CHECK_EQUAL( cdf(chi_squared_distribution<RealType>(2), static_cast<RealType>(0)) , static_cast<RealType>(0.0f)); BOOST_CHECK_EQUAL( cdf(chi_squared_distribution<RealType>(3), static_cast<RealType>(0)) , static_cast<RealType>(0.0f)); BOOST_CHECK_EQUAL( cdf(complement(chi_squared_distribution<RealType>(1), static_cast<RealType>(0))) , static_cast<RealType>(1)); BOOST_CHECK_EQUAL( cdf(complement(chi_squared_distribution<RealType>(2), static_cast<RealType>(0))) , static_cast<RealType>(1)); BOOST_CHECK_EQUAL( cdf(complement(chi_squared_distribution<RealType>(3), static_cast<RealType>(0))) , static_cast<RealType>(1)); BOOST_CHECK_THROW( pdf( chi_squared_distribution<RealType>(static_cast<RealType>(-1)), static_cast<RealType>(1)), std::domain_error ); BOOST_CHECK_THROW( pdf( chi_squared_distribution<RealType>(static_cast<RealType>(8)), static_cast<RealType>(-1)), std::domain_error ); BOOST_CHECK_THROW( cdf( chi_squared_distribution<RealType>(static_cast<RealType>(-1)), static_cast<RealType>(1)), std::domain_error ); BOOST_CHECK_THROW( cdf( chi_squared_distribution<RealType>(static_cast<RealType>(8)), static_cast<RealType>(-1)), std::domain_error ); BOOST_CHECK_THROW( cdf(complement( chi_squared_distribution<RealType>(static_cast<RealType>(-1)), static_cast<RealType>(1))), std::domain_error ); BOOST_CHECK_THROW( cdf(complement( chi_squared_distribution<RealType>(static_cast<RealType>(8)), static_cast<RealType>(-1))), std::domain_error ); BOOST_CHECK_THROW( quantile( chi_squared_distribution<RealType>(static_cast<RealType>(-1)), static_cast<RealType>(0.5)), std::domain_error ); BOOST_CHECK_THROW( quantile( chi_squared_distribution<RealType>(static_cast<RealType>(8)), static_cast<RealType>(-1)), std::domain_error ); BOOST_CHECK_THROW( quantile( chi_squared_distribution<RealType>(static_cast<RealType>(8)), static_cast<RealType>(1.1)), std::domain_error ); BOOST_CHECK_THROW( quantile(complement( chi_squared_distribution<RealType>(static_cast<RealType>(-1)), static_cast<RealType>(0.5))), std::domain_error ); BOOST_CHECK_THROW( quantile(complement( chi_squared_distribution<RealType>(static_cast<RealType>(8)), static_cast<RealType>(-1))), std::domain_error ); BOOST_CHECK_THROW( quantile(complement( chi_squared_distribution<RealType>(static_cast<RealType>(8)), static_cast<RealType>(1.1))), std::domain_error ); // This first test value is taken from an example here: // http://www.itl.nist.gov/div898/handbook/prc/section2/prc232.htm // Subsequent tests just test our empirically generated values, they // catch regressions, but otherwise aren't worth much. BOOST_CHECK_EQUAL( ceil(chi_squared_distribution<RealType>::find_degrees_of_freedom( 55, 0.05f, 0.01f, 100)), static_cast<RealType>(170)); BOOST_CHECK_EQUAL( ceil(chi_squared_distribution<RealType>::find_degrees_of_freedom( 10, 0.05f, 0.01f, 100)), static_cast<RealType>(3493)); BOOST_CHECK_EQUAL( ceil(chi_squared_distribution<RealType>::find_degrees_of_freedom( -55, 0.05f, 0.01f, 100)), static_cast<RealType>(49)); BOOST_CHECK_EQUAL( ceil(chi_squared_distribution<RealType>::find_degrees_of_freedom( -10, 0.05f, 0.01f, 100)), static_cast<RealType>(2826));} // template <class RealType>void test_spots(RealType)int test_main(int, char* []){ BOOST_MATH_CONTROL_FP; // Check that can generate chi_squared distribution using the two convenience methods: chi_squared_distribution<> mychisqr(8); chi_squared mychisqr2(8); // Basic sanity-check spot values. // (Parameter value, arbitrarily zero, only communicates the floating point type). test_spots(0.0F); // Test float. test_spots(0.0); // Test double.#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS test_spots(0.0L); // Test long double.#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.#endif#endif return 0;} // int test_main(int, char* [])/*Output:Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_chi_squared.exe"Running 1 test case...Tolerance = 0.1%.Tolerance = 0.1%.Tolerance = 0.1%.Tolerance = 0.1%.*** No errors detected*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -