1 ///////////////////////////////////////////////////////////////////////////////
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)
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MEAN_HPP_EAN_03_11_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MEAN_HPP_EAN_03_11_2005
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>
25 namespace boost { namespace accumulators
30 ///////////////////////////////////////////////////////////////////////////////
33 template<typename Sample, typename Weight, typename Tag>
34 struct weighted_mean_impl
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;
41 weighted_mean_impl(dont_care) {}
43 template<typename Args>
44 result_type result(Args const &args) const
48 is_same<Tag, tag::sample>
50 , tag::weighted_sum_of_variates<Sample, Tag>
54 extractor<weighted_sum_tag> const some_weighted_sum = {};
56 return numeric::fdiv(some_weighted_sum(args), sum_of_weights(args));
60 ///////////////////////////////////////////////////////////////////////////////
61 // immediate_weighted_mean_impl
63 template<typename Sample, typename Weight, typename Tag>
64 struct immediate_weighted_mean_impl
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;
71 template<typename Args>
72 immediate_weighted_mean_impl(Args const &args)
75 args[parameter::keyword<Tag>::get() | Sample()]
76 * numeric::one<Weight>::value
77 , numeric::one<Weight>::value
83 template<typename Args>
84 void operator ()(Args const &args)
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);
95 result_type result(dont_care) const
106 ///////////////////////////////////////////////////////////////////////////////
107 // tag::weighted_mean
108 // tag::immediate_weighted_mean
113 : depends_on<sum_of_weights, weighted_sum>
117 typedef accumulators::impl::weighted_mean_impl<mpl::_1, mpl::_2, tag::sample> impl;
119 struct immediate_weighted_mean
120 : depends_on<sum_of_weights>
124 typedef accumulators::impl::immediate_weighted_mean_impl<mpl::_1, mpl::_2, tag::sample> impl;
126 template<typename VariateType, typename VariateTag>
127 struct weighted_mean_of_variates
128 : depends_on<sum_of_weights, weighted_sum_of_variates<VariateType, VariateTag> >
132 typedef accumulators::impl::weighted_mean_impl<VariateType, mpl::_2, VariateTag> impl;
134 template<typename VariateType, typename VariateTag>
135 struct immediate_weighted_mean_of_variates
136 : depends_on<sum_of_weights>
140 typedef accumulators::impl::immediate_weighted_mean_impl<VariateType, mpl::_2, VariateTag> impl;
144 ///////////////////////////////////////////////////////////////////////////////
145 // extract::weighted_mean
146 // extract::weighted_mean_of_variates
150 extractor<tag::mean> const weighted_mean = {};
151 BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, weighted_mean_of_variates, (typename)(typename))
153 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_mean)
156 using extract::weighted_mean;
157 using extract::weighted_mean_of_variates;
159 // weighted_mean(lazy) -> weighted_mean
161 struct as_feature<tag::weighted_mean(lazy)>
163 typedef tag::weighted_mean type;
166 // weighted_mean(immediate) -> immediate_weighted_mean
168 struct as_feature<tag::weighted_mean(immediate)>
170 typedef tag::immediate_weighted_mean type;
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)>
177 typedef tag::weighted_mean_of_variates<VariateType, VariateTag> type;
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)>
184 typedef tag::immediate_weighted_mean_of_variates<VariateType, VariateTag> type;
187 }} // namespace boost::accumulators