1 ///////////////////////////////////////////////////////////////////////////////
2 // weighted_tail_quantile.hpp
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_WEIGHTED_TAIL_QUANTILE_HPP_DE_01_01_2006
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_QUANTILE_HPP_DE_01_01_2006
16 #include <boost/throw_exception.hpp>
17 #include <boost/parameter/keyword.hpp>
18 #include <boost/mpl/placeholders.hpp>
19 #include <boost/mpl/if.hpp>
20 #include <boost/type_traits/is_same.hpp>
21 #include <boost/accumulators/numeric/functional.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/framework/parameters/sample.hpp>
26 #include <boost/accumulators/statistics_fwd.hpp>
27 #include <boost/accumulators/statistics/tail.hpp>
28 #include <boost/accumulators/statistics/tail_quantile.hpp>
29 #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
32 # pragma warning(push)
33 # pragma warning(disable: 4127) // conditional expression is constant
36 namespace boost { namespace accumulators
41 ///////////////////////////////////////////////////////////////////////////////
42 // weighted_tail_quantile_impl
43 // Tail quantile estimation based on order statistics of weighted samples
45 @brief Tail quantile estimation based on order statistics of weighted samples (for both left and right tails)
47 An estimator \f$\hat{q}\f$ of tail quantiles with level \f$\alpha\f$ based on order statistics
48 \f$X_{1:n} \leq X_{2:n} \leq\dots\leq X_{n:n}\f$ of weighted samples are given by \f$X_{\lambda:n}\f$ (left tail)
49 and \f$X_{\rho:n}\f$ (right tail), where
52 \lambda = \inf\left\{ l \left| \frac{1}{\bar{w}_n}\sum_{i=1}^{l} w_i \geq \alpha \right. \right\}
58 \rho = \sup\left\{ r \left| \frac{1}{\bar{w}_n}\sum_{i=r}^{n} w_i \geq (1 - \alpha) \right. \right\},
61 \f$n\f$ being the number of samples and \f$\bar{w}_n\f$ the sum of all weights.
63 @param quantile_probability
65 template<typename Sample, typename Weight, typename LeftRight>
66 struct weighted_tail_quantile_impl
69 typedef typename numeric::functional::fdiv<Weight, std::size_t>::result_type float_type;
70 // for boost::result_of
71 typedef Sample result_type;
73 weighted_tail_quantile_impl(dont_care) {}
75 template<typename Args>
76 result_type result(Args const &args) const
78 float_type threshold = sum_of_weights(args)
79 * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] );
82 Weight sum = Weight(0);
84 while (sum < threshold)
86 if (n < static_cast<std::size_t>(tail_weights(args).size()))
88 sum += *(tail_weights(args).begin() + n);
93 if (std::numeric_limits<result_type>::has_quiet_NaN)
95 return std::numeric_limits<result_type>::quiet_NaN();
99 std::ostringstream msg;
100 msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
101 boost::throw_exception(std::runtime_error(msg.str()));
107 // Note that the cached samples of the left are sorted in ascending order,
108 // whereas the samples of the right tail are sorted in descending order
109 return *(boost::begin(tail(args)) + n - 1);
114 ///////////////////////////////////////////////////////////////////////////////
115 // tag::weighted_tail_quantile<>
119 template<typename LeftRight>
120 struct weighted_tail_quantile
121 : depends_on<sum_of_weights, tail_weights<LeftRight> >
124 typedef accumulators::impl::weighted_tail_quantile_impl<mpl::_1, mpl::_2, LeftRight> impl;
128 ///////////////////////////////////////////////////////////////////////////////
129 // extract::weighted_tail_quantile
133 extractor<tag::quantile> const weighted_tail_quantile = {};
135 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_tail_quantile)
138 using extract::weighted_tail_quantile;
140 }} // namespace boost::accumulators
143 # pragma warning(pop)