📄 test_triangular.cpp
字号:
pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(-std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); } // test for x = NaN using std::numeric_limits<>::quiet_NaN() // cdf BOOST_CHECK_EQUAL( // x < lower cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(-1)), static_cast<RealType>(0) ); BOOST_CHECK_CLOSE_FRACTION( // x == lower cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0)), static_cast<RealType>(0), tolerance); BOOST_CHECK_CLOSE_FRACTION( // x == upper cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(1)), static_cast<RealType>(1), tolerance); BOOST_CHECK_EQUAL( // x > upper cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(2)), static_cast<RealType>(1)); BOOST_CHECK_CLOSE_FRACTION( // x == mode cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(0)), //static_cast<RealType>((mode - lower) / (upper - lower)), static_cast<RealType>(0.5), // (0 --1) / (1 -- 1) = 0.5 tolerance); BOOST_CHECK_CLOSE_FRACTION( cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0.9L)), static_cast<RealType>(0.81L), tolerance); BOOST_CHECK_CLOSE_FRACTION( cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(-1)), static_cast<RealType>(0), tolerance); BOOST_CHECK_CLOSE_FRACTION( cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(-0.5L)), static_cast<RealType>(0.125L), tolerance); BOOST_CHECK_CLOSE_FRACTION( cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(0)), static_cast<RealType>(0.5), tolerance); BOOST_CHECK_CLOSE_FRACTION( cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(+0.5L)), static_cast<RealType>(0.875L), tolerance); BOOST_CHECK_CLOSE_FRACTION( cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(1)), static_cast<RealType>(1), tolerance); // cdf complement BOOST_CHECK_EQUAL( // x < lower cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(-1))), static_cast<RealType>(1)); BOOST_CHECK_EQUAL( // x == lower cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(0))), static_cast<RealType>(1)); BOOST_CHECK_EQUAL( // x == mode cdf(complement(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(0))), static_cast<RealType>(0.5)); BOOST_CHECK_EQUAL( // x == mode cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(0))), static_cast<RealType>(1)); BOOST_CHECK_EQUAL( // x == mode cdf(complement(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(1))), static_cast<RealType>(0)); BOOST_CHECK_EQUAL( // x > upper cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(2))), static_cast<RealType>(0)); BOOST_CHECK_EQUAL( // x == upper cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(1))), static_cast<RealType>(0)); BOOST_CHECK_CLOSE_FRACTION( // x = -0.5 cdf(complement(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(-0.5))), static_cast<RealType>(0.875L), tolerance); BOOST_CHECK_CLOSE_FRACTION( // x = +0.5 cdf(complement(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(0.5))), static_cast<RealType>(0.125), tolerance); triangular_distribution<RealType> triang; // Using typedef == triangular_distribution<double> tristd; triangular_distribution<RealType> tristd(0, 0.5, 1); // 'Standard' triangular distribution. BOOST_CHECK_CLOSE_FRACTION( // median of Standard triangular is sqrt(mode/2) if c > 1/2 else 1 - sqrt((1-c)/2) median(tristd), static_cast<RealType>(0.5), tolerance); triangular_distribution<RealType> tri011(0, 1, 1); // Using default RealType double. triangular_distribution<RealType> tri0q1(0, 0.25, 1); // mode is near bottom. triangular_distribution<RealType> tri0h1(0, 0.5, 1); // Equilateral triangle - mode is the middle. triangular_distribution<RealType> trim12(-1, -0.5, 2); // mode is negative. BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.02L), static_cast<RealType>(0.0016L), tolerance); BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.5L), static_cast<RealType>(0.66666666666666666666666666666666666666666666667L), tolerance); BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.98L), static_cast<RealType>(0.9994666666666666666666666666666666666666666666L), tolerance); // quantile BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, static_cast<RealType>(0.0016L)), static_cast<RealType>(0.02L), tol5eps); BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, static_cast<RealType>(0.66666666666666666666666666666666666666666666667L)), static_cast<RealType>(0.5), tol5eps); BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0q1, static_cast<RealType>(0.3333333333333333333333333333333333333333333333333L))), static_cast<RealType>(0.5), tol5eps); BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, static_cast<RealType>(0.999466666666666666666666666666666666666666666666666L)), static_cast<RealType>(98) / 100, 10 * tol5eps); BOOST_CHECK_CLOSE_FRACTION(pdf(trim12, 0), static_cast<RealType>(0.533333333333333333333333333333333333333333333L), tol5eps); BOOST_CHECK_CLOSE_FRACTION(cdf(trim12, 0), static_cast<RealType>(0.466666666666666666666666666666666666666666667L), tol5eps); BOOST_CHECK_CLOSE_FRACTION(cdf(complement(trim12, 0)), static_cast<RealType>(1 - 0.466666666666666666666666666666666666666666667L), tol5eps); BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0q1, static_cast<RealType>(1 - 0.999466666666666666666666666666666666666666666666L))), static_cast<RealType>(0.98L), 10 * tol5eps); BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, static_cast<RealType>(1))), static_cast<RealType>(0), tol5eps); BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, static_cast<RealType>(0.5))), static_cast<RealType>(0.5), tol5eps); // OK BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, static_cast<RealType>(1 - 0.02L))), static_cast<RealType>(0.1L), tol5eps); BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, static_cast<RealType>(1 - 0.98L))), static_cast<RealType>(0.9L), tol5eps); BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 0)), static_cast<RealType>(1), tol5eps); RealType xs [] = {0, 0.01L, 0.02L, 0.05L, 0.1L, 0.2L, 0.3L, 0.4L, 0.5L, 0.6L, 0.7L, 0.8L, 0.9L, 0.95L, 0.98L, 0.99L, 1}; const triangular_distribution<RealType>& distr = triang; BOOST_CHECK_CLOSE_FRACTION(quantile(complement(distr, 1.)), static_cast<RealType>(-1), tol5eps); const triangular_distribution<RealType>* distp = &triang; BOOST_CHECK_CLOSE_FRACTION(quantile(complement(*distp, 1.)), static_cast<RealType>(-1), tol5eps); const triangular_distribution<RealType>* dists [] = {&tristd, &tri011, &tri0q1, &tri0h1, &trim12}; BOOST_CHECK_CLOSE_FRACTION(quantile(complement(*dists[1], 1.)), static_cast<RealType>(0), tol5eps); for (int i = 0; i < 5; i++) { const triangular_distribution<RealType>* const dist = dists[i]; // cout << "Distribution " << i << endl; BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.5L), quantile(complement(*dist, 0.5L)), tol5eps); BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.98L), quantile(complement(*dist, 1.L - 0.98L)),tol5eps); BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.98L), quantile(complement(*dist, 1.L - 0.98L)),tol5eps); } // for i // quantile complement for (int i = 0; i < 5; i++) { const triangular_distribution<RealType>* const dist = dists[i]; //cout << "Distribution " << i << endl; BOOST_CHECK_EQUAL(quantile(complement(*dists[i], 1.)), quantile(*dists[i], 0.)); for (unsigned j = 0; j < sizeof(xs) /sizeof(RealType); j++) { RealType x = xs[j]; BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], x), quantile(complement(*dist, 1 - x)), tol5eps); } // for j } // for i check_triangular( static_cast<RealType>(0), // lower static_cast<RealType>(0.5), // mode static_cast<RealType>(1), // upper static_cast<RealType>(0.5), // x static_cast<RealType>(0.5), // p static_cast<RealType>(1 - 0.5), // q tolerance); // Some Not-standard triangular tests. check_triangular( static_cast<RealType>(-1), // lower static_cast<RealType>(0), // mode static_cast<RealType>(1), // upper static_cast<RealType>(0), // x static_cast<RealType>(0.5), // p static_cast<RealType>(1 - 0.5), // q = 1 - p tolerance); check_triangular( static_cast<RealType>(1), // lower static_cast<RealType>(1), // mode static_cast<RealType>(3), // upper static_cast<RealType>(2), // x static_cast<RealType>(0.75), // p static_cast<RealType>(1 - 0.75), // q = 1 - p tolerance); check_triangular( static_cast<RealType>(-1), // lower static_cast<RealType>(1), // mode static_cast<RealType>(2), // upper static_cast<RealType>(1), // x static_cast<RealType>(0.66666666666666666666666666666666666666666667L), // p static_cast<RealType>(0.33333333333333333333333333333333333333333333L), // q = 1 - p tolerance); tolerance = (std::max)( boost::math::tools::epsilon<RealType>(), static_cast<RealType>(boost::math::tools::epsilon<double>())) * 10; // 10 eps as a fraction. cout << "Tolerance (as fraction) for type " << typeid(RealType).name() << " is " << tolerance << "." << endl; triangular_distribution<RealType> tridef; // (-1, 0, 1) // default RealType x = static_cast<RealType>(0.5); using namespace std; // ADL of std names. // mean: BOOST_CHECK_CLOSE_FRACTION( mean(tridef), static_cast<RealType>(0), tolerance); // variance: BOOST_CHECK_CLOSE_FRACTION( variance(tridef), static_cast<RealType>(0.16666666666666666666666666666666666666666667L), tolerance); // was 0.0833333333333333333333333333333333333333333L // std deviation: BOOST_CHECK_CLOSE_FRACTION( standard_deviation(tridef), sqrt(variance(tridef)), tolerance); // hazard: BOOST_CHECK_CLOSE_FRACTION( hazard(tridef, x), pdf(tridef, x) / cdf(complement(tridef, x)), tolerance); // cumulative hazard: BOOST_CHECK_CLOSE_FRACTION( chf(tridef, x), -log(cdf(complement(tridef, x))), tolerance); // coefficient_of_variation: if (mean(tridef) != 0) { BOOST_CHECK_CLOSE_FRACTION( coefficient_of_variation(tridef), standard_deviation(tridef) / mean(tridef), tolerance); } // mode: BOOST_CHECK_CLOSE_FRACTION( mode(tridef), static_cast<RealType>(0), tolerance); // skewness: BOOST_CHECK_CLOSE_FRACTION( median(trim12), static_cast<RealType>(-0.13397459621556151), tolerance); BOOST_CHECK_EQUAL( skewness(tridef), static_cast<RealType>(0)); // kurtosis: BOOST_CHECK_CLOSE_FRACTION( kurtosis_excess(tridef), kurtosis(tridef) - static_cast<RealType>(3L), tolerance); // kurtosis excess = kurtosis - 3; BOOST_CHECK_CLOSE_FRACTION( kurtosis_excess(tridef), static_cast<RealType>(-0.6), tolerance); // for all distributions. if(std::numeric_limits<RealType>::has_infinity) { // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -