tail_quantile.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // tail_quantile.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_TAIL_QUANTILE_HPP_DE_01_01_2006
  8. #define BOOST_ACCUMULATORS_STATISTICS_TAIL_QUANTILE_HPP_DE_01_01_2006
  9. #include <vector>
  10. #include <limits>
  11. #include <functional>
  12. #include <sstream>
  13. #include <stdexcept>
  14. #include <boost/config/no_tr1/cmath.hpp> // For ceil
  15. #include <boost/throw_exception.hpp>
  16. #include <boost/parameter/keyword.hpp>
  17. #include <boost/mpl/placeholders.hpp>
  18. #include <boost/mpl/if.hpp>
  19. #include <boost/type_traits/is_same.hpp>
  20. #include <boost/accumulators/framework/depends_on.hpp>
  21. #include <boost/accumulators/framework/accumulator_base.hpp>
  22. #include <boost/accumulators/framework/extractor.hpp>
  23. #include <boost/accumulators/numeric/functional.hpp>
  24. #include <boost/accumulators/framework/parameters/sample.hpp>
  25. #include <boost/accumulators/statistics_fwd.hpp>
  26. #include <boost/accumulators/statistics/tail.hpp>
  27. #include <boost/accumulators/statistics/count.hpp>
  28. #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
  29. #ifdef _MSC_VER
  30. # pragma warning(push)
  31. # pragma warning(disable: 4127) // conditional expression is constant
  32. #endif
  33. namespace boost { namespace accumulators
  34. {
  35. namespace impl
  36. {
  37. ///////////////////////////////////////////////////////////////////////////////
  38. // tail_quantile_impl
  39. // Tail quantile estimation based on order statistics
  40. /**
  41. @brief Tail quantile estimation based on order statistics (for both left and right tails)
  42. The estimation of a tail quantile \f$\hat{q}\f$ with level \f$\alpha\f$ based on order statistics requires the
  43. caching of at least the \f$\lceil n\alpha\rceil\f$ smallest or the \f$\lceil n(1-\alpha)\rceil\f$ largest samples,
  44. \f$n\f$ being the total number of samples. The largest of the \f$\lceil n\alpha\rceil\f$ smallest samples or the
  45. smallest of the \f$\lceil n(1-\alpha)\rceil\f$ largest samples provides an estimate for the quantile:
  46. \f[
  47. \hat{q}_{n,\alpha} = X_{\lceil \alpha n \rceil:n}
  48. \f]
  49. @param quantile_probability
  50. */
  51. template<typename Sample, typename LeftRight>
  52. struct tail_quantile_impl
  53. : accumulator_base
  54. {
  55. // for boost::result_of
  56. typedef Sample result_type;
  57. tail_quantile_impl(dont_care) {}
  58. template<typename Args>
  59. result_type result(Args const &args) const
  60. {
  61. std::size_t cnt = count(args);
  62. std::size_t n = static_cast<std::size_t>(
  63. std::ceil(
  64. cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
  65. )
  66. );
  67. // If n is in a valid range, return result, otherwise return NaN or throw exception
  68. if ( n < static_cast<std::size_t>(tail(args).size()))
  69. {
  70. // Note that the cached samples of the left are sorted in ascending order,
  71. // whereas the samples of the right tail are sorted in descending order
  72. return *(boost::begin(tail(args)) + n - 1);
  73. }
  74. else
  75. {
  76. if (std::numeric_limits<result_type>::has_quiet_NaN)
  77. {
  78. return std::numeric_limits<result_type>::quiet_NaN();
  79. }
  80. else
  81. {
  82. std::ostringstream msg;
  83. msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
  84. boost::throw_exception(std::runtime_error(msg.str()));
  85. return Sample(0);
  86. }
  87. }
  88. }
  89. };
  90. } // namespace impl
  91. ///////////////////////////////////////////////////////////////////////////////
  92. // tag::tail_quantile<>
  93. //
  94. namespace tag
  95. {
  96. template<typename LeftRight>
  97. struct tail_quantile
  98. : depends_on<count, tail<LeftRight> >
  99. {
  100. /// INTERNAL ONLY
  101. ///
  102. typedef accumulators::impl::tail_quantile_impl<mpl::_1, LeftRight> impl;
  103. };
  104. }
  105. ///////////////////////////////////////////////////////////////////////////////
  106. // extract::tail_quantile
  107. //
  108. namespace extract
  109. {
  110. extractor<tag::quantile> const tail_quantile = {};
  111. BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_quantile)
  112. }
  113. using extract::tail_quantile;
  114. // for the purposes of feature-based dependency resolution,
  115. // tail_quantile<LeftRight> provide the same feature as quantile
  116. template<typename LeftRight>
  117. struct feature_of<tag::tail_quantile<LeftRight> >
  118. : feature_of<tag::quantile>
  119. {
  120. };
  121. // So that tail_quantile can be automatically substituted with
  122. // weighted_tail_quantile when the weight parameter is non-void.
  123. template<typename LeftRight>
  124. struct as_weighted_feature<tag::tail_quantile<LeftRight> >
  125. {
  126. typedef tag::weighted_tail_quantile<LeftRight> type;
  127. };
  128. template<typename LeftRight>
  129. struct feature_of<tag::weighted_tail_quantile<LeftRight> >
  130. : feature_of<tag::tail_quantile<LeftRight> >
  131. {};
  132. }} // namespace boost::accumulators
  133. #ifdef _MSC_VER
  134. # pragma warning(pop)
  135. #endif
  136. #endif