Ticket #6128: RollingMean.hpp

File RollingMean.hpp, 3.0 KB (added by Pieter Ober <jaapaap@…>, 11 years ago)
Line 
1///////////////////////////////////////////////////////////////////////////////
2// rolling_mean.hpp
3// Copyright (C) 2005 Eric Niebler
4// Copyright (C) 2011 Pieter Bastiaan Ober (Integricom). 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)
7
8#ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_mean_HPP_EAN_15_11_2011
9#define BOOST_ACCUMULATORS_STATISTICS_ROLLING_mean_HPP_EAN_15_11_2011
10
11#include <boost/accumulators/accumulators.hpp>
12#include <boost/accumulators/statistics/stats.hpp>
13
14#include <boost/mpl/placeholders.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/sample.hpp>
19#include <boost/accumulators/framework/depends_on.hpp>
20#include <boost/accumulators/statistics_fwd.hpp>
21#include <boost/accumulators/statistics/rolling_window.hpp>
22#include <boost/accumulators/statistics/rolling_count.hpp>
23
24namespace boost { namespace accumulators
25{
26namespace impl
27{
28 ///////////////////////////////////////////////////////////////////////////////
29 // rolling_mean_impl
30 //
31 template<typename Sample>
32 struct rolling_mean_impl
33 : accumulator_base
34 {
35 typedef Sample result_type;
36
37 template<typename Args>
38 rolling_mean_impl(Args const &args)
39 : mean_(0.0)
40 {}
41
42 template<typename Args>
43 void operator()(Args const &args)
44 {
45 if(is_rolling_window_plus1_full(args))
46 {
47 mean_ += (args[sample]-rolling_window_plus1(args).front())/rolling_count(args);
48 }
49 else
50 {
51 Sample prev_mean = mean_;
52 mean_ += (args[sample]-prev_mean)/rolling_count(args);
53 }
54 }
55
56 template<typename Args>
57 result_type result(Args const &args) const
58 {
59 return mean_;
60 }
61
62 private:
63
64 Sample mean_;
65 };
66} // namespace impl
67
68///////////////////////////////////////////////////////////////////////////////
69// tag:: rolling_mean
70//
71namespace tag
72{
73 struct rolling_mean
74 : depends_on< rolling_window_plus1, rolling_count>
75 {
76 /// INTERNAL ONLY
77 ///
78 typedef accumulators::impl::rolling_mean_impl< mpl::_1 > impl;
79
80 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
81 /// tag::rolling_window::window_size named parameter
82 static boost::parameter::keyword<tag::rolling_window_size> const window_size;
83 #endif
84 };
85} // namespace tag
86
87///////////////////////////////////////////////////////////////////////////////
88// extract::rolling_mean
89//
90namespace extract
91{
92 extractor<tag::rolling_mean> const rolling_mean = {};
93
94 BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_mean)
95}
96
97using extract::rolling_mean;
98
99}} // namespace boost::accumulators
100
101#endif