extended_p_square_quantile.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 extended_p_square_quantile.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/extended_p_square_quantile.hpp>
  16. #include <sstream>
  17. #include <boost/archive/text_oarchive.hpp>
  18. #include <boost/archive/text_iarchive.hpp>
  19. using namespace boost;
  20. using namespace unit_test;
  21. using namespace boost::accumulators;
  22. typedef accumulator_set<double, stats<tag::extended_p_square_quantile> > accumulator_t;
  23. typedef accumulator_set<double, stats<tag::weighted_extended_p_square_quantile>, double > accumulator_t_weighted;
  24. typedef accumulator_set<double, stats<tag::extended_p_square_quantile(quadratic)> > accumulator_t_quadratic;
  25. typedef accumulator_set<double, stats<tag::weighted_extended_p_square_quantile(quadratic)>, double > accumulator_t_weighted_quadratic;
  26. ///////////////////////////////////////////////////////////////////////////////
  27. // test_stat
  28. //
  29. void test_stat()
  30. {
  31. // tolerance
  32. double epsilon = 1;
  33. // a random number generator
  34. boost::lagged_fibonacci607 rng;
  35. std::vector<double> probs;
  36. probs.push_back(0.990);
  37. probs.push_back(0.991);
  38. probs.push_back(0.992);
  39. probs.push_back(0.993);
  40. probs.push_back(0.994);
  41. probs.push_back(0.995);
  42. probs.push_back(0.996);
  43. probs.push_back(0.997);
  44. probs.push_back(0.998);
  45. probs.push_back(0.999);
  46. accumulator_t acc(extended_p_square_probabilities = probs);
  47. accumulator_t_weighted acc_weighted(extended_p_square_probabilities = probs);
  48. accumulator_t_quadratic acc2(extended_p_square_probabilities = probs);
  49. accumulator_t_weighted_quadratic acc_weighted2(extended_p_square_probabilities = probs);
  50. for (int i=0; i<10000; ++i)
  51. {
  52. double sample = rng();
  53. acc(sample);
  54. acc2(sample);
  55. acc_weighted(sample, weight = 1.);
  56. acc_weighted2(sample, weight = 1.);
  57. }
  58. for (std::size_t i = 0; i < probs.size() - 1; ++i)
  59. {
  60. BOOST_CHECK_CLOSE(
  61. quantile(acc, quantile_probability = 0.99025 + i*0.001)
  62. , 0.99025 + i*0.001
  63. , epsilon
  64. );
  65. BOOST_CHECK_CLOSE(
  66. quantile(acc2, quantile_probability = 0.99025 + i*0.001)
  67. , 0.99025 + i*0.001
  68. , epsilon
  69. );
  70. BOOST_CHECK_CLOSE(
  71. quantile(acc_weighted, quantile_probability = 0.99025 + i*0.001)
  72. , 0.99025 + i*0.001
  73. , epsilon
  74. );
  75. BOOST_CHECK_CLOSE(
  76. quantile(acc_weighted2, quantile_probability = 0.99025 + i*0.001)
  77. , 0.99025 + i*0.001
  78. , epsilon
  79. );
  80. }
  81. }
  82. ///////////////////////////////////////////////////////////////////////////////
  83. // test_persistency
  84. //
  85. void test_persistency()
  86. {
  87. // "persistent" storage
  88. std::stringstream ss;
  89. // tolerance
  90. double epsilon = 1.;
  91. // a random number generator
  92. boost::lagged_fibonacci607 rng;
  93. std::vector<double> probs;
  94. probs.push_back(0.990);
  95. probs.push_back(0.991);
  96. probs.push_back(0.992);
  97. probs.push_back(0.993);
  98. probs.push_back(0.994);
  99. probs.push_back(0.995);
  100. probs.push_back(0.996);
  101. probs.push_back(0.997);
  102. probs.push_back(0.998);
  103. probs.push_back(0.999);
  104. {
  105. accumulator_t acc(extended_p_square_probabilities = probs);
  106. accumulator_t_weighted acc_weighted(extended_p_square_probabilities = probs);
  107. for (int i=0; i<10000; ++i)
  108. {
  109. double sample = rng();
  110. acc(sample);
  111. acc_weighted(sample, weight = 1.);
  112. }
  113. BOOST_CHECK_CLOSE(
  114. quantile(acc, quantile_probability = 0.99025)
  115. , 0.99025
  116. , epsilon
  117. );
  118. BOOST_CHECK_CLOSE(
  119. quantile(acc_weighted, quantile_probability = 0.99025)
  120. , 0.99025
  121. , epsilon
  122. );
  123. boost::archive::text_oarchive oa(ss);
  124. acc.serialize(oa, 0);
  125. acc_weighted.serialize(oa, 0);
  126. }
  127. accumulator_t acc(extended_p_square_probabilities = probs);
  128. accumulator_t_weighted acc_weighted(extended_p_square_probabilities = probs);
  129. boost::archive::text_iarchive ia(ss);
  130. acc.serialize(ia, 0);
  131. acc_weighted.serialize(ia, 0);
  132. BOOST_CHECK_CLOSE(
  133. quantile(acc, quantile_probability = 0.99025)
  134. , 0.99025
  135. , epsilon
  136. );
  137. BOOST_CHECK_CLOSE(
  138. quantile(acc_weighted, quantile_probability = 0.99025)
  139. , 0.99025
  140. , epsilon
  141. );
  142. }
  143. ///////////////////////////////////////////////////////////////////////////////
  144. // init_unit_test_suite
  145. //
  146. test_suite* init_unit_test_suite( int argc, char* argv[] )
  147. {
  148. test_suite *test = BOOST_TEST_SUITE("extended_p_square_quantile test");
  149. test->add(BOOST_TEST_CASE(&test_stat));
  150. test->add(BOOST_TEST_CASE(&test_persistency));
  151. return test;
  152. }