PageRenderTime 18ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/accumulators/statistics/weighted_tail_quantile.hpp

http://hadesmem.googlecode.com/
C++ Header | 146 lines | 89 code | 17 blank | 40 comment | 3 complexity | 164e99c4ba2a92a048ddb0eb93e8132d MD5 | raw file
  1///////////////////////////////////////////////////////////////////////////////
  2// weighted_tail_quantile.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
  8#ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_QUANTILE_HPP_DE_01_01_2006
  9#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_QUANTILE_HPP_DE_01_01_2006
 10
 11#include <vector>
 12#include <limits>
 13#include <functional>
 14#include <sstream>
 15#include <stdexcept>
 16#include <boost/throw_exception.hpp>
 17#include <boost/parameter/keyword.hpp>
 18#include <boost/mpl/placeholders.hpp>
 19#include <boost/mpl/if.hpp>
 20#include <boost/type_traits/is_same.hpp>
 21#include <boost/accumulators/numeric/functional.hpp>
 22#include <boost/accumulators/framework/depends_on.hpp>
 23#include <boost/accumulators/framework/accumulator_base.hpp>
 24#include <boost/accumulators/framework/extractor.hpp>
 25#include <boost/accumulators/framework/parameters/sample.hpp>
 26#include <boost/accumulators/statistics_fwd.hpp>
 27#include <boost/accumulators/statistics/tail.hpp>
 28#include <boost/accumulators/statistics/tail_quantile.hpp>
 29#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
 30
 31#ifdef _MSC_VER
 32# pragma warning(push)
 33# pragma warning(disable: 4127) // conditional expression is constant
 34#endif
 35
 36namespace boost { namespace accumulators
 37{
 38
 39namespace impl
 40{
 41    ///////////////////////////////////////////////////////////////////////////////
 42    // weighted_tail_quantile_impl
 43    //  Tail quantile estimation based on order statistics of weighted samples
 44    /**
 45        @brief Tail quantile estimation based on order statistics of weighted samples (for both left and right tails)
 46
 47        An estimator \f$\hat{q}\f$ of tail quantiles with level \f$\alpha\f$ based on order statistics
 48        \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)
 49        and \f$X_{\rho:n}\f$ (right tail), where
 50
 51            \f[
 52                \lambda = \inf\left\{ l \left| \frac{1}{\bar{w}_n}\sum_{i=1}^{l} w_i \geq \alpha \right. \right\}
 53            \f]
 54
 55        and
 56
 57            \f[
 58                \rho = \sup\left\{ r \left| \frac{1}{\bar{w}_n}\sum_{i=r}^{n} w_i \geq (1 - \alpha) \right. \right\},
 59            \f]
 60
 61        \f$n\f$ being the number of samples and \f$\bar{w}_n\f$ the sum of all weights.
 62
 63        @param quantile_probability
 64    */
 65    template<typename Sample, typename Weight, typename LeftRight>
 66    struct weighted_tail_quantile_impl
 67      : accumulator_base
 68    {
 69        typedef typename numeric::functional::average<Weight, std::size_t>::result_type float_type;
 70        // for boost::result_of
 71        typedef Sample result_type;
 72
 73        weighted_tail_quantile_impl(dont_care) {}
 74
 75        template<typename Args>
 76        result_type result(Args const &args) const
 77        {
 78            float_type threshold = sum_of_weights(args)
 79                             * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] );
 80
 81            std::size_t n = 0;
 82            Weight sum = Weight(0);
 83
 84            while (sum < threshold)
 85            {
 86                if (n < static_cast<std::size_t>(tail_weights(args).size()))
 87                {
 88                    sum += *(tail_weights(args).begin() + n);
 89                    n++;
 90                }
 91                else
 92                {
 93                    if (std::numeric_limits<result_type>::has_quiet_NaN)
 94                    {
 95                        return std::numeric_limits<result_type>::quiet_NaN();
 96                    }
 97                    else
 98                    {
 99                        std::ostringstream msg;
100                        msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
101                        boost::throw_exception(std::runtime_error(msg.str()));
102                        return Sample(0);
103                    }
104                }
105            }
106
107            // Note that the cached samples of the left are sorted in ascending order,
108            // whereas the samples of the right tail are sorted in descending order
109            return *(boost::begin(tail(args)) + n - 1);
110        }
111    };
112} // namespace impl
113
114///////////////////////////////////////////////////////////////////////////////
115// tag::weighted_tail_quantile<>
116//
117namespace tag
118{
119    template<typename LeftRight>
120    struct weighted_tail_quantile
121      : depends_on<sum_of_weights, tail_weights<LeftRight> >
122    {
123        /// INTERNAL ONLY
124        typedef accumulators::impl::weighted_tail_quantile_impl<mpl::_1, mpl::_2, LeftRight> impl;
125    };
126}
127
128///////////////////////////////////////////////////////////////////////////////
129// extract::weighted_tail_quantile
130//
131namespace extract
132{
133    extractor<tag::quantile> const weighted_tail_quantile = {};
134
135    BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_tail_quantile)
136}
137
138using extract::weighted_tail_quantile;
139
140}} // namespace boost::accumulators
141
142#ifdef _MSC_VER
143# pragma warning(pop)
144#endif
145
146#endif