⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_negative_binomial.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 CPP
📖 第 1 页 / 共 3 页
字号:
  static_cast<RealType>(0));  BOOST_CHECK_EQUAL(  quantile(  // min P < cdf(0) so should be exactly zero.  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),  static_cast<RealType>(boost::math::tools::min_value<RealType>())),  static_cast<RealType>(0));  BOOST_CHECK_CLOSE_FRACTION(  quantile(  // Small P < cdf(0) so should be near zero.  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),  static_cast<RealType>(boost::math::tools::epsilon<RealType>())), //   static_cast<RealType>(0),    tol5eps);  BOOST_CHECK_CLOSE(  quantile(  // Small P < cdf(0) so should be exactly zero.  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),  static_cast<RealType>(0.0001)),  static_cast<RealType>(0.95854156929288470),    tolerance);  //BOOST_CHECK(  // Fails with overflow for real_concept  //quantile(  // Small P near 1 so k failures should be big.  //negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),  //static_cast<RealType>(1 - boost::math::tools::epsilon<RealType>())) <=  //static_cast<RealType>(189.56999032670058)  // 106.462769 for float  //);  if(std::numeric_limits<RealType>::has_infinity)  { // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity()    // Note that infinity is not implemented for real_concept, so these tests    // are only done for types, like built-in float, double.. that have infinity.    // Note that these assume that  BOOST_MATH_OVERFLOW_ERROR_POLICY is NOT throw_on_error.    // #define BOOST_MATH_THROW_ON_OVERFLOW_POLICY ==  throw_on_error would throw here.    // #define BOOST_MAT_DOMAIN_ERROR_POLICY IS defined throw_on_error,    //  so the throw path of error handling is tested below with BOOST_CHECK_THROW tests.    BOOST_CHECK(    quantile(  // At P == 1 so k failures should be infinite.    negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),    static_cast<RealType>(1)) ==    //static_cast<RealType>(boost::math::tools::infinity<RealType>())    static_cast<RealType>(std::numeric_limits<RealType>::infinity()) );    BOOST_CHECK_EQUAL(    quantile(  // At 1 == P  so should be infinite.    negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),    static_cast<RealType>(1)), //    std::numeric_limits<RealType>::infinity() );    BOOST_CHECK_EQUAL(    quantile(complement(  // Q zero 1 so P == 1 < cdf(0) so should be exactly infinity.    negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),    static_cast<RealType>(0))),    std::numeric_limits<RealType>::infinity() );   } // test for infinity using std::numeric_limits<>::infinity()  else  { // real_concept case, so check it throws rather than returning infinity.    BOOST_CHECK_EQUAL(    quantile(  // At P == 1 so k failures should be infinite.    negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),    static_cast<RealType>(1)),    boost::math::tools::max_value<RealType>() );    BOOST_CHECK_EQUAL(    quantile(complement(  // Q zero 1 so P == 1 < cdf(0) so should be exactly infinity.    negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),    static_cast<RealType>(0))),    boost::math::tools::max_value<RealType>());  }  BOOST_CHECK( // Should work for built-in and real_concept.  quantile(complement(  // Q very near to 1 so P nearly 1  < so should be large > 384.  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),  static_cast<RealType>(boost::math::tools::min_value<RealType>())))   >= static_cast<RealType>(384) );  BOOST_CHECK_EQUAL(  quantile(  //  P ==  0 < cdf(0) so should be zero.  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),  static_cast<RealType>(0)),  static_cast<RealType>(0));  // Quantile Complement boundary cases:  BOOST_CHECK_EQUAL(  quantile(complement(  // Q = 1 so P = 0 < cdf(0) so should be exactly zero.  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),  static_cast<RealType>(1))),  static_cast<RealType>(0)  );  BOOST_CHECK_EQUAL(  quantile(complement(  // Q very near 1 so P == epsilon < cdf(0) so should be exactly zero.  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),  static_cast<RealType>(1 - boost::math::tools::epsilon<RealType>()))),  static_cast<RealType>(0)  );  // Check that duff arguments throw domain_error:  BOOST_CHECK_THROW(  pdf( // Negative successes!  negative_binomial_distribution<RealType>(static_cast<RealType>(-1), static_cast<RealType>(0.25)),  static_cast<RealType>(0)), std::domain_error  );  BOOST_CHECK_THROW(  pdf( // Negative success_fraction!  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(-0.25)),  static_cast<RealType>(0)), std::domain_error  );  BOOST_CHECK_THROW(  pdf( // Success_fraction > 1!  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25)),  static_cast<RealType>(0)),  std::domain_error  );  BOOST_CHECK_THROW(  pdf( // Negative k argument !  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),  static_cast<RealType>(-1)),  std::domain_error  );  //BOOST_CHECK_THROW(  //pdf( // Unlike binomial there is NO limit on k (failures)  //negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),  //static_cast<RealType>(9)), std::domain_error  //);  BOOST_CHECK_THROW(  cdf(  // Negative k argument !  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),  static_cast<RealType>(-1)),  std::domain_error  );  BOOST_CHECK_THROW(  cdf( // Negative success_fraction!  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(-0.25)),  static_cast<RealType>(0)), std::domain_error  );  BOOST_CHECK_THROW(  cdf( // Success_fraction > 1!  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25)),  static_cast<RealType>(0)), std::domain_error  );  BOOST_CHECK_THROW(  quantile(  // Negative success_fraction!  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(-0.25)),  static_cast<RealType>(0)), std::domain_error  );  BOOST_CHECK_THROW(  quantile( // Success_fraction > 1!  negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25)),  static_cast<RealType>(0)), std::domain_error  );  // End of check throwing 'duff' out-of-domain values.#define T RealType#include "negative_binomial_quantile.ipp"  for(unsigned i = 0; i < negative_binomial_quantile_data.size(); ++i)  {     using namespace boost::math::policies;     typedef policy<discrete_quantile<boost::math::policies::real> > P1;     typedef policy<discrete_quantile<integer_round_down> > P2;     typedef policy<discrete_quantile<integer_round_up> > P3;     typedef policy<discrete_quantile<integer_round_outwards> > P4;     typedef policy<discrete_quantile<integer_round_inwards> > P5;     typedef policy<discrete_quantile<integer_round_nearest> > P6;     RealType tol = boost::math::tools::epsilon<RealType>() * 700;     if(!boost::is_floating_point<RealType>::value)        tol *= 10;  // no lanczos approximation implies less accuracy     //     // Check full real value first:     //     negative_binomial_distribution<RealType, P1> p1(negative_binomial_quantile_data[i][0], negative_binomial_quantile_data[i][1]);     RealType x = quantile(p1, negative_binomial_quantile_data[i][2]);     BOOST_CHECK_CLOSE_FRACTION(x, negative_binomial_quantile_data[i][3], tol);     x = quantile(complement(p1, negative_binomial_quantile_data[i][2]));     BOOST_CHECK_CLOSE_FRACTION(x, negative_binomial_quantile_data[i][4], tol);     //     // Now with round down to integer:     //     negative_binomial_distribution<RealType, P2> p2(negative_binomial_quantile_data[i][0], negative_binomial_quantile_data[i][1]);     x = quantile(p2, negative_binomial_quantile_data[i][2]);     BOOST_CHECK_EQUAL(x, floor(negative_binomial_quantile_data[i][3]));     x = quantile(complement(p2, negative_binomial_quantile_data[i][2]));     BOOST_CHECK_EQUAL(x, floor(negative_binomial_quantile_data[i][4]));     //     // Now with round up to integer:     //     negative_binomial_distribution<RealType, P3> p3(negative_binomial_quantile_data[i][0], negative_binomial_quantile_data[i][1]);     x = quantile(p3, negative_binomial_quantile_data[i][2]);     BOOST_CHECK_EQUAL(x, ceil(negative_binomial_quantile_data[i][3]));     x = quantile(complement(p3, negative_binomial_quantile_data[i][2]));     BOOST_CHECK_EQUAL(x, ceil(negative_binomial_quantile_data[i][4]));     //     // Now with round to integer "outside":     //     negative_binomial_distribution<RealType, P4> p4(negative_binomial_quantile_data[i][0], negative_binomial_quantile_data[i][1]);     x = quantile(p4, negative_binomial_quantile_data[i][2]);     BOOST_CHECK_EQUAL(x, negative_binomial_quantile_data[i][2] < 0.5f ? floor(negative_binomial_quantile_data[i][3]) : ceil(negative_binomial_quantile_data[i][3]));     x = quantile(complement(p4, negative_binomial_quantile_data[i][2]));     BOOST_CHECK_EQUAL(x, negative_binomial_quantile_data[i][2] < 0.5f ? ceil(negative_binomial_quantile_data[i][4]) : floor(negative_binomial_quantile_data[i][4]));     //     // Now with round to integer "inside":     //     negative_binomial_distribution<RealType, P5> p5(negative_binomial_quantile_data[i][0], negative_binomial_quantile_data[i][1]);     x = quantile(p5, negative_binomial_quantile_data[i][2]);     BOOST_CHECK_EQUAL(x, negative_binomial_quantile_data[i][2] < 0.5f ? ceil(negative_binomial_quantile_data[i][3]) : floor(negative_binomial_quantile_data[i][3]));     x = quantile(complement(p5, negative_binomial_quantile_data[i][2]));     BOOST_CHECK_EQUAL(x, negative_binomial_quantile_data[i][2] < 0.5f ? floor(negative_binomial_quantile_data[i][4]) : ceil(negative_binomial_quantile_data[i][4]));     //     // Now with round to nearest integer:     //     negative_binomial_distribution<RealType, P6> p6(negative_binomial_quantile_data[i][0], negative_binomial_quantile_data[i][1]);     x = quantile(p6, negative_binomial_quantile_data[i][2]);     BOOST_CHECK_EQUAL(x, floor(negative_binomial_quantile_data[i][3] + 0.5f));     x = quantile(complement(p6, negative_binomial_quantile_data[i][2]));     BOOST_CHECK_EQUAL(x, floor(negative_binomial_quantile_data[i][4] + 0.5f));  }  return;} // template <class RealType> void test_spots(RealType) // Any floating-point type RealType.int test_main(int, char* []){  // Check that can generate negative_binomial distribution using the two convenience methods:  using namespace boost::math;   negative_binomial mynb1(2., 0.5); // Using typedef - default type is double.   negative_binomial_distribution<> myf2(2., 0.5); // Using default RealType double.  // Basic sanity-check spot values.  // Test some simple double only examples.  negative_binomial_distribution<double> my8dist(8., 0.25);  // 8 successes (r), 0.25 success fraction = 35% or 1 in 4 successes.  // Note: double values (matching the distribution definition) avoid the need for any casting.  // Check accessor functions return exact values for double at least.  BOOST_CHECK_EQUAL(my8dist.successes(), static_cast<double>(8));  BOOST_CHECK_EQUAL(my8dist.success_fraction(), static_cast<double>(1./4.));  // (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#else   std::cout << "<note>The long double tests have been disabled on this platform "      "either because the long double overloads of the usual math functions are "      "not available at all, or because they are too inaccurate for these tests "      "to pass.</note>" << std::cout;#endif  return 0;} // int test_main(int, char* [])/*Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_negative_binomial.exe"Running 1 test case...Tolerance = 0.0119209%.Tolerance 5 eps = 5.96046e-007%.Tolerance = 2.22045e-011%.Tolerance 5 eps = 1.11022e-015%.Tolerance = 2.22045e-011%.Tolerance 5 eps = 1.11022e-015%.Tolerance = 2.22045e-011%.Tolerance 5 eps = 1.11022e-015%.*** No errors detected*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -