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