Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // weighted_tail_mean.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_MEAN_HPP_DE_01_01_2006 Chris@16: #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_MEAN_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: 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: /////////////////////////////////////////////////////////////////////////////// Chris@16: // coherent_weighted_tail_mean_impl Chris@16: // Chris@16: // TODO Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // non_coherent_weighted_tail_mean_impl Chris@16: // Chris@16: /** Chris@16: @brief Estimation of the (non-coherent) weighted tail mean based on order statistics (for both left and right tails) Chris@16: Chris@16: Chris@16: Chris@16: An estimation of the non-coherent, weighted tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$ is given by the weighted mean Chris@16: of the 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: smallest samples (left tail) or the weighted mean of the Chris@16: Chris@16: \f[ Chris@16: n + 1 - \rho = n + 1 - \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: largest samples (right tail) above a quantile \f$\hat{q}_{\alpha}\f$ of level \f$\alpha\f$, \f$n\f$ being the total number of sample Chris@16: and \f$\bar{w}_n\f$ the sum of all \f$n\f$ weights: Chris@16: Chris@16: \f[ Chris@16: \widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X) = \frac{\sum_{i=1}^{\lambda} w_i X_{i:n}}{\sum_{i=1}^{\lambda} w_i}, Chris@16: \f] Chris@16: Chris@16: \f[ Chris@16: \widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X) = \frac{\sum_{i=\rho}^n w_i X_{i:n}}{\sum_{i=\rho}^n w_i}. Chris@16: \f] Chris@16: Chris@16: @param quantile_probability Chris@16: */ Chris@16: template Chris@16: struct non_coherent_weighted_tail_mean_impl Chris@16: : accumulator_base Chris@16: { Chris@16: typedef typename numeric::functional::multiplies::result_type weighted_sample; Chris@16: typedef typename numeric::functional::fdiv::result_type float_type; Chris@16: // for boost::result_of Chris@16: typedef typename numeric::functional::fdiv::result_type result_type; Chris@16: Chris@16: non_coherent_weighted_tail_mean_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 result_type(0); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: return numeric::fdiv( Chris@16: std::inner_product( Chris@16: tail(args).begin() Chris@16: , tail(args).begin() + n Chris@16: , tail_weights(args).begin() Chris@16: , weighted_sample(0) Chris@16: ) Chris@16: , sum Chris@16: ); Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace impl Chris@16: Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // tag::non_coherent_weighted_tail_mean<> Chris@16: // Chris@16: namespace tag Chris@16: { Chris@16: template Chris@16: struct non_coherent_weighted_tail_mean Chris@16: : depends_on > Chris@16: { Chris@16: typedef accumulators::impl::non_coherent_weighted_tail_mean_impl impl; Chris@16: }; Chris@16: } Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // extract::non_coherent_weighted_tail_mean; Chris@16: // Chris@16: namespace extract Chris@16: { Chris@16: extractor const non_coherent_weighted_tail_mean = {}; Chris@16: Chris@16: BOOST_ACCUMULATORS_IGNORE_GLOBAL(non_coherent_weighted_tail_mean) Chris@16: } Chris@16: Chris@16: using extract::non_coherent_weighted_tail_mean; 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