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 tail_variate_means.hpp |
7 | |
8 | #include <boost/random.hpp> |
9 | #include <boost/test/unit_test.hpp> |
10 | #include <boost/test/tools/floating_point_comparison.hpp> |
11 | #include <boost/accumulators/numeric/functional/vector.hpp> |
12 | #include <boost/accumulators/numeric/functional/complex.hpp> |
13 | #include <boost/accumulators/numeric/functional/valarray.hpp> |
14 | #include <boost/accumulators/accumulators.hpp> |
15 | #include <boost/accumulators/statistics/variates/covariate.hpp> |
16 | #include <boost/accumulators/statistics/stats.hpp> |
17 | #include <boost/accumulators/statistics/tail_variate_means.hpp> |
18 | |
19 | using namespace boost; |
20 | using namespace unit_test; |
21 | using namespace boost::accumulators; |
22 | |
23 | /////////////////////////////////////////////////////////////////////////////// |
24 | // test_stat |
25 | // |
26 | void test_stat() |
27 | { |
28 | std::size_t c = 5; // cache size |
29 | |
30 | typedef double variate_type; |
31 | typedef std::vector<variate_type> variate_set_type; |
32 | |
33 | typedef accumulator_set<double, stats<tag::tail_variate_means<right, variate_set_type, tag::covariate1>(relative)> > accumulator_t1; |
34 | typedef accumulator_set<double, stats<tag::tail_variate_means<right, variate_set_type, tag::covariate1>(absolute)> > accumulator_t2; |
35 | typedef accumulator_set<double, stats<tag::tail_variate_means<left, variate_set_type, tag::covariate1>(relative)> > accumulator_t3; |
36 | typedef accumulator_set<double, stats<tag::tail_variate_means<left, variate_set_type, tag::covariate1>(absolute)> > accumulator_t4; |
37 | |
38 | accumulator_t1 acc1( right_tail_cache_size = c ); |
39 | accumulator_t2 acc2( right_tail_cache_size = c ); |
40 | accumulator_t3 acc3( left_tail_cache_size = c ); |
41 | accumulator_t4 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., covariate1 = cov1); |
56 | acc1( 50., covariate1 = cov2); |
57 | acc1(200., covariate1 = cov3); |
58 | acc1( 80., covariate1 = cov4); |
59 | acc1( 20., covariate1 = cov5); |
60 | |
61 | acc2(100., covariate1 = cov1); |
62 | acc2( 50., covariate1 = cov2); |
63 | acc2(200., covariate1 = cov3); |
64 | acc2( 80., covariate1 = cov4); |
65 | acc2( 20., covariate1 = cov5); |
66 | |
67 | acc3(100., covariate1 = cov1); |
68 | acc3( 50., covariate1 = cov2); |
69 | acc3(200., covariate1 = cov3); |
70 | acc3( 80., covariate1 = cov4); |
71 | acc3( 20., covariate1 = cov5); |
72 | |
73 | acc4(100., covariate1 = cov1); |
74 | acc4( 50., covariate1 = cov2); |
75 | acc4(200., covariate1 = cov3); |
76 | acc4( 80., covariate1 = cov4); |
77 | acc4( 20., covariate1 = cov5); |
78 | |
79 | // check relative risk contributions |
80 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.7).begin() ), 14./75. ); // (10 + 46) / 300 = 14/75 |
81 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 1), 7./25. ); // (20 + 64) / 300 = 7/25 |
82 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 2), 7./30. ); // (30 + 40) / 300 = 7/30 |
83 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 3), 3./10. ); // (40 + 50) / 300 = 3/10 |
84 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.3).begin() ), 14./35. ); // (26 + 2) / 70 = 14/35 |
85 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 1), 3./35. ); // ( 4 + 2) / 70 = 3/35 |
86 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 2), 19./70. ); // (17 + 2) / 70 = 19/70 |
87 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 3), 17./70. ); // ( 3 + 14) / 70 = 17/70 |
88 | |
89 | // check absolute risk contributions |
90 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.7).begin() ), 28 ); // (10 + 46) / 2 = 28 |
91 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.7).begin() + 1), 42 ); // (20 + 64) / 2 = 42 |
92 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.7).begin() + 2), 35 ); // (30 + 40) / 2 = 35 |
93 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.7).begin() + 3), 45 ); // (40 + 50) / 2 = 45 |
94 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.3).begin() ), 14 ); // (26 + 2) / 2 = 14 |
95 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.3).begin() + 1), 3 ); // ( 4 + 2) / 2 = 3 |
96 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.3).begin() + 2),9.5 ); // (17 + 2) / 2 = 9.5 |
97 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.3).begin() + 3),8.5 ); // ( 3 + 14) / 2 = 8.5 |
98 | |
99 | // check relative risk contributions |
100 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.9).begin() ), 23./100. ); // 46/200 = 23/100 |
101 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 1), 8./25. ); // 64/200 = 8/25 |
102 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 2), 1./5. ); // 40/200 = 1/5 |
103 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 3), 1./4. ); // 50/200 = 1/4 |
104 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.1).begin() ), 1./10. ); // 2/ 20 = 1/10 |
105 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 1), 1./10. ); // 2/ 20 = 1/10 |
106 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 2), 1./10. ); // 2/ 20 = 1/10 |
107 | BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 3), 7./10. ); // 14/ 20 = 7/10 |
108 | |
109 | // check absolute risk contributions |
110 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.9).begin() ), 46 ); // 46 |
111 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.9).begin() + 1), 64 ); // 64 |
112 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.9).begin() + 2), 40 ); // 40 |
113 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.9).begin() + 3), 50 ); // 50 |
114 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.1).begin() ), 2 ); // 2 |
115 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.1).begin() + 1), 2 ); // 2 |
116 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.1).begin() + 2), 2 ); // 2 |
117 | BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.1).begin() + 3), 14 ); // 14 |
118 | } |
119 | |
120 | /////////////////////////////////////////////////////////////////////////////// |
121 | // init_unit_test_suite |
122 | // |
123 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
124 | { |
125 | test_suite *test = BOOST_TEST_SUITE("tail_variate_means test" ); |
126 | |
127 | test->add(BOOST_TEST_CASE(&test_stat)); |
128 | |
129 | return test; |
130 | } |
131 | |
132 | |