1 | // (C) Copyright 2006 Eric Niebler, Olivier Gygi |
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 | #include <boost/test/unit_test.hpp> |
7 | #include <boost/test/tools/floating_point_comparison.hpp> |
8 | #include <boost/random.hpp> |
9 | #include <boost/accumulators/accumulators.hpp> |
10 | #include <boost/accumulators/statistics/stats.hpp> |
11 | #include <boost/accumulators/statistics/median.hpp> |
12 | #include <sstream> |
13 | #include <boost/archive/text_oarchive.hpp> |
14 | #include <boost/archive/text_iarchive.hpp> |
15 | |
16 | using namespace boost; |
17 | using namespace unit_test; |
18 | using namespace accumulators; |
19 | |
20 | |
21 | typedef accumulator_set<double, stats<tag::median(with_p_square_quantile) > > p_square_median_t; |
22 | typedef accumulator_set<double, stats<tag::median(with_density) > > dist_median_t; |
23 | typedef accumulator_set<double, stats<tag::median(with_p_square_cumulative_distribution) > > p_square_dist_median_t; |
24 | |
25 | /////////////////////////////////////////////////////////////////////////////// |
26 | // test_stat |
27 | // |
28 | void test_stat() |
29 | { |
30 | // two random number generators |
31 | double mu = 1.; |
32 | boost::lagged_fibonacci607 rng; |
33 | boost::normal_distribution<> mean_sigma(mu,1); |
34 | boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma); |
35 | |
36 | p_square_median_t acc; |
37 | dist_median_t acc_dens( density_cache_size = 10000, density_num_bins = 1000 ); |
38 | p_square_dist_median_t acc_cdist( p_square_cumulative_distribution_num_cells = 100 ); |
39 | |
40 | for (std::size_t i=0; i<100000; ++i) |
41 | { |
42 | double sample = normal(); |
43 | acc(sample); |
44 | acc_dens(sample); |
45 | acc_cdist(sample); |
46 | } |
47 | |
48 | BOOST_CHECK_CLOSE(1., median(acc), 1.); |
49 | BOOST_CHECK_CLOSE(1., median(acc_dens), 1.); |
50 | BOOST_CHECK_CLOSE(1., median(acc_cdist), 3.); |
51 | } |
52 | |
53 | /////////////////////////////////////////////////////////////////////////////// |
54 | // test_persistency |
55 | // |
56 | void test_persistency() |
57 | { |
58 | // two random number generators |
59 | double mu = 1.; |
60 | boost::lagged_fibonacci607 rng; |
61 | boost::normal_distribution<> mean_sigma(mu,1); |
62 | boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma); |
63 | std::stringstream ss; |
64 | { |
65 | p_square_median_t acc; |
66 | dist_median_t acc_dens( density_cache_size = 10000, density_num_bins = 1000 ); |
67 | p_square_dist_median_t acc_cdist( p_square_cumulative_distribution_num_cells = 100 ); |
68 | |
69 | for (std::size_t i=0; i<100000; ++i) |
70 | { |
71 | double sample = normal(); |
72 | acc(sample); |
73 | acc_dens(sample); |
74 | acc_cdist(sample); |
75 | } |
76 | |
77 | BOOST_CHECK_CLOSE(1., median(acc), 1.); |
78 | BOOST_CHECK_CLOSE(1., median(acc_dens), 1.); |
79 | BOOST_CHECK_CLOSE(1., median(acc_cdist), 3.); |
80 | |
81 | boost::archive::text_oarchive oa(ss); |
82 | acc.serialize(ar&: oa, file_version: 0); |
83 | acc_dens.serialize(ar&: oa, file_version: 0); |
84 | acc_cdist.serialize(ar&: oa, file_version: 0); |
85 | } |
86 | |
87 | p_square_median_t acc; |
88 | dist_median_t acc_dens( density_cache_size = 10000, density_num_bins = 1000 ); |
89 | p_square_dist_median_t acc_cdist( p_square_cumulative_distribution_num_cells = 100 ); |
90 | boost::archive::text_iarchive ia(ss); |
91 | acc.serialize(ar&: ia, file_version: 0); |
92 | acc_dens.serialize(ar&: ia, file_version: 0); |
93 | acc_cdist.serialize(ar&: ia, file_version: 0); |
94 | BOOST_CHECK_CLOSE(1., median(acc), 1.); |
95 | BOOST_CHECK_CLOSE(1., median(acc_dens), 1.); |
96 | BOOST_CHECK_CLOSE(1., median(acc_cdist), 3.); |
97 | } |
98 | |
99 | /////////////////////////////////////////////////////////////////////////////// |
100 | // init_unit_test_suite |
101 | // |
102 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
103 | { |
104 | test_suite *test = BOOST_TEST_SUITE("median test" ); |
105 | |
106 | test->add(BOOST_TEST_CASE(&test_stat)); |
107 | test->add(BOOST_TEST_CASE(&test_persistency)); |
108 | |
109 | return test; |
110 | } |
111 | |