tail_quantile.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // serialization is done by accumulators it depends on
  90. template<class Archive>
  91. void serialize(Archive & ar, const unsigned int file_version) {}
  92. };
  93. } // namespace impl
  94. ///////////////////////////////////////////////////////////////////////////////
  95. // tag::tail_quantile<>
  96. //
  97. namespace tag
  98. {
  99. template<typename LeftRight>
  100. struct tail_quantile
  101. : depends_on<count, tail<LeftRight> >
  102. {
  103. /// INTERNAL ONLY
  104. ///
  105. typedef accumulators::impl::tail_quantile_impl<mpl::_1, LeftRight> impl;
  106. };
  107. }
  108. ///////////////////////////////////////////////////////////////////////////////
  109. // extract::tail_quantile
  110. //
  111. namespace extract
  112. {
  113. extractor<tag::quantile> const tail_quantile = {};
  114. BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_quantile)
  115. }
  116. using extract::tail_quantile;
  117. // for the purposes of feature-based dependency resolution,
  118. // tail_quantile<LeftRight> provide the same feature as quantile
  119. template<typename LeftRight>
  120. struct feature_of<tag::tail_quantile<LeftRight> >
  121. : feature_of<tag::quantile>
  122. {
  123. };
  124. // So that tail_quantile can be automatically substituted with
  125. // weighted_tail_quantile when the weight parameter is non-void.
  126. template<typename LeftRight>
  127. struct as_weighted_feature<tag::tail_quantile<LeftRight> >
  128. {
  129. typedef tag::weighted_tail_quantile<LeftRight> type;
  130. };
  131. template<typename LeftRight>
  132. struct feature_of<tag::weighted_tail_quantile<LeftRight> >
  133. : feature_of<tag::tail_quantile<LeftRight> >
  134. {};
  135. }} // namespace boost::accumulators
  136. #ifdef _MSC_VER
  137. # pragma warning(pop)
  138. #endif
  139. #endif