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