weighted_tail_mean.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // weighted_tail_mean.hpp
  3. //
  4. // Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_MEAN_HPP_DE_01_01_2006
  8. #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_MEAN_HPP_DE_01_01_2006
  9. #include <numeric>
  10. #include <vector>
  11. #include <limits>
  12. #include <functional>
  13. #include <sstream>
  14. #include <stdexcept>
  15. #include <boost/throw_exception.hpp>
  16. #include <boost/parameter/keyword.hpp>
  17. #include <boost/mpl/placeholders.hpp>
  18. #include <boost/type_traits/is_same.hpp>
  19. #include <boost/accumulators/numeric/functional.hpp>
  20. #include <boost/accumulators/framework/accumulator_base.hpp>
  21. #include <boost/accumulators/framework/extractor.hpp>
  22. #include <boost/accumulators/framework/parameters/sample.hpp>
  23. #include <boost/accumulators/statistics_fwd.hpp>
  24. #include <boost/accumulators/statistics/tail.hpp>
  25. #include <boost/accumulators/statistics/tail_mean.hpp>
  26. #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
  27. #ifdef _MSC_VER
  28. # pragma warning(push)
  29. # pragma warning(disable: 4127) // conditional expression is constant
  30. #endif
  31. namespace boost { namespace accumulators
  32. {
  33. namespace impl
  34. {
  35. ///////////////////////////////////////////////////////////////////////////////
  36. // coherent_weighted_tail_mean_impl
  37. //
  38. // TODO
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // non_coherent_weighted_tail_mean_impl
  41. //
  42. /**
  43. @brief Estimation of the (non-coherent) weighted tail mean based on order statistics (for both left and right tails)
  44. An estimation of the non-coherent, weighted tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$ is given by the weighted mean
  45. of the
  46. \f[
  47. \lambda = \inf\left\{ l \left| \frac{1}{\bar{w}_n}\sum_{i=1}^{l} w_i \geq \alpha \right. \right\}
  48. \f]
  49. smallest samples (left tail) or the weighted mean of the
  50. \f[
  51. n + 1 - \rho = n + 1 - \sup\left\{ r \left| \frac{1}{\bar{w}_n}\sum_{i=r}^{n} w_i \geq (1 - \alpha) \right. \right\}
  52. \f]
  53. largest samples (right tail) above a quantile \f$\hat{q}_{\alpha}\f$ of level \f$\alpha\f$, \f$n\f$ being the total number of sample
  54. and \f$\bar{w}_n\f$ the sum of all \f$n\f$ weights:
  55. \f[
  56. \widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X) = \frac{\sum_{i=1}^{\lambda} w_i X_{i:n}}{\sum_{i=1}^{\lambda} w_i},
  57. \f]
  58. \f[
  59. \widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X) = \frac{\sum_{i=\rho}^n w_i X_{i:n}}{\sum_{i=\rho}^n w_i}.
  60. \f]
  61. @param quantile_probability
  62. */
  63. template<typename Sample, typename Weight, typename LeftRight>
  64. struct non_coherent_weighted_tail_mean_impl
  65. : accumulator_base
  66. {
  67. typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
  68. typedef typename numeric::functional::fdiv<Weight, std::size_t>::result_type float_type;
  69. // for boost::result_of
  70. typedef typename numeric::functional::fdiv<weighted_sample, std::size_t>::result_type result_type;
  71. non_coherent_weighted_tail_mean_impl(dont_care) {}
  72. template<typename Args>
  73. result_type result(Args const &args) const
  74. {
  75. float_type threshold = sum_of_weights(args)
  76. * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] );
  77. std::size_t n = 0;
  78. Weight sum = Weight(0);
  79. while (sum < threshold)
  80. {
  81. if (n < static_cast<std::size_t>(tail_weights(args).size()))
  82. {
  83. sum += *(tail_weights(args).begin() + n);
  84. n++;
  85. }
  86. else
  87. {
  88. if (std::numeric_limits<result_type>::has_quiet_NaN)
  89. {
  90. return std::numeric_limits<result_type>::quiet_NaN();
  91. }
  92. else
  93. {
  94. std::ostringstream msg;
  95. msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
  96. boost::throw_exception(std::runtime_error(msg.str()));
  97. return result_type(0);
  98. }
  99. }
  100. }
  101. return numeric::fdiv(
  102. std::inner_product(
  103. tail(args).begin()
  104. , tail(args).begin() + n
  105. , tail_weights(args).begin()
  106. , weighted_sample(0)
  107. )
  108. , sum
  109. );
  110. }
  111. };
  112. } // namespace impl
  113. ///////////////////////////////////////////////////////////////////////////////
  114. // tag::non_coherent_weighted_tail_mean<>
  115. //
  116. namespace tag
  117. {
  118. template<typename LeftRight>
  119. struct non_coherent_weighted_tail_mean
  120. : depends_on<sum_of_weights, tail_weights<LeftRight> >
  121. {
  122. typedef accumulators::impl::non_coherent_weighted_tail_mean_impl<mpl::_1, mpl::_2, LeftRight> impl;
  123. };
  124. }
  125. ///////////////////////////////////////////////////////////////////////////////
  126. // extract::non_coherent_weighted_tail_mean;
  127. //
  128. namespace extract
  129. {
  130. extractor<tag::abstract_non_coherent_tail_mean> const non_coherent_weighted_tail_mean = {};
  131. BOOST_ACCUMULATORS_IGNORE_GLOBAL(non_coherent_weighted_tail_mean)
  132. }
  133. using extract::non_coherent_weighted_tail_mean;
  134. }} // namespace boost::accumulators
  135. #ifdef _MSC_VER
  136. # pragma warning(pop)
  137. #endif
  138. #endif