1 ///////////////////////////////////////////////////////////////////////////////
2 // weighted_peaks_over_threshold.hpp
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)
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
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>
33 # pragma warning(push)
34 # pragma warning(disable: 4127) // conditional expression is constant
37 namespace boost { namespace accumulators
43 ///////////////////////////////////////////////////////////////////////////////
44 // weighted_peaks_over_threshold_impl
45 // works with an explicit threshold value and does not depend on order statistics of weighted samples
47 @brief Weighted Peaks over Threshold Method for Weighted Quantile and Weighted Tail Mean Estimation
49 @sa peaks_over_threshold_impl
51 @param quantile_probability
52 @param pot_threshold_value
54 template<typename Sample, typename Weight, typename LeftRight>
55 struct weighted_peaks_over_threshold_impl
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;
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.))
75 template<typename Args>
76 void operator ()(Args const &args)
78 this->is_dirty_ = true;
80 if (this->sign_ * args[sample] > this->threshold_)
82 this->mu_ += args[weight] * args[sample];
83 this->sigma2_ += args[weight] * args[sample] * args[sample];
84 this->w_sum_ += args[weight];
88 template<typename Args>
89 result_type result(Args const &args) const
93 this->is_dirty_ = false;
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_;
99 float_type threshold_probability = numeric::fdiv(sum_of_weights(args) - this->w_sum_, sum_of_weights(args));
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);
109 return this->fit_parameters_;
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_;
122 ///////////////////////////////////////////////////////////////////////////////
123 // weighted_peaks_over_threshold_prob_impl
124 // determines threshold from a given threshold probability using order statistics
126 @brief Peaks over Threshold Method for Quantile and Tail Mean Estimation
128 @sa weighted_peaks_over_threshold_impl
130 @param quantile_probability
131 @param pot_threshold_probability
133 template<typename Sample, typename Weight, typename LeftRight>
134 struct weighted_peaks_over_threshold_prob_impl
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;
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.))
153 void operator ()(dont_care)
155 this->is_dirty_ = true;
158 template<typename Args>
159 result_type result(Args const &args) const
163 this->is_dirty_ = false;
165 float_type threshold = sum_of_weights(args)
166 * ( ( is_same<LeftRight, left>::value ) ? this->threshold_probability_ : 1. - this->threshold_probability_ );
169 Weight sum = Weight(0);
171 while (sum < threshold)
173 if (n < static_cast<std::size_t>(tail_weights(args).size()))
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);
182 if (std::numeric_limits<float_type>::has_quiet_NaN)
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()
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));
200 float_type u = *(tail(args).begin() + n - 1) * this->sign_;
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_;
207 if (is_same<LeftRight, left>::value)
208 this->threshold_probability_ = 1. - this->threshold_probability_;
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);
219 return this->fit_parameters_;
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_;
233 ///////////////////////////////////////////////////////////////////////////////
234 // tag::weighted_peaks_over_threshold
238 template<typename LeftRight>
239 struct weighted_peaks_over_threshold
240 : depends_on<sum_of_weights>
241 , pot_threshold_value
244 typedef accumulators::impl::weighted_peaks_over_threshold_impl<mpl::_1, mpl::_2, LeftRight> impl;
247 template<typename LeftRight>
248 struct weighted_peaks_over_threshold_prob
249 : depends_on<sum_of_weights, tail_weights<LeftRight> >
250 , pot_threshold_probability
253 typedef accumulators::impl::weighted_peaks_over_threshold_prob_impl<mpl::_1, mpl::_2, LeftRight> impl;
257 ///////////////////////////////////////////////////////////////////////////////
258 // extract::weighted_peaks_over_threshold
262 extractor<tag::abstract_peaks_over_threshold> const weighted_peaks_over_threshold = {};
264 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_peaks_over_threshold)
267 using extract::weighted_peaks_over_threshold;
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)>
273 typedef tag::weighted_peaks_over_threshold<LeftRight> type;
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)>
280 typedef tag::weighted_peaks_over_threshold_prob<LeftRight> type;
283 }} // namespace boost::accumulators
286 # pragma warning(pop)