Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // weighted_tail_quantile.hpp Chris@16: // Chris@16: // Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_QUANTILE_HPP_DE_01_01_2006 Chris@16: #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_QUANTILE_HPP_DE_01_01_2006 Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: # pragma warning(push) Chris@16: # pragma warning(disable: 4127) // conditional expression is constant Chris@16: #endif Chris@16: Chris@16: namespace boost { namespace accumulators Chris@16: { Chris@16: Chris@16: namespace impl Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // weighted_tail_quantile_impl Chris@16: // Tail quantile estimation based on order statistics of weighted samples Chris@16: /** Chris@16: @brief Tail quantile estimation based on order statistics of weighted samples (for both left and right tails) Chris@16: Chris@16: An estimator \f$\hat{q}\f$ of tail quantiles with level \f$\alpha\f$ based on order statistics Chris@16: \f$X_{1:n} \leq X_{2:n} \leq\dots\leq X_{n:n}\f$ of weighted samples are given by \f$X_{\lambda:n}\f$ (left tail) Chris@16: and \f$X_{\rho:n}\f$ (right tail), where Chris@16: Chris@16: \f[ Chris@16: \lambda = \inf\left\{ l \left| \frac{1}{\bar{w}_n}\sum_{i=1}^{l} w_i \geq \alpha \right. \right\} Chris@16: \f] Chris@16: Chris@16: and Chris@16: Chris@16: \f[ Chris@16: \rho = \sup\left\{ r \left| \frac{1}{\bar{w}_n}\sum_{i=r}^{n} w_i \geq (1 - \alpha) \right. \right\}, Chris@16: \f] Chris@16: Chris@16: \f$n\f$ being the number of samples and \f$\bar{w}_n\f$ the sum of all weights. Chris@16: Chris@16: @param quantile_probability Chris@16: */ Chris@16: template Chris@16: struct weighted_tail_quantile_impl Chris@16: : accumulator_base Chris@16: { Chris@16: typedef typename numeric::functional::fdiv::result_type float_type; Chris@16: // for boost::result_of Chris@16: typedef Sample result_type; Chris@16: Chris@16: weighted_tail_quantile_impl(dont_care) {} Chris@16: Chris@16: template Chris@16: result_type result(Args const &args) const Chris@16: { Chris@16: float_type threshold = sum_of_weights(args) Chris@16: * ( ( is_same::value ) ? args[quantile_probability] : 1. - args[quantile_probability] ); Chris@16: Chris@16: std::size_t n = 0; Chris@16: Weight sum = Weight(0); Chris@16: Chris@16: while (sum < threshold) Chris@16: { Chris@16: if (n < static_cast(tail_weights(args).size())) Chris@16: { Chris@16: sum += *(tail_weights(args).begin() + n); Chris@16: n++; Chris@16: } Chris@16: else Chris@16: { Chris@16: if (std::numeric_limits::has_quiet_NaN) Chris@16: { Chris@16: return std::numeric_limits::quiet_NaN(); Chris@16: } Chris@16: else Chris@16: { Chris@16: std::ostringstream msg; Chris@16: msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")"; Chris@16: boost::throw_exception(std::runtime_error(msg.str())); Chris@16: return Sample(0); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: // Note that the cached samples of the left are sorted in ascending order, Chris@16: // whereas the samples of the right tail are sorted in descending order Chris@16: return *(boost::begin(tail(args)) + n - 1); Chris@16: } Chris@16: }; Chris@16: } // namespace impl Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // tag::weighted_tail_quantile<> Chris@16: // Chris@16: namespace tag Chris@16: { Chris@16: template Chris@16: struct weighted_tail_quantile Chris@16: : depends_on > Chris@16: { Chris@16: /// INTERNAL ONLY Chris@16: typedef accumulators::impl::weighted_tail_quantile_impl impl; Chris@16: }; Chris@16: } Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // extract::weighted_tail_quantile Chris@16: // Chris@16: namespace extract Chris@16: { Chris@16: extractor const weighted_tail_quantile = {}; Chris@16: Chris@16: BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_tail_quantile) Chris@16: } Chris@16: Chris@16: using extract::weighted_tail_quantile; Chris@16: Chris@16: }} // namespace boost::accumulators Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: # pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: #endif