Chris@16
|
1 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 // weighted_moment.hpp
|
Chris@16
|
3 //
|
Chris@16
|
4 // Copyright 2006, Eric Niebler, Olivier Gygi. Distributed under the Boost
|
Chris@16
|
5 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7
|
Chris@16
|
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MOMENT_HPP_EAN_15_11_2005
|
Chris@16
|
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MOMENT_HPP_EAN_15_11_2005
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/config/no_tr1/cmath.hpp>
|
Chris@16
|
12 #include <boost/mpl/int.hpp>
|
Chris@16
|
13 #include <boost/mpl/assert.hpp>
|
Chris@16
|
14 #include <boost/mpl/placeholders.hpp>
|
Chris@16
|
15 #include <boost/preprocessor/arithmetic/inc.hpp>
|
Chris@16
|
16 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
|
Chris@16
|
17 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
|
Chris@16
|
18 #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
|
Chris@16
|
19 #include <boost/accumulators/framework/accumulator_base.hpp>
|
Chris@16
|
20 #include <boost/accumulators/framework/extractor.hpp>
|
Chris@16
|
21 #include <boost/accumulators/numeric/functional.hpp>
|
Chris@16
|
22 #include <boost/accumulators/framework/parameters/sample.hpp>
|
Chris@16
|
23 #include <boost/accumulators/framework/depends_on.hpp>
|
Chris@16
|
24 #include <boost/accumulators/statistics_fwd.hpp>
|
Chris@16
|
25 #include <boost/accumulators/statistics/count.hpp>
|
Chris@16
|
26 #include <boost/accumulators/statistics/moment.hpp> // for pow()
|
Chris@16
|
27 #include <boost/accumulators/statistics/sum.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 namespace boost { namespace accumulators
|
Chris@16
|
30 {
|
Chris@16
|
31
|
Chris@16
|
32 namespace impl
|
Chris@16
|
33 {
|
Chris@16
|
34 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
35 // weighted_moment_impl
|
Chris@16
|
36 template<typename N, typename Sample, typename Weight>
|
Chris@16
|
37 struct weighted_moment_impl
|
Chris@16
|
38 : accumulator_base // TODO: also depends_on sum of powers
|
Chris@16
|
39 {
|
Chris@16
|
40 BOOST_MPL_ASSERT_RELATION(N::value, >, 0);
|
Chris@16
|
41 typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
|
Chris@16
|
42 // for boost::result_of
|
Chris@16
|
43 typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
|
Chris@16
|
44
|
Chris@16
|
45 template<typename Args>
|
Chris@16
|
46 weighted_moment_impl(Args const &args)
|
Chris@16
|
47 : sum(args[sample | Sample()] * numeric::one<Weight>::value)
|
Chris@16
|
48 {
|
Chris@16
|
49 }
|
Chris@16
|
50
|
Chris@16
|
51 template<typename Args>
|
Chris@16
|
52 void operator ()(Args const &args)
|
Chris@16
|
53 {
|
Chris@16
|
54 this->sum += args[weight] * numeric::pow(args[sample], N());
|
Chris@16
|
55 }
|
Chris@16
|
56
|
Chris@16
|
57 template<typename Args>
|
Chris@16
|
58 result_type result(Args const &args) const
|
Chris@16
|
59 {
|
Chris@16
|
60 return numeric::fdiv(this->sum, sum_of_weights(args));
|
Chris@16
|
61 }
|
Chris@16
|
62
|
Chris@16
|
63 private:
|
Chris@16
|
64 weighted_sample sum;
|
Chris@16
|
65 };
|
Chris@16
|
66
|
Chris@16
|
67 } // namespace impl
|
Chris@16
|
68
|
Chris@16
|
69 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
70 // tag::weighted_moment
|
Chris@16
|
71 //
|
Chris@16
|
72 namespace tag
|
Chris@16
|
73 {
|
Chris@16
|
74 template<int N>
|
Chris@16
|
75 struct weighted_moment
|
Chris@16
|
76 : depends_on<count, sum_of_weights>
|
Chris@16
|
77 {
|
Chris@16
|
78 /// INTERNAL ONLY
|
Chris@16
|
79 ///
|
Chris@16
|
80 typedef accumulators::impl::weighted_moment_impl<mpl::int_<N>, mpl::_1, mpl::_2> impl;
|
Chris@16
|
81 };
|
Chris@16
|
82 }
|
Chris@16
|
83
|
Chris@16
|
84 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
85 // extract::weighted_moment
|
Chris@16
|
86 //
|
Chris@16
|
87 namespace extract
|
Chris@16
|
88 {
|
Chris@16
|
89 BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, weighted_moment, (int))
|
Chris@16
|
90 }
|
Chris@16
|
91
|
Chris@16
|
92 using extract::weighted_moment;
|
Chris@16
|
93
|
Chris@16
|
94 }} // namespace boost::accumulators
|
Chris@16
|
95
|
Chris@16
|
96 #endif
|