1 /* 2 * This file is part of the GROMACS molecular simulation package. 3 * 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. 8 * 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. 13 * 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. 18 * 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. 23 * 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. 31 * 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. 34 */ 35 /*! \libinternal \file 36 * \brief 37 * Declares an exponential moving average class. 38 * 39 * \author Christian Blau <blau@kth.se> 40 * \inlibraryapi 41 * \ingroup module_math 42 */ 43 #ifndef GMX_MATH_EXPONENTIALMOVINGAVERAGE_H 44 #define GMX_MATH_EXPONENTIALMOVINGAVERAGE_H 45 46 #include "gromacs/utility/keyvaluetreebuilder.h" 47 #include "gromacs/utility/real.h" 48 49 namespace gmx 50 { 51 52 /*! \libinternal \brief Store the state of exponential moving averages. 53 */ 54 struct ExponentialMovingAverageState 55 { 56 //! The weighted sum 57 real weightedSum_ = 0; 58 59 //! The weighted count, used for bias correction 60 real weightedCount_ = 0; 61 62 //! Remember if adding the latest data point increased the average 63 bool increasing_ = false; 64 }; 65 66 //! Convert the exponential moving average state as key-value-tree object 67 void exponentialMovingAverageStateAsKeyValueTree(KeyValueTreeObjectBuilder builder, 68 const ExponentialMovingAverageState& state); 69 70 //! Sets the expoential moving average state from a key-value-tree object 71 ExponentialMovingAverageState exponentialMovingAverageStateFromKeyValueTree(const KeyValueTreeObject& object); 72 73 /*! \libinternal 74 * \brief Evaluate the exponential moving average with bias correction. 75 * 76 * The exponential moving average at the 0th data point \f$Y_0\f$ is 77 * \f$ S_0 = Y_0 \f$ and at the n-th data point \f$Y_n\f$ with n>0 it is 78 * \f$ S_n = \alpha Y_n + (1-\alpha) S_{n-1} \f$, where the smoothing factor 79 * \f$\alpha=1/t\f$ is determined via a time constant \f$t\f$. 80 * 81 * To avoid large impact of the first data point in a "burn-in" phase, the 82 * weight of points are unbiased by substituting for \f$S_{n-1}\f$ above, 83 * \f$\hat{S}_{n-1} = S_{n-1} / (1-\alpha^{n})\f$. 84 */ 85 class ExponentialMovingAverage 86 { 87 public: 88 /*! \brief Construct by setting the time constant and state. 89 * Allows reinitiating with data from memory. 90 * \param[in] timeConstant time in number of data points 91 * \param[in] state of the exponential moving average 92 * \throws InconsistentInputError if timeConstant < 1 93 */ 94 ExponentialMovingAverage(real timeConstant, const ExponentialMovingAverageState& state = {}); 95 96 //! Update the moving average with a data point 97 void updateWithDataPoint(real dataPoint); 98 99 //! The exponential weighted average with bias correction 100 real biasCorrectedAverage() const; 101 102 //! Returns true if last added data point increased the average 103 bool increasing() const; 104 105 //! Return the current state of the exponential moving average 106 const ExponentialMovingAverageState& state() const; 107 108 //! The inverse time constant for the exponential moving average 109 real inverseTimeConstant() const; 110 111 private: 112 //! The current state of the exponential moving average 113 ExponentialMovingAverageState state_; 114 115 //! The inverse time constant for the exponential moving average 116 real inverseTimeConstant_; 117 }; 118 119 } // namespace gmx 120 121 #endif 122