peaks_over_threshold.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // peaks_over_threshold.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_PEAKS_OVER_THRESHOLD_HPP_DE_01_01_2006
  8. #define BOOST_ACCUMULATORS_STATISTICS_PEAKS_OVER_THRESHOLD_HPP_DE_01_01_2006
  9. #include <vector>
  10. #include <limits>
  11. #include <numeric>
  12. #include <functional>
  13. #include <boost/config/no_tr1/cmath.hpp> // pow
  14. #include <sstream> // stringstream
  15. #include <stdexcept> // runtime_error
  16. #include <boost/throw_exception.hpp>
  17. #include <boost/range.hpp>
  18. #include <boost/mpl/if.hpp>
  19. #include <boost/mpl/int.hpp>
  20. #include <boost/mpl/placeholders.hpp>
  21. #include <boost/parameter/keyword.hpp>
  22. #include <boost/tuple/tuple.hpp>
  23. #include <boost/accumulators/accumulators_fwd.hpp>
  24. #include <boost/accumulators/framework/accumulator_base.hpp>
  25. #include <boost/accumulators/framework/extractor.hpp>
  26. #include <boost/accumulators/numeric/functional.hpp>
  27. #include <boost/accumulators/framework/parameters/sample.hpp>
  28. #include <boost/accumulators/framework/depends_on.hpp>
  29. #include <boost/accumulators/statistics_fwd.hpp>
  30. #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
  31. #include <boost/accumulators/statistics/count.hpp>
  32. #include <boost/accumulators/statistics/tail.hpp>
  33. #ifdef _MSC_VER
  34. # pragma warning(push)
  35. # pragma warning(disable: 4127) // conditional expression is constant
  36. #endif
  37. namespace boost { namespace accumulators
  38. {
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // threshold_probability and threshold named parameters
  41. //
  42. BOOST_PARAMETER_NESTED_KEYWORD(tag, pot_threshold_value, threshold_value)
  43. BOOST_PARAMETER_NESTED_KEYWORD(tag, pot_threshold_probability, threshold_probability)
  44. BOOST_ACCUMULATORS_IGNORE_GLOBAL(pot_threshold_value)
  45. BOOST_ACCUMULATORS_IGNORE_GLOBAL(pot_threshold_probability)
  46. namespace impl
  47. {
  48. ///////////////////////////////////////////////////////////////////////////////
  49. // peaks_over_threshold_impl
  50. // works with an explicit threshold value and does not depend on order statistics
  51. /**
  52. @brief Peaks over Threshold Method for Quantile and Tail Mean Estimation
  53. According to the theorem of Pickands-Balkema-de Haan, the distribution function \f$F_u(x)\f$ of
  54. the excesses \f$x\f$ over some sufficiently high threshold \f$u\f$ of a distribution function \f$F(x)\f$
  55. may be approximated by a generalized Pareto distribution
  56. \f[
  57. G_{\xi,\beta}(x) =
  58. \left\{
  59. \begin{array}{ll}
  60. \beta^{-1}\left(1+\frac{\xi x}{\beta}\right)^{-1/\xi-1} & \textrm{if }\xi\neq0\\
  61. \beta^{-1}\exp\left(-\frac{x}{\beta}\right) & \textrm{if }\xi=0,
  62. \end{array}
  63. \right.
  64. \f]
  65. with suitable parameters \f$\xi\f$ and \f$\beta\f$ that can be estimated, e.g., with the method of moments, cf.
  66. Hosking and Wallis (1987),
  67. \f[
  68. \begin{array}{lll}
  69. \hat{\xi} & = & \frac{1}{2}\left[1-\frac{(\hat{\mu}-u)^2}{\hat{\sigma}^2}\right]\\
  70. \hat{\beta} & = & \frac{\hat{\mu}-u}{2}\left[\frac{(\hat{\mu}-u)^2}{\hat{\sigma}^2}+1\right],
  71. \end{array}
  72. \f]
  73. \f$\hat{\mu}\f$ and \f$\hat{\sigma}^2\f$ being the empirical mean and variance of the samples over
  74. the threshold \f$u\f$. Equivalently, the distribution function
  75. \f$F_u(x-u)\f$ of the exceedances \f$x-u\f$ can be approximated by
  76. \f$G_{\xi,\beta}(x-u)=G_{\xi,\beta,u}(x)\f$. Since for \f$x\geq u\f$ the distribution function \f$F(x)\f$
  77. can be written as
  78. \f[
  79. F(x) = [1 - \P(X \leq u)]F_u(x - u) + \P(X \leq u)
  80. \f]
  81. and the probability \f$\P(X \leq u)\f$ can be approximated by the empirical distribution function
  82. \f$F_n(u)\f$ evaluated at \f$u\f$, an estimator of \f$F(x)\f$ is given by
  83. \f[
  84. \widehat{F}(x) = [1 - F_n(u)]G_{\xi,\beta,u}(x) + F_n(u).
  85. \f]
  86. It can be shown that \f$\widehat{F}(x)\f$ is a generalized
  87. Pareto distribution \f$G_{\xi,\bar{\beta},\bar{u}}(x)\f$ with \f$\bar{\beta}=\beta[1-F_n(u)]^{\xi}\f$
  88. and \f$\bar{u}=u-\bar{\beta}\left\{[1-F_n(u)]^{-\xi}-1\right\}/\xi\f$. By inverting \f$\widehat{F}(x)\f$,
  89. one obtains an estimator for the \f$\alpha\f$-quantile,
  90. \f[
  91. \hat{q}_{\alpha} = \bar{u} + \frac{\bar{\beta}}{\xi}\left[(1-\alpha)^{-\xi}-1\right],
  92. \f]
  93. and similarly an estimator for the (coherent) tail mean,
  94. \f[
  95. \widehat{CTM}_{\alpha} = \hat{q}_{\alpha} - \frac{\bar{\beta}}{\xi-1}(1-\alpha)^{-\xi},
  96. \f]
  97. cf. McNeil and Frey (2000).
  98. Note that in case extreme values of the left tail are fitted, the distribution is mirrored with respect to the
  99. \f$y\f$ axis such that the left tail can be treated as a right tail. The computed fit parameters thus define
  100. the Pareto distribution that fits the mirrored left tail. When quantities like a quantile or a tail mean are
  101. computed using the fit parameters obtained from the mirrored data, the result is mirrored back, yielding the
  102. correct result.
  103. For further details, see
  104. J. R. M. Hosking and J. R. Wallis, Parameter and quantile estimation for the generalized Pareto distribution,
  105. Technometrics, Volume 29, 1987, p. 339-349
  106. A. J. McNeil and R. Frey, Estimation of Tail-Related Risk Measures for Heteroscedastic Financial Time Series:
  107. an Extreme Value Approach, Journal of Empirical Finance, Volume 7, 2000, p. 271-300
  108. @param quantile_probability
  109. @param pot_threshold_value
  110. */
  111. template<typename Sample, typename LeftRight>
  112. struct peaks_over_threshold_impl
  113. : accumulator_base
  114. {
  115. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
  116. // for boost::result_of
  117. typedef boost::tuple<float_type, float_type, float_type> result_type;
  118. // for left tail fitting, mirror the extreme values
  119. typedef mpl::int_<is_same<LeftRight, left>::value ? -1 : 1> sign;
  120. template<typename Args>
  121. peaks_over_threshold_impl(Args const &args)
  122. : Nu_(0)
  123. , mu_(sign::value * numeric::fdiv(args[sample | Sample()], (std::size_t)1))
  124. , sigma2_(numeric::fdiv(args[sample | Sample()], (std::size_t)1))
  125. , threshold_(sign::value * args[pot_threshold_value])
  126. , fit_parameters_(boost::make_tuple(0., 0., 0.))
  127. , is_dirty_(true)
  128. {
  129. }
  130. template<typename Args>
  131. void operator ()(Args const &args)
  132. {
  133. this->is_dirty_ = true;
  134. if (sign::value * args[sample] > this->threshold_)
  135. {
  136. this->mu_ += args[sample];
  137. this->sigma2_ += args[sample] * args[sample];
  138. ++this->Nu_;
  139. }
  140. }
  141. template<typename Args>
  142. result_type result(Args const &args) const
  143. {
  144. if (this->is_dirty_)
  145. {
  146. this->is_dirty_ = false;
  147. std::size_t cnt = count(args);
  148. this->mu_ = sign::value * numeric::fdiv(this->mu_, this->Nu_);
  149. this->sigma2_ = numeric::fdiv(this->sigma2_, this->Nu_);
  150. this->sigma2_ -= this->mu_ * this->mu_;
  151. float_type threshold_probability = numeric::fdiv(cnt - this->Nu_, cnt);
  152. float_type tmp = numeric::fdiv(( this->mu_ - this->threshold_ )*( this->mu_ - this->threshold_ ), this->sigma2_);
  153. float_type xi_hat = 0.5 * ( 1. - tmp );
  154. float_type beta_hat = 0.5 * ( this->mu_ - this->threshold_ ) * ( 1. + tmp );
  155. float_type beta_bar = beta_hat * std::pow(1. - threshold_probability, xi_hat);
  156. float_type u_bar = this->threshold_ - beta_bar * ( std::pow(1. - threshold_probability, -xi_hat) - 1.)/xi_hat;
  157. this->fit_parameters_ = boost::make_tuple(u_bar, beta_bar, xi_hat);
  158. }
  159. return this->fit_parameters_;
  160. }
  161. private:
  162. std::size_t Nu_; // number of samples larger than threshold
  163. mutable float_type mu_; // mean of Nu_ largest samples
  164. mutable float_type sigma2_; // variance of Nu_ largest samples
  165. float_type threshold_;
  166. mutable result_type fit_parameters_; // boost::tuple that stores fit parameters
  167. mutable bool is_dirty_;
  168. };
  169. ///////////////////////////////////////////////////////////////////////////////
  170. // peaks_over_threshold_prob_impl
  171. // determines threshold from a given threshold probability using order statistics
  172. /**
  173. @brief Peaks over Threshold Method for Quantile and Tail Mean Estimation
  174. @sa peaks_over_threshold_impl
  175. @param quantile_probability
  176. @param pot_threshold_probability
  177. */
  178. template<typename Sample, typename LeftRight>
  179. struct peaks_over_threshold_prob_impl
  180. : accumulator_base
  181. {
  182. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
  183. // for boost::result_of
  184. typedef boost::tuple<float_type, float_type, float_type> result_type;
  185. // for left tail fitting, mirror the extreme values
  186. typedef mpl::int_<is_same<LeftRight, left>::value ? -1 : 1> sign;
  187. template<typename Args>
  188. peaks_over_threshold_prob_impl(Args const &args)
  189. : mu_(sign::value * numeric::fdiv(args[sample | Sample()], (std::size_t)1))
  190. , sigma2_(numeric::fdiv(args[sample | Sample()], (std::size_t)1))
  191. , threshold_probability_(args[pot_threshold_probability])
  192. , fit_parameters_(boost::make_tuple(0., 0., 0.))
  193. , is_dirty_(true)
  194. {
  195. }
  196. void operator ()(dont_care)
  197. {
  198. this->is_dirty_ = true;
  199. }
  200. template<typename Args>
  201. result_type result(Args const &args) const
  202. {
  203. if (this->is_dirty_)
  204. {
  205. this->is_dirty_ = false;
  206. std::size_t cnt = count(args);
  207. // the n'th cached sample provides an approximate threshold value u
  208. std::size_t n = static_cast<std::size_t>(
  209. std::ceil(
  210. cnt * ( ( is_same<LeftRight, left>::value ) ? this->threshold_probability_ : 1. - this->threshold_probability_ )
  211. )
  212. );
  213. // If n is in a valid range, return result, otherwise return NaN or throw exception
  214. if ( n >= static_cast<std::size_t>(tail(args).size()))
  215. {
  216. if (std::numeric_limits<float_type>::has_quiet_NaN)
  217. {
  218. return boost::make_tuple(
  219. std::numeric_limits<float_type>::quiet_NaN()
  220. , std::numeric_limits<float_type>::quiet_NaN()
  221. , std::numeric_limits<float_type>::quiet_NaN()
  222. );
  223. }
  224. else
  225. {
  226. std::ostringstream msg;
  227. msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
  228. boost::throw_exception(std::runtime_error(msg.str()));
  229. return boost::make_tuple(Sample(0), Sample(0), Sample(0));
  230. }
  231. }
  232. else
  233. {
  234. float_type u = *(tail(args).begin() + n - 1) * sign::value;
  235. // compute mean and variance of samples above/under threshold value u
  236. for (std::size_t i = 0; i < n; ++i)
  237. {
  238. mu_ += *(tail(args).begin() + i);
  239. sigma2_ += *(tail(args).begin() + i) * (*(tail(args).begin() + i));
  240. }
  241. this->mu_ = sign::value * numeric::fdiv(this->mu_, n);
  242. this->sigma2_ = numeric::fdiv(this->sigma2_, n);
  243. this->sigma2_ -= this->mu_ * this->mu_;
  244. if (is_same<LeftRight, left>::value)
  245. this->threshold_probability_ = 1. - this->threshold_probability_;
  246. float_type tmp = numeric::fdiv(( this->mu_ - u )*( this->mu_ - u ), this->sigma2_);
  247. float_type xi_hat = 0.5 * ( 1. - tmp );
  248. float_type beta_hat = 0.5 * ( this->mu_ - u ) * ( 1. + tmp );
  249. float_type beta_bar = beta_hat * std::pow(1. - threshold_probability_, xi_hat);
  250. float_type u_bar = u - beta_bar * ( std::pow(1. - threshold_probability_, -xi_hat) - 1.)/xi_hat;
  251. this->fit_parameters_ = boost::make_tuple(u_bar, beta_bar, xi_hat);
  252. }
  253. }
  254. return this->fit_parameters_;
  255. }
  256. private:
  257. mutable float_type mu_; // mean of samples above threshold u
  258. mutable float_type sigma2_; // variance of samples above threshold u
  259. mutable float_type threshold_probability_;
  260. mutable result_type fit_parameters_; // boost::tuple that stores fit parameters
  261. mutable bool is_dirty_;
  262. };
  263. } // namespace impl
  264. ///////////////////////////////////////////////////////////////////////////////
  265. // tag::peaks_over_threshold
  266. //
  267. namespace tag
  268. {
  269. template<typename LeftRight>
  270. struct peaks_over_threshold
  271. : depends_on<count>
  272. , pot_threshold_value
  273. {
  274. /// INTERNAL ONLY
  275. ///
  276. typedef accumulators::impl::peaks_over_threshold_impl<mpl::_1, LeftRight> impl;
  277. };
  278. template<typename LeftRight>
  279. struct peaks_over_threshold_prob
  280. : depends_on<count, tail<LeftRight> >
  281. , pot_threshold_probability
  282. {
  283. /// INTERNAL ONLY
  284. ///
  285. typedef accumulators::impl::peaks_over_threshold_prob_impl<mpl::_1, LeftRight> impl;
  286. };
  287. struct abstract_peaks_over_threshold
  288. : depends_on<>
  289. {
  290. };
  291. }
  292. ///////////////////////////////////////////////////////////////////////////////
  293. // extract::peaks_over_threshold
  294. //
  295. namespace extract
  296. {
  297. extractor<tag::abstract_peaks_over_threshold> const peaks_over_threshold = {};
  298. BOOST_ACCUMULATORS_IGNORE_GLOBAL(peaks_over_threshold)
  299. }
  300. using extract::peaks_over_threshold;
  301. // peaks_over_threshold<LeftRight>(with_threshold_value) -> peaks_over_threshold<LeftRight>
  302. template<typename LeftRight>
  303. struct as_feature<tag::peaks_over_threshold<LeftRight>(with_threshold_value)>
  304. {
  305. typedef tag::peaks_over_threshold<LeftRight> type;
  306. };
  307. // peaks_over_threshold<LeftRight>(with_threshold_probability) -> peaks_over_threshold_prob<LeftRight>
  308. template<typename LeftRight>
  309. struct as_feature<tag::peaks_over_threshold<LeftRight>(with_threshold_probability)>
  310. {
  311. typedef tag::peaks_over_threshold_prob<LeftRight> type;
  312. };
  313. template<typename LeftRight>
  314. struct feature_of<tag::peaks_over_threshold<LeftRight> >
  315. : feature_of<tag::abstract_peaks_over_threshold>
  316. {
  317. };
  318. template<typename LeftRight>
  319. struct feature_of<tag::peaks_over_threshold_prob<LeftRight> >
  320. : feature_of<tag::abstract_peaks_over_threshold>
  321. {
  322. };
  323. // So that peaks_over_threshold can be automatically substituted
  324. // with weighted_peaks_over_threshold when the weight parameter is non-void.
  325. template<typename LeftRight>
  326. struct as_weighted_feature<tag::peaks_over_threshold<LeftRight> >
  327. {
  328. typedef tag::weighted_peaks_over_threshold<LeftRight> type;
  329. };
  330. template<typename LeftRight>
  331. struct feature_of<tag::weighted_peaks_over_threshold<LeftRight> >
  332. : feature_of<tag::peaks_over_threshold<LeftRight> >
  333. {};
  334. // So that peaks_over_threshold_prob can be automatically substituted
  335. // with weighted_peaks_over_threshold_prob when the weight parameter is non-void.
  336. template<typename LeftRight>
  337. struct as_weighted_feature<tag::peaks_over_threshold_prob<LeftRight> >
  338. {
  339. typedef tag::weighted_peaks_over_threshold_prob<LeftRight> type;
  340. };
  341. template<typename LeftRight>
  342. struct feature_of<tag::weighted_peaks_over_threshold_prob<LeftRight> >
  343. : feature_of<tag::peaks_over_threshold_prob<LeftRight> >
  344. {};
  345. }} // namespace boost::accumulators
  346. #ifdef _MSC_VER
  347. # pragma warning(pop)
  348. #endif
  349. #endif