Statistics
| Revision:

root / tmp / org.txm.statsengine.r.core.win32 / res / win32 / library / BH / include / boost / accumulators / statistics / weighted_tail_mean.hpp @ 2486

History | View | Annotate | Download (5.7 kB)

1 2486 sjacqu01
///////////////////////////////////////////////////////////////////////////////
2 2486 sjacqu01
// weighted_tail_mean.hpp
3 2486 sjacqu01
//
4 2486 sjacqu01
//  Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
5 2486 sjacqu01
//  Software License, Version 1.0. (See accompanying file
6 2486 sjacqu01
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 2486 sjacqu01
8 2486 sjacqu01
#ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_MEAN_HPP_DE_01_01_2006
9 2486 sjacqu01
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_MEAN_HPP_DE_01_01_2006
10 2486 sjacqu01
11 2486 sjacqu01
#include <numeric>
12 2486 sjacqu01
#include <vector>
13 2486 sjacqu01
#include <limits>
14 2486 sjacqu01
#include <functional>
15 2486 sjacqu01
#include <sstream>
16 2486 sjacqu01
#include <stdexcept>
17 2486 sjacqu01
#include <boost/throw_exception.hpp>
18 2486 sjacqu01
#include <boost/parameter/keyword.hpp>
19 2486 sjacqu01
#include <boost/mpl/placeholders.hpp>
20 2486 sjacqu01
#include <boost/type_traits/is_same.hpp>
21 2486 sjacqu01
#include <boost/accumulators/numeric/functional.hpp>
22 2486 sjacqu01
#include <boost/accumulators/framework/accumulator_base.hpp>
23 2486 sjacqu01
#include <boost/accumulators/framework/extractor.hpp>
24 2486 sjacqu01
#include <boost/accumulators/framework/parameters/sample.hpp>
25 2486 sjacqu01
#include <boost/accumulators/statistics_fwd.hpp>
26 2486 sjacqu01
#include <boost/accumulators/statistics/tail.hpp>
27 2486 sjacqu01
#include <boost/accumulators/statistics/tail_mean.hpp>
28 2486 sjacqu01
#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
29 2486 sjacqu01
30 2486 sjacqu01
#ifdef _MSC_VER
31 2486 sjacqu01
# pragma warning(push)
32 2486 sjacqu01
# pragma warning(disable: 4127) // conditional expression is constant
33 2486 sjacqu01
#endif
34 2486 sjacqu01
35 2486 sjacqu01
namespace boost { namespace accumulators
36 2486 sjacqu01
{
37 2486 sjacqu01
38 2486 sjacqu01
namespace impl
39 2486 sjacqu01
{
40 2486 sjacqu01
41 2486 sjacqu01
    ///////////////////////////////////////////////////////////////////////////////
42 2486 sjacqu01
    // coherent_weighted_tail_mean_impl
43 2486 sjacqu01
    //
44 2486 sjacqu01
    // TODO
45 2486 sjacqu01
46 2486 sjacqu01
    ///////////////////////////////////////////////////////////////////////////////
47 2486 sjacqu01
    // non_coherent_weighted_tail_mean_impl
48 2486 sjacqu01
    //
49 2486 sjacqu01
    /**
50 2486 sjacqu01
        @brief Estimation of the (non-coherent) weighted tail mean based on order statistics (for both left and right tails)
51 2486 sjacqu01

52 2486 sjacqu01

53 2486 sjacqu01

54 2486 sjacqu01
        An estimation of the non-coherent, weighted tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$ is given by the weighted mean
55 2486 sjacqu01
        of the
56 2486 sjacqu01

57 2486 sjacqu01
        \f[
58 2486 sjacqu01
            \lambda = \inf\left\{ l \left| \frac{1}{\bar{w}_n}\sum_{i=1}^{l} w_i \geq \alpha \right. \right\}
59 2486 sjacqu01
        \f]
60 2486 sjacqu01

61 2486 sjacqu01
        smallest samples (left tail) or the weighted mean of the
62 2486 sjacqu01

63 2486 sjacqu01
        \f[
64 2486 sjacqu01
            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\}
65 2486 sjacqu01
        \f]
66 2486 sjacqu01

67 2486 sjacqu01
        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
68 2486 sjacqu01
        and \f$\bar{w}_n\f$ the sum of all \f$n\f$ weights:
69 2486 sjacqu01

70 2486 sjacqu01
        \f[
71 2486 sjacqu01
            \widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X) = \frac{\sum_{i=1}^{\lambda} w_i X_{i:n}}{\sum_{i=1}^{\lambda} w_i},
72 2486 sjacqu01
        \f]
73 2486 sjacqu01

74 2486 sjacqu01
        \f[
75 2486 sjacqu01
            \widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X) = \frac{\sum_{i=\rho}^n w_i X_{i:n}}{\sum_{i=\rho}^n w_i}.
76 2486 sjacqu01
        \f]
77 2486 sjacqu01

78 2486 sjacqu01
        @param quantile_probability
79 2486 sjacqu01
    */
80 2486 sjacqu01
    template<typename Sample, typename Weight, typename LeftRight>
81 2486 sjacqu01
    struct non_coherent_weighted_tail_mean_impl
82 2486 sjacqu01
      : accumulator_base
83 2486 sjacqu01
    {
84 2486 sjacqu01
        typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
85 2486 sjacqu01
        typedef typename numeric::functional::fdiv<Weight, std::size_t>::result_type float_type;
86 2486 sjacqu01
        // for boost::result_of
87 2486 sjacqu01
        typedef typename numeric::functional::fdiv<weighted_sample, std::size_t>::result_type result_type;
88 2486 sjacqu01
89 2486 sjacqu01
        non_coherent_weighted_tail_mean_impl(dont_care) {}
90 2486 sjacqu01
91 2486 sjacqu01
        template<typename Args>
92 2486 sjacqu01
        result_type result(Args const &args) const
93 2486 sjacqu01
        {
94 2486 sjacqu01
            float_type threshold = sum_of_weights(args)
95 2486 sjacqu01
                             * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] );
96 2486 sjacqu01
97 2486 sjacqu01
            std::size_t n = 0;
98 2486 sjacqu01
            Weight sum = Weight(0);
99 2486 sjacqu01
100 2486 sjacqu01
            while (sum < threshold)
101 2486 sjacqu01
            {
102 2486 sjacqu01
                if (n < static_cast<std::size_t>(tail_weights(args).size()))
103 2486 sjacqu01
                {
104 2486 sjacqu01
                    sum += *(tail_weights(args).begin() + n);
105 2486 sjacqu01
                    n++;
106 2486 sjacqu01
                }
107 2486 sjacqu01
                else
108 2486 sjacqu01
                {
109 2486 sjacqu01
                    if (std::numeric_limits<result_type>::has_quiet_NaN)
110 2486 sjacqu01
                    {
111 2486 sjacqu01
                        return std::numeric_limits<result_type>::quiet_NaN();
112 2486 sjacqu01
                    }
113 2486 sjacqu01
                    else
114 2486 sjacqu01
                    {
115 2486 sjacqu01
                        std::ostringstream msg;
116 2486 sjacqu01
                        msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
117 2486 sjacqu01
                        boost::throw_exception(std::runtime_error(msg.str()));
118 2486 sjacqu01
                        return result_type(0);
119 2486 sjacqu01
                    }
120 2486 sjacqu01
                }
121 2486 sjacqu01
            }
122 2486 sjacqu01
123 2486 sjacqu01
            return numeric::fdiv(
124 2486 sjacqu01
                std::inner_product(
125 2486 sjacqu01
                    tail(args).begin()
126 2486 sjacqu01
                  , tail(args).begin() + n
127 2486 sjacqu01
                  , tail_weights(args).begin()
128 2486 sjacqu01
                  , weighted_sample(0)
129 2486 sjacqu01
                )
130 2486 sjacqu01
              , sum
131 2486 sjacqu01
            );
132 2486 sjacqu01
        }
133 2486 sjacqu01
    };
134 2486 sjacqu01
135 2486 sjacqu01
} // namespace impl
136 2486 sjacqu01
137 2486 sjacqu01
138 2486 sjacqu01
///////////////////////////////////////////////////////////////////////////////
139 2486 sjacqu01
// tag::non_coherent_weighted_tail_mean<>
140 2486 sjacqu01
//
141 2486 sjacqu01
namespace tag
142 2486 sjacqu01
{
143 2486 sjacqu01
    template<typename LeftRight>
144 2486 sjacqu01
    struct non_coherent_weighted_tail_mean
145 2486 sjacqu01
      : depends_on<sum_of_weights, tail_weights<LeftRight> >
146 2486 sjacqu01
    {
147 2486 sjacqu01
        typedef accumulators::impl::non_coherent_weighted_tail_mean_impl<mpl::_1, mpl::_2, LeftRight> impl;
148 2486 sjacqu01
    };
149 2486 sjacqu01
}
150 2486 sjacqu01
151 2486 sjacqu01
///////////////////////////////////////////////////////////////////////////////
152 2486 sjacqu01
// extract::non_coherent_weighted_tail_mean;
153 2486 sjacqu01
//
154 2486 sjacqu01
namespace extract
155 2486 sjacqu01
{
156 2486 sjacqu01
    extractor<tag::abstract_non_coherent_tail_mean> const non_coherent_weighted_tail_mean = {};
157 2486 sjacqu01
158 2486 sjacqu01
    BOOST_ACCUMULATORS_IGNORE_GLOBAL(non_coherent_weighted_tail_mean)
159 2486 sjacqu01
}
160 2486 sjacqu01
161 2486 sjacqu01
using extract::non_coherent_weighted_tail_mean;
162 2486 sjacqu01
163 2486 sjacqu01
}} // namespace boost::accumulators
164 2486 sjacqu01
165 2486 sjacqu01
#ifdef _MSC_VER
166 2486 sjacqu01
# pragma warning(pop)
167 2486 sjacqu01
#endif
168 2486 sjacqu01
169 2486 sjacqu01
#endif