]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // rolling_mean.hpp | |
3 | // Copyright (C) 2008 Eric Niebler. | |
4 | // Copyright (C) 2012 Pieter Bastiaan Ober (Integricom). | |
5 | // Distributed under the Boost Software License, Version 1.0. | |
6 | // (See accompanying file LICENSE_1_0.txt or copy at | |
7 | // http://www.boost.org/LICENSE_1_0.txt) | |
8 | ||
9 | #ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_MEAN_HPP_EAN_26_12_2008 | |
10 | #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MEAN_HPP_EAN_26_12_2008 | |
11 | ||
12 | #include <boost/mpl/placeholders.hpp> | |
13 | #include <boost/accumulators/framework/accumulator_base.hpp> | |
14 | #include <boost/accumulators/framework/extractor.hpp> | |
15 | #include <boost/accumulators/numeric/functional.hpp> | |
16 | #include <boost/accumulators/framework/parameters/sample.hpp> | |
17 | #include <boost/accumulators/framework/depends_on.hpp> | |
18 | #include <boost/accumulators/statistics_fwd.hpp> | |
19 | #include <boost/accumulators/statistics/rolling_sum.hpp> | |
20 | #include <boost/accumulators/statistics/rolling_count.hpp> | |
21 | ||
22 | namespace boost { namespace accumulators | |
23 | { | |
24 | namespace impl | |
25 | { | |
26 | /////////////////////////////////////////////////////////////////////////////// | |
27 | // lazy_rolling_mean_impl | |
28 | // returns the mean over the rolling window and is calculated only | |
29 | // when the result is requested | |
30 | template<typename Sample> | |
31 | struct lazy_rolling_mean_impl | |
32 | : accumulator_base | |
33 | { | |
34 | // for boost::result_of | |
35 | typedef typename numeric::functional::fdiv<Sample, std::size_t, void, void>::result_type result_type; | |
36 | ||
37 | lazy_rolling_mean_impl(dont_care) | |
38 | { | |
39 | } | |
40 | ||
41 | template<typename Args> | |
42 | result_type result(Args const &args) const | |
43 | { | |
44 | return numeric::fdiv(rolling_sum(args), rolling_count(args)); | |
45 | } | |
46 | }; | |
47 | ||
48 | /////////////////////////////////////////////////////////////////////////////// | |
49 | // immediate_rolling_mean_impl | |
50 | // The non-lazy version computes the rolling mean recursively when a new | |
51 | // sample is added | |
52 | template<typename Sample> | |
53 | struct immediate_rolling_mean_impl | |
54 | : accumulator_base | |
55 | { | |
56 | // for boost::result_of | |
57 | typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type; | |
58 | ||
59 | template<typename Args> | |
60 | immediate_rolling_mean_impl(Args const &args) | |
61 | : mean_(numeric::fdiv(args[sample | Sample()],numeric::one<std::size_t>::value)) | |
62 | { | |
63 | } | |
64 | ||
65 | template<typename Args> | |
66 | void operator()(Args const &args) | |
67 | { | |
68 | if(is_rolling_window_plus1_full(args)) | |
69 | { | |
70 | mean_ += numeric::fdiv(args[sample]-rolling_window_plus1(args).front(),rolling_count(args)); | |
71 | } | |
72 | else | |
73 | { | |
74 | result_type prev_mean = mean_; | |
75 | mean_ += numeric::fdiv(args[sample]-prev_mean,rolling_count(args)); | |
76 | } | |
77 | } | |
78 | ||
79 | template<typename Args> | |
80 | result_type result(Args const &) const | |
81 | { | |
82 | return mean_; | |
83 | } | |
84 | ||
85 | private: | |
86 | ||
87 | result_type mean_; | |
88 | }; | |
89 | } // namespace impl | |
90 | ||
91 | /////////////////////////////////////////////////////////////////////////////// | |
92 | // tag::lazy_rolling_mean | |
93 | // tag::immediate_rolling_mean | |
94 | // tag::rolling_mean | |
95 | // | |
96 | namespace tag | |
97 | { | |
98 | struct lazy_rolling_mean | |
99 | : depends_on< rolling_sum, rolling_count > | |
100 | { | |
101 | /// INTERNAL ONLY | |
102 | /// | |
103 | typedef accumulators::impl::lazy_rolling_mean_impl< mpl::_1 > impl; | |
104 | ||
105 | #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED | |
106 | /// tag::rolling_window::window_size named parameter | |
107 | static boost::parameter::keyword<tag::rolling_window_size> const window_size; | |
108 | #endif | |
109 | }; | |
110 | ||
111 | struct immediate_rolling_mean | |
112 | : depends_on< rolling_window_plus1, rolling_count> | |
113 | { | |
114 | /// INTERNAL ONLY | |
115 | /// | |
116 | typedef accumulators::impl::immediate_rolling_mean_impl< mpl::_1> impl; | |
117 | ||
118 | #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED | |
119 | /// tag::rolling_window::window_size named parameter | |
120 | static boost::parameter::keyword<tag::rolling_window_size> const window_size; | |
121 | #endif | |
122 | }; | |
123 | ||
124 | // make immediate_rolling_mean the default implementation | |
125 | struct rolling_mean : immediate_rolling_mean {}; | |
126 | } // namespace tag | |
127 | ||
128 | /////////////////////////////////////////////////////////////////////////////// | |
129 | // extract::lazy_rolling_mean | |
130 | // extract::immediate_rolling_mean | |
131 | // extract::rolling_mean | |
132 | // | |
133 | namespace extract | |
134 | { | |
135 | extractor<tag::lazy_rolling_mean> const lazy_rolling_mean = {}; | |
136 | extractor<tag::immediate_rolling_mean> const immediate_rolling_mean = {}; | |
137 | extractor<tag::rolling_mean> const rolling_mean = {}; | |
138 | ||
139 | BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_rolling_mean) | |
140 | BOOST_ACCUMULATORS_IGNORE_GLOBAL(immediate_rolling_mean) | |
141 | BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_mean) | |
142 | } | |
143 | ||
144 | using extract::lazy_rolling_mean; | |
145 | using extract::immediate_rolling_mean; | |
146 | using extract::rolling_mean; | |
147 | ||
148 | // rolling_mean(lazy) -> lazy_rolling_mean | |
149 | template<> | |
150 | struct as_feature<tag::rolling_mean(lazy)> | |
151 | { | |
152 | typedef tag::lazy_rolling_mean type; | |
153 | }; | |
154 | ||
155 | // rolling_mean(immediate) -> immediate_rolling_mean | |
156 | template<> | |
157 | struct as_feature<tag::rolling_mean(immediate)> | |
158 | { | |
159 | typedef tag::immediate_rolling_mean type; | |
160 | }; | |
161 | ||
162 | // for the purposes of feature-based dependency resolution, | |
163 | // immediate_rolling_mean provides the same feature as rolling_mean | |
164 | template<> | |
165 | struct feature_of<tag::immediate_rolling_mean> | |
166 | : feature_of<tag::rolling_mean> | |
167 | { | |
168 | }; | |
169 | ||
170 | // for the purposes of feature-based dependency resolution, | |
171 | // lazy_rolling_mean provides the same feature as rolling_mean | |
172 | template<> | |
173 | struct feature_of<tag::lazy_rolling_mean> | |
174 | : feature_of<tag::rolling_mean> | |
175 | { | |
176 | }; | |
177 | }} // namespace boost::accumulators | |
178 | ||
179 | #endif |