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 weighted_tail_mean.hpp
7
8#define BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT
9#define BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT
10#define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
11
12#include <boost/random.hpp>
13#include <boost/test/unit_test.hpp>
14#include <boost/test/tools/floating_point_comparison.hpp>
15#include <boost/accumulators/accumulators.hpp>
16#include <boost/accumulators/statistics.hpp>
17#include <boost/accumulators/statistics/weighted_tail_mean.hpp>
18#include <boost/accumulators/statistics/weighted_tail_quantile.hpp>
19
20using namespace boost;
21using namespace unit_test;
22using namespace boost::accumulators;
23
24///////////////////////////////////////////////////////////////////////////////
25// test_stat
26//
27void test_stat()
28{
29 // tolerance in %
30 double epsilon = 1;
31
32 std::size_t n = 100000; // number of MC steps
33 std::size_t c = 25000; // cache size
34
35 accumulator_set<double, stats<tag::non_coherent_weighted_tail_mean<right> >, double >
36 acc0( right_tail_cache_size = c );
37 accumulator_set<double, stats<tag::non_coherent_weighted_tail_mean<left> >, double >
38 acc1( left_tail_cache_size = c );
39
40 // random number generators
41 boost::lagged_fibonacci607 rng;
42
43 for (std::size_t i = 0; i < n; ++i)
44 {
45 double smpl = std::sqrt(x: rng());
46 acc0(smpl, weight = 1./smpl);
47 }
48
49 for (std::size_t i = 0; i < n; ++i)
50 {
51 double smpl = rng();
52 acc1(smpl*smpl, weight = smpl);
53 }
54
55 // check uniform distribution
56 BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc0, quantile_probability = 0.95), 0.975, epsilon );
57 BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc0, quantile_probability = 0.975), 0.9875, epsilon );
58 BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc0, quantile_probability = 0.99), 0.995, epsilon );
59 BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc0, quantile_probability = 0.999), 0.9995, epsilon );
60 BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc1, quantile_probability = 0.05), 0.025, epsilon );
61 BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc1, quantile_probability = 0.025), 0.0125, epsilon );
62 BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc1, quantile_probability = 0.01), 0.005, epsilon );
63 BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc1, quantile_probability = 0.001), 0.0005, 5*epsilon );
64}
65
66///////////////////////////////////////////////////////////////////////////////
67// init_unit_test_suite
68//
69test_suite* init_unit_test_suite( int argc, char* argv[] )
70{
71 test_suite *test = BOOST_TEST_SUITE("weighted_tail_mean test");
72
73 test->add(BOOST_TEST_CASE(&test_stat));
74
75 return test;
76}
77
78

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