weighted_extended_p_square.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // (C) Copyright Eric Niebler 2005.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // Test case for weighted_extended_p_square.hpp
  6. #include <iostream>
  7. #include <boost/random.hpp>
  8. #include <boost/test/unit_test.hpp>
  9. #include <boost/test/floating_point_comparison.hpp>
  10. #include <boost/accumulators/numeric/functional/vector.hpp>
  11. #include <boost/accumulators/numeric/functional/complex.hpp>
  12. #include <boost/accumulators/numeric/functional/valarray.hpp>
  13. #include <boost/accumulators/accumulators.hpp>
  14. #include <boost/accumulators/statistics/stats.hpp>
  15. #include <boost/accumulators/statistics/weighted_extended_p_square.hpp>
  16. using namespace boost;
  17. using namespace unit_test;
  18. using namespace boost::accumulators;
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // test_stat
  21. //
  22. void test_stat()
  23. {
  24. typedef accumulator_set<double, stats<tag::weighted_extended_p_square>, double> accumulator_t;
  25. // problem with small results: epsilon is relative (in percent), not absolute
  26. // tolerance in %
  27. double epsilon = 1;
  28. // some random number generators
  29. double mu1 = -1.0;
  30. double mu2 = 1.0;
  31. boost::lagged_fibonacci607 rng;
  32. boost::normal_distribution<> mean_sigma1(mu1, 1);
  33. boost::normal_distribution<> mean_sigma2(mu2, 1);
  34. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal1(rng, mean_sigma1);
  35. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal2(rng, mean_sigma2);
  36. std::vector<double> probs_uniform, probs_normal1, probs_normal2, probs_normal_exact1, probs_normal_exact2;
  37. double p1[] = {/*0.001,*/ 0.01, 0.1, 0.5, 0.9, 0.99, 0.999};
  38. probs_uniform.assign(p1, p1 + sizeof(p1) / sizeof(double));
  39. double p2[] = {0.001, 0.025};
  40. double p3[] = {0.975, 0.999};
  41. probs_normal1.assign(p2, p2 + sizeof(p2) / sizeof(double));
  42. probs_normal2.assign(p3, p3 + sizeof(p3) / sizeof(double));
  43. double p4[] = {-3.090232, -1.959963};
  44. double p5[] = {1.959963, 3.090232};
  45. probs_normal_exact1.assign(p4, p4 + sizeof(p4) / sizeof(double));
  46. probs_normal_exact2.assign(p5, p5 + sizeof(p5) / sizeof(double));
  47. accumulator_t acc_uniform(extended_p_square_probabilities = probs_uniform);
  48. accumulator_t acc_normal1(extended_p_square_probabilities = probs_normal1);
  49. accumulator_t acc_normal2(extended_p_square_probabilities = probs_normal2);
  50. for (std::size_t i = 0; i < 100000; ++i)
  51. {
  52. acc_uniform(rng(), weight = 1.);
  53. double sample1 = normal1();
  54. double sample2 = normal2();
  55. acc_normal1(sample1, weight = std::exp(-mu1 * (sample1 - 0.5 * mu1)));
  56. acc_normal2(sample2, weight = std::exp(-mu2 * (sample2 - 0.5 * mu2)));
  57. }
  58. // check for uniform distribution
  59. BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_uniform)[0], probs_uniform[0], 6*epsilon);
  60. BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_uniform)[1], probs_uniform[1], 3*epsilon);
  61. BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_uniform)[2], probs_uniform[2], epsilon);
  62. BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_uniform)[3], probs_uniform[3], epsilon);
  63. BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_uniform)[4], probs_uniform[4], epsilon);
  64. BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_uniform)[5], probs_uniform[5], epsilon);
  65. // check for standard normal distribution
  66. for (std::size_t i = 0; i < probs_normal1.size(); ++i)
  67. {
  68. BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_normal1)[i], probs_normal_exact1[i], epsilon);
  69. BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_normal2)[i], probs_normal_exact2[i], epsilon);
  70. }
  71. }
  72. ///////////////////////////////////////////////////////////////////////////////
  73. // init_unit_test_suite
  74. //
  75. test_suite* init_unit_test_suite( int argc, char* argv[] )
  76. {
  77. test_suite *test = BOOST_TEST_SUITE("weighted_extended_p_square test");
  78. test->add(BOOST_TEST_CASE(&test_stat));
  79. return test;
  80. }