📄 test_nc_beta.cpp
字号:
// test_nc_beta.cpp// Copyright John Maddock 2008.// Use, modification and distribution are subject to the// Boost Software License, Version 1.0.// (See accompanying file LICENSE_1_0.txt// or copy at http://www.boost.org/LICENSE_1_0.txt)#ifdef _MSC_VER#pragma warning (disable:4127 4512)#endif#if !defined(TEST_FLOAT) && !defined(TEST_DOUBLE) && !defined(TEST_LDOUBLE) && !defined(TEST_REAL_CONCEPT)# define TEST_FLOAT# define TEST_DOUBLE# define TEST_LDOUBLE# define TEST_REAL_CONCEPT#endif#include <boost/math/concepts/real_concept.hpp> // for real_concept#include <boost/math/distributions/non_central_beta.hpp> // for chi_squared_distribution#include <boost/math/distributions/poisson.hpp> // for chi_squared_distribution#include <boost/test/included/test_exec_monitor.hpp> // for test_main#include <boost/test/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE#include "functor.hpp"#include "handle_test_result.hpp"#include "test_ncbeta_hooks.hpp"#include <iostream>using std::cout;using std::endl;#include <limits>using std::numeric_limits;#define BOOST_CHECK_CLOSE_EX(a, b, prec, i) \ {\ unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\ BOOST_CHECK_CLOSE(a, b, prec); \ if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\ {\ std::cerr << "Failure was at row " << i << std::endl;\ std::cerr << std::setprecision(35); \ std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\ std::cerr << " , " << data[i][3] << " , " << data[i][4] << " } " << std::endl;\ }\ }#define BOOST_CHECK_EX(a, i) \ {\ unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\ BOOST_CHECK(a); \ if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\ {\ std::cerr << "Failure was at row " << i << std::endl;\ std::cerr << std::setprecision(35); \ std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\ std::cerr << " , " << data[i][3] << " , " << data[i][4] << " } " << std::endl;\ }\ }void expected_results(){ // // Define the max and mean errors expected for // various compilers and platforms. // const char* largest_type;#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS if(boost::math::policies::digits<double, boost::math::policies::policy<> >() == boost::math::policies::digits<long double, boost::math::policies::policy<> >()) { largest_type = "(long\\s+)?double|real_concept"; } else { largest_type = "long double|real_concept"; }#else largest_type = "(long\\s+)?double|real_concept";#endif#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS if(boost::math::tools::digits<long double>() == 64) { // // Allow a small amount of error leakage from long double to double: // add_expected_result( "[^|]*", // compiler "[^|]*", // stdlib "[^|]*", // platform "double", // test type(s) "[^|]*large[^|]*", // test data group "[^|]*", 5, 5); // test function } if(boost::math::tools::digits<long double>() == 64) { add_expected_result( "[^|]*", // compiler "[^|]*", // stdlib "[^|]*", // platform largest_type, // test type(s) "[^|]*medium[^|]*", // test data group "[^|]*", 900, 500); // test function add_expected_result( "[^|]*", // compiler "[^|]*", // stdlib "[^|]*", // platform largest_type, // test type(s) "[^|]*large[^|]*", // test data group "[^|]*", 40000, 5500); // test function }#endif // // Catch all cases come last: // add_expected_result( "[^|]*", // compiler "[^|]*", // stdlib "[^|]*", // platform largest_type, // test type(s) "[^|]*medium[^|]*", // test data group "[^|]*", 700, 500); // test function add_expected_result( "[^|]*", // compiler "[^|]*", // stdlib "[^|]*", // platform "real_concept", // test type(s) "[^|]*large[^|]*", // test data group "[^|]*", 30000, 4000); // test function add_expected_result( "[^|]*", // compiler "[^|]*", // stdlib "[^|]*", // platform largest_type, // test type(s) "[^|]*large[^|]*", // test data group "[^|]*", 10000, 2000); // test function // // Finish off by printing out the compiler/stdlib/platform names, // we do this to make it easier to mark up expected error rates. // std::cout << "Tests run with " << BOOST_COMPILER << ", " << BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;}template <class RealType>RealType naive_pdf(RealType a, RealType b, RealType lam, RealType x){ using namespace boost::math; RealType term = pdf(poisson_distribution<RealType>(lam/2), 0) * ibeta_derivative(a, b, x); RealType sum = term; int i = 1; while(term / sum > tools::epsilon<RealType>()) { term = pdf(poisson_distribution<RealType>(lam/2), i) * ibeta_derivative(a + i, b, x); ++i; sum += term; } return sum;}template <class RealType>void test_spot( RealType a, // alpha RealType b, // beta RealType ncp, // non-centrality param RealType cs, // Chi Square statistic RealType P, // CDF RealType Q, // Complement of CDF RealType D, // PDF RealType tol) // Test tolerance{ boost::math::non_central_beta_distribution<RealType> dist(a, b, ncp); BOOST_CHECK_CLOSE( cdf(dist, cs), P, tol); // // Sanity checking using the naive PDF calculation above fails at // float precision: // if(!boost::is_same<float, RealType>::value) { BOOST_CHECK_CLOSE( pdf(dist, cs), naive_pdf(dist.alpha(), dist.beta(), ncp, cs), tol); } BOOST_CHECK_CLOSE( pdf(dist, cs), D, tol); if((P < 0.99) && (Q < 0.99)) { // // We can only check this if P is not too close to 1, // so that we can guarentee Q is reasonably free of error: // BOOST_CHECK_CLOSE( cdf(complement(dist, cs)), Q, tol); BOOST_CHECK_CLOSE( quantile(dist, P), cs, tol * 10); BOOST_CHECK_CLOSE( quantile(complement(dist, Q)), cs, tol * 10); }}template <class RealType> // Any floating-point type RealType.void test_spots(RealType){ RealType tolerance = (std::max)( boost::math::tools::epsilon<RealType>() * 100, (RealType)1e-6) * 100; cout << "Tolerance = " << tolerance << "%." << endl; // // Spot tests use values computed by the R statistical // package and the pbeta and dbeta functions: // test_spot( RealType(2), // alpha RealType(5), // beta
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -