1// (C) Copyright 2005 Daniel Egloff, Eric Niebler
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/accumulators/accumulators.hpp>
9#include <boost/accumulators/statistics/stats.hpp>
10#include <boost/accumulators/statistics/variance.hpp>
11#include <sstream>
12#include <boost/archive/text_oarchive.hpp>
13#include <boost/archive/text_iarchive.hpp>
14
15using namespace boost;
16using namespace unit_test;
17using namespace accumulators;
18
19///////////////////////////////////////////////////////////////////////////////
20// test_stat
21//
22void test_stat()
23{
24 // matlab
25 // >> samples = [1:5];
26 // >> mean(samples)
27 // ans = 3
28 // >> sum(samples .* samples) / length(samples)
29 // ans = 11
30 // >> sum(samples .* samples) / length(samples) - mean(samples)^2
31 // ans = 2
32
33 // lazy variance, now lazy with syntactic sugar, thanks to Eric
34 accumulator_set<int, stats<tag::variance(lazy)> > acc1;
35
36 acc1(1);
37 acc1(2);
38 acc1(3);
39 acc1(4);
40 acc1(5);
41
42 BOOST_CHECK_EQUAL(5u, count(acc1));
43 BOOST_CHECK_CLOSE(3., mean(acc1), 1e-5);
44 BOOST_CHECK_CLOSE(11., accumulators::moment<2>(acc1), 1e-5);
45 BOOST_CHECK_CLOSE(2., variance(acc1), 1e-5);
46
47 // immediate variance
48 accumulator_set<int, stats<tag::variance> > acc2;
49
50 acc2(1);
51 acc2(2);
52 acc2(3);
53 acc2(4);
54 acc2(5);
55
56 BOOST_CHECK_EQUAL(5u, count(acc2));
57 BOOST_CHECK_CLOSE(3., mean(acc2), 1e-5);
58 BOOST_CHECK_CLOSE(2., variance(acc2), 1e-5);
59}
60
61///////////////////////////////////////////////////////////////////////////////
62// test_persistency
63//
64void test_persistency()
65{
66 double epsilon = 1e-5;
67 std::stringstream ss;
68 {
69 accumulator_set<int, stats<tag::variance(lazy)> > acc1;
70 accumulator_set<int, stats<tag::variance> > acc2;
71 acc1(1);
72 acc1(2);
73 acc1(3);
74 acc1(4);
75 acc1(5);
76 acc2(1);
77 acc2(2);
78 acc2(3);
79 acc2(4);
80 acc2(5);
81 BOOST_CHECK_CLOSE(2., variance(acc2), epsilon);
82 BOOST_CHECK_CLOSE(2., variance(acc1), epsilon);
83 boost::archive::text_oarchive oa(ss);
84 acc1.serialize(ar&: oa, file_version: 0);
85 acc2.serialize(ar&: oa, file_version: 0);
86 }
87 accumulator_set<int, stats<tag::variance(lazy)> > acc1;
88 accumulator_set<int, stats<tag::variance> > acc2;
89 boost::archive::text_iarchive ia(ss);
90 acc1.serialize(ar&: ia, file_version: 0);
91 acc2.serialize(ar&: ia, file_version: 0);
92 BOOST_CHECK_CLOSE(2., variance(acc2), epsilon);
93 BOOST_CHECK_CLOSE(2., variance(acc1), epsilon);
94}
95
96///////////////////////////////////////////////////////////////////////////////
97// init_unit_test_suite
98//
99test_suite* init_unit_test_suite( int argc, char* argv[] )
100{
101 test_suite *test = BOOST_TEST_SUITE("variance test");
102
103 test->add(BOOST_TEST_CASE(&test_stat));
104 test->add(BOOST_TEST_CASE(&test_persistency));
105
106 return test;
107}
108

source code of boost/libs/accumulators/test/variance.cpp