]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // pot_quantile.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_POT_QUANTILE_HPP_DE_01_01_2006 | |
9 | #define BOOST_ACCUMULATORS_STATISTICS_POT_QUANTILE_HPP_DE_01_01_2006 | |
10 | ||
11 | #include <vector> | |
12 | #include <limits> | |
13 | #include <numeric> | |
14 | #include <functional> | |
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> | |
28 | ||
29 | namespace boost { namespace accumulators | |
30 | { | |
31 | ||
32 | namespace impl | |
33 | { | |
34 | /////////////////////////////////////////////////////////////////////////////// | |
35 | // pot_quantile_impl | |
36 | // | |
37 | /** | |
38 | @brief Quantile Estimation based on Peaks over Threshold Method (for both left and right tails) | |
39 | ||
40 | Computes an estimate | |
41 | \f[ | |
42 | \hat{q}_{\alpha} = \bar{u} + \frac{\bar{\beta}}{\xi}\left[(1-\alpha)^{-\xi}-1\right] | |
43 | \f] | |
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. | |
47 | */ | |
48 | template<typename Sample, typename Impl, typename LeftRight> | |
49 | struct pot_quantile_impl | |
50 | : accumulator_base | |
51 | { | |
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; | |
55 | ||
56 | pot_quantile_impl(dont_care) | |
57 | : sign_((is_same<LeftRight, left>::value) ? -1 : 1) | |
58 | { | |
59 | } | |
60 | ||
61 | template<typename Args> | |
62 | result_type result(Args const &args) const | |
63 | { | |
64 | typedef | |
65 | typename mpl::if_< | |
66 | is_same<Impl, weighted> | |
67 | , tag::weighted_peaks_over_threshold<LeftRight> | |
68 | , tag::peaks_over_threshold<LeftRight> | |
69 | >::type | |
70 | peaks_over_threshold_tag; | |
71 | ||
72 | extractor<peaks_over_threshold_tag> const some_peaks_over_threshold = {}; | |
73 | ||
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>(); | |
77 | ||
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] | |
80 | , -xi_hat | |
81 | ) - 1.)); | |
82 | } | |
83 | ||
84 | private: | |
85 | short sign_; // if the fit parameters from the mirrored left tail extreme values are used, mirror back the result | |
86 | }; | |
87 | ||
88 | } // namespace impl | |
89 | ||
90 | /////////////////////////////////////////////////////////////////////////////// | |
91 | // tag::pot_quantile<> | |
92 | // tag::pot_quantile_prob<> | |
93 | // tag::weighted_pot_quantile<> | |
94 | // tag::weighted_pot_quantile_prob<> | |
95 | // | |
96 | namespace tag | |
97 | { | |
98 | template<typename LeftRight> | |
99 | struct pot_quantile | |
100 | : depends_on<peaks_over_threshold<LeftRight> > | |
101 | { | |
102 | /// INTERNAL ONLY | |
103 | /// | |
104 | typedef accumulators::impl::pot_quantile_impl<mpl::_1, unweighted, LeftRight> impl; | |
105 | }; | |
106 | template<typename LeftRight> | |
107 | struct pot_quantile_prob | |
108 | : depends_on<peaks_over_threshold_prob<LeftRight> > | |
109 | { | |
110 | /// INTERNAL ONLY | |
111 | /// | |
112 | typedef accumulators::impl::pot_quantile_impl<mpl::_1, unweighted, LeftRight> impl; | |
113 | }; | |
114 | template<typename LeftRight> | |
115 | struct weighted_pot_quantile | |
116 | : depends_on<weighted_peaks_over_threshold<LeftRight> > | |
117 | { | |
118 | /// INTERNAL ONLY | |
119 | /// | |
120 | typedef accumulators::impl::pot_quantile_impl<mpl::_1, weighted, LeftRight> impl; | |
121 | }; | |
122 | template<typename LeftRight> | |
123 | struct weighted_pot_quantile_prob | |
124 | : depends_on<weighted_peaks_over_threshold_prob<LeftRight> > | |
125 | { | |
126 | /// INTERNAL ONLY | |
127 | /// | |
128 | typedef accumulators::impl::pot_quantile_impl<mpl::_1, weighted, LeftRight> impl; | |
129 | }; | |
130 | } | |
131 | ||
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)> | |
135 | { | |
136 | typedef tag::pot_quantile<LeftRight> type; | |
137 | }; | |
138 | ||
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)> | |
142 | { | |
143 | typedef tag::pot_quantile_prob<LeftRight> type; | |
144 | }; | |
145 | ||
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)> | |
149 | { | |
150 | typedef tag::weighted_pot_quantile<LeftRight> type; | |
151 | }; | |
152 | ||
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)> | |
156 | { | |
157 | typedef tag::weighted_pot_quantile_prob<LeftRight> type; | |
158 | }; | |
159 | ||
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> | |
166 | { | |
167 | }; | |
168 | ||
169 | template<typename LeftRight> | |
170 | struct feature_of<tag::pot_quantile_prob<LeftRight> > | |
171 | : feature_of<tag::quantile> | |
172 | { | |
173 | }; | |
174 | ||
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> > | |
179 | { | |
180 | typedef tag::weighted_pot_quantile<LeftRight> type; | |
181 | }; | |
182 | ||
183 | template<typename LeftRight> | |
184 | struct feature_of<tag::weighted_pot_quantile<LeftRight> > | |
185 | : feature_of<tag::pot_quantile<LeftRight> > | |
186 | { | |
187 | }; | |
188 | ||
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> > | |
193 | { | |
194 | typedef tag::weighted_pot_quantile_prob<LeftRight> type; | |
195 | }; | |
196 | ||
197 | template<typename LeftRight> | |
198 | struct feature_of<tag::weighted_pot_quantile_prob<LeftRight> > | |
199 | : feature_of<tag::pot_quantile_prob<LeftRight> > | |
200 | { | |
201 | }; | |
202 | ||
203 | }} // namespace boost::accumulators | |
204 | ||
205 | #endif |