1 ///////////////////////////////////////////////////////////////////////////////
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_POT_TAIL_MEAN_HPP_DE_01_01_2006
9 #define BOOST_ACCUMULATORS_STATISTICS_POT_TAIL_MEAN_HPP_DE_01_01_2006
15 #include <boost/range.hpp>
16 #include <boost/parameter/keyword.hpp>
17 #include <boost/tuple/tuple.hpp>
18 #include <boost/mpl/if.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/peaks_over_threshold.hpp>
27 #include <boost/accumulators/statistics/weighted_peaks_over_threshold.hpp>
28 #include <boost/accumulators/statistics/pot_quantile.hpp>
29 #include <boost/accumulators/statistics/tail_mean.hpp>
31 namespace boost { namespace accumulators
36 ///////////////////////////////////////////////////////////////////////////////
40 @brief Estimation of the (coherent) tail mean based on the peaks over threshold method (for both left and right tails)
42 Computes an estimate for the (coherent) tail mean
44 \widehat{CTM}_{\alpha} = \hat{q}_{\alpha} - \frac{\bar{\beta}}{\xi-1}(1-\alpha)^{-\xi},
46 where \f$\bar[u]\f$, \f$\bar{\beta}\f$ and \f$\xi\f$ are the parameters of the
47 generalized Pareto distribution that approximates the right tail of the distribution (or the
48 mirrored left tail, in case the left tail is used). In the latter case, the result is mirrored
49 back, yielding the correct result.
51 template<typename Sample, typename Impl, typename LeftRight>
52 struct pot_tail_mean_impl
55 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
56 // for boost::result_of
57 typedef float_type result_type;
59 pot_tail_mean_impl(dont_care)
60 : sign_((is_same<LeftRight, left>::value) ? -1 : 1)
64 template<typename Args>
65 result_type result(Args const &args) const
69 is_same<Impl, weighted>
70 , tag::weighted_peaks_over_threshold<LeftRight>
71 , tag::peaks_over_threshold<LeftRight>
73 peaks_over_threshold_tag;
77 is_same<Impl, weighted>
78 , tag::weighted_pot_quantile<LeftRight>
79 , tag::pot_quantile<LeftRight>
83 extractor<peaks_over_threshold_tag> const some_peaks_over_threshold = {};
84 extractor<pot_quantile_tag> const some_pot_quantile = {};
86 float_type beta_bar = some_peaks_over_threshold(args).template get<1>();
87 float_type xi_hat = some_peaks_over_threshold(args).template get<2>();
89 return some_pot_quantile(args) - this->sign_ * beta_bar/( xi_hat - 1. ) * std::pow(
90 is_same<LeftRight, left>::value ? args[quantile_probability] : 1. - args[quantile_probability]
94 short sign_; // if the fit parameters from the mirrored left tail extreme values are used, mirror back the result
98 ///////////////////////////////////////////////////////////////////////////////
100 // tag::pot_tail_mean_prob
104 template<typename LeftRight>
106 : depends_on<peaks_over_threshold<LeftRight>, pot_quantile<LeftRight> >
110 typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, unweighted, LeftRight> impl;
112 template<typename LeftRight>
113 struct pot_tail_mean_prob
114 : depends_on<peaks_over_threshold_prob<LeftRight>, pot_quantile_prob<LeftRight> >
118 typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, unweighted, LeftRight> impl;
120 template<typename LeftRight>
121 struct weighted_pot_tail_mean
122 : depends_on<weighted_peaks_over_threshold<LeftRight>, weighted_pot_quantile<LeftRight> >
126 typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, weighted, LeftRight> impl;
128 template<typename LeftRight>
129 struct weighted_pot_tail_mean_prob
130 : depends_on<weighted_peaks_over_threshold_prob<LeftRight>, weighted_pot_quantile_prob<LeftRight> >
134 typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, weighted, LeftRight> impl;
138 // pot_tail_mean<LeftRight>(with_threshold_value) -> pot_tail_mean<LeftRight>
139 template<typename LeftRight>
140 struct as_feature<tag::pot_tail_mean<LeftRight>(with_threshold_value)>
142 typedef tag::pot_tail_mean<LeftRight> type;
145 // pot_tail_mean<LeftRight>(with_threshold_probability) -> pot_tail_mean_prob<LeftRight>
146 template<typename LeftRight>
147 struct as_feature<tag::pot_tail_mean<LeftRight>(with_threshold_probability)>
149 typedef tag::pot_tail_mean_prob<LeftRight> type;
152 // weighted_pot_tail_mean<LeftRight>(with_threshold_value) -> weighted_pot_tail_mean<LeftRight>
153 template<typename LeftRight>
154 struct as_feature<tag::weighted_pot_tail_mean<LeftRight>(with_threshold_value)>
156 typedef tag::weighted_pot_tail_mean<LeftRight> type;
159 // weighted_pot_tail_mean<LeftRight>(with_threshold_probability) -> weighted_pot_tail_mean_prob<LeftRight>
160 template<typename LeftRight>
161 struct as_feature<tag::weighted_pot_tail_mean<LeftRight>(with_threshold_probability)>
163 typedef tag::weighted_pot_tail_mean_prob<LeftRight> type;
166 // for the purposes of feature-based dependency resolution,
167 // pot_tail_mean<LeftRight> and pot_tail_mean_prob<LeftRight> provide
168 // the same feature as tail_mean
169 template<typename LeftRight>
170 struct feature_of<tag::pot_tail_mean<LeftRight> >
171 : feature_of<tag::tail_mean>
175 template<typename LeftRight>
176 struct feature_of<tag::pot_tail_mean_prob<LeftRight> >
177 : feature_of<tag::tail_mean>
181 // So that pot_tail_mean can be automatically substituted
182 // with weighted_pot_tail_mean when the weight parameter is non-void.
183 template<typename LeftRight>
184 struct as_weighted_feature<tag::pot_tail_mean<LeftRight> >
186 typedef tag::weighted_pot_tail_mean<LeftRight> type;
189 template<typename LeftRight>
190 struct feature_of<tag::weighted_pot_tail_mean<LeftRight> >
191 : feature_of<tag::pot_tail_mean<LeftRight> >
195 // So that pot_tail_mean_prob can be automatically substituted
196 // with weighted_pot_tail_mean_prob when the weight parameter is non-void.
197 template<typename LeftRight>
198 struct as_weighted_feature<tag::pot_tail_mean_prob<LeftRight> >
200 typedef tag::weighted_pot_tail_mean_prob<LeftRight> type;
203 template<typename LeftRight>
204 struct feature_of<tag::weighted_pot_tail_mean_prob<LeftRight> >
205 : feature_of<tag::pot_tail_mean_prob<LeftRight> >
209 }} // namespace boost::accumulators