pot_quantile.hpp 7.2 KB

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