Skip to content

Commit fdabe51

Browse files
authored
Merge pull request #14 from oliness/patch-11437
Fix #11437: correct immediate_rolling_mean
2 parents b133840 + 5ff527e commit fdabe51

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

include/boost/accumulators/statistics/rolling_mean.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,18 @@ namespace boost { namespace accumulators
6767
{
6868
if(is_rolling_window_plus1_full(args))
6969
{
70-
mean_ += numeric::fdiv(args[sample]-rolling_window_plus1(args).front(),rolling_count(args));
70+
if (rolling_window_plus1(args).front() > args[sample])
71+
mean_ -= numeric::fdiv(rolling_window_plus1(args).front()-args[sample],rolling_count(args));
72+
else if (rolling_window_plus1(args).front() < args[sample])
73+
mean_ += numeric::fdiv(args[sample]-rolling_window_plus1(args).front(),rolling_count(args));
7174
}
7275
else
7376
{
7477
result_type prev_mean = mean_;
75-
mean_ += numeric::fdiv(args[sample]-prev_mean,rolling_count(args));
78+
if (prev_mean > args[sample])
79+
mean_ -= numeric::fdiv(prev_mean-args[sample],rolling_count(args));
80+
else if (prev_mean < args[sample])
81+
mean_ += numeric::fdiv(args[sample]-prev_mean,rolling_count(args));
7682
}
7783
}
7884

test/rolling_mean.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,34 @@ test_rolling_mean_test_impl(accumulator_set_type& acc)
5454
assert_is_double(rolling_mean(acc));
5555
}
5656

57+
template<typename accumulator_set_type>
58+
void
59+
test_rolling_mean_unsigned_test_impl(accumulator_set_type& acc)
60+
{
61+
acc(7U);
62+
BOOST_CHECK_CLOSE(7., rolling_mean(acc), 1e-5);
63+
64+
acc(6U);
65+
BOOST_CHECK_CLOSE(6.5, rolling_mean(acc), 1e-5);
66+
67+
acc(5U);
68+
BOOST_CHECK_CLOSE(6., rolling_mean(acc), 1e-5);
69+
70+
acc(4U);
71+
BOOST_CHECK_CLOSE(5.5, rolling_mean(acc), 1e-5);
72+
73+
acc(3U);
74+
BOOST_CHECK_CLOSE(5., rolling_mean(acc), 1e-5);
75+
76+
acc(2U);
77+
BOOST_CHECK_CLOSE(4., rolling_mean(acc), 1e-5);
78+
79+
acc(1U);
80+
BOOST_CHECK_CLOSE(3., rolling_mean(acc), 1e-5);
81+
82+
assert_is_double(rolling_mean(acc));
83+
}
84+
5785
///////////////////////////////////////////////////////////////////////////////
5886
// test_rolling_mean
5987
void test_rolling_mean()
@@ -97,6 +125,14 @@ void test_rolling_mean()
97125
BOOST_CHECK (sizeof(acc_lazy_rolling_mean) == sizeof(acc_lazy_rolling_mean3));
98126
BOOST_CHECK (sizeof(acc_immediate_rolling_mean) == sizeof(acc_immediate_rolling_mean2));
99127
BOOST_CHECK (sizeof(acc_immediate_rolling_mean) == sizeof(acc_immediate_rolling_mean3));
128+
129+
//// test unsigned int with both implementations
130+
accumulator_set<unsigned int,stats<tag::immediate_rolling_mean> >
131+
acc_immediate_rolling_mean4(tag::immediate_rolling_mean::window_size = window_size),
132+
acc_immediate_rolling_mean5(tag::immediate_rolling_mean::window_size = window_size, sample = 0);
133+
134+
test_rolling_mean_unsigned_test_impl(acc_immediate_rolling_mean4);
135+
test_rolling_mean_unsigned_test_impl(acc_immediate_rolling_mean5);
100136
}
101137

102138
///////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)