CBOE Emulator  1.0
exponential_moving_average.hpp
1 // A structure for calculating an exponential moving average.
2 // Copyright 2020 Christian Kauten
3 //
4 // Author: Christian Kauten (kautenja@auburn.edu)
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef MATHS_EXPONENTIAL_MOVING_AVERAGE
19 #define MATHS_EXPONENTIAL_MOVING_AVERAGE
20 
21 #include <algorithm>
22 #include <atomic>
23 
25 namespace Maths {
26 
43 template<typename T = float>
45  private:
47  std::atomic<T> alpha;
49  std::atomic<T> average;
50 
51  public:
53  static constexpr T ALPHA_MIN = 0;
55  static constexpr T ALPHA_MAX = 1;
56 
63  explicit ExponentialMovingAverage(T alpha_, T observation = 0) :
64  alpha(std::clamp(alpha_, ALPHA_MIN, ALPHA_MAX)), average(observation) { }
65 
71  inline void reset(T observation = 0) { average = observation; }
72 
77  inline void set_alpha(T alpha_) {
78  alpha = std::clamp(alpha_, ALPHA_MIN, ALPHA_MAX);
79  }
80 
85  inline T get_alpha() const { return alpha; }
86 
92  inline void process(T observation) {
93  average = average + alpha * (observation - average);
94  }
95 
101  inline T get_average() const { return average; }
102 };
103 
104 } // namespace Maths
105 
106 #endif // MATHS_EXPONENTIAL_MOVING_AVERAGE
Maths::ExponentialMovingAverage::get_alpha
T get_alpha() const
Return the parameter.
Definition: exponential_moving_average.hpp:85
Maths::ExponentialMovingAverage::process
void process(T observation)
Calculate the next average based on observation .
Definition: exponential_moving_average.hpp:92
Maths::ExponentialMovingAverage::reset
void reset(T observation=0)
Reset the average to initial state with observation .
Definition: exponential_moving_average.hpp:71
Maths::ExponentialMovingAverage::ExponentialMovingAverage
ExponentialMovingAverage(T alpha_, T observation=0)
Initialize an exponential moving average with given and .
Definition: exponential_moving_average.hpp:63
Maths::ExponentialMovingAverage::set_alpha
void set_alpha(T alpha_)
Set the parameter to a new value.
Definition: exponential_moving_average.hpp:77
Maths::ExponentialMovingAverage
A structure for calculating an exponential moving average.
Definition: exponential_moving_average.hpp:44
Maths
Functions for doing maths.
Definition: exponential_moving_average.hpp:25
Maths::ExponentialMovingAverage::ALPHA_MAX
static constexpr T ALPHA_MAX
the maximal value for alpha to take
Definition: exponential_moving_average.hpp:55
Maths::ExponentialMovingAverage::get_average
T get_average() const
Return the current value of the exponential moving average .
Definition: exponential_moving_average.hpp:101
Maths::ExponentialMovingAverage::ALPHA_MIN
static constexpr T ALPHA_MIN
the minimal value for alpha to take
Definition: exponential_moving_average.hpp:53