1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // tail_variate_means.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_TAIL_VARIATE_MEANS_HPP_DE_01_01_2006 |
9 | #define BOOST_ACCUMULATORS_STATISTICS_TAIL_VARIATE_MEANS_HPP_DE_01_01_2006 |
10 | |
11 | #include <numeric> |
12 | #include <vector> |
13 | #include <limits> |
14 | #include <functional> |
15 | #include <sstream> |
16 | #include <stdexcept> |
17 | #include <boost/throw_exception.hpp> |
18 | #include <boost/parameter/keyword.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/tail.hpp> |
27 | #include <boost/accumulators/statistics/tail_variate.hpp> |
28 | #include <boost/accumulators/statistics/tail_mean.hpp> |
29 | #include <boost/accumulators/statistics/parameters/quantile_probability.hpp> |
30 | #include <boost/serialization/vector.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 | @brief Estimation of the absolute and relative tail variate means (for both left and right tails) |
44 | |
45 | For all \f$j\f$-th variates associated to the \f$\lceil n(1-\alpha)\rceil\f$ largest samples (or the |
46 | \f$\lceil n(1-\alpha)\rceil\f$ smallest samples in case of the left tail), the absolute tail means |
47 | \f$\widehat{ATM}_{n,\alpha}(X, j)\f$ are computed and returned as an iterator range. Alternatively, |
48 | the relative tail means \f$\widehat{RTM}_{n,\alpha}(X, j)\f$ are returned, which are the absolute |
49 | tail means normalized with the (non-coherent) sample tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$. |
50 | |
51 | \f[ |
52 | \widehat{ATM}_{n,\alpha}^{\mathrm{right}}(X, j) = |
53 | \frac{1}{\lceil n(1-\alpha) \rceil} |
54 | \sum_{i=\lceil \alpha n \rceil}^n \xi_{j,i} |
55 | \f] |
56 | |
57 | \f[ |
58 | \widehat{ATM}_{n,\alpha}^{\mathrm{left}}(X, j) = |
59 | \frac{1}{\lceil n\alpha \rceil} |
60 | \sum_{i=1}^{\lceil n\alpha \rceil} \xi_{j,i} |
61 | \f] |
62 | |
63 | \f[ |
64 | \widehat{RTM}_{n,\alpha}^{\mathrm{right}}(X, j) = |
65 | \frac{\sum_{i=\lceil n\alpha \rceil}^n \xi_{j,i}} |
66 | {\lceil n(1-\alpha)\rceil\widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X)} |
67 | \f] |
68 | |
69 | \f[ |
70 | \widehat{RTM}_{n,\alpha}^{\mathrm{left}}(X, j) = |
71 | \frac{\sum_{i=1}^{\lceil n\alpha \rceil} \xi_{j,i}} |
72 | {\lceil n\alpha\rceil\widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X)} |
73 | \f] |
74 | */ |
75 | |
76 | /////////////////////////////////////////////////////////////////////////////// |
77 | // tail_variate_means_impl |
78 | // by default: absolute tail_variate_means |
79 | template<typename Sample, typename Impl, typename LeftRight, typename VariateTag> |
80 | struct tail_variate_means_impl |
81 | : accumulator_base |
82 | { |
83 | typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type; |
84 | typedef std::vector<float_type> array_type; |
85 | // for boost::result_of |
86 | typedef iterator_range<typename array_type::iterator> result_type; |
87 | |
88 | tail_variate_means_impl(dont_care) {} |
89 | |
90 | template<typename Args> |
91 | result_type result(Args const &args) const |
92 | { |
93 | std::size_t cnt = count(args); |
94 | |
95 | std::size_t n = static_cast<std::size_t>( |
96 | std::ceil( |
97 | cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] ) |
98 | ) |
99 | ); |
100 | |
101 | std::size_t num_variates = tail_variate(args).begin()->size(); |
102 | |
103 | this->tail_means_.clear(); |
104 | this->tail_means_.resize(num_variates, Sample(0)); |
105 | |
106 | // If n is in a valid range, return result, otherwise return NaN or throw exception |
107 | if (n < static_cast<std::size_t>(tail(args).size())) |
108 | { |
109 | this->tail_means_ = std::accumulate( |
110 | tail_variate(args).begin() |
111 | , tail_variate(args).begin() + n |
112 | , this->tail_means_ |
113 | , numeric::plus |
114 | ); |
115 | |
116 | float_type factor = n * ( (is_same<Impl, relative>::value) ? non_coherent_tail_mean(args) : 1. ); |
117 | |
118 | std::transform( |
119 | this->tail_means_.begin() |
120 | , this->tail_means_.end() |
121 | , this->tail_means_.begin() |
122 | #ifdef BOOST_NO_CXX98_BINDERS |
123 | , std::bind(std::divides<float_type>(), std::placeholders::_1, factor) |
124 | #else |
125 | , std::bind2nd(std::divides<float_type>(), factor) |
126 | #endif |
127 | ); |
128 | } |
129 | else |
130 | { |
131 | if (std::numeric_limits<float_type>::has_quiet_NaN) |
132 | { |
133 | std::fill( |
134 | this->tail_means_.begin() |
135 | , this->tail_means_.end() |
136 | , std::numeric_limits<float_type>::quiet_NaN() |
137 | ); |
138 | } |
139 | else |
140 | { |
141 | std::ostringstream msg; |
142 | msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")" ; |
143 | boost::throw_exception(e: std::runtime_error(msg.str())); |
144 | } |
145 | } |
146 | return make_iterator_range(this->tail_means_); |
147 | } |
148 | |
149 | // make this accumulator serializeable |
150 | template<class Archive> |
151 | void serialize(Archive & ar, const unsigned int file_version) |
152 | { |
153 | ar & tail_means_; |
154 | } |
155 | |
156 | private: |
157 | |
158 | mutable array_type tail_means_; |
159 | |
160 | }; |
161 | |
162 | } // namespace impl |
163 | |
164 | /////////////////////////////////////////////////////////////////////////////// |
165 | // tag::absolute_tail_variate_means |
166 | // tag::relative_tail_variate_means |
167 | // |
168 | namespace tag |
169 | { |
170 | template<typename LeftRight, typename VariateType, typename VariateTag> |
171 | struct absolute_tail_variate_means |
172 | : depends_on<count, non_coherent_tail_mean<LeftRight>, tail_variate<VariateType, VariateTag, LeftRight> > |
173 | { |
174 | typedef accumulators::impl::tail_variate_means_impl<mpl::_1, absolute, LeftRight, VariateTag> impl; |
175 | }; |
176 | template<typename LeftRight, typename VariateType, typename VariateTag> |
177 | struct relative_tail_variate_means |
178 | : depends_on<count, non_coherent_tail_mean<LeftRight>, tail_variate<VariateType, VariateTag, LeftRight> > |
179 | { |
180 | typedef accumulators::impl::tail_variate_means_impl<mpl::_1, relative, LeftRight, VariateTag> impl; |
181 | }; |
182 | struct abstract_absolute_tail_variate_means |
183 | : depends_on<> |
184 | { |
185 | }; |
186 | struct abstract_relative_tail_variate_means |
187 | : depends_on<> |
188 | { |
189 | }; |
190 | } |
191 | |
192 | /////////////////////////////////////////////////////////////////////////////// |
193 | // extract::tail_variate_means |
194 | // extract::relative_tail_variate_means |
195 | // |
196 | namespace extract |
197 | { |
198 | extractor<tag::abstract_absolute_tail_variate_means> const = {}; |
199 | extractor<tag::abstract_relative_tail_variate_means> const = {}; |
200 | |
201 | BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_variate_means) |
202 | BOOST_ACCUMULATORS_IGNORE_GLOBAL(relative_tail_variate_means) |
203 | } |
204 | |
205 | using extract::tail_variate_means; |
206 | using extract::relative_tail_variate_means; |
207 | |
208 | // tail_variate_means<LeftRight, VariateType, VariateTag>(absolute) -> absolute_tail_variate_means<LeftRight, VariateType, VariateTag> |
209 | template<typename LeftRight, typename VariateType, typename VariateTag> |
210 | struct as_feature<tag::tail_variate_means<LeftRight, VariateType, VariateTag>(absolute)> |
211 | { |
212 | typedef tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> type; |
213 | }; |
214 | |
215 | // tail_variate_means<LeftRight, VariateType, VariateTag>(relative) ->relative_tail_variate_means<LeftRight, VariateType, VariateTag> |
216 | template<typename LeftRight, typename VariateType, typename VariateTag> |
217 | struct as_feature<tag::tail_variate_means<LeftRight, VariateType, VariateTag>(relative)> |
218 | { |
219 | typedef tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> type; |
220 | }; |
221 | |
222 | // Provides non-templatized extractor |
223 | template<typename LeftRight, typename VariateType, typename VariateTag> |
224 | struct feature_of<tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> > |
225 | : feature_of<tag::abstract_absolute_tail_variate_means> |
226 | { |
227 | }; |
228 | |
229 | // Provides non-templatized extractor |
230 | template<typename LeftRight, typename VariateType, typename VariateTag> |
231 | struct feature_of<tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> > |
232 | : feature_of<tag::abstract_relative_tail_variate_means> |
233 | { |
234 | }; |
235 | |
236 | // So that absolute_tail_means can be automatically substituted |
237 | // with absolute_weighted_tail_means when the weight parameter is non-void. |
238 | template<typename LeftRight, typename VariateType, typename VariateTag> |
239 | struct as_weighted_feature<tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> > |
240 | { |
241 | typedef tag::absolute_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> type; |
242 | }; |
243 | |
244 | template<typename LeftRight, typename VariateType, typename VariateTag> |
245 | struct feature_of<tag::absolute_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> > |
246 | : feature_of<tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> > |
247 | { |
248 | }; |
249 | |
250 | // So that relative_tail_means can be automatically substituted |
251 | // with relative_weighted_tail_means when the weight parameter is non-void. |
252 | template<typename LeftRight, typename VariateType, typename VariateTag> |
253 | struct as_weighted_feature<tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> > |
254 | { |
255 | typedef tag::relative_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> type; |
256 | }; |
257 | |
258 | template<typename LeftRight, typename VariateType, typename VariateTag> |
259 | struct feature_of<tag::relative_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> > |
260 | : feature_of<tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> > |
261 | { |
262 | }; |
263 | |
264 | }} // namespace boost::accumulators |
265 | |
266 | #ifdef _MSC_VER |
267 | # pragma warning(pop) |
268 | #endif |
269 | |
270 | #endif |
271 | |