]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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/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 | ||
12 | using namespace boost; | |
13 | using namespace unit_test; | |
14 | using namespace accumulators; | |
15 | ||
16 | /////////////////////////////////////////////////////////////////////////////// | |
17 | // test_stat | |
18 | // | |
19 | void test_stat() | |
20 | { | |
21 | // matlab | |
22 | // >> samples = [1:5]; | |
23 | // >> mean(samples) | |
24 | // ans = 3 | |
25 | // >> sum(samples .* samples) / length(samples) | |
26 | // ans = 11 | |
27 | // >> sum(samples .* samples) / length(samples) - mean(samples)^2 | |
28 | // ans = 2 | |
29 | ||
30 | // lazy variance, now lazy with syntactic sugar, thanks to Eric | |
31 | accumulator_set<int, stats<tag::variance(lazy)> > acc1; | |
32 | ||
33 | acc1(1); | |
34 | acc1(2); | |
35 | acc1(3); | |
36 | acc1(4); | |
37 | acc1(5); | |
38 | ||
39 | BOOST_CHECK_EQUAL(5u, count(acc1)); | |
40 | BOOST_CHECK_CLOSE(3., mean(acc1), 1e-5); | |
41 | BOOST_CHECK_CLOSE(11., accumulators::moment<2>(acc1), 1e-5); | |
42 | BOOST_CHECK_CLOSE(2., variance(acc1), 1e-5); | |
43 | ||
44 | // immediate variance | |
45 | accumulator_set<int, stats<tag::variance> > acc2; | |
46 | ||
47 | acc2(1); | |
48 | acc2(2); | |
49 | acc2(3); | |
50 | acc2(4); | |
51 | acc2(5); | |
52 | ||
53 | BOOST_CHECK_EQUAL(5u, count(acc2)); | |
54 | BOOST_CHECK_CLOSE(3., mean(acc2), 1e-5); | |
55 | BOOST_CHECK_CLOSE(2., variance(acc2), 1e-5); | |
56 | } | |
57 | ||
58 | /////////////////////////////////////////////////////////////////////////////// | |
59 | // init_unit_test_suite | |
60 | // | |
61 | test_suite* init_unit_test_suite( int argc, char* argv[] ) | |
62 | { | |
63 | test_suite *test = BOOST_TEST_SUITE("variance test"); | |
64 | ||
65 | test->add(BOOST_TEST_CASE(&test_stat)); | |
66 | ||
67 | return test; | |
68 | } |