weighted_p_square_quantile.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_p_square_quantile.hpp
  6. #include <cmath> // for std::exp()
  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_p_square_quantile.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_p_square_quantile>, double> accumulator_t;
  25. // tolerance in %
  26. double epsilon = 1;
  27. // some random number generators
  28. double mu4 = -1.0;
  29. double mu5 = -1.0;
  30. double mu6 = 1.0;
  31. double mu7 = 1.0;
  32. boost::lagged_fibonacci607 rng;
  33. boost::normal_distribution<> mean_sigma4(mu4, 1);
  34. boost::normal_distribution<> mean_sigma5(mu5, 1);
  35. boost::normal_distribution<> mean_sigma6(mu6, 1);
  36. boost::normal_distribution<> mean_sigma7(mu7, 1);
  37. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal4(rng, mean_sigma4);
  38. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal5(rng, mean_sigma5);
  39. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal6(rng, mean_sigma6);
  40. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal7(rng, mean_sigma7);
  41. accumulator_t acc0(quantile_probability = 0.001);
  42. accumulator_t acc1(quantile_probability = 0.025);
  43. accumulator_t acc2(quantile_probability = 0.975);
  44. accumulator_t acc3(quantile_probability = 0.999);
  45. accumulator_t acc4(quantile_probability = 0.001);
  46. accumulator_t acc5(quantile_probability = 0.025);
  47. accumulator_t acc6(quantile_probability = 0.975);
  48. accumulator_t acc7(quantile_probability = 0.999);
  49. for (std::size_t i=0; i<100000; ++i)
  50. {
  51. double sample = rng();
  52. acc0(sample, weight = 1.);
  53. acc1(sample, weight = 1.);
  54. acc2(sample, weight = 1.);
  55. acc3(sample, weight = 1.);
  56. double sample4 = normal4();
  57. double sample5 = normal5();
  58. double sample6 = normal6();
  59. double sample7 = normal7();
  60. acc4(sample4, weight = std::exp(-mu4 * (sample4 - 0.5 * mu4)));
  61. acc5(sample5, weight = std::exp(-mu5 * (sample5 - 0.5 * mu5)));
  62. acc6(sample6, weight = std::exp(-mu6 * (sample6 - 0.5 * mu6)));
  63. acc7(sample7, weight = std::exp(-mu7 * (sample7 - 0.5 * mu7)));
  64. }
  65. // check for uniform distribution with weight = 1
  66. BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc0), 0.001, 28 );
  67. BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc1), 0.025, 5 );
  68. BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc2), 0.975, epsilon );
  69. BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc3), 0.999, epsilon );
  70. // check for shifted standard normal distribution ("importance sampling")
  71. BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc4), -3.090232, epsilon );
  72. BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc5), -1.959963, epsilon );
  73. BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc6), 1.959963, epsilon );
  74. BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc7), 3.090232, epsilon );
  75. }
  76. ///////////////////////////////////////////////////////////////////////////////
  77. // init_unit_test_suite
  78. //
  79. test_suite* init_unit_test_suite( int argc, char* argv[] )
  80. {
  81. test_suite *test = BOOST_TEST_SUITE("weighted_p_square_quantile test");
  82. test->add(BOOST_TEST_CASE(&test_stat));
  83. return test;
  84. }