Chris@16
|
1 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 // tail_mean.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_MEAN_HPP_DE_01_01_2006
|
Chris@16
|
9 #define BOOST_ACCUMULATORS_STATISTICS_TAIL_MEAN_HPP_DE_01_01_2006
|
Chris@16
|
10
|
Chris@16
|
11 #include <numeric>
|
Chris@16
|
12 #include <vector>
|
Chris@16
|
13 #include <limits>
|
Chris@16
|
14 #include <functional>
|
Chris@16
|
15 #include <sstream>
|
Chris@16
|
16 #include <stdexcept>
|
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/type_traits/is_same.hpp>
|
Chris@16
|
21 #include <boost/accumulators/framework/accumulator_base.hpp>
|
Chris@16
|
22 #include <boost/accumulators/framework/extractor.hpp>
|
Chris@16
|
23 #include <boost/accumulators/numeric/functional.hpp>
|
Chris@16
|
24 #include <boost/accumulators/framework/parameters/sample.hpp>
|
Chris@16
|
25 #include <boost/accumulators/statistics_fwd.hpp>
|
Chris@16
|
26 #include <boost/accumulators/statistics/count.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 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
43 // coherent_tail_mean_impl
|
Chris@16
|
44 //
|
Chris@16
|
45 /**
|
Chris@16
|
46 @brief Estimation of the coherent tail mean based on order statistics (for both left and right tails)
|
Chris@16
|
47
|
Chris@16
|
48 The coherent tail mean \f$\widehat{CTM}_{n,\alpha}(X)\f$ is equal to the non-coherent tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$
|
Chris@16
|
49 plus a correction term that ensures coherence in case of non-continuous distributions.
|
Chris@16
|
50
|
Chris@16
|
51 \f[
|
Chris@16
|
52 \widehat{CTM}_{n,\alpha}^{\mathrm{right}}(X) = \widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X) +
|
Chris@16
|
53 \frac{1}{\lceil n(1-\alpha)\rceil}\hat{q}_{n,\alpha}(X)\left(1 - \alpha - \frac{1}{n}\lceil n(1-\alpha)\rceil \right)
|
Chris@16
|
54 \f]
|
Chris@16
|
55
|
Chris@16
|
56 \f[
|
Chris@16
|
57 \widehat{CTM}_{n,\alpha}^{\mathrm{left}}(X) = \widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X) +
|
Chris@16
|
58 \frac{1}{\lceil n\alpha\rceil}\hat{q}_{n,\alpha}(X)\left(\alpha - \frac{1}{n}\lceil n\alpha\rceil \right)
|
Chris@16
|
59 \f]
|
Chris@16
|
60 */
|
Chris@16
|
61 template<typename Sample, typename LeftRight>
|
Chris@16
|
62 struct coherent_tail_mean_impl
|
Chris@16
|
63 : accumulator_base
|
Chris@16
|
64 {
|
Chris@16
|
65 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
Chris@16
|
66 // for boost::result_of
|
Chris@16
|
67 typedef float_type result_type;
|
Chris@16
|
68
|
Chris@16
|
69 coherent_tail_mean_impl(dont_care) {}
|
Chris@16
|
70
|
Chris@16
|
71 template<typename Args>
|
Chris@16
|
72 result_type result(Args const &args) const
|
Chris@16
|
73 {
|
Chris@16
|
74 std::size_t cnt = count(args);
|
Chris@16
|
75
|
Chris@16
|
76 std::size_t n = static_cast<std::size_t>(
|
Chris@16
|
77 std::ceil(
|
Chris@16
|
78 cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
|
Chris@16
|
79 )
|
Chris@16
|
80 );
|
Chris@16
|
81
|
Chris@16
|
82 extractor<tag::non_coherent_tail_mean<LeftRight> > const some_non_coherent_tail_mean = {};
|
Chris@16
|
83
|
Chris@16
|
84 return some_non_coherent_tail_mean(args)
|
Chris@16
|
85 + numeric::fdiv(quantile(args), n)
|
Chris@16
|
86 * (
|
Chris@16
|
87 ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability]
|
Chris@16
|
88 - numeric::fdiv(n, count(args))
|
Chris@16
|
89 );
|
Chris@16
|
90 }
|
Chris@16
|
91 };
|
Chris@16
|
92
|
Chris@16
|
93 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
94 // non_coherent_tail_mean_impl
|
Chris@16
|
95 //
|
Chris@16
|
96 /**
|
Chris@16
|
97 @brief Estimation of the (non-coherent) tail mean based on order statistics (for both left and right tails)
|
Chris@16
|
98
|
Chris@16
|
99 An estimation of the non-coherent tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$ is given by the mean of the
|
Chris@16
|
100 \f$\lceil n\alpha\rceil\f$ smallest samples (left tail) or the mean of the \f$\lceil n(1-\alpha)\rceil\f$
|
Chris@16
|
101 largest samples (right tail), \f$n\f$ being the total number of samples and \f$\alpha\f$ the quantile level:
|
Chris@16
|
102
|
Chris@16
|
103 \f[
|
Chris@16
|
104 \widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X) = \frac{1}{\lceil n(1-\alpha)\rceil} \sum_{i=\lceil \alpha n \rceil}^n X_{i:n}
|
Chris@16
|
105 \f]
|
Chris@16
|
106
|
Chris@16
|
107 \f[
|
Chris@16
|
108 \widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X) = \frac{1}{\lceil n\alpha\rceil} \sum_{i=1}^{\lceil \alpha n \rceil} X_{i:n}
|
Chris@16
|
109 \f]
|
Chris@16
|
110
|
Chris@16
|
111 It thus requires the caching of at least the \f$\lceil n\alpha\rceil\f$ smallest or the \f$\lceil n(1-\alpha)\rceil\f$
|
Chris@16
|
112 largest samples.
|
Chris@16
|
113
|
Chris@16
|
114 @param quantile_probability
|
Chris@16
|
115 */
|
Chris@16
|
116 template<typename Sample, typename LeftRight>
|
Chris@16
|
117 struct non_coherent_tail_mean_impl
|
Chris@16
|
118 : accumulator_base
|
Chris@16
|
119 {
|
Chris@16
|
120 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
Chris@16
|
121 // for boost::result_of
|
Chris@16
|
122 typedef float_type result_type;
|
Chris@16
|
123
|
Chris@16
|
124 non_coherent_tail_mean_impl(dont_care) {}
|
Chris@16
|
125
|
Chris@16
|
126 template<typename Args>
|
Chris@16
|
127 result_type result(Args const &args) const
|
Chris@16
|
128 {
|
Chris@16
|
129 std::size_t cnt = count(args);
|
Chris@16
|
130
|
Chris@16
|
131 std::size_t n = static_cast<std::size_t>(
|
Chris@16
|
132 std::ceil(
|
Chris@16
|
133 cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
|
Chris@16
|
134 )
|
Chris@16
|
135 );
|
Chris@16
|
136
|
Chris@16
|
137 // If n is in a valid range, return result, otherwise return NaN or throw exception
|
Chris@16
|
138 if (n <= static_cast<std::size_t>(tail(args).size()))
|
Chris@16
|
139 return numeric::fdiv(
|
Chris@16
|
140 std::accumulate(
|
Chris@16
|
141 tail(args).begin()
|
Chris@16
|
142 , tail(args).begin() + n
|
Chris@16
|
143 , Sample(0)
|
Chris@16
|
144 )
|
Chris@16
|
145 , n
|
Chris@16
|
146 );
|
Chris@16
|
147 else
|
Chris@16
|
148 {
|
Chris@16
|
149 if (std::numeric_limits<result_type>::has_quiet_NaN)
|
Chris@16
|
150 {
|
Chris@16
|
151 return std::numeric_limits<result_type>::quiet_NaN();
|
Chris@16
|
152 }
|
Chris@16
|
153 else
|
Chris@16
|
154 {
|
Chris@16
|
155 std::ostringstream msg;
|
Chris@16
|
156 msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
|
Chris@16
|
157 boost::throw_exception(std::runtime_error(msg.str()));
|
Chris@16
|
158 return Sample(0);
|
Chris@16
|
159 }
|
Chris@16
|
160 }
|
Chris@16
|
161 }
|
Chris@16
|
162 };
|
Chris@16
|
163
|
Chris@16
|
164 } // namespace impl
|
Chris@16
|
165
|
Chris@16
|
166
|
Chris@16
|
167 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
168 // tag::coherent_tail_mean<>
|
Chris@16
|
169 // tag::non_coherent_tail_mean<>
|
Chris@16
|
170 //
|
Chris@16
|
171 namespace tag
|
Chris@16
|
172 {
|
Chris@16
|
173 template<typename LeftRight>
|
Chris@16
|
174 struct coherent_tail_mean
|
Chris@16
|
175 : depends_on<count, quantile, non_coherent_tail_mean<LeftRight> >
|
Chris@16
|
176 {
|
Chris@16
|
177 typedef accumulators::impl::coherent_tail_mean_impl<mpl::_1, LeftRight> impl;
|
Chris@16
|
178 };
|
Chris@16
|
179
|
Chris@16
|
180 template<typename LeftRight>
|
Chris@16
|
181 struct non_coherent_tail_mean
|
Chris@16
|
182 : depends_on<count, tail<LeftRight> >
|
Chris@16
|
183 {
|
Chris@16
|
184 typedef accumulators::impl::non_coherent_tail_mean_impl<mpl::_1, LeftRight> impl;
|
Chris@16
|
185 };
|
Chris@16
|
186
|
Chris@16
|
187 struct abstract_non_coherent_tail_mean
|
Chris@16
|
188 : depends_on<>
|
Chris@16
|
189 {
|
Chris@16
|
190 };
|
Chris@16
|
191 }
|
Chris@16
|
192
|
Chris@16
|
193 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
194 // extract::non_coherent_tail_mean;
|
Chris@16
|
195 // extract::coherent_tail_mean;
|
Chris@16
|
196 //
|
Chris@16
|
197 namespace extract
|
Chris@16
|
198 {
|
Chris@16
|
199 extractor<tag::abstract_non_coherent_tail_mean> const non_coherent_tail_mean = {};
|
Chris@16
|
200 extractor<tag::tail_mean> const coherent_tail_mean = {};
|
Chris@16
|
201
|
Chris@16
|
202 BOOST_ACCUMULATORS_IGNORE_GLOBAL(non_coherent_tail_mean)
|
Chris@16
|
203 BOOST_ACCUMULATORS_IGNORE_GLOBAL(coherent_tail_mean)
|
Chris@16
|
204 }
|
Chris@16
|
205
|
Chris@16
|
206 using extract::non_coherent_tail_mean;
|
Chris@16
|
207 using extract::coherent_tail_mean;
|
Chris@16
|
208
|
Chris@16
|
209 // for the purposes of feature-based dependency resolution,
|
Chris@16
|
210 // coherent_tail_mean<LeftRight> provides the same feature as tail_mean
|
Chris@16
|
211 template<typename LeftRight>
|
Chris@16
|
212 struct feature_of<tag::coherent_tail_mean<LeftRight> >
|
Chris@16
|
213 : feature_of<tag::tail_mean>
|
Chris@16
|
214 {
|
Chris@16
|
215 };
|
Chris@16
|
216
|
Chris@16
|
217 template<typename LeftRight>
|
Chris@16
|
218 struct feature_of<tag::non_coherent_tail_mean<LeftRight> >
|
Chris@16
|
219 : feature_of<tag::abstract_non_coherent_tail_mean>
|
Chris@16
|
220 {
|
Chris@16
|
221 };
|
Chris@16
|
222
|
Chris@16
|
223 // So that non_coherent_tail_mean can be automatically substituted
|
Chris@16
|
224 // with weighted_non_coherent_tail_mean when the weight parameter is non-void.
|
Chris@16
|
225 template<typename LeftRight>
|
Chris@16
|
226 struct as_weighted_feature<tag::non_coherent_tail_mean<LeftRight> >
|
Chris@16
|
227 {
|
Chris@16
|
228 typedef tag::non_coherent_weighted_tail_mean<LeftRight> type;
|
Chris@16
|
229 };
|
Chris@16
|
230
|
Chris@16
|
231 template<typename LeftRight>
|
Chris@16
|
232 struct feature_of<tag::non_coherent_weighted_tail_mean<LeftRight> >
|
Chris@16
|
233 : feature_of<tag::non_coherent_tail_mean<LeftRight> >
|
Chris@16
|
234 {};
|
Chris@16
|
235
|
Chris@16
|
236 // NOTE that non_coherent_tail_mean cannot be feature-grouped with tail_mean,
|
Chris@16
|
237 // which is the base feature for coherent tail means, since (at least for
|
Chris@16
|
238 // non-continuous distributions) non_coherent_tail_mean is a different measure!
|
Chris@16
|
239
|
Chris@16
|
240 }} // namespace boost::accumulators
|
Chris@16
|
241
|
Chris@16
|
242 #ifdef _MSC_VER
|
Chris@16
|
243 # pragma warning(pop)
|
Chris@16
|
244 #endif
|
Chris@16
|
245
|
Chris@16
|
246 #endif
|