weighted_tail_variate_means.cpp 7.8 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 weighted_tail_variate_means.hpp
  6. #define BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT
  7. #define BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT
  8. #define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
  9. #include <boost/random.hpp>
  10. #include <boost/test/unit_test.hpp>
  11. #include <boost/test/floating_point_comparison.hpp>
  12. #include <boost/accumulators/accumulators.hpp>
  13. #include <boost/accumulators/statistics/variates/covariate.hpp>
  14. #include <boost/accumulators/statistics.hpp>
  15. #include <boost/accumulators/statistics/weighted_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. accumulator_set<double, stats<tag::weighted_tail_variate_means<right, variate_set_type, tag::covariate1>(relative)>, double >
  28. acc1( right_tail_cache_size = c );
  29. accumulator_set<double, stats<tag::weighted_tail_variate_means<right, variate_set_type, tag::covariate1>(absolute)>, double >
  30. acc2( right_tail_cache_size = c );
  31. accumulator_set<double, stats<tag::weighted_tail_variate_means<left, variate_set_type, tag::covariate1>(relative)>, double >
  32. acc3( left_tail_cache_size = c );
  33. accumulator_set<double, stats<tag::weighted_tail_variate_means<left, variate_set_type, tag::covariate1>(absolute)>, double >
  34. 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., weight = 0.8, covariate1 = cov1);
  47. acc1( 50., weight = 0.9, covariate1 = cov2);
  48. acc1(200., weight = 1.0, covariate1 = cov3);
  49. acc1( 80., weight = 1.1, covariate1 = cov4);
  50. acc1( 20., weight = 1.2, covariate1 = cov5);
  51. acc2(100., weight = 0.8, covariate1 = cov1);
  52. acc2( 50., weight = 0.9, covariate1 = cov2);
  53. acc2(200., weight = 1.0, covariate1 = cov3);
  54. acc2( 80., weight = 1.1, covariate1 = cov4);
  55. acc2( 20., weight = 1.2, covariate1 = cov5);
  56. acc3(100., weight = 0.8, covariate1 = cov1);
  57. acc3( 50., weight = 0.9, covariate1 = cov2);
  58. acc3(200., weight = 1.0, covariate1 = cov3);
  59. acc3( 80., weight = 1.1, covariate1 = cov4);
  60. acc3( 20., weight = 1.2, covariate1 = cov5);
  61. acc4(100., weight = 0.8, covariate1 = cov1);
  62. acc4( 50., weight = 0.9, covariate1 = cov2);
  63. acc4(200., weight = 1.0, covariate1 = cov3);
  64. acc4( 80., weight = 1.1, covariate1 = cov4);
  65. acc4( 20., weight = 1.2, covariate1 = cov5);
  66. // check relative risk contributions
  67. 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) );
  68. 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) );
  69. 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) );
  70. 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) );
  71. 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) );
  72. 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) );
  73. 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) );
  74. 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) );
  75. // check absolute risk contributions
  76. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.7).begin() ), (0.8*10 + 1.0*46)/1.8 );
  77. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.7).begin() + 1), (0.8*20 + 1.0*64)/1.8 );
  78. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.7).begin() + 2), (0.8*30 + 1.0*40)/1.8 );
  79. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.7).begin() + 3), (0.8*40 + 1.0*50)/1.8 );
  80. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.3).begin() ), (0.9*26 + 1.2*2)/2.1 );
  81. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.3).begin() + 1), (0.9*4 + 1.2*2)/2.1 );
  82. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.3).begin() + 2), (0.9*17 + 1.2*2)/2.1 );
  83. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.3).begin() + 3), (0.9*3 + 1.2*14)/2.1 );
  84. // check relative risk contributions
  85. BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.9).begin() ), 1.0*46/(1.0*200) );
  86. BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 1), 1.0*64/(1.0*200) );
  87. BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 2), 1.0*40/(1.0*200) );
  88. BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 3), 1.0*50/(1.0*200) );
  89. BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.1).begin() ), 1.2*2/(1.2*20) );
  90. BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 1), 1.2*2/(1.2*20) );
  91. BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 2), 1.2*2/(1.2*20) );
  92. BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 3), 1.2*14/(1.2*20) );
  93. // check absolute risk contributions
  94. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.9).begin() ), 1.0*46/1.0 );
  95. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.9).begin() + 1), 1.0*64/1.0 );
  96. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.9).begin() + 2), 1.0*40/1.0 );
  97. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.9).begin() + 3), 1.0*50/1.0 );
  98. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.1).begin() ), 1.2*2/1.2 );
  99. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.1).begin() + 1), 1.2*2/1.2 );
  100. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.1).begin() + 2), 1.2*2/1.2 );
  101. BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.1).begin() + 3), 1.2*14/1.2 );
  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("weighted_tail_variate_means test");
  109. test->add(BOOST_TEST_CASE(&test_stat));
  110. return test;
  111. }