peaks_over_threshold.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. // make this accumulator serializeable
  162. // TODO: do we need to split to load/save and verify that threshold did not change?
  163. template<class Archive>
  164. void serialize(Archive & ar, const unsigned int file_version)
  165. {
  166. ar & Nu_;
  167. ar & mu_;
  168. ar & sigma2_;
  169. ar & threshold_;
  170. ar & get<0>(fit_parameters_);
  171. ar & get<1>(fit_parameters_);
  172. ar & get<2>(fit_parameters_);
  173. ar & is_dirty_;
  174. }
  175. private:
  176. std::size_t Nu_; // number of samples larger than threshold
  177. mutable float_type mu_; // mean of Nu_ largest samples
  178. mutable float_type sigma2_; // variance of Nu_ largest samples
  179. float_type threshold_;
  180. mutable result_type fit_parameters_; // boost::tuple that stores fit parameters
  181. mutable bool is_dirty_;
  182. };
  183. ///////////////////////////////////////////////////////////////////////////////
  184. // peaks_over_threshold_prob_impl
  185. // determines threshold from a given threshold probability using order statistics
  186. /**
  187. @brief Peaks over Threshold Method for Quantile and Tail Mean Estimation
  188. @sa peaks_over_threshold_impl
  189. @param quantile_probability
  190. @param pot_threshold_probability
  191. */
  192. template<typename Sample, typename LeftRight>
  193. struct peaks_over_threshold_prob_impl
  194. : accumulator_base
  195. {
  196. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
  197. // for boost::result_of
  198. typedef boost::tuple<float_type, float_type, float_type> result_type;
  199. // for left tail fitting, mirror the extreme values
  200. typedef mpl::int_<is_same<LeftRight, left>::value ? -1 : 1> sign;
  201. template<typename Args>
  202. peaks_over_threshold_prob_impl(Args const &args)
  203. : mu_(sign::value * numeric::fdiv(args[sample | Sample()], (std::size_t)1))
  204. , sigma2_(numeric::fdiv(args[sample | Sample()], (std::size_t)1))
  205. , threshold_probability_(args[pot_threshold_probability])
  206. , fit_parameters_(boost::make_tuple(0., 0., 0.))
  207. , is_dirty_(true)
  208. {
  209. }
  210. void operator ()(dont_care)
  211. {
  212. this->is_dirty_ = true;
  213. }
  214. template<typename Args>
  215. result_type result(Args const &args) const
  216. {
  217. if (this->is_dirty_)
  218. {
  219. this->is_dirty_ = false;
  220. std::size_t cnt = count(args);
  221. // the n'th cached sample provides an approximate threshold value u
  222. std::size_t n = static_cast<std::size_t>(
  223. std::ceil(
  224. cnt * ( ( is_same<LeftRight, left>::value ) ? this->threshold_probability_ : 1. - this->threshold_probability_ )
  225. )
  226. );
  227. // If n is in a valid range, return result, otherwise return NaN or throw exception
  228. if ( n >= static_cast<std::size_t>(tail(args).size()))
  229. {
  230. if (std::numeric_limits<float_type>::has_quiet_NaN)
  231. {
  232. return boost::make_tuple(
  233. std::numeric_limits<float_type>::quiet_NaN()
  234. , std::numeric_limits<float_type>::quiet_NaN()
  235. , std::numeric_limits<float_type>::quiet_NaN()
  236. );
  237. }
  238. else
  239. {
  240. std::ostringstream msg;
  241. msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
  242. boost::throw_exception(std::runtime_error(msg.str()));
  243. return boost::make_tuple(Sample(0), Sample(0), Sample(0));
  244. }
  245. }
  246. else
  247. {
  248. float_type u = *(tail(args).begin() + n - 1) * sign::value;
  249. // compute mean and variance of samples above/under threshold value u
  250. for (std::size_t i = 0; i < n; ++i)
  251. {
  252. mu_ += *(tail(args).begin() + i);
  253. sigma2_ += *(tail(args).begin() + i) * (*(tail(args).begin() + i));
  254. }
  255. this->mu_ = sign::value * numeric::fdiv(this->mu_, n);
  256. this->sigma2_ = numeric::fdiv(this->sigma2_, n);
  257. this->sigma2_ -= this->mu_ * this->mu_;
  258. if (is_same<LeftRight, left>::value)
  259. this->threshold_probability_ = 1. - this->threshold_probability_;
  260. float_type tmp = numeric::fdiv(( this->mu_ - u )*( this->mu_ - u ), this->sigma2_);
  261. float_type xi_hat = 0.5 * ( 1. - tmp );
  262. float_type beta_hat = 0.5 * ( this->mu_ - u ) * ( 1. + tmp );
  263. float_type beta_bar = beta_hat * std::pow(1. - threshold_probability_, xi_hat);
  264. float_type u_bar = u - beta_bar * ( std::pow(1. - threshold_probability_, -xi_hat) - 1.)/xi_hat;
  265. this->fit_parameters_ = boost::make_tuple(u_bar, beta_bar, xi_hat);
  266. }
  267. }
  268. return this->fit_parameters_;
  269. }
  270. // make this accumulator serializeable
  271. // TODO: do we need to split to load/save and verify that threshold did not change?
  272. template<class Archive>
  273. void serialize(Archive & ar, const unsigned int file_version)
  274. {
  275. ar & mu_;
  276. ar & sigma2_;
  277. ar & threshold_probability_;
  278. ar & get<0>(fit_parameters_);
  279. ar & get<1>(fit_parameters_);
  280. ar & get<2>(fit_parameters_);
  281. ar & is_dirty_;
  282. }
  283. private:
  284. mutable float_type mu_; // mean of samples above threshold u
  285. mutable float_type sigma2_; // variance of samples above threshold u
  286. mutable float_type threshold_probability_;
  287. mutable result_type fit_parameters_; // boost::tuple that stores fit parameters
  288. mutable bool is_dirty_;
  289. };
  290. } // namespace impl
  291. ///////////////////////////////////////////////////////////////////////////////
  292. // tag::peaks_over_threshold
  293. //
  294. namespace tag
  295. {
  296. template<typename LeftRight>
  297. struct peaks_over_threshold
  298. : depends_on<count>
  299. , pot_threshold_value
  300. {
  301. /// INTERNAL ONLY
  302. ///
  303. typedef accumulators::impl::peaks_over_threshold_impl<mpl::_1, LeftRight> impl;
  304. };
  305. template<typename LeftRight>
  306. struct peaks_over_threshold_prob
  307. : depends_on<count, tail<LeftRight> >
  308. , pot_threshold_probability
  309. {
  310. /// INTERNAL ONLY
  311. ///
  312. typedef accumulators::impl::peaks_over_threshold_prob_impl<mpl::_1, LeftRight> impl;
  313. };
  314. struct abstract_peaks_over_threshold
  315. : depends_on<>
  316. {
  317. };
  318. }
  319. ///////////////////////////////////////////////////////////////////////////////
  320. // extract::peaks_over_threshold
  321. //
  322. namespace extract
  323. {
  324. extractor<tag::abstract_peaks_over_threshold> const peaks_over_threshold = {};
  325. BOOST_ACCUMULATORS_IGNORE_GLOBAL(peaks_over_threshold)
  326. }
  327. using extract::peaks_over_threshold;
  328. // peaks_over_threshold<LeftRight>(with_threshold_value) -> peaks_over_threshold<LeftRight>
  329. template<typename LeftRight>
  330. struct as_feature<tag::peaks_over_threshold<LeftRight>(with_threshold_value)>
  331. {
  332. typedef tag::peaks_over_threshold<LeftRight> type;
  333. };
  334. // peaks_over_threshold<LeftRight>(with_threshold_probability) -> peaks_over_threshold_prob<LeftRight>
  335. template<typename LeftRight>
  336. struct as_feature<tag::peaks_over_threshold<LeftRight>(with_threshold_probability)>
  337. {
  338. typedef tag::peaks_over_threshold_prob<LeftRight> type;
  339. };
  340. template<typename LeftRight>
  341. struct feature_of<tag::peaks_over_threshold<LeftRight> >
  342. : feature_of<tag::abstract_peaks_over_threshold>
  343. {
  344. };
  345. template<typename LeftRight>
  346. struct feature_of<tag::peaks_over_threshold_prob<LeftRight> >
  347. : feature_of<tag::abstract_peaks_over_threshold>
  348. {
  349. };
  350. // So that peaks_over_threshold can be automatically substituted
  351. // with weighted_peaks_over_threshold when the weight parameter is non-void.
  352. template<typename LeftRight>
  353. struct as_weighted_feature<tag::peaks_over_threshold<LeftRight> >
  354. {
  355. typedef tag::weighted_peaks_over_threshold<LeftRight> type;
  356. };
  357. template<typename LeftRight>
  358. struct feature_of<tag::weighted_peaks_over_threshold<LeftRight> >
  359. : feature_of<tag::peaks_over_threshold<LeftRight> >
  360. {};
  361. // So that peaks_over_threshold_prob can be automatically substituted
  362. // with weighted_peaks_over_threshold_prob when the weight parameter is non-void.
  363. template<typename LeftRight>
  364. struct as_weighted_feature<tag::peaks_over_threshold_prob<LeftRight> >
  365. {
  366. typedef tag::weighted_peaks_over_threshold_prob<LeftRight> type;
  367. };
  368. template<typename LeftRight>
  369. struct feature_of<tag::weighted_peaks_over_threshold_prob<LeftRight> >
  370. : feature_of<tag::peaks_over_threshold_prob<LeftRight> >
  371. {};
  372. }} // namespace boost::accumulators
  373. #ifdef _MSC_VER
  374. # pragma warning(pop)
  375. #endif
  376. #endif