peaks_over_threshold.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 400 行 · 第 1/2 页

HPP
400
字号
///////////////////////////////////////////////////////////////////////////////// peaks_over_threshold.hpp////  Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost//  Software License, Version 1.0. (See accompanying file//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)#ifndef BOOST_ACCUMULATORS_STATISTICS_PEAKS_OVER_THRESHOLD_HPP_DE_01_01_2006#define BOOST_ACCUMULATORS_STATISTICS_PEAKS_OVER_THRESHOLD_HPP_DE_01_01_2006#include <vector>#include <limits>#include <numeric>#include <functional>#include <boost/config/no_tr1/cmath.hpp> // pow#include <sstream> // stringstream#include <stdexcept> // runtime_error#include <boost/throw_exception.hpp>#include <boost/range.hpp>#include <boost/mpl/if.hpp>#include <boost/mpl/int.hpp>#include <boost/mpl/placeholders.hpp>#include <boost/parameter/keyword.hpp>#include <boost/tuple/tuple.hpp>#include <boost/accumulators/framework/accumulator_base.hpp>#include <boost/accumulators/framework/extractor.hpp>#include <boost/accumulators/numeric/functional.hpp>#include <boost/accumulators/framework/parameters/sample.hpp>#include <boost/accumulators/framework/depends_on.hpp>#include <boost/accumulators/statistics_fwd.hpp>#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>#include <boost/accumulators/statistics/count.hpp>#include <boost/accumulators/statistics/tail.hpp>#ifdef _MSC_VER# pragma warning(push)# pragma warning(disable: 4127) // conditional expression is constant#endifnamespace boost { namespace accumulators{///////////////////////////////////////////////////////////////////////////////// threshold_probability and threshold named parameters//BOOST_PARAMETER_NESTED_KEYWORD(tag, pot_threshold_value, threshold_value)BOOST_PARAMETER_NESTED_KEYWORD(tag, pot_threshold_probability, threshold_probability)namespace impl{    ///////////////////////////////////////////////////////////////////////////////    // peaks_over_threshold_impl    //  works with an explicit threshold value and does not depend on order statistics    /**        @brief Peaks over Threshold Method for Quantile and Tail Mean Estimation        According to the theorem of Pickands-Balkema-de Haan, the distribution function \f$F_u(x)\f$ of        the excesses \f$x\f$ over some sufficiently high threshold \f$u\f$ of a distribution function \f$F(x)\f$        may be approximated by a generalized Pareto distribution        \f[            G_{\xi,\beta}(x) =            \left\{            \begin{array}{ll}                \beta^{-1}\left(1+\frac{\xi x}{\beta}\right)^{-1/\xi-1} & \textrm{if }\xi\neq0\\                \beta^{-1}\exp\left(-\frac{x}{\beta}\right) & \textrm{if }\xi=0,            \end{array}            \right.        \f]        with suitable parameters \f$\xi\f$ and \f$\beta\f$ that can be estimated, e.g., with the method of moments, cf.        Hosking and Wallis (1987),        \f[            \begin{array}{lll}            \hat{\xi} & = & \frac{1}{2}\left[1-\frac{(\hat{\mu}-u)^2}{\hat{\sigma}^2}\right]\\            \hat{\beta} & = & \frac{\hat{\mu}-u}{2}\left[\frac{(\hat{\mu}-u)^2}{\hat{\sigma}^2}+1\right],            \end{array}        \f]        \f$\hat{\mu}\f$ and \f$\hat{\sigma}^2\f$ being the empirical mean and variance of the samples over        the threshold \f$u\f$. Equivalently, the distribution function        \f$F_u(x-u)\f$ of the exceedances \f$x-u\f$ can be approximated by        \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$        can be written as        \f[            F(x) = [1 - \P(X \leq u)]F_u(x - u) + \P(X \leq u)        \f]        and the probability \f$\P(X \leq u)\f$ can be approximated by the empirical distribution function        \f$F_n(u)\f$ evaluated at \f$u\f$, an estimator of \f$F(x)\f$ is given by        \f[            \widehat{F}(x) = [1 - F_n(u)]G_{\xi,\beta,u}(x) + F_n(u).        \f]        It can be shown that \f$\widehat{F}(x)\f$ is a generalized        Pareto distribution \f$G_{\xi,\bar{\beta},\bar{u}}(x)\f$ with \f$\bar{\beta}=\beta[1-F_n(u)]^{\xi}\f$        and \f$\bar{u}=u-\bar{\beta}\left\{[1-F_n(u)]^{-\xi}-1\right\}/\xi\f$. By inverting \f$\widehat{F}(x)\f$,        one obtains an estimator for the \f$\alpha\f$-quantile,        \f[            \hat{q}_{\alpha} = \bar{u} + \frac{\bar{\beta}}{\xi}\left[(1-\alpha)^{-\xi}-1\right],        \f]        and similarly an estimator for the (coherent) tail mean,        \f[            \widehat{CTM}_{\alpha} = \hat{q}_{\alpha} - \frac{\bar{\beta}}{\xi-1}(1-\alpha)^{-\xi},        \f]        cf. McNeil and Frey (2000).        Note that in case extreme values of the left tail are fitted, the distribution is mirrored with respect to the        \f$y\f$ axis such that the left tail can be treated as a right tail. The computed fit parameters thus define        the Pareto distribution that fits the mirrored left tail. When quantities like a quantile or a tail mean are        computed using the fit parameters obtained from the mirrored data, the result is mirrored back, yielding the        correct result.        For further details, see        J. R. M. Hosking and J. R. Wallis, Parameter and quantile estimation for the generalized Pareto distribution,        Technometrics, Volume 29, 1987, p. 339-349        A. J. McNeil and R. Frey, Estimation of Tail-Related Risk Measures for Heteroscedastic Financial Time Series:        an Extreme Value Approach, Journal of Empirical Finance, Volume 7, 2000, p. 271-300        @param quantile_probability        @param pot_threshold_value    */    template<typename Sample, typename LeftRight>    struct peaks_over_threshold_impl      : accumulator_base    {        typedef typename numeric::functional::average<Sample, std::size_t>::result_type float_type;        // for boost::result_of        typedef boost::tuple<float_type, float_type, float_type> result_type;        // for left tail fitting, mirror the extreme values        typedef mpl::int_<is_same<LeftRight, left>::value ? -1 : 1> sign;        template<typename Args>        peaks_over_threshold_impl(Args const &args)          : Nu_(0)          , mu_(sign::value * numeric::average(args[sample | Sample()], (std::size_t)1))          , sigma2_(numeric::average(args[sample | Sample()], (std::size_t)1))          , threshold_(sign::value * args[pot_threshold_value])          , fit_parameters_(boost::make_tuple(0., 0., 0.))          , is_dirty_(true)        {        }        template<typename Args>        void operator ()(Args const &args)        {            this->is_dirty_ = true;            if (sign::value * args[sample] > this->threshold_)            {                this->mu_ += args[sample];                this->sigma2_ += args[sample] * args[sample];                ++this->Nu_;            }        }        template<typename Args>        result_type result(Args const &args) const        {            if (this->is_dirty_)            {                this->is_dirty_ = false;                std::size_t cnt = count(args);                this->mu_ = sign::value * numeric::average(this->mu_, this->Nu_);                this->sigma2_ = numeric::average(this->sigma2_, this->Nu_);                this->sigma2_ -= this->mu_ * this->mu_;                float_type threshold_probability = numeric::average(cnt - this->Nu_, cnt);                float_type tmp = numeric::average(( this->mu_ - this->threshold_ )*( this->mu_ - this->threshold_ ), this->sigma2_);                float_type xi_hat = 0.5 * ( 1. - tmp );                float_type beta_hat = 0.5 * ( this->mu_ - this->threshold_ ) * ( 1. + tmp );                float_type beta_bar = beta_hat * std::pow(1. - threshold_probability, xi_hat);                float_type u_bar = this->threshold_ - beta_bar * ( std::pow(1. - threshold_probability, -xi_hat) - 1.)/xi_hat;                this->fit_parameters_ = boost::make_tuple(u_bar, beta_bar, xi_hat);            }            return this->fit_parameters_;        }    private:        std::size_t Nu_;                     // number of samples larger than threshold        mutable float_type mu_;              // mean of Nu_ largest samples        mutable float_type sigma2_;          // variance of Nu_ largest samples        float_type threshold_;        mutable result_type fit_parameters_; // boost::tuple that stores fit parameters        mutable bool is_dirty_;    };    ///////////////////////////////////////////////////////////////////////////////    // peaks_over_threshold_prob_impl    //  determines threshold from a given threshold probability using order statistics    /**        @brief Peaks over Threshold Method for Quantile and Tail Mean Estimation        @sa peaks_over_threshold_impl        @param quantile_probability        @param pot_threshold_probability    */    template<typename Sample, typename LeftRight>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?