tail_variate_means.cpp 7.4 KB

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