pot_tail_mean.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // pot_tail_mean.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. #ifndef BOOST_ACCUMULATORS_STATISTICS_POT_TAIL_MEAN_HPP_DE_01_01_2006
  8. #define BOOST_ACCUMULATORS_STATISTICS_POT_TAIL_MEAN_HPP_DE_01_01_2006
  9. #include <vector>
  10. #include <limits>
  11. #include <numeric>
  12. #include <functional>
  13. #include <boost/range.hpp>
  14. #include <boost/parameter/keyword.hpp>
  15. #include <boost/tuple/tuple.hpp>
  16. #include <boost/mpl/if.hpp>
  17. #include <boost/mpl/placeholders.hpp>
  18. #include <boost/type_traits/is_same.hpp>
  19. #include <boost/accumulators/framework/accumulator_base.hpp>
  20. #include <boost/accumulators/framework/extractor.hpp>
  21. #include <boost/accumulators/numeric/functional.hpp>
  22. #include <boost/accumulators/framework/parameters/sample.hpp>
  23. #include <boost/accumulators/statistics_fwd.hpp>
  24. #include <boost/accumulators/statistics/peaks_over_threshold.hpp>
  25. #include <boost/accumulators/statistics/weighted_peaks_over_threshold.hpp>
  26. #include <boost/accumulators/statistics/pot_quantile.hpp>
  27. #include <boost/accumulators/statistics/tail_mean.hpp>
  28. namespace boost { namespace accumulators
  29. {
  30. namespace impl
  31. {
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // pot_tail_mean_impl
  34. //
  35. /**
  36. @brief Estimation of the (coherent) tail mean based on the peaks over threshold method (for both left and right tails)
  37. Computes an estimate for the (coherent) tail mean
  38. \f[
  39. \widehat{CTM}_{\alpha} = \hat{q}_{\alpha} - \frac{\bar{\beta}}{\xi-1}(1-\alpha)^{-\xi},
  40. \f]
  41. where \f$\bar[u]\f$, \f$\bar{\beta}\f$ and \f$\xi\f$ are the parameters of the
  42. generalized Pareto distribution that approximates the right tail of the distribution (or the
  43. mirrored left tail, in case the left tail is used). In the latter case, the result is mirrored
  44. back, yielding the correct result.
  45. */
  46. template<typename Sample, typename Impl, typename LeftRight>
  47. struct pot_tail_mean_impl
  48. : accumulator_base
  49. {
  50. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
  51. // for boost::result_of
  52. typedef float_type result_type;
  53. pot_tail_mean_impl(dont_care)
  54. : sign_((is_same<LeftRight, left>::value) ? -1 : 1)
  55. {
  56. }
  57. template<typename Args>
  58. result_type result(Args const &args) const
  59. {
  60. typedef
  61. typename mpl::if_<
  62. is_same<Impl, weighted>
  63. , tag::weighted_peaks_over_threshold<LeftRight>
  64. , tag::peaks_over_threshold<LeftRight>
  65. >::type
  66. peaks_over_threshold_tag;
  67. typedef
  68. typename mpl::if_<
  69. is_same<Impl, weighted>
  70. , tag::weighted_pot_quantile<LeftRight>
  71. , tag::pot_quantile<LeftRight>
  72. >::type
  73. pot_quantile_tag;
  74. extractor<peaks_over_threshold_tag> const some_peaks_over_threshold = {};
  75. extractor<pot_quantile_tag> const some_pot_quantile = {};
  76. float_type beta_bar = some_peaks_over_threshold(args).template get<1>();
  77. float_type xi_hat = some_peaks_over_threshold(args).template get<2>();
  78. return some_pot_quantile(args) - this->sign_ * beta_bar/( xi_hat - 1. ) * std::pow(
  79. is_same<LeftRight, left>::value ? args[quantile_probability] : 1. - args[quantile_probability]
  80. , -xi_hat);
  81. }
  82. // make this accumulator serializeable
  83. template<class Archive>
  84. void serialize(Archive & ar, const unsigned int file_version)
  85. {
  86. ar & sign_;
  87. }
  88. private:
  89. short sign_; // if the fit parameters from the mirrored left tail extreme values are used, mirror back the result
  90. };
  91. } // namespace impl
  92. ///////////////////////////////////////////////////////////////////////////////
  93. // tag::pot_tail_mean
  94. // tag::pot_tail_mean_prob
  95. //
  96. namespace tag
  97. {
  98. template<typename LeftRight>
  99. struct pot_tail_mean
  100. : depends_on<peaks_over_threshold<LeftRight>, pot_quantile<LeftRight> >
  101. {
  102. /// INTERNAL ONLY
  103. ///
  104. typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, unweighted, LeftRight> impl;
  105. };
  106. template<typename LeftRight>
  107. struct pot_tail_mean_prob
  108. : depends_on<peaks_over_threshold_prob<LeftRight>, pot_quantile_prob<LeftRight> >
  109. {
  110. /// INTERNAL ONLY
  111. ///
  112. typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, unweighted, LeftRight> impl;
  113. };
  114. template<typename LeftRight>
  115. struct weighted_pot_tail_mean
  116. : depends_on<weighted_peaks_over_threshold<LeftRight>, weighted_pot_quantile<LeftRight> >
  117. {
  118. /// INTERNAL ONLY
  119. ///
  120. typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, weighted, LeftRight> impl;
  121. };
  122. template<typename LeftRight>
  123. struct weighted_pot_tail_mean_prob
  124. : depends_on<weighted_peaks_over_threshold_prob<LeftRight>, weighted_pot_quantile_prob<LeftRight> >
  125. {
  126. /// INTERNAL ONLY
  127. ///
  128. typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, weighted, LeftRight> impl;
  129. };
  130. }
  131. // pot_tail_mean<LeftRight>(with_threshold_value) -> pot_tail_mean<LeftRight>
  132. template<typename LeftRight>
  133. struct as_feature<tag::pot_tail_mean<LeftRight>(with_threshold_value)>
  134. {
  135. typedef tag::pot_tail_mean<LeftRight> type;
  136. };
  137. // pot_tail_mean<LeftRight>(with_threshold_probability) -> pot_tail_mean_prob<LeftRight>
  138. template<typename LeftRight>
  139. struct as_feature<tag::pot_tail_mean<LeftRight>(with_threshold_probability)>
  140. {
  141. typedef tag::pot_tail_mean_prob<LeftRight> type;
  142. };
  143. // weighted_pot_tail_mean<LeftRight>(with_threshold_value) -> weighted_pot_tail_mean<LeftRight>
  144. template<typename LeftRight>
  145. struct as_feature<tag::weighted_pot_tail_mean<LeftRight>(with_threshold_value)>
  146. {
  147. typedef tag::weighted_pot_tail_mean<LeftRight> type;
  148. };
  149. // weighted_pot_tail_mean<LeftRight>(with_threshold_probability) -> weighted_pot_tail_mean_prob<LeftRight>
  150. template<typename LeftRight>
  151. struct as_feature<tag::weighted_pot_tail_mean<LeftRight>(with_threshold_probability)>
  152. {
  153. typedef tag::weighted_pot_tail_mean_prob<LeftRight> type;
  154. };
  155. // for the purposes of feature-based dependency resolution,
  156. // pot_tail_mean<LeftRight> and pot_tail_mean_prob<LeftRight> provide
  157. // the same feature as tail_mean
  158. template<typename LeftRight>
  159. struct feature_of<tag::pot_tail_mean<LeftRight> >
  160. : feature_of<tag::tail_mean>
  161. {
  162. };
  163. template<typename LeftRight>
  164. struct feature_of<tag::pot_tail_mean_prob<LeftRight> >
  165. : feature_of<tag::tail_mean>
  166. {
  167. };
  168. // So that pot_tail_mean can be automatically substituted
  169. // with weighted_pot_tail_mean when the weight parameter is non-void.
  170. template<typename LeftRight>
  171. struct as_weighted_feature<tag::pot_tail_mean<LeftRight> >
  172. {
  173. typedef tag::weighted_pot_tail_mean<LeftRight> type;
  174. };
  175. template<typename LeftRight>
  176. struct feature_of<tag::weighted_pot_tail_mean<LeftRight> >
  177. : feature_of<tag::pot_tail_mean<LeftRight> >
  178. {
  179. };
  180. // So that pot_tail_mean_prob can be automatically substituted
  181. // with weighted_pot_tail_mean_prob when the weight parameter is non-void.
  182. template<typename LeftRight>
  183. struct as_weighted_feature<tag::pot_tail_mean_prob<LeftRight> >
  184. {
  185. typedef tag::weighted_pot_tail_mean_prob<LeftRight> type;
  186. };
  187. template<typename LeftRight>
  188. struct feature_of<tag::weighted_pot_tail_mean_prob<LeftRight> >
  189. : feature_of<tag::pot_tail_mean_prob<LeftRight> >
  190. {
  191. };
  192. }} // namespace boost::accumulators
  193. #endif