]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // weighted_peaks_over_threshold.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_WEIGHTED_PEAKS_OVER_THRESHOLD_HPP_DE_01_01_2006 | |
9 | #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_PEAKS_OVER_THRESHOLD_HPP_DE_01_01_2006 | |
10 | ||
11 | #include <vector> | |
12 | #include <limits> | |
13 | #include <numeric> | |
14 | #include <functional> | |
15 | #include <boost/throw_exception.hpp> | |
16 | #include <boost/range.hpp> | |
17 | #include <boost/mpl/if.hpp> | |
18 | #include <boost/mpl/placeholders.hpp> | |
19 | #include <boost/parameter/keyword.hpp> | |
20 | #include <boost/tuple/tuple.hpp> | |
21 | #include <boost/accumulators/numeric/functional.hpp> | |
22 | #include <boost/accumulators/framework/accumulator_base.hpp> | |
23 | #include <boost/accumulators/framework/extractor.hpp> | |
24 | #include <boost/accumulators/framework/parameters/sample.hpp> | |
25 | #include <boost/accumulators/framework/depends_on.hpp> | |
26 | #include <boost/accumulators/statistics_fwd.hpp> | |
27 | #include <boost/accumulators/statistics/parameters/quantile_probability.hpp> | |
28 | #include <boost/accumulators/statistics/peaks_over_threshold.hpp> // for named parameters pot_threshold_value and pot_threshold_probability | |
29 | #include <boost/accumulators/statistics/sum.hpp> | |
30 | #include <boost/accumulators/statistics/tail_variate.hpp> | |
31 | ||
32 | #ifdef _MSC_VER | |
33 | # pragma warning(push) | |
34 | # pragma warning(disable: 4127) // conditional expression is constant | |
35 | #endif | |
36 | ||
37 | namespace boost { namespace accumulators | |
38 | { | |
39 | ||
40 | namespace impl | |
41 | { | |
42 | ||
43 | /////////////////////////////////////////////////////////////////////////////// | |
44 | // weighted_peaks_over_threshold_impl | |
45 | // works with an explicit threshold value and does not depend on order statistics of weighted samples | |
46 | /** | |
47 | @brief Weighted Peaks over Threshold Method for Weighted Quantile and Weighted Tail Mean Estimation | |
48 | ||
49 | @sa peaks_over_threshold_impl | |
50 | ||
51 | @param quantile_probability | |
52 | @param pot_threshold_value | |
53 | */ | |
54 | template<typename Sample, typename Weight, typename LeftRight> | |
55 | struct weighted_peaks_over_threshold_impl | |
56 | : accumulator_base | |
57 | { | |
58 | typedef typename numeric::functional::multiplies<Weight, Sample>::result_type weighted_sample; | |
59 | typedef typename numeric::functional::fdiv<weighted_sample, std::size_t>::result_type float_type; | |
60 | // for boost::result_of | |
61 | typedef boost::tuple<float_type, float_type, float_type> result_type; | |
62 | ||
63 | template<typename Args> | |
64 | weighted_peaks_over_threshold_impl(Args const &args) | |
65 | : sign_((is_same<LeftRight, left>::value) ? -1 : 1) | |
66 | , mu_(sign_ * numeric::fdiv(args[sample | Sample()], (std::size_t)1)) | |
67 | , sigma2_(numeric::fdiv(args[sample | Sample()], (std::size_t)1)) | |
68 | , w_sum_(numeric::fdiv(args[weight | Weight()], (std::size_t)1)) | |
69 | , threshold_(sign_ * args[pot_threshold_value]) | |
70 | , fit_parameters_(boost::make_tuple(0., 0., 0.)) | |
71 | , is_dirty_(true) | |
72 | { | |
73 | } | |
74 | ||
75 | template<typename Args> | |
76 | void operator ()(Args const &args) | |
77 | { | |
78 | this->is_dirty_ = true; | |
79 | ||
80 | if (this->sign_ * args[sample] > this->threshold_) | |
81 | { | |
82 | this->mu_ += args[weight] * args[sample]; | |
83 | this->sigma2_ += args[weight] * args[sample] * args[sample]; | |
84 | this->w_sum_ += args[weight]; | |
85 | } | |
86 | } | |
87 | ||
88 | template<typename Args> | |
89 | result_type result(Args const &args) const | |
90 | { | |
91 | if (this->is_dirty_) | |
92 | { | |
93 | this->is_dirty_ = false; | |
94 | ||
95 | this->mu_ = this->sign_ * numeric::fdiv(this->mu_, this->w_sum_); | |
96 | this->sigma2_ = numeric::fdiv(this->sigma2_, this->w_sum_); | |
97 | this->sigma2_ -= this->mu_ * this->mu_; | |
98 | ||
99 | float_type threshold_probability = numeric::fdiv(sum_of_weights(args) - this->w_sum_, sum_of_weights(args)); | |
100 | ||
101 | float_type tmp = numeric::fdiv(( this->mu_ - this->threshold_ )*( this->mu_ - this->threshold_ ), this->sigma2_); | |
102 | float_type xi_hat = 0.5 * ( 1. - tmp ); | |
103 | float_type beta_hat = 0.5 * ( this->mu_ - this->threshold_ ) * ( 1. + tmp ); | |
104 | float_type beta_bar = beta_hat * std::pow(1. - threshold_probability, xi_hat); | |
105 | float_type u_bar = this->threshold_ - beta_bar * ( std::pow(1. - threshold_probability, -xi_hat) - 1.)/xi_hat; | |
106 | this->fit_parameters_ = boost::make_tuple(u_bar, beta_bar, xi_hat); | |
107 | } | |
108 | ||
109 | return this->fit_parameters_; | |
110 | } | |
111 | ||
112 | private: | |
113 | short sign_; // for left tail fitting, mirror the extreme values | |
114 | mutable float_type mu_; // mean of samples above threshold | |
115 | mutable float_type sigma2_; // variance of samples above threshold | |
116 | mutable float_type w_sum_; // sum of weights of samples above threshold | |
117 | float_type threshold_; | |
118 | mutable result_type fit_parameters_; // boost::tuple that stores fit parameters | |
119 | mutable bool is_dirty_; | |
120 | }; | |
121 | ||
122 | /////////////////////////////////////////////////////////////////////////////// | |
123 | // weighted_peaks_over_threshold_prob_impl | |
124 | // determines threshold from a given threshold probability using order statistics | |
125 | /** | |
126 | @brief Peaks over Threshold Method for Quantile and Tail Mean Estimation | |
127 | ||
128 | @sa weighted_peaks_over_threshold_impl | |
129 | ||
130 | @param quantile_probability | |
131 | @param pot_threshold_probability | |
132 | */ | |
133 | template<typename Sample, typename Weight, typename LeftRight> | |
134 | struct weighted_peaks_over_threshold_prob_impl | |
135 | : accumulator_base | |
136 | { | |
137 | typedef typename numeric::functional::multiplies<Weight, Sample>::result_type weighted_sample; | |
138 | typedef typename numeric::functional::fdiv<weighted_sample, std::size_t>::result_type float_type; | |
139 | // for boost::result_of | |
140 | typedef boost::tuple<float_type, float_type, float_type> result_type; | |
141 | ||
142 | template<typename Args> | |
143 | weighted_peaks_over_threshold_prob_impl(Args const &args) | |
144 | : sign_((is_same<LeftRight, left>::value) ? -1 : 1) | |
145 | , mu_(sign_ * numeric::fdiv(args[sample | Sample()], (std::size_t)1)) | |
146 | , sigma2_(numeric::fdiv(args[sample | Sample()], (std::size_t)1)) | |
147 | , threshold_probability_(args[pot_threshold_probability]) | |
148 | , fit_parameters_(boost::make_tuple(0., 0., 0.)) | |
149 | , is_dirty_(true) | |
150 | { | |
151 | } | |
152 | ||
153 | void operator ()(dont_care) | |
154 | { | |
155 | this->is_dirty_ = true; | |
156 | } | |
157 | ||
158 | template<typename Args> | |
159 | result_type result(Args const &args) const | |
160 | { | |
161 | if (this->is_dirty_) | |
162 | { | |
163 | this->is_dirty_ = false; | |
164 | ||
165 | float_type threshold = sum_of_weights(args) | |
166 | * ( ( is_same<LeftRight, left>::value ) ? this->threshold_probability_ : 1. - this->threshold_probability_ ); | |
167 | ||
168 | std::size_t n = 0; | |
169 | Weight sum = Weight(0); | |
170 | ||
171 | while (sum < threshold) | |
172 | { | |
173 | if (n < static_cast<std::size_t>(tail_weights(args).size())) | |
174 | { | |
175 | mu_ += *(tail_weights(args).begin() + n) * *(tail(args).begin() + n); | |
176 | sigma2_ += *(tail_weights(args).begin() + n) * *(tail(args).begin() + n) * (*(tail(args).begin() + n)); | |
177 | sum += *(tail_weights(args).begin() + n); | |
178 | n++; | |
179 | } | |
180 | else | |
181 | { | |
182 | if (std::numeric_limits<float_type>::has_quiet_NaN) | |
183 | { | |
184 | return boost::make_tuple( | |
185 | std::numeric_limits<float_type>::quiet_NaN() | |
186 | , std::numeric_limits<float_type>::quiet_NaN() | |
187 | , std::numeric_limits<float_type>::quiet_NaN() | |
188 | ); | |
189 | } | |
190 | else | |
191 | { | |
192 | std::ostringstream msg; | |
193 | msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")"; | |
194 | boost::throw_exception(std::runtime_error(msg.str())); | |
195 | return boost::make_tuple(Sample(0), Sample(0), Sample(0)); | |
196 | } | |
197 | } | |
198 | } | |
199 | ||
200 | float_type u = *(tail(args).begin() + n - 1) * this->sign_; | |
201 | ||
202 | ||
203 | this->mu_ = this->sign_ * numeric::fdiv(this->mu_, sum); | |
204 | this->sigma2_ = numeric::fdiv(this->sigma2_, sum); | |
205 | this->sigma2_ -= this->mu_ * this->mu_; | |
206 | ||
207 | if (is_same<LeftRight, left>::value) | |
208 | this->threshold_probability_ = 1. - this->threshold_probability_; | |
209 | ||
210 | float_type tmp = numeric::fdiv(( this->mu_ - u )*( this->mu_ - u ), this->sigma2_); | |
211 | float_type xi_hat = 0.5 * ( 1. - tmp ); | |
212 | float_type beta_hat = 0.5 * ( this->mu_ - u ) * ( 1. + tmp ); | |
213 | float_type beta_bar = beta_hat * std::pow(1. - threshold_probability_, xi_hat); | |
214 | float_type u_bar = u - beta_bar * ( std::pow(1. - threshold_probability_, -xi_hat) - 1.)/xi_hat; | |
215 | this->fit_parameters_ = boost::make_tuple(u_bar, beta_bar, xi_hat); | |
216 | ||
217 | } | |
218 | ||
219 | return this->fit_parameters_; | |
220 | } | |
221 | ||
222 | private: | |
223 | short sign_; // for left tail fitting, mirror the extreme values | |
224 | mutable float_type mu_; // mean of samples above threshold u | |
225 | mutable float_type sigma2_; // variance of samples above threshold u | |
226 | mutable float_type threshold_probability_; | |
227 | mutable result_type fit_parameters_; // boost::tuple that stores fit parameters | |
228 | mutable bool is_dirty_; | |
229 | }; | |
230 | ||
231 | } // namespace impl | |
232 | ||
233 | /////////////////////////////////////////////////////////////////////////////// | |
234 | // tag::weighted_peaks_over_threshold | |
235 | // | |
236 | namespace tag | |
237 | { | |
238 | template<typename LeftRight> | |
239 | struct weighted_peaks_over_threshold | |
240 | : depends_on<sum_of_weights> | |
241 | , pot_threshold_value | |
242 | { | |
243 | /// INTERNAL ONLY | |
244 | typedef accumulators::impl::weighted_peaks_over_threshold_impl<mpl::_1, mpl::_2, LeftRight> impl; | |
245 | }; | |
246 | ||
247 | template<typename LeftRight> | |
248 | struct weighted_peaks_over_threshold_prob | |
249 | : depends_on<sum_of_weights, tail_weights<LeftRight> > | |
250 | , pot_threshold_probability | |
251 | { | |
252 | /// INTERNAL ONLY | |
253 | typedef accumulators::impl::weighted_peaks_over_threshold_prob_impl<mpl::_1, mpl::_2, LeftRight> impl; | |
254 | }; | |
255 | } | |
256 | ||
257 | /////////////////////////////////////////////////////////////////////////////// | |
258 | // extract::weighted_peaks_over_threshold | |
259 | // | |
260 | namespace extract | |
261 | { | |
262 | extractor<tag::abstract_peaks_over_threshold> const weighted_peaks_over_threshold = {}; | |
263 | ||
264 | BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_peaks_over_threshold) | |
265 | } | |
266 | ||
267 | using extract::weighted_peaks_over_threshold; | |
268 | ||
269 | // weighted_peaks_over_threshold<LeftRight>(with_threshold_value) -> weighted_peaks_over_threshold<LeftRight> | |
270 | template<typename LeftRight> | |
271 | struct as_feature<tag::weighted_peaks_over_threshold<LeftRight>(with_threshold_value)> | |
272 | { | |
273 | typedef tag::weighted_peaks_over_threshold<LeftRight> type; | |
274 | }; | |
275 | ||
276 | // weighted_peaks_over_threshold<LeftRight>(with_threshold_probability) -> weighted_peaks_over_threshold_prob<LeftRight> | |
277 | template<typename LeftRight> | |
278 | struct as_feature<tag::weighted_peaks_over_threshold<LeftRight>(with_threshold_probability)> | |
279 | { | |
280 | typedef tag::weighted_peaks_over_threshold_prob<LeftRight> type; | |
281 | }; | |
282 | ||
283 | }} // namespace boost::accumulators | |
284 | ||
285 | #ifdef _MSC_VER | |
286 | # pragma warning(pop) | |
287 | #endif | |
288 | ||
289 | #endif |