Statistics
| Revision:

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

History | View | Annotate | Download (8.6 kB)

1 2486 sjacqu01
///////////////////////////////////////////////////////////////////////////////
2 2486 sjacqu01
// 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_TAIL_MEAN_HPP_DE_01_01_2006
9 2486 sjacqu01
#define BOOST_ACCUMULATORS_STATISTICS_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/framework/accumulator_base.hpp>
22 2486 sjacqu01
#include <boost/accumulators/framework/extractor.hpp>
23 2486 sjacqu01
#include <boost/accumulators/numeric/functional.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/count.hpp>
27 2486 sjacqu01
#include <boost/accumulators/statistics/tail.hpp>
28 2486 sjacqu01
#include <boost/accumulators/statistics/tail_quantile.hpp>
29 2486 sjacqu01
#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
30 2486 sjacqu01
31 2486 sjacqu01
#ifdef _MSC_VER
32 2486 sjacqu01
# pragma warning(push)
33 2486 sjacqu01
# pragma warning(disable: 4127) // conditional expression is constant
34 2486 sjacqu01
#endif
35 2486 sjacqu01
36 2486 sjacqu01
namespace boost { namespace accumulators
37 2486 sjacqu01
{
38 2486 sjacqu01
39 2486 sjacqu01
namespace impl
40 2486 sjacqu01
{
41 2486 sjacqu01
42 2486 sjacqu01
    ///////////////////////////////////////////////////////////////////////////////
43 2486 sjacqu01
    // coherent_tail_mean_impl
44 2486 sjacqu01
    //
45 2486 sjacqu01
    /**
46 2486 sjacqu01
        @brief Estimation of the coherent tail mean based on order statistics (for both left and right tails)
47 2486 sjacqu01

48 2486 sjacqu01
        The coherent tail mean \f$\widehat{CTM}_{n,\alpha}(X)\f$ is equal to the non-coherent tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$
49 2486 sjacqu01
        plus a correction term that ensures coherence in case of non-continuous distributions.
50 2486 sjacqu01

51 2486 sjacqu01
        \f[
52 2486 sjacqu01
            \widehat{CTM}_{n,\alpha}^{\mathrm{right}}(X) = \widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X) +
53 2486 sjacqu01
            \frac{1}{\lceil n(1-\alpha)\rceil}\hat{q}_{n,\alpha}(X)\left(1 - \alpha - \frac{1}{n}\lceil n(1-\alpha)\rceil \right)
54 2486 sjacqu01
        \f]
55 2486 sjacqu01

56 2486 sjacqu01
        \f[
57 2486 sjacqu01
            \widehat{CTM}_{n,\alpha}^{\mathrm{left}}(X) = \widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X) +
58 2486 sjacqu01
            \frac{1}{\lceil n\alpha\rceil}\hat{q}_{n,\alpha}(X)\left(\alpha - \frac{1}{n}\lceil n\alpha\rceil \right)
59 2486 sjacqu01
        \f]
60 2486 sjacqu01
    */
61 2486 sjacqu01
    template<typename Sample, typename LeftRight>
62 2486 sjacqu01
    struct coherent_tail_mean_impl
63 2486 sjacqu01
      : accumulator_base
64 2486 sjacqu01
    {
65 2486 sjacqu01
        typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
66 2486 sjacqu01
        // for boost::result_of
67 2486 sjacqu01
        typedef float_type result_type;
68 2486 sjacqu01
69 2486 sjacqu01
        coherent_tail_mean_impl(dont_care) {}
70 2486 sjacqu01
71 2486 sjacqu01
        template<typename Args>
72 2486 sjacqu01
        result_type result(Args const &args) const
73 2486 sjacqu01
        {
74 2486 sjacqu01
            std::size_t cnt = count(args);
75 2486 sjacqu01
76 2486 sjacqu01
            std::size_t n = static_cast<std::size_t>(
77 2486 sjacqu01
                std::ceil(
78 2486 sjacqu01
                    cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
79 2486 sjacqu01
                )
80 2486 sjacqu01
            );
81 2486 sjacqu01
82 2486 sjacqu01
            extractor<tag::non_coherent_tail_mean<LeftRight> > const some_non_coherent_tail_mean = {};
83 2486 sjacqu01
84 2486 sjacqu01
            return some_non_coherent_tail_mean(args)
85 2486 sjacqu01
                 + numeric::fdiv(quantile(args), n)
86 2486 sjacqu01
                 * (
87 2486 sjacqu01
                     ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability]
88 2486 sjacqu01
                     - numeric::fdiv(n, count(args))
89 2486 sjacqu01
                   );
90 2486 sjacqu01
        }
91 2486 sjacqu01
    };
92 2486 sjacqu01
93 2486 sjacqu01
    ///////////////////////////////////////////////////////////////////////////////
94 2486 sjacqu01
    // non_coherent_tail_mean_impl
95 2486 sjacqu01
    //
96 2486 sjacqu01
    /**
97 2486 sjacqu01
        @brief Estimation of the (non-coherent) tail mean based on order statistics (for both left and right tails)
98 2486 sjacqu01

99 2486 sjacqu01
        An estimation of the non-coherent tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$ is given by the mean of the
100 2486 sjacqu01
        \f$\lceil n\alpha\rceil\f$ smallest samples (left tail) or the mean of the  \f$\lceil n(1-\alpha)\rceil\f$
101 2486 sjacqu01
        largest samples (right tail), \f$n\f$ being the total number of samples and \f$\alpha\f$ the quantile level:
102 2486 sjacqu01

103 2486 sjacqu01
        \f[
104 2486 sjacqu01
            \widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X) = \frac{1}{\lceil n(1-\alpha)\rceil} \sum_{i=\lceil \alpha n \rceil}^n X_{i:n}
105 2486 sjacqu01
        \f]
106 2486 sjacqu01

107 2486 sjacqu01
        \f[
108 2486 sjacqu01
            \widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X) = \frac{1}{\lceil n\alpha\rceil} \sum_{i=1}^{\lceil \alpha n \rceil} X_{i:n}
109 2486 sjacqu01
        \f]
110 2486 sjacqu01

111 2486 sjacqu01
        It thus requires the caching of at least the \f$\lceil n\alpha\rceil\f$ smallest or the \f$\lceil n(1-\alpha)\rceil\f$
112 2486 sjacqu01
        largest samples.
113 2486 sjacqu01

114 2486 sjacqu01
        @param quantile_probability
115 2486 sjacqu01
    */
116 2486 sjacqu01
    template<typename Sample, typename LeftRight>
117 2486 sjacqu01
    struct non_coherent_tail_mean_impl
118 2486 sjacqu01
      : accumulator_base
119 2486 sjacqu01
    {
120 2486 sjacqu01
        typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
121 2486 sjacqu01
        // for boost::result_of
122 2486 sjacqu01
        typedef float_type result_type;
123 2486 sjacqu01
124 2486 sjacqu01
        non_coherent_tail_mean_impl(dont_care) {}
125 2486 sjacqu01
126 2486 sjacqu01
        template<typename Args>
127 2486 sjacqu01
        result_type result(Args const &args) const
128 2486 sjacqu01
        {
129 2486 sjacqu01
            std::size_t cnt = count(args);
130 2486 sjacqu01
131 2486 sjacqu01
            std::size_t n = static_cast<std::size_t>(
132 2486 sjacqu01
                std::ceil(
133 2486 sjacqu01
                    cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
134 2486 sjacqu01
                )
135 2486 sjacqu01
            );
136 2486 sjacqu01
137 2486 sjacqu01
            // If n is in a valid range, return result, otherwise return NaN or throw exception
138 2486 sjacqu01
            if (n <= static_cast<std::size_t>(tail(args).size()))
139 2486 sjacqu01
                return numeric::fdiv(
140 2486 sjacqu01
                    std::accumulate(
141 2486 sjacqu01
                        tail(args).begin()
142 2486 sjacqu01
                      , tail(args).begin() + n
143 2486 sjacqu01
                      , Sample(0)
144 2486 sjacqu01
                    )
145 2486 sjacqu01
                  , n
146 2486 sjacqu01
                );
147 2486 sjacqu01
            else
148 2486 sjacqu01
            {
149 2486 sjacqu01
                if (std::numeric_limits<result_type>::has_quiet_NaN)
150 2486 sjacqu01
                {
151 2486 sjacqu01
                    return std::numeric_limits<result_type>::quiet_NaN();
152 2486 sjacqu01
                }
153 2486 sjacqu01
                else
154 2486 sjacqu01
                {
155 2486 sjacqu01
                    std::ostringstream msg;
156 2486 sjacqu01
                    msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
157 2486 sjacqu01
                    boost::throw_exception(std::runtime_error(msg.str()));
158 2486 sjacqu01
                    return Sample(0);
159 2486 sjacqu01
                }
160 2486 sjacqu01
            }
161 2486 sjacqu01
        }
162 2486 sjacqu01
    };
163 2486 sjacqu01
164 2486 sjacqu01
} // namespace impl
165 2486 sjacqu01
166 2486 sjacqu01
167 2486 sjacqu01
///////////////////////////////////////////////////////////////////////////////
168 2486 sjacqu01
// tag::coherent_tail_mean<>
169 2486 sjacqu01
// tag::non_coherent_tail_mean<>
170 2486 sjacqu01
//
171 2486 sjacqu01
namespace tag
172 2486 sjacqu01
{
173 2486 sjacqu01
    template<typename LeftRight>
174 2486 sjacqu01
    struct coherent_tail_mean
175 2486 sjacqu01
      : depends_on<count, quantile, non_coherent_tail_mean<LeftRight> >
176 2486 sjacqu01
    {
177 2486 sjacqu01
        typedef accumulators::impl::coherent_tail_mean_impl<mpl::_1, LeftRight> impl;
178 2486 sjacqu01
    };
179 2486 sjacqu01
180 2486 sjacqu01
    template<typename LeftRight>
181 2486 sjacqu01
    struct non_coherent_tail_mean
182 2486 sjacqu01
      : depends_on<count, tail<LeftRight> >
183 2486 sjacqu01
    {
184 2486 sjacqu01
        typedef accumulators::impl::non_coherent_tail_mean_impl<mpl::_1, LeftRight> impl;
185 2486 sjacqu01
    };
186 2486 sjacqu01
187 2486 sjacqu01
    struct abstract_non_coherent_tail_mean
188 2486 sjacqu01
      : depends_on<>
189 2486 sjacqu01
    {
190 2486 sjacqu01
    };
191 2486 sjacqu01
}
192 2486 sjacqu01
193 2486 sjacqu01
///////////////////////////////////////////////////////////////////////////////
194 2486 sjacqu01
// extract::non_coherent_tail_mean;
195 2486 sjacqu01
// extract::coherent_tail_mean;
196 2486 sjacqu01
//
197 2486 sjacqu01
namespace extract
198 2486 sjacqu01
{
199 2486 sjacqu01
    extractor<tag::abstract_non_coherent_tail_mean> const non_coherent_tail_mean = {};
200 2486 sjacqu01
    extractor<tag::tail_mean> const coherent_tail_mean = {};
201 2486 sjacqu01
202 2486 sjacqu01
    BOOST_ACCUMULATORS_IGNORE_GLOBAL(non_coherent_tail_mean)
203 2486 sjacqu01
    BOOST_ACCUMULATORS_IGNORE_GLOBAL(coherent_tail_mean)
204 2486 sjacqu01
}
205 2486 sjacqu01
206 2486 sjacqu01
using extract::non_coherent_tail_mean;
207 2486 sjacqu01
using extract::coherent_tail_mean;
208 2486 sjacqu01
209 2486 sjacqu01
// for the purposes of feature-based dependency resolution,
210 2486 sjacqu01
// coherent_tail_mean<LeftRight> provides the same feature as tail_mean
211 2486 sjacqu01
template<typename LeftRight>
212 2486 sjacqu01
struct feature_of<tag::coherent_tail_mean<LeftRight> >
213 2486 sjacqu01
  : feature_of<tag::tail_mean>
214 2486 sjacqu01
{
215 2486 sjacqu01
};
216 2486 sjacqu01
217 2486 sjacqu01
template<typename LeftRight>
218 2486 sjacqu01
struct feature_of<tag::non_coherent_tail_mean<LeftRight> >
219 2486 sjacqu01
  : feature_of<tag::abstract_non_coherent_tail_mean>
220 2486 sjacqu01
{
221 2486 sjacqu01
};
222 2486 sjacqu01
223 2486 sjacqu01
// So that non_coherent_tail_mean can be automatically substituted
224 2486 sjacqu01
// with weighted_non_coherent_tail_mean when the weight parameter is non-void.
225 2486 sjacqu01
template<typename LeftRight>
226 2486 sjacqu01
struct as_weighted_feature<tag::non_coherent_tail_mean<LeftRight> >
227 2486 sjacqu01
{
228 2486 sjacqu01
    typedef tag::non_coherent_weighted_tail_mean<LeftRight> type;
229 2486 sjacqu01
};
230 2486 sjacqu01
231 2486 sjacqu01
template<typename LeftRight>
232 2486 sjacqu01
struct feature_of<tag::non_coherent_weighted_tail_mean<LeftRight> >
233 2486 sjacqu01
  : feature_of<tag::non_coherent_tail_mean<LeftRight> >
234 2486 sjacqu01
{};
235 2486 sjacqu01
236 2486 sjacqu01
// NOTE that non_coherent_tail_mean cannot be feature-grouped with tail_mean,
237 2486 sjacqu01
// which is the base feature for coherent tail means, since (at least for
238 2486 sjacqu01
// non-continuous distributions) non_coherent_tail_mean is a different measure!
239 2486 sjacqu01
240 2486 sjacqu01
}} // namespace boost::accumulators
241 2486 sjacqu01
242 2486 sjacqu01
#ifdef _MSC_VER
243 2486 sjacqu01
# pragma warning(pop)
244 2486 sjacqu01
#endif
245 2486 sjacqu01
246 2486 sjacqu01
#endif