1 ///////////////////////////////////////////////////////////////////////////////
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)
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_MEAN_HPP_EAN_28_10_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_MEAN_HPP_EAN_28_10_2005
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>
21 namespace boost { namespace accumulators
26 ///////////////////////////////////////////////////////////////////////////////
29 template<typename Sample, typename SumFeature>
33 // for boost::result_of
34 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
36 mean_impl(dont_care) {}
38 template<typename Args>
39 result_type result(Args const &args) const
41 extractor<SumFeature> sum;
42 return numeric::fdiv(sum(args), count(args));
46 template<typename Sample, typename Tag>
47 struct immediate_mean_impl
50 // for boost::result_of
51 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
53 template<typename Args>
54 immediate_mean_impl(Args const &args)
55 : mean(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
59 template<typename Args>
60 void operator ()(Args const &args)
62 std::size_t cnt = count(args);
63 this->mean = numeric::fdiv(
64 (this->mean * (cnt - 1)) + args[parameter::keyword<Tag>::get()]
69 result_type result(dont_care) const
80 ///////////////////////////////////////////////////////////////////////////////
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
91 : depends_on<count, sum>
95 typedef accumulators::impl::mean_impl<mpl::_1, sum> impl;
102 typedef accumulators::impl::immediate_mean_impl<mpl::_1, tag::sample> impl;
104 struct mean_of_weights
105 : depends_on<count, sum_of_weights>
107 typedef mpl::true_ is_weight_accumulator;
110 typedef accumulators::impl::mean_impl<mpl::_2, sum_of_weights> impl;
112 struct immediate_mean_of_weights
115 typedef mpl::true_ is_weight_accumulator;
118 typedef accumulators::impl::immediate_mean_impl<mpl::_2, tag::weight> impl;
120 template<typename VariateType, typename VariateTag>
121 struct mean_of_variates
122 : depends_on<count, sum_of_variates<VariateType, VariateTag> >
126 typedef mpl::always<accumulators::impl::mean_impl<VariateType, sum_of_variates<VariateType, VariateTag> > > impl;
128 template<typename VariateType, typename VariateTag>
129 struct immediate_mean_of_variates
134 typedef mpl::always<accumulators::impl::immediate_mean_impl<VariateType, VariateTag> > impl;
138 ///////////////////////////////////////////////////////////////////////////////
140 // extract::mean_of_weights
141 // extract::mean_of_variates
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))
149 BOOST_ACCUMULATORS_IGNORE_GLOBAL(mean)
150 BOOST_ACCUMULATORS_IGNORE_GLOBAL(mean_of_weights)
154 using extract::mean_of_weights;
155 using extract::mean_of_variates;
157 // mean(lazy) -> mean
159 struct as_feature<tag::mean(lazy)>
161 typedef tag::mean type;
164 // mean(immediate) -> immediate_mean
166 struct as_feature<tag::mean(immediate)>
168 typedef tag::immediate_mean type;
171 // mean_of_weights(lazy) -> mean_of_weights
173 struct as_feature<tag::mean_of_weights(lazy)>
175 typedef tag::mean_of_weights type;
178 // mean_of_weights(immediate) -> immediate_mean_of_weights
180 struct as_feature<tag::mean_of_weights(immediate)>
182 typedef tag::immediate_mean_of_weights type;
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)>
189 typedef tag::mean_of_variates<VariateType, VariateTag> type;
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)>
196 typedef tag::immediate_mean_of_variates<VariateType, VariateTag> type;
199 // for the purposes of feature-based dependency resolution,
200 // immediate_mean provides the same feature as mean
202 struct feature_of<tag::immediate_mean>
203 : feature_of<tag::mean>
207 // for the purposes of feature-based dependency resolution,
208 // immediate_mean provides the same feature as mean
210 struct feature_of<tag::immediate_mean_of_weights>
211 : feature_of<tag::mean_of_weights>
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> >
223 // So that mean can be automatically substituted with
224 // weighted_mean when the weight parameter is non-void.
226 struct as_weighted_feature<tag::mean>
228 typedef tag::weighted_mean type;
232 struct feature_of<tag::weighted_mean>
233 : feature_of<tag::mean>
236 // So that immediate_mean can be automatically substituted with
237 // immediate_weighted_mean when the weight parameter is non-void.
239 struct as_weighted_feature<tag::immediate_mean>
241 typedef tag::immediate_weighted_mean type;
245 struct feature_of<tag::immediate_weighted_mean>
246 : feature_of<tag::immediate_mean>
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> >
254 typedef tag::weighted_mean_of_variates<VariateType, VariateTag> type;
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> >
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> >
268 typedef tag::immediate_weighted_mean_of_variates<VariateType, VariateTag> type;
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> >
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.
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> >
289 // template<typename Args>
290 // droppable_accumulator(Args const &args)
291 // : droppable_accumulator::base(args)
296 }} // namespace boost::accumulators