/Src/Dependencies/Boost/boost/accumulators/statistics/pot_tail_mean.hpp

http://hadesmem.googlecode.com/ · C++ Header · 211 lines · 143 code · 23 blank · 45 comment · 0 complexity · 2585cb286d8c6bd57e648c00b8c82eee MD5 · raw file

  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::average<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. private:
  83. short sign_; // if the fit parameters from the mirrored left tail extreme values are used, mirror back the result
  84. };
  85. } // namespace impl
  86. ///////////////////////////////////////////////////////////////////////////////
  87. // tag::pot_tail_mean
  88. // tag::pot_tail_mean_prob
  89. //
  90. namespace tag
  91. {
  92. template<typename LeftRight>
  93. struct pot_tail_mean
  94. : depends_on<peaks_over_threshold<LeftRight>, pot_quantile<LeftRight> >
  95. {
  96. /// INTERNAL ONLY
  97. ///
  98. typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, unweighted, LeftRight> impl;
  99. };
  100. template<typename LeftRight>
  101. struct pot_tail_mean_prob
  102. : depends_on<peaks_over_threshold_prob<LeftRight>, pot_quantile_prob<LeftRight> >
  103. {
  104. /// INTERNAL ONLY
  105. ///
  106. typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, unweighted, LeftRight> impl;
  107. };
  108. template<typename LeftRight>
  109. struct weighted_pot_tail_mean
  110. : depends_on<weighted_peaks_over_threshold<LeftRight>, weighted_pot_quantile<LeftRight> >
  111. {
  112. /// INTERNAL ONLY
  113. ///
  114. typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, weighted, LeftRight> impl;
  115. };
  116. template<typename LeftRight>
  117. struct weighted_pot_tail_mean_prob
  118. : depends_on<weighted_peaks_over_threshold_prob<LeftRight>, weighted_pot_quantile_prob<LeftRight> >
  119. {
  120. /// INTERNAL ONLY
  121. ///
  122. typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, weighted, LeftRight> impl;
  123. };
  124. }
  125. // pot_tail_mean<LeftRight>(with_threshold_value) -> pot_tail_mean<LeftRight>
  126. template<typename LeftRight>
  127. struct as_feature<tag::pot_tail_mean<LeftRight>(with_threshold_value)>
  128. {
  129. typedef tag::pot_tail_mean<LeftRight> type;
  130. };
  131. // pot_tail_mean<LeftRight>(with_threshold_probability) -> pot_tail_mean_prob<LeftRight>
  132. template<typename LeftRight>
  133. struct as_feature<tag::pot_tail_mean<LeftRight>(with_threshold_probability)>
  134. {
  135. typedef tag::pot_tail_mean_prob<LeftRight> type;
  136. };
  137. // weighted_pot_tail_mean<LeftRight>(with_threshold_value) -> weighted_pot_tail_mean<LeftRight>
  138. template<typename LeftRight>
  139. struct as_feature<tag::weighted_pot_tail_mean<LeftRight>(with_threshold_value)>
  140. {
  141. typedef tag::weighted_pot_tail_mean<LeftRight> type;
  142. };
  143. // weighted_pot_tail_mean<LeftRight>(with_threshold_probability) -> weighted_pot_tail_mean_prob<LeftRight>
  144. template<typename LeftRight>
  145. struct as_feature<tag::weighted_pot_tail_mean<LeftRight>(with_threshold_probability)>
  146. {
  147. typedef tag::weighted_pot_tail_mean_prob<LeftRight> type;
  148. };
  149. // for the purposes of feature-based dependency resolution,
  150. // pot_tail_mean<LeftRight> and pot_tail_mean_prob<LeftRight> provide
  151. // the same feature as tail_mean
  152. template<typename LeftRight>
  153. struct feature_of<tag::pot_tail_mean<LeftRight> >
  154. : feature_of<tag::tail_mean>
  155. {
  156. };
  157. template<typename LeftRight>
  158. struct feature_of<tag::pot_tail_mean_prob<LeftRight> >
  159. : feature_of<tag::tail_mean>
  160. {
  161. };
  162. // So that pot_tail_mean can be automatically substituted
  163. // with weighted_pot_tail_mean when the weight parameter is non-void.
  164. template<typename LeftRight>
  165. struct as_weighted_feature<tag::pot_tail_mean<LeftRight> >
  166. {
  167. typedef tag::weighted_pot_tail_mean<LeftRight> type;
  168. };
  169. template<typename LeftRight>
  170. struct feature_of<tag::weighted_pot_tail_mean<LeftRight> >
  171. : feature_of<tag::pot_tail_mean<LeftRight> >
  172. {
  173. };
  174. // So that pot_tail_mean_prob can be automatically substituted
  175. // with weighted_pot_tail_mean_prob when the weight parameter is non-void.
  176. template<typename LeftRight>
  177. struct as_weighted_feature<tag::pot_tail_mean_prob<LeftRight> >
  178. {
  179. typedef tag::weighted_pot_tail_mean_prob<LeftRight> type;
  180. };
  181. template<typename LeftRight>
  182. struct feature_of<tag::weighted_pot_tail_mean_prob<LeftRight> >
  183. : feature_of<tag::pot_tail_mean_prob<LeftRight> >
  184. {
  185. };
  186. }} // namespace boost::accumulators
  187. #endif