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 // Test case for kurtosis.hpp
7
8 #include <boost/random.hpp>
9 #include <boost/test/unit_test.hpp>
10 #include <boost/test/floating_point_comparison.hpp>
11 #include <boost/accumulators/numeric/functional/vector.hpp>
12 #include <boost/accumulators/numeric/functional/complex.hpp>
13 #include <boost/accumulators/numeric/functional/valarray.hpp>
14 #include <boost/accumulators/accumulators.hpp>
15 #include <boost/accumulators/statistics/stats.hpp>
16 #include <boost/accumulators/statistics/kurtosis.hpp>
17 #include <sstream>
18 #include <boost/archive/text_oarchive.hpp>
19 #include <boost/archive/text_iarchive.hpp>
20
21 using namespace boost;
22 using namespace unit_test;
23 using namespace boost::accumulators;
24
25 ///////////////////////////////////////////////////////////////////////////////
26 // test_stat
27 //
test_stat()28 void test_stat()
29 {
30 // tolerance in %
31 // double epsilon = 1;
32
33 accumulator_set<double, stats<tag::kurtosis > > acc1;
34 accumulator_set<int, stats<tag::kurtosis > > acc2;
35
36 // two random number generators
37 boost::lagged_fibonacci607 rng;
38 boost::normal_distribution<> mean_sigma(0,1);
39 boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma);
40
41 for (std::size_t i=0; i<100000; ++i)
42 {
43 acc1(normal());
44 }
45
46 // This check fails because epsilon is relative and not absolute
47 // BOOST_CHECK_CLOSE( kurtosis(acc1), 0., epsilon );
48
49 acc2(2);
50 acc2(7);
51 acc2(4);
52 acc2(9);
53 acc2(3);
54
55 BOOST_CHECK_EQUAL( mean(acc2), 5 );
56 BOOST_CHECK_EQUAL( accumulators::moment<2>(acc2), 159./5. );
57 BOOST_CHECK_EQUAL( accumulators::moment<3>(acc2), 1171./5. );
58 BOOST_CHECK_EQUAL( accumulators::moment<4>(acc2), 1863 );
59 BOOST_CHECK_CLOSE( kurtosis(acc2), -1.39965397924, 1e-6 );
60 }
61
62 ///////////////////////////////////////////////////////////////////////////////
63 // test_persistency
64 //
test_persistency()65 void test_persistency()
66 {
67 // "persistent" storage
68 std::stringstream ss;
69 // tolerance
70 double epsilon = 1e-6;
71 double kurtosis_value = -1.39965397924;
72 {
73 accumulator_set<int, stats<tag::kurtosis > > acc;
74 acc(2);
75 acc(7);
76 acc(4);
77 acc(9);
78 acc(3);
79 BOOST_CHECK_CLOSE( kurtosis(acc), kurtosis_value, epsilon);
80 boost::archive::text_oarchive oa(ss);
81 acc.serialize(oa, 0);
82 }
83
84 accumulator_set<double, stats<tag::kurtosis > > acc;
85 boost::archive::text_iarchive ia(ss);
86 acc.serialize(ia, 0);
87 BOOST_CHECK_CLOSE( kurtosis(acc), kurtosis_value, epsilon);
88
89 }
90 ///////////////////////////////////////////////////////////////////////////////
91 // init_unit_test_suite
92 //
init_unit_test_suite(int argc,char * argv[])93 test_suite* init_unit_test_suite( int argc, char* argv[] )
94 {
95 test_suite *test = BOOST_TEST_SUITE("kurtosis test");
96
97 test->add(BOOST_TEST_CASE(&test_stat));
98 test->add(BOOST_TEST_CASE(&test_persistency));
99
100 return test;
101 }
102
103