]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/accumulators/statistics/tail_variate_means.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / accumulators / statistics / tail_variate_means.hpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // tail_variate_means.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_VARIATE_MEANS_HPP_DE_01_01_2006
9 #define BOOST_ACCUMULATORS_STATISTICS_TAIL_VARIATE_MEANS_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/tail.hpp>
27 #include <boost/accumulators/statistics/tail_variate.hpp>
28 #include <boost/accumulators/statistics/tail_mean.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
36 namespace boost { namespace accumulators
37 {
38
39 namespace impl
40 {
41 /**
42 @brief Estimation of the absolute and relative tail variate means (for both left and right tails)
43
44 For all \f$j\f$-th variates associated to the \f$\lceil n(1-\alpha)\rceil\f$ largest samples (or the
45 \f$\lceil n(1-\alpha)\rceil\f$ smallest samples in case of the left tail), the absolute tail means
46 \f$\widehat{ATM}_{n,\alpha}(X, j)\f$ are computed and returned as an iterator range. Alternatively,
47 the relative tail means \f$\widehat{RTM}_{n,\alpha}(X, j)\f$ are returned, which are the absolute
48 tail means normalized with the (non-coherent) sample tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$.
49
50 \f[
51 \widehat{ATM}_{n,\alpha}^{\mathrm{right}}(X, j) =
52 \frac{1}{\lceil n(1-\alpha) \rceil}
53 \sum_{i=\lceil \alpha n \rceil}^n \xi_{j,i}
54 \f]
55
56 \f[
57 \widehat{ATM}_{n,\alpha}^{\mathrm{left}}(X, j) =
58 \frac{1}{\lceil n\alpha \rceil}
59 \sum_{i=1}^{\lceil n\alpha \rceil} \xi_{j,i}
60 \f]
61
62 \f[
63 \widehat{RTM}_{n,\alpha}^{\mathrm{right}}(X, j) =
64 \frac{\sum_{i=\lceil n\alpha \rceil}^n \xi_{j,i}}
65 {\lceil n(1-\alpha)\rceil\widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X)}
66 \f]
67
68 \f[
69 \widehat{RTM}_{n,\alpha}^{\mathrm{left}}(X, j) =
70 \frac{\sum_{i=1}^{\lceil n\alpha \rceil} \xi_{j,i}}
71 {\lceil n\alpha\rceil\widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X)}
72 \f]
73 */
74
75 ///////////////////////////////////////////////////////////////////////////////
76 // tail_variate_means_impl
77 // by default: absolute tail_variate_means
78 template<typename Sample, typename Impl, typename LeftRight, typename VariateTag>
79 struct tail_variate_means_impl
80 : accumulator_base
81 {
82 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
83 typedef std::vector<float_type> array_type;
84 // for boost::result_of
85 typedef iterator_range<typename array_type::iterator> result_type;
86
87 tail_variate_means_impl(dont_care) {}
88
89 template<typename Args>
90 result_type result(Args const &args) const
91 {
92 std::size_t cnt = count(args);
93
94 std::size_t n = static_cast<std::size_t>(
95 std::ceil(
96 cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
97 )
98 );
99
100 std::size_t num_variates = tail_variate(args).begin()->size();
101
102 this->tail_means_.clear();
103 this->tail_means_.resize(num_variates, Sample(0));
104
105 // If n is in a valid range, return result, otherwise return NaN or throw exception
106 if (n < static_cast<std::size_t>(tail(args).size()))
107 {
108 this->tail_means_ = std::accumulate(
109 tail_variate(args).begin()
110 , tail_variate(args).begin() + n
111 , this->tail_means_
112 , numeric::plus
113 );
114
115 float_type factor = n * ( (is_same<Impl, relative>::value) ? non_coherent_tail_mean(args) : 1. );
116
117 std::transform(
118 this->tail_means_.begin()
119 , this->tail_means_.end()
120 , this->tail_means_.begin()
121 #ifdef BOOST_NO_CXX98_BINDERS
122 , std::bind(std::divides<float_type>(), std::placeholders::_1, factor)
123 #else
124 , std::bind2nd(std::divides<float_type>(), factor)
125 #endif
126 );
127 }
128 else
129 {
130 if (std::numeric_limits<float_type>::has_quiet_NaN)
131 {
132 std::fill(
133 this->tail_means_.begin()
134 , this->tail_means_.end()
135 , std::numeric_limits<float_type>::quiet_NaN()
136 );
137 }
138 else
139 {
140 std::ostringstream msg;
141 msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
142 boost::throw_exception(std::runtime_error(msg.str()));
143 }
144 }
145 return make_iterator_range(this->tail_means_);
146 }
147
148 private:
149
150 mutable array_type tail_means_;
151
152 };
153
154 } // namespace impl
155
156 ///////////////////////////////////////////////////////////////////////////////
157 // tag::absolute_tail_variate_means
158 // tag::relative_tail_variate_means
159 //
160 namespace tag
161 {
162 template<typename LeftRight, typename VariateType, typename VariateTag>
163 struct absolute_tail_variate_means
164 : depends_on<count, non_coherent_tail_mean<LeftRight>, tail_variate<VariateType, VariateTag, LeftRight> >
165 {
166 typedef accumulators::impl::tail_variate_means_impl<mpl::_1, absolute, LeftRight, VariateTag> impl;
167 };
168 template<typename LeftRight, typename VariateType, typename VariateTag>
169 struct relative_tail_variate_means
170 : depends_on<count, non_coherent_tail_mean<LeftRight>, tail_variate<VariateType, VariateTag, LeftRight> >
171 {
172 typedef accumulators::impl::tail_variate_means_impl<mpl::_1, relative, LeftRight, VariateTag> impl;
173 };
174 struct abstract_absolute_tail_variate_means
175 : depends_on<>
176 {
177 };
178 struct abstract_relative_tail_variate_means
179 : depends_on<>
180 {
181 };
182 }
183
184 ///////////////////////////////////////////////////////////////////////////////
185 // extract::tail_variate_means
186 // extract::relative_tail_variate_means
187 //
188 namespace extract
189 {
190 extractor<tag::abstract_absolute_tail_variate_means> const tail_variate_means = {};
191 extractor<tag::abstract_relative_tail_variate_means> const relative_tail_variate_means = {};
192
193 BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_variate_means)
194 BOOST_ACCUMULATORS_IGNORE_GLOBAL(relative_tail_variate_means)
195 }
196
197 using extract::tail_variate_means;
198 using extract::relative_tail_variate_means;
199
200 // tail_variate_means<LeftRight, VariateType, VariateTag>(absolute) -> absolute_tail_variate_means<LeftRight, VariateType, VariateTag>
201 template<typename LeftRight, typename VariateType, typename VariateTag>
202 struct as_feature<tag::tail_variate_means<LeftRight, VariateType, VariateTag>(absolute)>
203 {
204 typedef tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> type;
205 };
206
207 // tail_variate_means<LeftRight, VariateType, VariateTag>(relative) ->relative_tail_variate_means<LeftRight, VariateType, VariateTag>
208 template<typename LeftRight, typename VariateType, typename VariateTag>
209 struct as_feature<tag::tail_variate_means<LeftRight, VariateType, VariateTag>(relative)>
210 {
211 typedef tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> type;
212 };
213
214 // Provides non-templatized extractor
215 template<typename LeftRight, typename VariateType, typename VariateTag>
216 struct feature_of<tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> >
217 : feature_of<tag::abstract_absolute_tail_variate_means>
218 {
219 };
220
221 // Provides non-templatized extractor
222 template<typename LeftRight, typename VariateType, typename VariateTag>
223 struct feature_of<tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> >
224 : feature_of<tag::abstract_relative_tail_variate_means>
225 {
226 };
227
228 // So that absolute_tail_means can be automatically substituted
229 // with absolute_weighted_tail_means when the weight parameter is non-void.
230 template<typename LeftRight, typename VariateType, typename VariateTag>
231 struct as_weighted_feature<tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> >
232 {
233 typedef tag::absolute_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> type;
234 };
235
236 template<typename LeftRight, typename VariateType, typename VariateTag>
237 struct feature_of<tag::absolute_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> >
238 : feature_of<tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> >
239 {
240 };
241
242 // So that relative_tail_means can be automatically substituted
243 // with relative_weighted_tail_means when the weight parameter is non-void.
244 template<typename LeftRight, typename VariateType, typename VariateTag>
245 struct as_weighted_feature<tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> >
246 {
247 typedef tag::relative_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> type;
248 };
249
250 template<typename LeftRight, typename VariateType, typename VariateTag>
251 struct feature_of<tag::relative_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> >
252 : feature_of<tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> >
253 {
254 };
255
256 }} // namespace boost::accumulators
257
258 #ifdef _MSC_VER
259 # pragma warning(pop)
260 #endif
261
262 #endif