weighted_p_square_cumul_dist.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // (C) Copyright Eric Niebler, Olivier Gygi 2006.
  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_cumul_dist.hpp
  6. #include <cmath>
  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_cumul_dist.hpp>
  16. using namespace boost;
  17. using namespace unit_test;
  18. using namespace boost::accumulators;
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // erf() not known by VC++ compiler!
  21. // my_erf() computes error function by numerically integrating with trapezoidal rule
  22. //
  23. double my_erf(double const& x, int const& n = 1000)
  24. {
  25. double sum = 0.;
  26. double delta = x/n;
  27. for (int i = 1; i < n; ++i)
  28. sum += std::exp(-i*i*delta*delta) * delta;
  29. sum += 0.5 * delta * (1. + std::exp(-x*x));
  30. return sum * 2. / std::sqrt(3.141592653);
  31. }
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // test_stat
  34. //
  35. void test_stat()
  36. {
  37. // tolerance in %
  38. double epsilon = 4;
  39. typedef accumulator_set<double, stats<tag::weighted_p_square_cumulative_distribution>, double > accumulator_t;
  40. accumulator_t acc_upper(p_square_cumulative_distribution_num_cells = 100);
  41. accumulator_t acc_lower(p_square_cumulative_distribution_num_cells = 100);
  42. // two random number generators
  43. double mu_upper = 1.0;
  44. double mu_lower = -1.0;
  45. boost::lagged_fibonacci607 rng;
  46. boost::normal_distribution<> mean_sigma_upper(mu_upper,1);
  47. boost::normal_distribution<> mean_sigma_lower(mu_lower,1);
  48. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal_upper(rng, mean_sigma_upper);
  49. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal_lower(rng, mean_sigma_lower);
  50. for (std::size_t i=0; i<100000; ++i)
  51. {
  52. double sample = normal_upper();
  53. acc_upper(sample, weight = std::exp(-mu_upper * (sample - 0.5 * mu_upper)));
  54. }
  55. for (std::size_t i=0; i<100000; ++i)
  56. {
  57. double sample = normal_lower();
  58. acc_lower(sample, weight = std::exp(-mu_lower * (sample - 0.5 * mu_lower)));
  59. }
  60. typedef iterator_range<std::vector<std::pair<double, double> >::iterator > histogram_type;
  61. histogram_type histogram_upper = weighted_p_square_cumulative_distribution(acc_upper);
  62. histogram_type histogram_lower = weighted_p_square_cumulative_distribution(acc_lower);
  63. // Note that applying importance sampling results in a region of the distribution
  64. // to be estimated more accurately and another region to be estimated less accurately
  65. // than without importance sampling, i.e., with unweighted samples
  66. for (std::size_t i = 0; i < histogram_upper.size(); ++i)
  67. {
  68. // problem with small results: epsilon is relative (in percent), not absolute!
  69. // check upper region of distribution
  70. if ( histogram_upper[i].second > 0.1 )
  71. BOOST_CHECK_CLOSE( 0.5 * (1.0 + my_erf( histogram_upper[i].first / std::sqrt(2.0) )), histogram_upper[i].second, epsilon );
  72. // check lower region of distribution
  73. if ( histogram_lower[i].second < -0.1 )
  74. BOOST_CHECK_CLOSE( 0.5 * (1.0 + my_erf( histogram_lower[i].first / std::sqrt(2.0) )), histogram_lower[i].second, epsilon );
  75. }
  76. }
  77. ///////////////////////////////////////////////////////////////////////////////
  78. // init_unit_test_suite
  79. //
  80. test_suite* init_unit_test_suite( int argc, char* argv[] )
  81. {
  82. test_suite *test = BOOST_TEST_SUITE("weighted_p_square_cumulative_distribution test");
  83. test->add(BOOST_TEST_CASE(&test_stat));
  84. return test;
  85. }