Statistics
| Revision:

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

History | View | Annotate | Download (4.9 kB)

1
///////////////////////////////////////////////////////////////////////////////
2
// weighted_covariance.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_COVARIANCE_HPP_DE_01_01_2006
9
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_COVARIANCE_HPP_DE_01_01_2006
10

    
11
#include <vector>
12
#include <limits>
13
#include <numeric>
14
#include <functional>
15
#include <complex>
16
#include <boost/mpl/assert.hpp>
17
#include <boost/mpl/bool.hpp>
18
#include <boost/range.hpp>
19
#include <boost/parameter/keyword.hpp>
20
#include <boost/mpl/placeholders.hpp>
21
#include <boost/numeric/ublas/io.hpp>
22
#include <boost/numeric/ublas/matrix.hpp>
23
#include <boost/type_traits/is_scalar.hpp>
24
#include <boost/type_traits/is_same.hpp>
25
#include <boost/accumulators/framework/accumulator_base.hpp>
26
#include <boost/accumulators/framework/extractor.hpp>
27
#include <boost/accumulators/numeric/functional.hpp>
28
#include <boost/accumulators/framework/parameters/sample.hpp>
29
#include <boost/accumulators/statistics_fwd.hpp>
30
#include <boost/accumulators/statistics/count.hpp>
31
#include <boost/accumulators/statistics/covariance.hpp> // for numeric::outer_product() and type traits
32
#include <boost/accumulators/statistics/weighted_mean.hpp>
33

    
34
namespace boost { namespace accumulators
35
{
36

    
37
namespace impl
38
{
39
    ///////////////////////////////////////////////////////////////////////////////
40
    // weighted_covariance_impl
41
    //
42
    /**
43
        @brief Weighted Covariance Estimator
44

45
        An iterative Monte Carlo estimator for the weighted covariance \f$\mathrm{Cov}(X,X')\f$, where \f$X\f$ is a sample
46
        and \f$X'\f$ a variate, is given by:
47

48
        \f[
49
            \hat{c}_n = \frac{\bar{w}_n-w_n}{\bar{w}_n} \hat{c}_{n-1} + \frac{w_n}{\bar{w}_n-w_n}(X_n - \hat{\mu}_n)(X_n' - \hat{\mu}_n'),
50
            \quad n\ge2,\quad\hat{c}_1 = 0,
51
        \f]
52

53
        \f$\hat{\mu}_n\f$ and \f$\hat{\mu}_n'\f$ being the weighted means of the samples and variates and
54
        \f$\bar{w}_n\f$ the sum of the \f$n\f$ first weights \f$w_i\f$.
55
    */
56
    template<typename Sample, typename Weight, typename VariateType, typename VariateTag>
57
    struct weighted_covariance_impl
58
      : accumulator_base
59
    {
60
        typedef typename numeric::functional::multiplies<Weight, typename numeric::functional::fdiv<Sample, std::size_t>::result_type>::result_type weighted_sample_type;
61
        typedef typename numeric::functional::multiplies<Weight, typename numeric::functional::fdiv<VariateType, std::size_t>::result_type>::result_type weighted_variate_type;
62
        // for boost::result_of
63
        typedef typename numeric::functional::outer_product<weighted_sample_type, weighted_variate_type>::result_type result_type;
64

    
65
        template<typename Args>
66
        weighted_covariance_impl(Args const &args)
67
          : cov_(
68
                numeric::outer_product(
69
                    numeric::fdiv(args[sample | Sample()], (std::size_t)1)
70
                      * numeric::one<Weight>::value
71
                  , numeric::fdiv(args[parameter::keyword<VariateTag>::get() | VariateType()], (std::size_t)1)
72
                      * numeric::one<Weight>::value
73
                )
74
            )
75
        {
76
        }
77

    
78
        template<typename Args>
79
        void operator ()(Args const &args)
80
        {
81
            std::size_t cnt = count(args);
82

    
83
            if (cnt > 1)
84
            {
85
                extractor<tag::weighted_mean_of_variates<VariateType, VariateTag> > const some_weighted_mean_of_variates = {};
86

    
87
                this->cov_ = this->cov_ * (sum_of_weights(args) - args[weight]) / sum_of_weights(args)
88
                           + numeric::outer_product(
89
                                 some_weighted_mean_of_variates(args) - args[parameter::keyword<VariateTag>::get()]
90
                               , weighted_mean(args) - args[sample]
91
                             ) * args[weight] / (sum_of_weights(args) - args[weight]);
92
            }
93
        }
94

    
95
        result_type result(dont_care) const
96
        {
97
            return this->cov_;
98
        }
99

    
100
    private:
101
        result_type cov_;
102
    };
103

    
104
} // namespace impl
105

    
106
///////////////////////////////////////////////////////////////////////////////
107
// tag::weighted_covariance
108
//
109
namespace tag
110
{
111
    template<typename VariateType, typename VariateTag>
112
    struct weighted_covariance
113
      : depends_on<count, sum_of_weights, weighted_mean, weighted_mean_of_variates<VariateType, VariateTag> >
114
    {
115
        typedef accumulators::impl::weighted_covariance_impl<mpl::_1, mpl::_2, VariateType, VariateTag> impl;
116
    };
117
}
118

    
119
///////////////////////////////////////////////////////////////////////////////
120
// extract::weighted_covariance
121
//
122
namespace extract
123
{
124
    extractor<tag::abstract_covariance> const weighted_covariance = {};
125

    
126
    BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_covariance)
127
}
128

    
129
using extract::weighted_covariance;
130

    
131
}} // namespace boost::accumulators
132

    
133
#endif