Chris@16
|
1 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 // weighted_tail_quantile.hpp
|
Chris@16
|
3 //
|
Chris@16
|
4 // Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
Chris@16
|
5 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7
|
Chris@16
|
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_QUANTILE_HPP_DE_01_01_2006
|
Chris@16
|
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_QUANTILE_HPP_DE_01_01_2006
|
Chris@16
|
10
|
Chris@16
|
11 #include <vector>
|
Chris@16
|
12 #include <limits>
|
Chris@16
|
13 #include <functional>
|
Chris@16
|
14 #include <sstream>
|
Chris@16
|
15 #include <stdexcept>
|
Chris@16
|
16 #include <boost/throw_exception.hpp>
|
Chris@16
|
17 #include <boost/parameter/keyword.hpp>
|
Chris@16
|
18 #include <boost/mpl/placeholders.hpp>
|
Chris@16
|
19 #include <boost/mpl/if.hpp>
|
Chris@16
|
20 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
21 #include <boost/accumulators/numeric/functional.hpp>
|
Chris@16
|
22 #include <boost/accumulators/framework/depends_on.hpp>
|
Chris@16
|
23 #include <boost/accumulators/framework/accumulator_base.hpp>
|
Chris@16
|
24 #include <boost/accumulators/framework/extractor.hpp>
|
Chris@16
|
25 #include <boost/accumulators/framework/parameters/sample.hpp>
|
Chris@16
|
26 #include <boost/accumulators/statistics_fwd.hpp>
|
Chris@16
|
27 #include <boost/accumulators/statistics/tail.hpp>
|
Chris@16
|
28 #include <boost/accumulators/statistics/tail_quantile.hpp>
|
Chris@16
|
29 #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 #ifdef _MSC_VER
|
Chris@16
|
32 # pragma warning(push)
|
Chris@16
|
33 # pragma warning(disable: 4127) // conditional expression is constant
|
Chris@16
|
34 #endif
|
Chris@16
|
35
|
Chris@16
|
36 namespace boost { namespace accumulators
|
Chris@16
|
37 {
|
Chris@16
|
38
|
Chris@16
|
39 namespace impl
|
Chris@16
|
40 {
|
Chris@16
|
41 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
42 // weighted_tail_quantile_impl
|
Chris@16
|
43 // Tail quantile estimation based on order statistics of weighted samples
|
Chris@16
|
44 /**
|
Chris@16
|
45 @brief Tail quantile estimation based on order statistics of weighted samples (for both left and right tails)
|
Chris@16
|
46
|
Chris@16
|
47 An estimator \f$\hat{q}\f$ of tail quantiles with level \f$\alpha\f$ based on order statistics
|
Chris@16
|
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)
|
Chris@16
|
49 and \f$X_{\rho:n}\f$ (right tail), where
|
Chris@16
|
50
|
Chris@16
|
51 \f[
|
Chris@16
|
52 \lambda = \inf\left\{ l \left| \frac{1}{\bar{w}_n}\sum_{i=1}^{l} w_i \geq \alpha \right. \right\}
|
Chris@16
|
53 \f]
|
Chris@16
|
54
|
Chris@16
|
55 and
|
Chris@16
|
56
|
Chris@16
|
57 \f[
|
Chris@16
|
58 \rho = \sup\left\{ r \left| \frac{1}{\bar{w}_n}\sum_{i=r}^{n} w_i \geq (1 - \alpha) \right. \right\},
|
Chris@16
|
59 \f]
|
Chris@16
|
60
|
Chris@16
|
61 \f$n\f$ being the number of samples and \f$\bar{w}_n\f$ the sum of all weights.
|
Chris@16
|
62
|
Chris@16
|
63 @param quantile_probability
|
Chris@16
|
64 */
|
Chris@16
|
65 template<typename Sample, typename Weight, typename LeftRight>
|
Chris@16
|
66 struct weighted_tail_quantile_impl
|
Chris@16
|
67 : accumulator_base
|
Chris@16
|
68 {
|
Chris@16
|
69 typedef typename numeric::functional::fdiv<Weight, std::size_t>::result_type float_type;
|
Chris@16
|
70 // for boost::result_of
|
Chris@16
|
71 typedef Sample result_type;
|
Chris@16
|
72
|
Chris@16
|
73 weighted_tail_quantile_impl(dont_care) {}
|
Chris@16
|
74
|
Chris@16
|
75 template<typename Args>
|
Chris@16
|
76 result_type result(Args const &args) const
|
Chris@16
|
77 {
|
Chris@16
|
78 float_type threshold = sum_of_weights(args)
|
Chris@16
|
79 * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] );
|
Chris@16
|
80
|
Chris@16
|
81 std::size_t n = 0;
|
Chris@16
|
82 Weight sum = Weight(0);
|
Chris@16
|
83
|
Chris@16
|
84 while (sum < threshold)
|
Chris@16
|
85 {
|
Chris@16
|
86 if (n < static_cast<std::size_t>(tail_weights(args).size()))
|
Chris@16
|
87 {
|
Chris@16
|
88 sum += *(tail_weights(args).begin() + n);
|
Chris@16
|
89 n++;
|
Chris@16
|
90 }
|
Chris@16
|
91 else
|
Chris@16
|
92 {
|
Chris@16
|
93 if (std::numeric_limits<result_type>::has_quiet_NaN)
|
Chris@16
|
94 {
|
Chris@16
|
95 return std::numeric_limits<result_type>::quiet_NaN();
|
Chris@16
|
96 }
|
Chris@16
|
97 else
|
Chris@16
|
98 {
|
Chris@16
|
99 std::ostringstream msg;
|
Chris@16
|
100 msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
|
Chris@16
|
101 boost::throw_exception(std::runtime_error(msg.str()));
|
Chris@16
|
102 return Sample(0);
|
Chris@16
|
103 }
|
Chris@16
|
104 }
|
Chris@16
|
105 }
|
Chris@16
|
106
|
Chris@16
|
107 // Note that the cached samples of the left are sorted in ascending order,
|
Chris@16
|
108 // whereas the samples of the right tail are sorted in descending order
|
Chris@16
|
109 return *(boost::begin(tail(args)) + n - 1);
|
Chris@16
|
110 }
|
Chris@16
|
111 };
|
Chris@16
|
112 } // namespace impl
|
Chris@16
|
113
|
Chris@16
|
114 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
115 // tag::weighted_tail_quantile<>
|
Chris@16
|
116 //
|
Chris@16
|
117 namespace tag
|
Chris@16
|
118 {
|
Chris@16
|
119 template<typename LeftRight>
|
Chris@16
|
120 struct weighted_tail_quantile
|
Chris@16
|
121 : depends_on<sum_of_weights, tail_weights<LeftRight> >
|
Chris@16
|
122 {
|
Chris@16
|
123 /// INTERNAL ONLY
|
Chris@16
|
124 typedef accumulators::impl::weighted_tail_quantile_impl<mpl::_1, mpl::_2, LeftRight> impl;
|
Chris@16
|
125 };
|
Chris@16
|
126 }
|
Chris@16
|
127
|
Chris@16
|
128 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
129 // extract::weighted_tail_quantile
|
Chris@16
|
130 //
|
Chris@16
|
131 namespace extract
|
Chris@16
|
132 {
|
Chris@16
|
133 extractor<tag::quantile> const weighted_tail_quantile = {};
|
Chris@16
|
134
|
Chris@16
|
135 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_tail_quantile)
|
Chris@16
|
136 }
|
Chris@16
|
137
|
Chris@16
|
138 using extract::weighted_tail_quantile;
|
Chris@16
|
139
|
Chris@16
|
140 }} // namespace boost::accumulators
|
Chris@16
|
141
|
Chris@16
|
142 #ifdef _MSC_VER
|
Chris@16
|
143 # pragma warning(pop)
|
Chris@16
|
144 #endif
|
Chris@16
|
145
|
Chris@16
|
146 #endif
|