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_QUANTILE_HPP_DE_01_01_2006
9 #define BOOST_ACCUMULATORS_STATISTICS_POT_QUANTILE_HPP_DE_01_01_2006
15 #include <boost/parameter/keyword.hpp>
16 #include <boost/tuple/tuple.hpp>
17 #include <boost/mpl/if.hpp>
18 #include <boost/type_traits/is_same.hpp>
19 #include <boost/mpl/placeholders.hpp>
20 #include <boost/accumulators/framework/accumulator_base.hpp>
21 #include <boost/accumulators/framework/extractor.hpp>
22 #include <boost/accumulators/numeric/functional.hpp>
23 #include <boost/accumulators/framework/parameters/sample.hpp>
24 #include <boost/accumulators/statistics_fwd.hpp>
25 #include <boost/accumulators/statistics/tail.hpp>
26 #include <boost/accumulators/statistics/peaks_over_threshold.hpp>
27 #include <boost/accumulators/statistics/weighted_peaks_over_threshold.hpp>
29 namespace boost { namespace accumulators
34 ///////////////////////////////////////////////////////////////////////////////
38 @brief Quantile Estimation based on Peaks over Threshold Method (for both left and right tails)
42 \hat{q}_{\alpha} = \bar{u} + \frac{\bar{\beta}}{\xi}\left[(1-\alpha)^{-\xi}-1\right]
44 for a right or left extreme quantile, \f$\bar[u]\f$, \f$\bar{\beta}\f$ and \f$\xi\f$ being the parameters of the
45 generalized Pareto distribution that approximates the right tail of the distribution (or the mirrored left tail,
46 in case the left tail is used). In the latter case, the result is mirrored back, yielding the correct result.
48 template<typename Sample, typename Impl, typename LeftRight>
49 struct pot_quantile_impl
52 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
53 // for boost::result_of
54 typedef float_type result_type;
56 pot_quantile_impl(dont_care)
57 : sign_((is_same<LeftRight, left>::value) ? -1 : 1)
61 template<typename Args>
62 result_type result(Args const &args) const
66 is_same<Impl, weighted>
67 , tag::weighted_peaks_over_threshold<LeftRight>
68 , tag::peaks_over_threshold<LeftRight>
70 peaks_over_threshold_tag;
72 extractor<peaks_over_threshold_tag> const some_peaks_over_threshold = {};
74 float_type u_bar = some_peaks_over_threshold(args).template get<0>();
75 float_type beta_bar = some_peaks_over_threshold(args).template get<1>();
76 float_type xi_hat = some_peaks_over_threshold(args).template get<2>();
78 return this->sign_ * (u_bar + beta_bar/xi_hat * ( std::pow(
79 is_same<LeftRight, left>::value ? args[quantile_probability] : 1. - args[quantile_probability]
85 short sign_; // if the fit parameters from the mirrored left tail extreme values are used, mirror back the result
90 ///////////////////////////////////////////////////////////////////////////////
91 // tag::pot_quantile<>
92 // tag::pot_quantile_prob<>
93 // tag::weighted_pot_quantile<>
94 // tag::weighted_pot_quantile_prob<>
98 template<typename LeftRight>
100 : depends_on<peaks_over_threshold<LeftRight> >
104 typedef accumulators::impl::pot_quantile_impl<mpl::_1, unweighted, LeftRight> impl;
106 template<typename LeftRight>
107 struct pot_quantile_prob
108 : depends_on<peaks_over_threshold_prob<LeftRight> >
112 typedef accumulators::impl::pot_quantile_impl<mpl::_1, unweighted, LeftRight> impl;
114 template<typename LeftRight>
115 struct weighted_pot_quantile
116 : depends_on<weighted_peaks_over_threshold<LeftRight> >
120 typedef accumulators::impl::pot_quantile_impl<mpl::_1, weighted, LeftRight> impl;
122 template<typename LeftRight>
123 struct weighted_pot_quantile_prob
124 : depends_on<weighted_peaks_over_threshold_prob<LeftRight> >
128 typedef accumulators::impl::pot_quantile_impl<mpl::_1, weighted, LeftRight> impl;
132 // pot_quantile<LeftRight>(with_threshold_value) -> pot_quantile<LeftRight>
133 template<typename LeftRight>
134 struct as_feature<tag::pot_quantile<LeftRight>(with_threshold_value)>
136 typedef tag::pot_quantile<LeftRight> type;
139 // pot_quantile<LeftRight>(with_threshold_probability) -> pot_quantile_prob<LeftRight>
140 template<typename LeftRight>
141 struct as_feature<tag::pot_quantile<LeftRight>(with_threshold_probability)>
143 typedef tag::pot_quantile_prob<LeftRight> type;
146 // weighted_pot_quantile<LeftRight>(with_threshold_value) -> weighted_pot_quantile<LeftRight>
147 template<typename LeftRight>
148 struct as_feature<tag::weighted_pot_quantile<LeftRight>(with_threshold_value)>
150 typedef tag::weighted_pot_quantile<LeftRight> type;
153 // weighted_pot_quantile<LeftRight>(with_threshold_probability) -> weighted_pot_quantile_prob<LeftRight>
154 template<typename LeftRight>
155 struct as_feature<tag::weighted_pot_quantile<LeftRight>(with_threshold_probability)>
157 typedef tag::weighted_pot_quantile_prob<LeftRight> type;
160 // for the purposes of feature-based dependency resolution,
161 // pot_quantile<LeftRight> and pot_quantile_prob<LeftRight> provide
162 // the same feature as quantile
163 template<typename LeftRight>
164 struct feature_of<tag::pot_quantile<LeftRight> >
165 : feature_of<tag::quantile>
169 template<typename LeftRight>
170 struct feature_of<tag::pot_quantile_prob<LeftRight> >
171 : feature_of<tag::quantile>
175 // So that pot_quantile can be automatically substituted
176 // with weighted_pot_quantile when the weight parameter is non-void.
177 template<typename LeftRight>
178 struct as_weighted_feature<tag::pot_quantile<LeftRight> >
180 typedef tag::weighted_pot_quantile<LeftRight> type;
183 template<typename LeftRight>
184 struct feature_of<tag::weighted_pot_quantile<LeftRight> >
185 : feature_of<tag::pot_quantile<LeftRight> >
189 // So that pot_quantile_prob can be automatically substituted
190 // with weighted_pot_quantile_prob when the weight parameter is non-void.
191 template<typename LeftRight>
192 struct as_weighted_feature<tag::pot_quantile_prob<LeftRight> >
194 typedef tag::weighted_pot_quantile_prob<LeftRight> type;
197 template<typename LeftRight>
198 struct feature_of<tag::weighted_pot_quantile_prob<LeftRight> >
199 : feature_of<tag::pot_quantile_prob<LeftRight> >
203 }} // namespace boost::accumulators