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