Statistics
| Revision:

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

History | View | Annotate | Download (6.3 kB)

1 2486 sjacqu01
///////////////////////////////////////////////////////////////////////////////
2 2486 sjacqu01
// weighted_mean.hpp
3 2486 sjacqu01
//
4 2486 sjacqu01
//  Copyright 2006 Eric Niebler, 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_MEAN_HPP_EAN_03_11_2005
9 2486 sjacqu01
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MEAN_HPP_EAN_03_11_2005
10 2486 sjacqu01
11 2486 sjacqu01
#include <boost/mpl/assert.hpp>
12 2486 sjacqu01
#include <boost/mpl/eval_if.hpp>
13 2486 sjacqu01
#include <boost/mpl/placeholders.hpp>
14 2486 sjacqu01
#include <boost/type_traits/is_same.hpp>
15 2486 sjacqu01
#include <boost/accumulators/framework/accumulator_base.hpp>
16 2486 sjacqu01
#include <boost/accumulators/framework/extractor.hpp>
17 2486 sjacqu01
#include <boost/accumulators/numeric/functional.hpp>
18 2486 sjacqu01
#include <boost/accumulators/framework/parameters/weights.hpp>
19 2486 sjacqu01
#include <boost/accumulators/framework/depends_on.hpp>
20 2486 sjacqu01
#include <boost/accumulators/statistics_fwd.hpp>
21 2486 sjacqu01
#include <boost/accumulators/statistics/sum.hpp>
22 2486 sjacqu01
#include <boost/accumulators/statistics/mean.hpp>
23 2486 sjacqu01
#include <boost/accumulators/statistics/weighted_sum.hpp>
24 2486 sjacqu01
25 2486 sjacqu01
namespace boost { namespace accumulators
26 2486 sjacqu01
{
27 2486 sjacqu01
28 2486 sjacqu01
namespace impl
29 2486 sjacqu01
{
30 2486 sjacqu01
    ///////////////////////////////////////////////////////////////////////////////
31 2486 sjacqu01
    // weighted_mean_impl
32 2486 sjacqu01
    //      lazy, by default
33 2486 sjacqu01
    template<typename Sample, typename Weight, typename Tag>
34 2486 sjacqu01
    struct weighted_mean_impl
35 2486 sjacqu01
      : accumulator_base
36 2486 sjacqu01
    {
37 2486 sjacqu01
        typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
38 2486 sjacqu01
        // for boost::result_of
39 2486 sjacqu01
        typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
40 2486 sjacqu01
41 2486 sjacqu01
        weighted_mean_impl(dont_care) {}
42 2486 sjacqu01
43 2486 sjacqu01
        template<typename Args>
44 2486 sjacqu01
        result_type result(Args const &args) const
45 2486 sjacqu01
        {
46 2486 sjacqu01
            typedef
47 2486 sjacqu01
                typename mpl::if_<
48 2486 sjacqu01
                    is_same<Tag, tag::sample>
49 2486 sjacqu01
                  , tag::weighted_sum
50 2486 sjacqu01
                  , tag::weighted_sum_of_variates<Sample, Tag>
51 2486 sjacqu01
                >::type
52 2486 sjacqu01
            weighted_sum_tag;
53 2486 sjacqu01
54 2486 sjacqu01
            extractor<weighted_sum_tag> const some_weighted_sum = {};
55 2486 sjacqu01
56 2486 sjacqu01
            return numeric::fdiv(some_weighted_sum(args), sum_of_weights(args));
57 2486 sjacqu01
        }
58 2486 sjacqu01
    };
59 2486 sjacqu01
60 2486 sjacqu01
    ///////////////////////////////////////////////////////////////////////////////
61 2486 sjacqu01
    // immediate_weighted_mean_impl
62 2486 sjacqu01
    //      immediate
63 2486 sjacqu01
    template<typename Sample, typename Weight, typename Tag>
64 2486 sjacqu01
    struct immediate_weighted_mean_impl
65 2486 sjacqu01
      : accumulator_base
66 2486 sjacqu01
    {
67 2486 sjacqu01
        typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
68 2486 sjacqu01
        // for boost::result_of
69 2486 sjacqu01
        typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
70 2486 sjacqu01
71 2486 sjacqu01
        template<typename Args>
72 2486 sjacqu01
        immediate_weighted_mean_impl(Args const &args)
73 2486 sjacqu01
          : mean(
74 2486 sjacqu01
                numeric::fdiv(
75 2486 sjacqu01
                    args[parameter::keyword<Tag>::get() | Sample()]
76 2486 sjacqu01
                      * numeric::one<Weight>::value
77 2486 sjacqu01
                  , numeric::one<Weight>::value
78 2486 sjacqu01
                )
79 2486 sjacqu01
            )
80 2486 sjacqu01
        {
81 2486 sjacqu01
        }
82 2486 sjacqu01
83 2486 sjacqu01
        template<typename Args>
84 2486 sjacqu01
        void operator ()(Args const &args)
85 2486 sjacqu01
        {
86 2486 sjacqu01
            // Matthias:
87 2486 sjacqu01
            //  need to pass the argument pack since the weight might be an external
88 2486 sjacqu01
            //  accumulator set passed as a named parameter
89 2486 sjacqu01
            Weight w_sum = sum_of_weights(args);
90 2486 sjacqu01
            Weight w = args[weight];
91 2486 sjacqu01
            weighted_sample const &s = args[parameter::keyword<Tag>::get()] * w;
92 2486 sjacqu01
            this->mean = numeric::fdiv(this->mean * (w_sum - w) + s, w_sum);
93 2486 sjacqu01
        }
94 2486 sjacqu01
95 2486 sjacqu01
        result_type result(dont_care) const
96 2486 sjacqu01
        {
97 2486 sjacqu01
            return this->mean;
98 2486 sjacqu01
        }
99 2486 sjacqu01
100 2486 sjacqu01
    private:
101 2486 sjacqu01
        result_type mean;
102 2486 sjacqu01
    };
103 2486 sjacqu01
104 2486 sjacqu01
} // namespace impl
105 2486 sjacqu01
106 2486 sjacqu01
///////////////////////////////////////////////////////////////////////////////
107 2486 sjacqu01
// tag::weighted_mean
108 2486 sjacqu01
// tag::immediate_weighted_mean
109 2486 sjacqu01
//
110 2486 sjacqu01
namespace tag
111 2486 sjacqu01
{
112 2486 sjacqu01
    struct weighted_mean
113 2486 sjacqu01
      : depends_on<sum_of_weights, weighted_sum>
114 2486 sjacqu01
    {
115 2486 sjacqu01
        /// INTERNAL ONLY
116 2486 sjacqu01
        ///
117 2486 sjacqu01
        typedef accumulators::impl::weighted_mean_impl<mpl::_1, mpl::_2, tag::sample> impl;
118 2486 sjacqu01
    };
119 2486 sjacqu01
    struct immediate_weighted_mean
120 2486 sjacqu01
      : depends_on<sum_of_weights>
121 2486 sjacqu01
    {
122 2486 sjacqu01
        /// INTERNAL ONLY
123 2486 sjacqu01
        ///
124 2486 sjacqu01
        typedef accumulators::impl::immediate_weighted_mean_impl<mpl::_1, mpl::_2, tag::sample> impl;
125 2486 sjacqu01
    };
126 2486 sjacqu01
    template<typename VariateType, typename VariateTag>
127 2486 sjacqu01
    struct weighted_mean_of_variates
128 2486 sjacqu01
      : depends_on<sum_of_weights, weighted_sum_of_variates<VariateType, VariateTag> >
129 2486 sjacqu01
    {
130 2486 sjacqu01
        /// INTERNAL ONLY
131 2486 sjacqu01
        ///
132 2486 sjacqu01
        typedef accumulators::impl::weighted_mean_impl<VariateType, mpl::_2, VariateTag> impl;
133 2486 sjacqu01
    };
134 2486 sjacqu01
    template<typename VariateType, typename VariateTag>
135 2486 sjacqu01
    struct immediate_weighted_mean_of_variates
136 2486 sjacqu01
      : depends_on<sum_of_weights>
137 2486 sjacqu01
    {
138 2486 sjacqu01
        /// INTERNAL ONLY
139 2486 sjacqu01
        ///
140 2486 sjacqu01
        typedef accumulators::impl::immediate_weighted_mean_impl<VariateType, mpl::_2, VariateTag> impl;
141 2486 sjacqu01
    };
142 2486 sjacqu01
}
143 2486 sjacqu01
144 2486 sjacqu01
///////////////////////////////////////////////////////////////////////////////
145 2486 sjacqu01
// extract::weighted_mean
146 2486 sjacqu01
// extract::weighted_mean_of_variates
147 2486 sjacqu01
//
148 2486 sjacqu01
namespace extract
149 2486 sjacqu01
{
150 2486 sjacqu01
    extractor<tag::mean> const weighted_mean = {};
151 2486 sjacqu01
    BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, weighted_mean_of_variates, (typename)(typename))
152 2486 sjacqu01
153 2486 sjacqu01
    BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_mean)
154 2486 sjacqu01
}
155 2486 sjacqu01
156 2486 sjacqu01
using extract::weighted_mean;
157 2486 sjacqu01
using extract::weighted_mean_of_variates;
158 2486 sjacqu01
159 2486 sjacqu01
// weighted_mean(lazy) -> weighted_mean
160 2486 sjacqu01
template<>
161 2486 sjacqu01
struct as_feature<tag::weighted_mean(lazy)>
162 2486 sjacqu01
{
163 2486 sjacqu01
    typedef tag::weighted_mean type;
164 2486 sjacqu01
};
165 2486 sjacqu01
166 2486 sjacqu01
// weighted_mean(immediate) -> immediate_weighted_mean
167 2486 sjacqu01
template<>
168 2486 sjacqu01
struct as_feature<tag::weighted_mean(immediate)>
169 2486 sjacqu01
{
170 2486 sjacqu01
    typedef tag::immediate_weighted_mean type;
171 2486 sjacqu01
};
172 2486 sjacqu01
173 2486 sjacqu01
// weighted_mean_of_variates<VariateType, VariateTag>(lazy) -> weighted_mean_of_variates<VariateType, VariateTag>
174 2486 sjacqu01
template<typename VariateType, typename VariateTag>
175 2486 sjacqu01
struct as_feature<tag::weighted_mean_of_variates<VariateType, VariateTag>(lazy)>
176 2486 sjacqu01
{
177 2486 sjacqu01
    typedef tag::weighted_mean_of_variates<VariateType, VariateTag> type;
178 2486 sjacqu01
};
179 2486 sjacqu01
180 2486 sjacqu01
// weighted_mean_of_variates<VariateType, VariateTag>(immediate) -> immediate_weighted_mean_of_variates<VariateType, VariateTag>
181 2486 sjacqu01
template<typename VariateType, typename VariateTag>
182 2486 sjacqu01
struct as_feature<tag::weighted_mean_of_variates<VariateType, VariateTag>(immediate)>
183 2486 sjacqu01
{
184 2486 sjacqu01
    typedef tag::immediate_weighted_mean_of_variates<VariateType, VariateTag> type;
185 2486 sjacqu01
};
186 2486 sjacqu01
187 2486 sjacqu01
}} // namespace boost::accumulators
188 2486 sjacqu01
189 2486 sjacqu01
#endif