Chris@16
|
1 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 // 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_TAIL_QUANTILE_HPP_DE_01_01_2006
|
Chris@16
|
9 #define BOOST_ACCUMULATORS_STATISTICS_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/config/no_tr1/cmath.hpp> // For ceil
|
Chris@16
|
17 #include <boost/throw_exception.hpp>
|
Chris@16
|
18 #include <boost/parameter/keyword.hpp>
|
Chris@16
|
19 #include <boost/mpl/placeholders.hpp>
|
Chris@16
|
20 #include <boost/mpl/if.hpp>
|
Chris@16
|
21 #include <boost/type_traits/is_same.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/numeric/functional.hpp>
|
Chris@16
|
26 #include <boost/accumulators/framework/parameters/sample.hpp>
|
Chris@16
|
27 #include <boost/accumulators/statistics_fwd.hpp>
|
Chris@16
|
28 #include <boost/accumulators/statistics/tail.hpp>
|
Chris@16
|
29 #include <boost/accumulators/statistics/count.hpp>
|
Chris@16
|
30 #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
|
Chris@16
|
31
|
Chris@16
|
32 #ifdef _MSC_VER
|
Chris@16
|
33 # pragma warning(push)
|
Chris@16
|
34 # pragma warning(disable: 4127) // conditional expression is constant
|
Chris@16
|
35 #endif
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost { namespace accumulators
|
Chris@16
|
38 {
|
Chris@16
|
39
|
Chris@16
|
40 namespace impl
|
Chris@16
|
41 {
|
Chris@16
|
42 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
43 // tail_quantile_impl
|
Chris@16
|
44 // Tail quantile estimation based on order statistics
|
Chris@16
|
45 /**
|
Chris@16
|
46 @brief Tail quantile estimation based on order statistics (for both left and right tails)
|
Chris@16
|
47
|
Chris@16
|
48 The estimation of a tail quantile \f$\hat{q}\f$ with level \f$\alpha\f$ based on order statistics requires the
|
Chris@16
|
49 caching of at least the \f$\lceil n\alpha\rceil\f$ smallest or the \f$\lceil n(1-\alpha)\rceil\f$ largest samples,
|
Chris@16
|
50 \f$n\f$ being the total number of samples. The largest of the \f$\lceil n\alpha\rceil\f$ smallest samples or the
|
Chris@16
|
51 smallest of the \f$\lceil n(1-\alpha)\rceil\f$ largest samples provides an estimate for the quantile:
|
Chris@16
|
52
|
Chris@16
|
53 \f[
|
Chris@16
|
54 \hat{q}_{n,\alpha} = X_{\lceil \alpha n \rceil:n}
|
Chris@16
|
55 \f]
|
Chris@16
|
56
|
Chris@16
|
57 @param quantile_probability
|
Chris@16
|
58 */
|
Chris@16
|
59 template<typename Sample, typename LeftRight>
|
Chris@16
|
60 struct tail_quantile_impl
|
Chris@16
|
61 : accumulator_base
|
Chris@16
|
62 {
|
Chris@16
|
63 // for boost::result_of
|
Chris@16
|
64 typedef Sample result_type;
|
Chris@16
|
65
|
Chris@16
|
66 tail_quantile_impl(dont_care) {}
|
Chris@16
|
67
|
Chris@16
|
68 template<typename Args>
|
Chris@16
|
69 result_type result(Args const &args) const
|
Chris@16
|
70 {
|
Chris@16
|
71 std::size_t cnt = count(args);
|
Chris@16
|
72
|
Chris@16
|
73 std::size_t n = static_cast<std::size_t>(
|
Chris@16
|
74 std::ceil(
|
Chris@16
|
75 cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
|
Chris@16
|
76 )
|
Chris@16
|
77 );
|
Chris@16
|
78
|
Chris@16
|
79 // If n is in a valid range, return result, otherwise return NaN or throw exception
|
Chris@16
|
80 if ( n < static_cast<std::size_t>(tail(args).size()))
|
Chris@16
|
81 {
|
Chris@16
|
82 // Note that the cached samples of the left are sorted in ascending order,
|
Chris@16
|
83 // whereas the samples of the right tail are sorted in descending order
|
Chris@16
|
84 return *(boost::begin(tail(args)) + n - 1);
|
Chris@16
|
85 }
|
Chris@16
|
86 else
|
Chris@16
|
87 {
|
Chris@16
|
88 if (std::numeric_limits<result_type>::has_quiet_NaN)
|
Chris@16
|
89 {
|
Chris@16
|
90 return std::numeric_limits<result_type>::quiet_NaN();
|
Chris@16
|
91 }
|
Chris@16
|
92 else
|
Chris@16
|
93 {
|
Chris@16
|
94 std::ostringstream msg;
|
Chris@16
|
95 msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
|
Chris@16
|
96 boost::throw_exception(std::runtime_error(msg.str()));
|
Chris@16
|
97 return Sample(0);
|
Chris@16
|
98 }
|
Chris@16
|
99 }
|
Chris@16
|
100 }
|
Chris@16
|
101 };
|
Chris@16
|
102 } // namespace impl
|
Chris@16
|
103
|
Chris@16
|
104 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
105 // tag::tail_quantile<>
|
Chris@16
|
106 //
|
Chris@16
|
107 namespace tag
|
Chris@16
|
108 {
|
Chris@16
|
109 template<typename LeftRight>
|
Chris@16
|
110 struct tail_quantile
|
Chris@16
|
111 : depends_on<count, tail<LeftRight> >
|
Chris@16
|
112 {
|
Chris@16
|
113 /// INTERNAL ONLY
|
Chris@16
|
114 ///
|
Chris@16
|
115 typedef accumulators::impl::tail_quantile_impl<mpl::_1, LeftRight> impl;
|
Chris@16
|
116 };
|
Chris@16
|
117 }
|
Chris@16
|
118
|
Chris@16
|
119 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
120 // extract::tail_quantile
|
Chris@16
|
121 //
|
Chris@16
|
122 namespace extract
|
Chris@16
|
123 {
|
Chris@16
|
124 extractor<tag::quantile> const tail_quantile = {};
|
Chris@16
|
125
|
Chris@16
|
126 BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_quantile)
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129 using extract::tail_quantile;
|
Chris@16
|
130
|
Chris@16
|
131 // for the purposes of feature-based dependency resolution,
|
Chris@16
|
132 // tail_quantile<LeftRight> provide the same feature as quantile
|
Chris@16
|
133 template<typename LeftRight>
|
Chris@16
|
134 struct feature_of<tag::tail_quantile<LeftRight> >
|
Chris@16
|
135 : feature_of<tag::quantile>
|
Chris@16
|
136 {
|
Chris@16
|
137 };
|
Chris@16
|
138
|
Chris@16
|
139 // So that tail_quantile can be automatically substituted with
|
Chris@16
|
140 // weighted_tail_quantile when the weight parameter is non-void.
|
Chris@16
|
141 template<typename LeftRight>
|
Chris@16
|
142 struct as_weighted_feature<tag::tail_quantile<LeftRight> >
|
Chris@16
|
143 {
|
Chris@16
|
144 typedef tag::weighted_tail_quantile<LeftRight> type;
|
Chris@16
|
145 };
|
Chris@16
|
146
|
Chris@16
|
147 template<typename LeftRight>
|
Chris@16
|
148 struct feature_of<tag::weighted_tail_quantile<LeftRight> >
|
Chris@16
|
149 : feature_of<tag::tail_quantile<LeftRight> >
|
Chris@16
|
150 {};
|
Chris@16
|
151
|
Chris@16
|
152 }} // namespace boost::accumulators
|
Chris@16
|
153
|
Chris@16
|
154 #ifdef _MSC_VER
|
Chris@16
|
155 # pragma warning(pop)
|
Chris@16
|
156 #endif
|
Chris@16
|
157
|
Chris@16
|
158 #endif
|