fix doc example typo
[boost.git] / boost / accumulators / statistics / weighted_tail_quantile.hpp
blob997ac3a21b80d747c05bfda55b318b674855e0f6
1 ///////////////////////////////////////////////////////////////////////////////
2 // weighted_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)
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
11 #include <vector>
12 #include <limits>
13 #include <functional>
14 #include <sstream>
15 #include <stdexcept>
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>
31 #ifdef _MSC_VER
32 # pragma warning(push)
33 # pragma warning(disable: 4127) // conditional expression is constant
34 #endif
36 namespace boost { namespace accumulators
39 namespace impl
41 ///////////////////////////////////////////////////////////////////////////////
42 // weighted_tail_quantile_impl
43 // Tail quantile estimation based on order statistics of weighted samples
44 /**
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
51 \f[
52 \lambda = \inf\left\{ l \left| \frac{1}{\bar{w}_n}\sum_{i=1}^{l} w_i \geq \alpha \right. \right\}
53 \f]
55 and
57 \f[
58 \rho = \sup\left\{ r \left| \frac{1}{\bar{w}_n}\sum_{i=r}^{n} w_i \geq (1 - \alpha) \right. \right\},
59 \f]
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
67 : accumulator_base
69 typedef typename numeric::functional::average<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] );
81 std::size_t n = 0;
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);
89 n++;
91 else
93 if (std::numeric_limits<result_type>::has_quiet_NaN)
95 return std::numeric_limits<result_type>::quiet_NaN();
97 else
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()));
102 return Sample(0);
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);
112 } // namespace impl
114 ///////////////////////////////////////////////////////////////////////////////
115 // tag::weighted_tail_quantile<>
117 namespace tag
119 template<typename LeftRight>
120 struct weighted_tail_quantile
121 : depends_on<sum_of_weights, tail_weights<LeftRight> >
123 /// INTERNAL ONLY
124 typedef accumulators::impl::weighted_tail_quantile_impl<mpl::_1, mpl::_2, LeftRight> impl;
128 ///////////////////////////////////////////////////////////////////////////////
129 // extract::weighted_tail_quantile
131 namespace extract
133 extractor<tag::quantile> const weighted_tail_quantile = {};
136 using extract::weighted_tail_quantile;
138 }} // namespace boost::accumulators
140 #ifdef _MSC_VER
141 # pragma warning(pop)
142 #endif
144 #endif