Statistics
| Revision:

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

History | View | Annotate | Download (7 kB)

1 2486 sjacqu01
///////////////////////////////////////////////////////////////////////////////
2 2486 sjacqu01
// covariance.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_COVARIANCE_HPP_DE_01_01_2006
9 2486 sjacqu01
#define BOOST_ACCUMULATORS_STATISTICS_COVARIANCE_HPP_DE_01_01_2006
10 2486 sjacqu01
11 2486 sjacqu01
#include <vector>
12 2486 sjacqu01
#include <limits>
13 2486 sjacqu01
#include <numeric>
14 2486 sjacqu01
#include <functional>
15 2486 sjacqu01
#include <complex>
16 2486 sjacqu01
#include <boost/mpl/assert.hpp>
17 2486 sjacqu01
#include <boost/mpl/bool.hpp>
18 2486 sjacqu01
#include <boost/range.hpp>
19 2486 sjacqu01
#include <boost/parameter/keyword.hpp>
20 2486 sjacqu01
#include <boost/mpl/placeholders.hpp>
21 2486 sjacqu01
#include <boost/numeric/ublas/io.hpp>
22 2486 sjacqu01
#include <boost/numeric/ublas/matrix.hpp>
23 2486 sjacqu01
#include <boost/type_traits/is_scalar.hpp>
24 2486 sjacqu01
#include <boost/type_traits/is_same.hpp>
25 2486 sjacqu01
#include <boost/accumulators/framework/accumulator_base.hpp>
26 2486 sjacqu01
#include <boost/accumulators/framework/extractor.hpp>
27 2486 sjacqu01
#include <boost/accumulators/numeric/functional.hpp>
28 2486 sjacqu01
#include <boost/accumulators/framework/parameters/sample.hpp>
29 2486 sjacqu01
#include <boost/accumulators/statistics_fwd.hpp>
30 2486 sjacqu01
#include <boost/accumulators/statistics/count.hpp>
31 2486 sjacqu01
#include <boost/accumulators/statistics/mean.hpp>
32 2486 sjacqu01
33 2486 sjacqu01
namespace boost { namespace numeric
34 2486 sjacqu01
{
35 2486 sjacqu01
    namespace functional
36 2486 sjacqu01
    {
37 2486 sjacqu01
        struct std_vector_tag;
38 2486 sjacqu01
39 2486 sjacqu01
        ///////////////////////////////////////////////////////////////////////////////
40 2486 sjacqu01
        // functional::outer_product
41 2486 sjacqu01
        template<typename Left, typename Right, typename EnableIf = void>
42 2486 sjacqu01
        struct outer_product_base
43 2486 sjacqu01
          : functional::multiplies<Left, Right>
44 2486 sjacqu01
        {};
45 2486 sjacqu01
46 2486 sjacqu01
        template<typename Left, typename Right, typename LeftTag = typename tag<Left>::type, typename RightTag = typename tag<Right>::type>
47 2486 sjacqu01
        struct outer_product
48 2486 sjacqu01
          : outer_product_base<Left, Right, void>
49 2486 sjacqu01
        {};
50 2486 sjacqu01
51 2486 sjacqu01
        template<typename Left, typename Right>
52 2486 sjacqu01
        struct outer_product<Left, Right, std_vector_tag, std_vector_tag>
53 2486 sjacqu01
          : std::binary_function<
54 2486 sjacqu01
                Left
55 2486 sjacqu01
              , Right
56 2486 sjacqu01
              , ublas::matrix<
57 2486 sjacqu01
                    typename functional::multiplies<
58 2486 sjacqu01
                        typename Left::value_type
59 2486 sjacqu01
                      , typename Right::value_type
60 2486 sjacqu01
                    >::result_type
61 2486 sjacqu01
                >
62 2486 sjacqu01
            >
63 2486 sjacqu01
        {
64 2486 sjacqu01
            typedef
65 2486 sjacqu01
                ublas::matrix<
66 2486 sjacqu01
                    typename functional::multiplies<
67 2486 sjacqu01
                        typename Left::value_type
68 2486 sjacqu01
                      , typename Right::value_type
69 2486 sjacqu01
                    >::result_type
70 2486 sjacqu01
                >
71 2486 sjacqu01
            result_type;
72 2486 sjacqu01
73 2486 sjacqu01
            result_type
74 2486 sjacqu01
            operator ()(Left & left, Right & right) const
75 2486 sjacqu01
            {
76 2486 sjacqu01
                std::size_t left_size = left.size();
77 2486 sjacqu01
                std::size_t right_size = right.size();
78 2486 sjacqu01
                result_type result(left_size, right_size);
79 2486 sjacqu01
                for (std::size_t i = 0; i < left_size; ++i)
80 2486 sjacqu01
                    for (std::size_t j = 0; j < right_size; ++j)
81 2486 sjacqu01
                        result(i,j) = numeric::multiplies(left[i], right[j]);
82 2486 sjacqu01
                return result;
83 2486 sjacqu01
            }
84 2486 sjacqu01
        };
85 2486 sjacqu01
    }
86 2486 sjacqu01
87 2486 sjacqu01
    namespace op
88 2486 sjacqu01
    {
89 2486 sjacqu01
        struct outer_product
90 2486 sjacqu01
          : boost::detail::function2<functional::outer_product<_1, _2, functional::tag<_1>, functional::tag<_2> > >
91 2486 sjacqu01
        {};
92 2486 sjacqu01
    }
93 2486 sjacqu01
94 2486 sjacqu01
    namespace
95 2486 sjacqu01
    {
96 2486 sjacqu01
        op::outer_product const &outer_product = boost::detail::pod_singleton<op::outer_product>::instance;
97 2486 sjacqu01
    }
98 2486 sjacqu01
99 2486 sjacqu01
}}
100 2486 sjacqu01
101 2486 sjacqu01
namespace boost { namespace accumulators
102 2486 sjacqu01
{
103 2486 sjacqu01
104 2486 sjacqu01
namespace impl
105 2486 sjacqu01
{
106 2486 sjacqu01
    ///////////////////////////////////////////////////////////////////////////////
107 2486 sjacqu01
    // covariance_impl
108 2486 sjacqu01
    //
109 2486 sjacqu01
    /**
110 2486 sjacqu01
        @brief Covariance Estimator
111 2486 sjacqu01

112 2486 sjacqu01
        An iterative Monte Carlo estimator for the covariance \f$\mathrm{Cov}(X,X')\f$, where \f$X\f$ is a sample
113 2486 sjacqu01
        and \f$X'\f$ is a variate, is given by:
114 2486 sjacqu01

115 2486 sjacqu01
        \f[
116 2486 sjacqu01
            \hat{c}_n = \frac{n-1}{n} \hat{c}_{n-1} + \frac{1}{n-1}(X_n - \hat{\mu}_n)(X_n' - \hat{\mu}_n'),\quad n\ge2,\quad\hat{c}_1 = 0,
117 2486 sjacqu01
        \f]
118 2486 sjacqu01

119 2486 sjacqu01
        \f$\hat{\mu}_n\f$ and \f$\hat{\mu}_n'\f$ being the means of the samples and variates.
120 2486 sjacqu01
    */
121 2486 sjacqu01
    template<typename Sample, typename VariateType, typename VariateTag>
122 2486 sjacqu01
    struct covariance_impl
123 2486 sjacqu01
      : accumulator_base
124 2486 sjacqu01
    {
125 2486 sjacqu01
        typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type sample_type;
126 2486 sjacqu01
        typedef typename numeric::functional::fdiv<VariateType, std::size_t>::result_type variate_type;
127 2486 sjacqu01
        // for boost::result_of
128 2486 sjacqu01
        typedef typename numeric::functional::outer_product<sample_type, variate_type>::result_type result_type;
129 2486 sjacqu01
130 2486 sjacqu01
        template<typename Args>
131 2486 sjacqu01
        covariance_impl(Args const &args)
132 2486 sjacqu01
          : cov_(
133 2486 sjacqu01
                numeric::outer_product(
134 2486 sjacqu01
                    numeric::fdiv(args[sample | Sample()], (std::size_t)1)
135 2486 sjacqu01
                  , numeric::fdiv(args[parameter::keyword<VariateTag>::get() | VariateType()], (std::size_t)1)
136 2486 sjacqu01
                )
137 2486 sjacqu01
            )
138 2486 sjacqu01
        {
139 2486 sjacqu01
        }
140 2486 sjacqu01
141 2486 sjacqu01
        template<typename Args>
142 2486 sjacqu01
        void operator ()(Args const &args)
143 2486 sjacqu01
        {
144 2486 sjacqu01
            std::size_t cnt = count(args);
145 2486 sjacqu01
146 2486 sjacqu01
            if (cnt > 1)
147 2486 sjacqu01
            {
148 2486 sjacqu01
                extractor<tag::mean_of_variates<VariateType, VariateTag> > const some_mean_of_variates = {};
149 2486 sjacqu01
150 2486 sjacqu01
                this->cov_ = this->cov_*(cnt-1.)/cnt
151 2486 sjacqu01
                           + numeric::outer_product(
152 2486 sjacqu01
                                 some_mean_of_variates(args) - args[parameter::keyword<VariateTag>::get()]
153 2486 sjacqu01
                               , mean(args) - args[sample]
154 2486 sjacqu01
                             ) / (cnt-1.);
155 2486 sjacqu01
            }
156 2486 sjacqu01
        }
157 2486 sjacqu01
158 2486 sjacqu01
        result_type result(dont_care) const
159 2486 sjacqu01
        {
160 2486 sjacqu01
            return this->cov_;
161 2486 sjacqu01
        }
162 2486 sjacqu01
163 2486 sjacqu01
    private:
164 2486 sjacqu01
        result_type cov_;
165 2486 sjacqu01
    };
166 2486 sjacqu01
167 2486 sjacqu01
} // namespace impl
168 2486 sjacqu01
169 2486 sjacqu01
///////////////////////////////////////////////////////////////////////////////
170 2486 sjacqu01
// tag::covariance
171 2486 sjacqu01
//
172 2486 sjacqu01
namespace tag
173 2486 sjacqu01
{
174 2486 sjacqu01
    template<typename VariateType, typename VariateTag>
175 2486 sjacqu01
    struct covariance
176 2486 sjacqu01
      : depends_on<count, mean, mean_of_variates<VariateType, VariateTag> >
177 2486 sjacqu01
    {
178 2486 sjacqu01
        typedef accumulators::impl::covariance_impl<mpl::_1, VariateType, VariateTag> impl;
179 2486 sjacqu01
    };
180 2486 sjacqu01
181 2486 sjacqu01
    struct abstract_covariance
182 2486 sjacqu01
      : depends_on<>
183 2486 sjacqu01
    {
184 2486 sjacqu01
    };
185 2486 sjacqu01
}
186 2486 sjacqu01
187 2486 sjacqu01
///////////////////////////////////////////////////////////////////////////////
188 2486 sjacqu01
// extract::covariance
189 2486 sjacqu01
//
190 2486 sjacqu01
namespace extract
191 2486 sjacqu01
{
192 2486 sjacqu01
    extractor<tag::abstract_covariance> const covariance = {};
193 2486 sjacqu01
194 2486 sjacqu01
    BOOST_ACCUMULATORS_IGNORE_GLOBAL(covariance)
195 2486 sjacqu01
}
196 2486 sjacqu01
197 2486 sjacqu01
using extract::covariance;
198 2486 sjacqu01
199 2486 sjacqu01
template<typename VariateType, typename VariateTag>
200 2486 sjacqu01
struct feature_of<tag::covariance<VariateType, VariateTag> >
201 2486 sjacqu01
  : feature_of<tag::abstract_covariance>
202 2486 sjacqu01
{
203 2486 sjacqu01
};
204 2486 sjacqu01
205 2486 sjacqu01
// So that covariance can be automatically substituted with
206 2486 sjacqu01
// weighted_covariance when the weight parameter is non-void.
207 2486 sjacqu01
template<typename VariateType, typename VariateTag>
208 2486 sjacqu01
struct as_weighted_feature<tag::covariance<VariateType, VariateTag> >
209 2486 sjacqu01
{
210 2486 sjacqu01
    typedef tag::weighted_covariance<VariateType, VariateTag> type;
211 2486 sjacqu01
};
212 2486 sjacqu01
213 2486 sjacqu01
template<typename VariateType, typename VariateTag>
214 2486 sjacqu01
struct feature_of<tag::weighted_covariance<VariateType, VariateTag> >
215 2486 sjacqu01
  : feature_of<tag::covariance<VariateType, VariateTag> >
216 2486 sjacqu01
{};
217 2486 sjacqu01
218 2486 sjacqu01
}} // namespace boost::accumulators
219 2486 sjacqu01
220 2486 sjacqu01
#endif