]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // mean.hpp | |
3 | // | |
4 | // Copyright 2005 Eric Niebler. 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_MEAN_HPP_EAN_28_10_2005 | |
9 | #define BOOST_ACCUMULATORS_STATISTICS_MEAN_HPP_EAN_28_10_2005 | |
10 | ||
11 | #include <boost/mpl/placeholders.hpp> | |
12 | #include <boost/accumulators/framework/accumulator_base.hpp> | |
13 | #include <boost/accumulators/framework/extractor.hpp> | |
14 | #include <boost/accumulators/numeric/functional.hpp> | |
15 | #include <boost/accumulators/framework/parameters/sample.hpp> | |
16 | #include <boost/accumulators/framework/depends_on.hpp> | |
17 | #include <boost/accumulators/statistics_fwd.hpp> | |
18 | #include <boost/accumulators/statistics/count.hpp> | |
19 | #include <boost/accumulators/statistics/sum.hpp> | |
20 | ||
21 | namespace boost { namespace accumulators | |
22 | { | |
23 | ||
24 | namespace impl | |
25 | { | |
26 | /////////////////////////////////////////////////////////////////////////////// | |
27 | // mean_impl | |
28 | // lazy, by default | |
29 | template<typename Sample, typename SumFeature> | |
30 | struct mean_impl | |
31 | : accumulator_base | |
32 | { | |
33 | // for boost::result_of | |
34 | typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type; | |
35 | ||
36 | mean_impl(dont_care) {} | |
37 | ||
38 | template<typename Args> | |
39 | result_type result(Args const &args) const | |
40 | { | |
41 | extractor<SumFeature> sum; | |
42 | return numeric::fdiv(sum(args), count(args)); | |
43 | } | |
44 | }; | |
45 | ||
46 | template<typename Sample, typename Tag> | |
47 | struct immediate_mean_impl | |
48 | : accumulator_base | |
49 | { | |
50 | // for boost::result_of | |
51 | typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type; | |
52 | ||
53 | template<typename Args> | |
54 | immediate_mean_impl(Args const &args) | |
55 | : mean(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value)) | |
56 | { | |
57 | } | |
58 | ||
59 | template<typename Args> | |
60 | void operator ()(Args const &args) | |
61 | { | |
62 | std::size_t cnt = count(args); | |
63 | this->mean = numeric::fdiv( | |
64 | (this->mean * (cnt - 1)) + args[parameter::keyword<Tag>::get()] | |
65 | , cnt | |
66 | ); | |
67 | } | |
68 | ||
69 | result_type result(dont_care) const | |
70 | { | |
71 | return this->mean; | |
72 | } | |
73 | ||
74 | private: | |
75 | result_type mean; | |
76 | }; | |
77 | ||
78 | } // namespace impl | |
79 | ||
80 | /////////////////////////////////////////////////////////////////////////////// | |
81 | // tag::mean | |
82 | // tag::immediate_mean | |
83 | // tag::mean_of_weights | |
84 | // tag::immediate_mean_of_weights | |
85 | // tag::mean_of_variates | |
86 | // tag::immediate_mean_of_variates | |
87 | // | |
88 | namespace tag | |
89 | { | |
90 | struct mean | |
91 | : depends_on<count, sum> | |
92 | { | |
93 | /// INTERNAL ONLY | |
94 | /// | |
95 | typedef accumulators::impl::mean_impl<mpl::_1, sum> impl; | |
96 | }; | |
97 | struct immediate_mean | |
98 | : depends_on<count> | |
99 | { | |
100 | /// INTERNAL ONLY | |
101 | /// | |
102 | typedef accumulators::impl::immediate_mean_impl<mpl::_1, tag::sample> impl; | |
103 | }; | |
104 | struct mean_of_weights | |
105 | : depends_on<count, sum_of_weights> | |
106 | { | |
107 | typedef mpl::true_ is_weight_accumulator; | |
108 | /// INTERNAL ONLY | |
109 | /// | |
110 | typedef accumulators::impl::mean_impl<mpl::_2, sum_of_weights> impl; | |
111 | }; | |
112 | struct immediate_mean_of_weights | |
113 | : depends_on<count> | |
114 | { | |
115 | typedef mpl::true_ is_weight_accumulator; | |
116 | /// INTERNAL ONLY | |
117 | /// | |
118 | typedef accumulators::impl::immediate_mean_impl<mpl::_2, tag::weight> impl; | |
119 | }; | |
120 | template<typename VariateType, typename VariateTag> | |
121 | struct mean_of_variates | |
122 | : depends_on<count, sum_of_variates<VariateType, VariateTag> > | |
123 | { | |
124 | /// INTERNAL ONLY | |
125 | /// | |
126 | typedef mpl::always<accumulators::impl::mean_impl<VariateType, sum_of_variates<VariateType, VariateTag> > > impl; | |
127 | }; | |
128 | template<typename VariateType, typename VariateTag> | |
129 | struct immediate_mean_of_variates | |
130 | : depends_on<count> | |
131 | { | |
132 | /// INTERNAL ONLY | |
133 | /// | |
134 | typedef mpl::always<accumulators::impl::immediate_mean_impl<VariateType, VariateTag> > impl; | |
135 | }; | |
136 | } | |
137 | ||
138 | /////////////////////////////////////////////////////////////////////////////// | |
139 | // extract::mean | |
140 | // extract::mean_of_weights | |
141 | // extract::mean_of_variates | |
142 | // | |
143 | namespace extract | |
144 | { | |
145 | extractor<tag::mean> const mean = {}; | |
146 | extractor<tag::mean_of_weights> const mean_of_weights = {}; | |
147 | BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, mean_of_variates, (typename)(typename)) | |
148 | ||
149 | BOOST_ACCUMULATORS_IGNORE_GLOBAL(mean) | |
150 | BOOST_ACCUMULATORS_IGNORE_GLOBAL(mean_of_weights) | |
151 | } | |
152 | ||
153 | using extract::mean; | |
154 | using extract::mean_of_weights; | |
155 | using extract::mean_of_variates; | |
156 | ||
157 | // mean(lazy) -> mean | |
158 | template<> | |
159 | struct as_feature<tag::mean(lazy)> | |
160 | { | |
161 | typedef tag::mean type; | |
162 | }; | |
163 | ||
164 | // mean(immediate) -> immediate_mean | |
165 | template<> | |
166 | struct as_feature<tag::mean(immediate)> | |
167 | { | |
168 | typedef tag::immediate_mean type; | |
169 | }; | |
170 | ||
171 | // mean_of_weights(lazy) -> mean_of_weights | |
172 | template<> | |
173 | struct as_feature<tag::mean_of_weights(lazy)> | |
174 | { | |
175 | typedef tag::mean_of_weights type; | |
176 | }; | |
177 | ||
178 | // mean_of_weights(immediate) -> immediate_mean_of_weights | |
179 | template<> | |
180 | struct as_feature<tag::mean_of_weights(immediate)> | |
181 | { | |
182 | typedef tag::immediate_mean_of_weights type; | |
183 | }; | |
184 | ||
185 | // mean_of_variates<VariateType, VariateTag>(lazy) -> mean_of_variates<VariateType, VariateTag> | |
186 | template<typename VariateType, typename VariateTag> | |
187 | struct as_feature<tag::mean_of_variates<VariateType, VariateTag>(lazy)> | |
188 | { | |
189 | typedef tag::mean_of_variates<VariateType, VariateTag> type; | |
190 | }; | |
191 | ||
192 | // mean_of_variates<VariateType, VariateTag>(immediate) -> immediate_mean_of_variates<VariateType, VariateTag> | |
193 | template<typename VariateType, typename VariateTag> | |
194 | struct as_feature<tag::mean_of_variates<VariateType, VariateTag>(immediate)> | |
195 | { | |
196 | typedef tag::immediate_mean_of_variates<VariateType, VariateTag> type; | |
197 | }; | |
198 | ||
199 | // for the purposes of feature-based dependency resolution, | |
200 | // immediate_mean provides the same feature as mean | |
201 | template<> | |
202 | struct feature_of<tag::immediate_mean> | |
203 | : feature_of<tag::mean> | |
204 | { | |
205 | }; | |
206 | ||
207 | // for the purposes of feature-based dependency resolution, | |
208 | // immediate_mean provides the same feature as mean | |
209 | template<> | |
210 | struct feature_of<tag::immediate_mean_of_weights> | |
211 | : feature_of<tag::mean_of_weights> | |
212 | { | |
213 | }; | |
214 | ||
215 | // for the purposes of feature-based dependency resolution, | |
216 | // immediate_mean provides the same feature as mean | |
217 | template<typename VariateType, typename VariateTag> | |
218 | struct feature_of<tag::immediate_mean_of_variates<VariateType, VariateTag> > | |
219 | : feature_of<tag::mean_of_variates<VariateType, VariateTag> > | |
220 | { | |
221 | }; | |
222 | ||
223 | // So that mean can be automatically substituted with | |
224 | // weighted_mean when the weight parameter is non-void. | |
225 | template<> | |
226 | struct as_weighted_feature<tag::mean> | |
227 | { | |
228 | typedef tag::weighted_mean type; | |
229 | }; | |
230 | ||
231 | template<> | |
232 | struct feature_of<tag::weighted_mean> | |
233 | : feature_of<tag::mean> | |
234 | {}; | |
235 | ||
236 | // So that immediate_mean can be automatically substituted with | |
237 | // immediate_weighted_mean when the weight parameter is non-void. | |
238 | template<> | |
239 | struct as_weighted_feature<tag::immediate_mean> | |
240 | { | |
241 | typedef tag::immediate_weighted_mean type; | |
242 | }; | |
243 | ||
244 | template<> | |
245 | struct feature_of<tag::immediate_weighted_mean> | |
246 | : feature_of<tag::immediate_mean> | |
247 | {}; | |
248 | ||
249 | // So that mean_of_weights<> can be automatically substituted with | |
250 | // weighted_mean_of_variates<> when the weight parameter is non-void. | |
251 | template<typename VariateType, typename VariateTag> | |
252 | struct as_weighted_feature<tag::mean_of_variates<VariateType, VariateTag> > | |
253 | { | |
254 | typedef tag::weighted_mean_of_variates<VariateType, VariateTag> type; | |
255 | }; | |
256 | ||
257 | template<typename VariateType, typename VariateTag> | |
258 | struct feature_of<tag::weighted_mean_of_variates<VariateType, VariateTag> > | |
259 | : feature_of<tag::mean_of_variates<VariateType, VariateTag> > | |
260 | { | |
261 | }; | |
262 | ||
263 | // So that immediate_mean_of_weights<> can be automatically substituted with | |
264 | // immediate_weighted_mean_of_variates<> when the weight parameter is non-void. | |
265 | template<typename VariateType, typename VariateTag> | |
266 | struct as_weighted_feature<tag::immediate_mean_of_variates<VariateType, VariateTag> > | |
267 | { | |
268 | typedef tag::immediate_weighted_mean_of_variates<VariateType, VariateTag> type; | |
269 | }; | |
270 | ||
271 | template<typename VariateType, typename VariateTag> | |
272 | struct feature_of<tag::immediate_weighted_mean_of_variates<VariateType, VariateTag> > | |
273 | : feature_of<tag::immediate_mean_of_variates<VariateType, VariateTag> > | |
274 | { | |
275 | }; | |
276 | ||
277 | //////////////////////////////////////////////////////////////////////////// | |
278 | //// droppable_accumulator<mean_impl> | |
279 | //// need to specialize droppable lazy mean to cache the result at the | |
280 | //// point the accumulator is dropped. | |
281 | ///// INTERNAL ONLY | |
282 | ///// | |
283 | //template<typename Sample, typename SumFeature> | |
284 | //struct droppable_accumulator<impl::mean_impl<Sample, SumFeature> > | |
285 | // : droppable_accumulator_base< | |
286 | // with_cached_result<impl::mean_impl<Sample, SumFeature> > | |
287 | // > | |
288 | //{ | |
289 | // template<typename Args> | |
290 | // droppable_accumulator(Args const &args) | |
291 | // : droppable_accumulator::base(args) | |
292 | // { | |
293 | // } | |
294 | //}; | |
295 | ||
296 | }} // namespace boost::accumulators | |
297 | ||
298 | #endif |