2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Tests for the exponential moving average.
39 * \author Christian Blau <blau@kth.se>
40 * \ingroup module_math
44 #include "gromacs/math/exponentialmovingaverage.h"
46 #include <gtest/gtest.h>
48 #include "testutils/testasserts.h"
59 TEST(ExponentialMovingAverage, ThrowsWhenLagTimeIsZero)
61 EXPECT_THROW_GMX(ExponentialMovingAverage(0), InconsistentInputError);
64 TEST(ExponentialMovingAverage, ThrowsWhenLagTimeIsNegative)
66 EXPECT_THROW_GMX(ExponentialMovingAverage(-10), InconsistentInputError);
69 TEST(ExponentialMovingAverage, LagTimeOneYieldsInstantaneousValue)
71 const real lagTime = 1;
72 ExponentialMovingAverage exponentialMovingAverage(lagTime);
74 exponentialMovingAverage.updateWithDataPoint(10);
75 EXPECT_REAL_EQ(10, exponentialMovingAverage.biasCorrectedAverage());
77 exponentialMovingAverage.updateWithDataPoint(-10);
78 EXPECT_REAL_EQ(-10, exponentialMovingAverage.biasCorrectedAverage());
81 TEST(ExponentialMovingAverage, YieldsCorrectValue)
83 const real lagTime = 100;
84 ExponentialMovingAverage exponentialMovingAverage(lagTime);
86 exponentialMovingAverage.updateWithDataPoint(10);
87 EXPECT_REAL_EQ(10, exponentialMovingAverage.biasCorrectedAverage());
89 exponentialMovingAverage.updateWithDataPoint(-10);
90 EXPECT_REAL_EQ(-0.050251256281406857, exponentialMovingAverage.biasCorrectedAverage());
92 exponentialMovingAverage.updateWithDataPoint(0);
93 EXPECT_REAL_EQ(-0.03333221103666531, exponentialMovingAverage.biasCorrectedAverage());
96 TEST(ExponentialMovingAverage, SetAverageCorrectly)
98 const real lagTime = 100;
99 ExponentialMovingAverage exponentialMovingAverage(lagTime);
101 exponentialMovingAverage.updateWithDataPoint(10);
102 EXPECT_REAL_EQ(10, exponentialMovingAverage.biasCorrectedAverage());
104 ExponentialMovingAverageState thisState = exponentialMovingAverage.state();
106 ExponentialMovingAverage other(lagTime, thisState);
108 other.updateWithDataPoint(-10);
109 EXPECT_REAL_EQ(-0.050251256281406857, other.biasCorrectedAverage());
111 other.updateWithDataPoint(0);
112 EXPECT_REAL_EQ(-0.03333221103666531, other.biasCorrectedAverage());
115 TEST(ExponentialMovingAverage, DeterminesCorrectlyIfIncreasing)
117 const real lagTime = 100;
118 ExponentialMovingAverage exponentialMovingAverage(lagTime);
120 exponentialMovingAverage.updateWithDataPoint(10);
121 exponentialMovingAverage.updateWithDataPoint(10);
123 EXPECT_FALSE(exponentialMovingAverage.increasing());
125 exponentialMovingAverage.updateWithDataPoint(-10);
127 EXPECT_FALSE(exponentialMovingAverage.increasing());
129 exponentialMovingAverage.updateWithDataPoint(100);
130 EXPECT_TRUE(exponentialMovingAverage.increasing());
134 TEST(ExponentialMovingAverage, InverseLagTimeCorrect)
136 const real lagTime = 2.;
137 ExponentialMovingAverage exponentialMovingAverage(lagTime);
138 EXPECT_REAL_EQ(0.5, exponentialMovingAverage.inverseTimeConstant());
141 TEST(ExponentialMovingAverage, RoundTripAsKeyValueTree)
143 KeyValueTreeBuilder builder;
144 const real weightedSum = 9;
145 const real weightedCount = 1;
146 const bool increasing = true;
147 ExponentialMovingAverageState state = { weightedSum, weightedCount, increasing };
148 exponentialMovingAverageStateAsKeyValueTree(builder.rootObject(), state);
150 KeyValueTreeObject result = builder.build();
151 state = exponentialMovingAverageStateFromKeyValueTree(result);
152 EXPECT_EQ(weightedSum, state.weightedSum_);
153 EXPECT_EQ(weightedCount, state.weightedCount_);
154 EXPECT_EQ(increasing, state.increasing_);