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_quantile.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_quantile.hpp>
18
19using namespace boost;
20using namespace unit_test;
21using namespace boost::accumulators;
22
23///////////////////////////////////////////////////////////////////////////////
24// test_stat
25//
26void test_stat()
27{
28 // tolerance in %
29 double epsilon = 1;
30
31 std::size_t n = 100000; // number of MC steps
32 std::size_t c = 20000; // cache size
33
34 double mu1 = 1.0;
35 double mu2 = -1.0;
36 boost::lagged_fibonacci607 rng;
37 boost::normal_distribution<> mean_sigma1(mu1,1);
38 boost::normal_distribution<> mean_sigma2(mu2,1);
39 boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal1(rng, mean_sigma1);
40 boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal2(rng, mean_sigma2);
41
42 accumulator_set<double, stats<tag::weighted_tail_quantile<right> >, double>
43 acc1(right_tail_cache_size = c);
44
45 accumulator_set<double, stats<tag::weighted_tail_quantile<left> >, double>
46 acc2(left_tail_cache_size = c);
47
48 for (std::size_t i = 0; i < n; ++i)
49 {
50 double sample1 = normal1();
51 double sample2 = normal2();
52 acc1(sample1, weight = std::exp(x: -mu1 * (sample1 - 0.5 * mu1)));
53 acc2(sample2, weight = std::exp(x: -mu2 * (sample2 - 0.5 * mu2)));
54 }
55
56 // check standard normal distribution
57 BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.975), 1.959963, epsilon );
58 BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.999), 3.090232, epsilon );
59 BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.025), -1.959963, epsilon );
60 BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.001), -3.090232, epsilon );
61
62}
63
64///////////////////////////////////////////////////////////////////////////////
65// init_unit_test_suite
66//
67test_suite* init_unit_test_suite( int argc, char* argv[] )
68{
69 test_suite *test = BOOST_TEST_SUITE("weighted_tail_quantile test");
70
71 test->add(BOOST_TEST_CASE(&test_stat));
72
73 return test;
74}
75
76

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