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_variate_means.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/variates/covariate.hpp>
17#include <boost/accumulators/statistics.hpp>
18#include <boost/accumulators/statistics/weighted_tail_variate_means.hpp>
19
20using namespace boost;
21using namespace unit_test;
22using namespace boost::accumulators;
23
24///////////////////////////////////////////////////////////////////////////////
25// test_stat
26//
27void test_stat()
28{
29 std::size_t c = 5; // cache size
30
31 typedef double variate_type;
32 typedef std::vector<variate_type> variate_set_type;
33
34 accumulator_set<double, stats<tag::weighted_tail_variate_means<right, variate_set_type, tag::covariate1>(relative)>, double >
35 acc1( right_tail_cache_size = c );
36 accumulator_set<double, stats<tag::weighted_tail_variate_means<right, variate_set_type, tag::covariate1>(absolute)>, double >
37 acc2( right_tail_cache_size = c );
38 accumulator_set<double, stats<tag::weighted_tail_variate_means<left, variate_set_type, tag::covariate1>(relative)>, double >
39 acc3( left_tail_cache_size = c );
40 accumulator_set<double, stats<tag::weighted_tail_variate_means<left, variate_set_type, tag::covariate1>(absolute)>, double >
41 acc4( left_tail_cache_size = c );
42
43 variate_set_type cov1, cov2, cov3, cov4, cov5;
44 double c1[] = { 10., 20., 30., 40. }; // 100
45 double c2[] = { 26., 4., 17., 3. }; // 50
46 double c3[] = { 46., 64., 40., 50. }; // 200
47 double c4[] = { 1., 3., 70., 6. }; // 80
48 double c5[] = { 2., 2., 2., 14. }; // 20
49 cov1.assign(first: c1, last: c1 + sizeof(c1)/sizeof(variate_type));
50 cov2.assign(first: c2, last: c2 + sizeof(c2)/sizeof(variate_type));
51 cov3.assign(first: c3, last: c3 + sizeof(c3)/sizeof(variate_type));
52 cov4.assign(first: c4, last: c4 + sizeof(c4)/sizeof(variate_type));
53 cov5.assign(first: c5, last: c5 + sizeof(c5)/sizeof(variate_type));
54
55 acc1(100., weight = 0.8, covariate1 = cov1);
56 acc1( 50., weight = 0.9, covariate1 = cov2);
57 acc1(200., weight = 1.0, covariate1 = cov3);
58 acc1( 80., weight = 1.1, covariate1 = cov4);
59 acc1( 20., weight = 1.2, covariate1 = cov5);
60
61 acc2(100., weight = 0.8, covariate1 = cov1);
62 acc2( 50., weight = 0.9, covariate1 = cov2);
63 acc2(200., weight = 1.0, covariate1 = cov3);
64 acc2( 80., weight = 1.1, covariate1 = cov4);
65 acc2( 20., weight = 1.2, covariate1 = cov5);
66
67 acc3(100., weight = 0.8, covariate1 = cov1);
68 acc3( 50., weight = 0.9, covariate1 = cov2);
69 acc3(200., weight = 1.0, covariate1 = cov3);
70 acc3( 80., weight = 1.1, covariate1 = cov4);
71 acc3( 20., weight = 1.2, covariate1 = cov5);
72
73 acc4(100., weight = 0.8, covariate1 = cov1);
74 acc4( 50., weight = 0.9, covariate1 = cov2);
75 acc4(200., weight = 1.0, covariate1 = cov3);
76 acc4( 80., weight = 1.1, covariate1 = cov4);
77 acc4( 20., weight = 1.2, covariate1 = cov5);
78
79 // check relative risk contributions
80 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.7).begin() ), (0.8*10 + 1.0*46)/(0.8*100 + 1.0*200) );
81 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 1), (0.8*20 + 1.0*64)/(0.8*100 + 1.0*200) );
82 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 2), (0.8*30 + 1.0*40)/(0.8*100 + 1.0*200) );
83 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 3), (0.8*40 + 1.0*50)/(0.8*100 + 1.0*200) );
84 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.3).begin() ), (0.9*26 + 1.2*2)/(0.9*50 + 1.2*20) );
85 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 1), (0.9*4 + 1.2*2)/(0.9*50 + 1.2*20) );
86 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 2), (0.9*17 + 1.2*2)/(0.9*50 + 1.2*20) );
87 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 3), (0.9*3 + 1.2*14)/(0.9*50 + 1.2*20) );
88
89 // check absolute risk contributions
90 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.7).begin() ), (0.8*10 + 1.0*46)/1.8 );
91 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.7).begin() + 1), (0.8*20 + 1.0*64)/1.8 );
92 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.7).begin() + 2), (0.8*30 + 1.0*40)/1.8 );
93 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.7).begin() + 3), (0.8*40 + 1.0*50)/1.8 );
94 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.3).begin() ), (0.9*26 + 1.2*2)/2.1 );
95 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.3).begin() + 1), (0.9*4 + 1.2*2)/2.1 );
96 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.3).begin() + 2), (0.9*17 + 1.2*2)/2.1 );
97 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.3).begin() + 3), (0.9*3 + 1.2*14)/2.1 );
98
99 // check relative risk contributions
100 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.9).begin() ), 1.0*46/(1.0*200) );
101 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 1), 1.0*64/(1.0*200) );
102 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 2), 1.0*40/(1.0*200) );
103 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 3), 1.0*50/(1.0*200) );
104 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.1).begin() ), 1.2*2/(1.2*20) );
105 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 1), 1.2*2/(1.2*20) );
106 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 2), 1.2*2/(1.2*20) );
107 BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 3), 1.2*14/(1.2*20) );
108
109 // check absolute risk contributions
110 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.9).begin() ), 1.0*46/1.0 );
111 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.9).begin() + 1), 1.0*64/1.0 );
112 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.9).begin() + 2), 1.0*40/1.0 );
113 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.9).begin() + 3), 1.0*50/1.0 );
114 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.1).begin() ), 1.2*2/1.2 );
115 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.1).begin() + 1), 1.2*2/1.2 );
116 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.1).begin() + 2), 1.2*2/1.2 );
117 BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.1).begin() + 3), 1.2*14/1.2 );
118}
119
120///////////////////////////////////////////////////////////////////////////////
121// init_unit_test_suite
122//
123test_suite* init_unit_test_suite( int argc, char* argv[] )
124{
125 test_suite *test = BOOST_TEST_SUITE("weighted_tail_variate_means test");
126
127 test->add(BOOST_TEST_CASE(&test_stat));
128
129 return test;
130}
131
132

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