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