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 skewness.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/skewness.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::skewness > > acc1;
34 accumulator_set<int, stats<tag::skewness > > 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( skewness(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_CLOSE( skewness(acc2), 0.406040288214, 1e-6 );
59 }
60
61 ///////////////////////////////////////////////////////////////////////////////
62 // test_persistency
63 //
test_persistency()64 void test_persistency()
65 {
66 double epsilon = 1e-6;
67 std::stringstream ss;
68 {
69 accumulator_set<int, stats<tag::skewness > > acc;
70 acc(2);
71 acc(7);
72 acc(4);
73 acc(9);
74 acc(3);
75
76 BOOST_CHECK_EQUAL(accumulators::moment<3>(acc), 1171./5.);
77 BOOST_CHECK_CLOSE(skewness(acc), 0.406040288214, epsilon);
78 boost::archive::text_oarchive oa(ss);
79 acc.serialize(oa, 0);
80 }
81 accumulator_set<int, stats<tag::skewness > > acc;
82 boost::archive::text_iarchive ia(ss);
83 acc.serialize(ia, 0);
84 BOOST_CHECK_EQUAL(accumulators::moment<3>(acc), 1171./5.);
85 BOOST_CHECK_CLOSE(skewness(acc), 0.406040288214, epsilon);
86 }
87
88 ///////////////////////////////////////////////////////////////////////////////
89 // init_unit_test_suite
90 //
init_unit_test_suite(int argc,char * argv[])91 test_suite* init_unit_test_suite( int argc, char* argv[] )
92 {
93 test_suite *test = BOOST_TEST_SUITE("skewness test");
94
95 test->add(BOOST_TEST_CASE(&test_stat));
96 test->add(BOOST_TEST_CASE(&test_persistency));
97
98 return test;
99 }
100
101