1 ///////////////////////////////////////////////////////////////////////////////
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)
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_TAIL_QUANTILE_HPP_DE_01_01_2006
9 #define BOOST_ACCUMULATORS_STATISTICS_TAIL_QUANTILE_HPP_DE_01_01_2006
16 #include <boost/config/no_tr1/cmath.hpp> // For ceil
17 #include <boost/throw_exception.hpp>
18 #include <boost/parameter/keyword.hpp>
19 #include <boost/mpl/placeholders.hpp>
20 #include <boost/mpl/if.hpp>
21 #include <boost/type_traits/is_same.hpp>
22 #include <boost/accumulators/framework/depends_on.hpp>
23 #include <boost/accumulators/framework/accumulator_base.hpp>
24 #include <boost/accumulators/framework/extractor.hpp>
25 #include <boost/accumulators/numeric/functional.hpp>
26 #include <boost/accumulators/framework/parameters/sample.hpp>
27 #include <boost/accumulators/statistics_fwd.hpp>
28 #include <boost/accumulators/statistics/tail.hpp>
29 #include <boost/accumulators/statistics/count.hpp>
30 #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
33 # pragma warning(push)
34 # pragma warning(disable: 4127) // conditional expression is constant
37 namespace boost { namespace accumulators
42 ///////////////////////////////////////////////////////////////////////////////
44 // Tail quantile estimation based on order statistics
46 @brief Tail quantile estimation based on order statistics (for both left and right tails)
48 The estimation of a tail quantile \f$\hat{q}\f$ with level \f$\alpha\f$ based on order statistics requires the
49 caching of at least the \f$\lceil n\alpha\rceil\f$ smallest or the \f$\lceil n(1-\alpha)\rceil\f$ largest samples,
50 \f$n\f$ being the total number of samples. The largest of the \f$\lceil n\alpha\rceil\f$ smallest samples or the
51 smallest of the \f$\lceil n(1-\alpha)\rceil\f$ largest samples provides an estimate for the quantile:
54 \hat{q}_{n,\alpha} = X_{\lceil \alpha n \rceil:n}
57 @param quantile_probability
59 template<typename Sample, typename LeftRight>
60 struct tail_quantile_impl
63 // for boost::result_of
64 typedef Sample result_type;
66 tail_quantile_impl(dont_care) {}
68 template<typename Args>
69 result_type result(Args const &args) const
71 std::size_t cnt = count(args);
73 std::size_t n = static_cast<std::size_t>(
75 cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
79 // If n is in a valid range, return result, otherwise return NaN or throw exception
80 if ( n < static_cast<std::size_t>(tail(args).size()))
82 // Note that the cached samples of the left are sorted in ascending order,
83 // whereas the samples of the right tail are sorted in descending order
84 return *(boost::begin(tail(args)) + n - 1);
88 if (std::numeric_limits<result_type>::has_quiet_NaN)
90 return std::numeric_limits<result_type>::quiet_NaN();
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()));
104 ///////////////////////////////////////////////////////////////////////////////
105 // tag::tail_quantile<>
109 template<typename LeftRight>
111 : depends_on<count, tail<LeftRight> >
115 typedef accumulators::impl::tail_quantile_impl<mpl::_1, LeftRight> impl;
119 ///////////////////////////////////////////////////////////////////////////////
120 // extract::tail_quantile
124 extractor<tag::quantile> const tail_quantile = {};
126 BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_quantile)
129 using extract::tail_quantile;
131 // for the purposes of feature-based dependency resolution,
132 // tail_quantile<LeftRight> provide the same feature as quantile
133 template<typename LeftRight>
134 struct feature_of<tag::tail_quantile<LeftRight> >
135 : feature_of<tag::quantile>
139 // So that tail_quantile can be automatically substituted with
140 // weighted_tail_quantile when the weight parameter is non-void.
141 template<typename LeftRight>
142 struct as_weighted_feature<tag::tail_quantile<LeftRight> >
144 typedef tag::weighted_tail_quantile<LeftRight> type;
147 template<typename LeftRight>
148 struct feature_of<tag::weighted_tail_quantile<LeftRight> >
149 : feature_of<tag::tail_quantile<LeftRight> >
152 }} // namespace boost::accumulators
155 # pragma warning(pop)