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_MOMENT_HPP_EAN_15_11_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MOMENT_HPP_EAN_15_11_2005
11 #include <boost/config/no_tr1/cmath.hpp>
12 #include <boost/mpl/int.hpp>
13 #include <boost/mpl/assert.hpp>
14 #include <boost/mpl/placeholders.hpp>
15 #include <boost/preprocessor/arithmetic/inc.hpp>
16 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
17 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
18 #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
19 #include <boost/accumulators/framework/accumulator_base.hpp>
20 #include <boost/accumulators/framework/extractor.hpp>
21 #include <boost/accumulators/numeric/functional.hpp>
22 #include <boost/accumulators/framework/parameters/sample.hpp>
23 #include <boost/accumulators/framework/depends_on.hpp>
24 #include <boost/accumulators/statistics_fwd.hpp>
25 #include <boost/accumulators/statistics/count.hpp>
26 #include <boost/accumulators/statistics/moment.hpp> // for pow()
27 #include <boost/accumulators/statistics/sum.hpp>
29 namespace boost { namespace accumulators
34 ///////////////////////////////////////////////////////////////////////////////
35 // weighted_moment_impl
36 template<typename N, typename Sample, typename Weight>
37 struct weighted_moment_impl
38 : accumulator_base // TODO: also depends_on sum of powers
40 BOOST_MPL_ASSERT_RELATION(N::value, >, 0);
41 typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
42 // for boost::result_of
43 typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
45 template<typename Args>
46 weighted_moment_impl(Args const &args)
47 : sum(args[sample | Sample()] * numeric::one<Weight>::value)
51 template<typename Args>
52 void operator ()(Args const &args)
54 this->sum += args[weight] * numeric::pow(args[sample], N());
57 template<typename Args>
58 result_type result(Args const &args) const
60 return numeric::fdiv(this->sum, sum_of_weights(args));
69 ///////////////////////////////////////////////////////////////////////////////
70 // tag::weighted_moment
75 struct weighted_moment
76 : depends_on<count, sum_of_weights>
80 typedef accumulators::impl::weighted_moment_impl<mpl::int_<N>, mpl::_1, mpl::_2> impl;
84 ///////////////////////////////////////////////////////////////////////////////
85 // extract::weighted_moment
89 BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, weighted_moment, (int))
92 using extract::weighted_moment;
94 }} // namespace boost::accumulators