• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.hpp
7 
8 #include <iostream>
9 #include <boost/random.hpp>
10 #include <boost/test/unit_test.hpp>
11 #include <boost/test/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.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> > accumulator_t;
27 
28 ///////////////////////////////////////////////////////////////////////////////
29 // test_stat
30 //
test_stat()31 void test_stat()
32 {
33 
34     // tolerance
35     double epsilon = 3;
36 
37     // a random number generator
38     boost::lagged_fibonacci607 rng;
39 
40     std::vector<double> probs;
41 
42     probs.push_back(0.001);
43     probs.push_back(0.01 );
44     probs.push_back(0.1  );
45     probs.push_back(0.25 );
46     probs.push_back(0.5  );
47     probs.push_back(0.75 );
48     probs.push_back(0.9  );
49     probs.push_back(0.99 );
50     probs.push_back(0.999);
51 
52     accumulator_t acc(extended_p_square_probabilities = probs);
53 
54     for (int i=0; i<10000; ++i)
55         acc(rng());
56 
57     BOOST_CHECK_GE(extended_p_square(acc)[0], 0.0005);
58     BOOST_CHECK_LE(extended_p_square(acc)[0], 0.0015);
59     BOOST_CHECK_CLOSE(extended_p_square(acc)[1], probs[1], 15);
60     BOOST_CHECK_CLOSE(extended_p_square(acc)[2], probs[2], 5);
61 
62     for (std::size_t i=3; i<probs.size(); ++i)
63     {
64         BOOST_CHECK_CLOSE(extended_p_square(acc)[i], probs[i], epsilon);
65     }
66 }
67 
68 ///////////////////////////////////////////////////////////////////////////////
69 // test_persistency
70 //
test_persistency()71 void test_persistency()
72 {
73     // "persistent" storage
74     std::stringstream ss;
75     // tolerance
76     double epsilon = 3.;
77     std::vector<double> probs;
78     probs.push_back(0.75);
79     {
80         accumulator_t acc(extended_p_square_probabilities = probs);
81         // a random number generator
82         boost::lagged_fibonacci607 rng;
83 
84         for (int i=0; i<10000; ++i)
85             acc(rng());
86 
87         BOOST_CHECK_CLOSE(extended_p_square(acc)[0], probs[0], epsilon);
88         boost::archive::text_oarchive oa(ss);
89         acc.serialize(oa, 0);
90     }
91     accumulator_t acc(extended_p_square_probabilities = probs);
92     boost::archive::text_iarchive ia(ss);
93     acc.serialize(ia, 0);
94     BOOST_CHECK_CLOSE(extended_p_square(acc)[0], probs[0], epsilon);
95 
96 }
97 
98 ///////////////////////////////////////////////////////////////////////////////
99 // init_unit_test_suite
100 //
init_unit_test_suite(int argc,char * argv[])101 test_suite* init_unit_test_suite( int argc, char* argv[] )
102 {
103     test_suite *test = BOOST_TEST_SUITE("extended_p_square test");
104 
105     test->add(BOOST_TEST_CASE(&test_stat));
106     test->add(BOOST_TEST_CASE(&test_persistency));
107 
108     return test;
109 }
110 
111