CBOE Emulator  1.0
exponential_moving_variance.hpp
1 // A structure for calculating an exponential moving variance.
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_VARIANCE
19 #define MATHS_EXPONENTIAL_MOVING_VARIANCE
20 
21 #include <algorithm>
22 #include <atomic>
23 #include <cmath>
24 
26 namespace Maths {
27 
56 template<typename T = float>
58  private:
60  std::atomic<T> alpha;
62  std::atomic<T> average;
64  std::atomic<T> variance = 0.f;
65 
66  public:
68  static constexpr T ALPHA_MIN = 0;
70  static constexpr T ALPHA_MAX = 1;
71 
78  explicit ExponentialMovingVariance(T alpha_, T observation = 0) :
79  alpha(std::clamp(alpha_, ALPHA_MIN, ALPHA_MAX)), average(observation) { }
80 
86  inline void reset(T observation = 0) {
87  average = observation;
88  variance = 0;
89  }
90 
95  inline void set_alpha(T alpha_) {
96  alpha = std::clamp(alpha_, ALPHA_MIN, ALPHA_MAX);
97  }
98 
104  inline T get_alpha() const { return alpha; }
105 
113  inline T process(T observation) {
114  T delta = observation - average;
115  average = average + alpha * delta;
116  variance = (ALPHA_MAX - alpha) * (variance + alpha * delta * delta);
117  return delta;
118  }
119 
125  inline T get_average() const { return average; }
126 
132  inline T get_variance() const { return variance; }
133 
139  inline T get_stddev() const { return sqrt(variance); }
140 };
141 
142 } // namespace Maths
143 
144 #endif // MATHS_EXPONENTIAL_MOVING_VARIANCE
Maths::ExponentialMovingVariance::get_stddev
T get_stddev() const
Return the current value of the exponential moving standard deviation .
Definition: exponential_moving_variance.hpp:139
Maths::ExponentialMovingVariance::ALPHA_MAX
static constexpr T ALPHA_MAX
the maximal value for alpha to take
Definition: exponential_moving_variance.hpp:70
Maths::ExponentialMovingVariance::ExponentialMovingVariance
ExponentialMovingVariance(T alpha_, T observation=0)
Initialize an exponential moving variance with given and .
Definition: exponential_moving_variance.hpp:78
Maths::ExponentialMovingVariance
A structure for calculating an exponential moving variance.
Definition: exponential_moving_variance.hpp:57
Maths::ExponentialMovingVariance::set_alpha
void set_alpha(T alpha_)
Set the parameter to a new value.
Definition: exponential_moving_variance.hpp:95
Maths::ExponentialMovingVariance::ALPHA_MIN
static constexpr T ALPHA_MIN
the minimal value for alpha to take
Definition: exponential_moving_variance.hpp:68
Maths::ExponentialMovingVariance::get_alpha
T get_alpha() const
Return the parameter.
Definition: exponential_moving_variance.hpp:104
Maths::ExponentialMovingVariance::get_variance
T get_variance() const
Return the current value of the exponential moving variance .
Definition: exponential_moving_variance.hpp:132
Maths::ExponentialMovingVariance::get_average
T get_average() const
Return the current value of the exponential moving average .
Definition: exponential_moving_variance.hpp:125
Maths
Functions for doing maths.
Definition: exponential_moving_average.hpp:25
Maths::ExponentialMovingVariance::process
T process(T observation)
Calculate the next average and variance based on observation .
Definition: exponential_moving_variance.hpp:113
Maths::ExponentialMovingVariance::reset
void reset(T observation=0)
Reset the average to initial state with observation . The initial variance is 0.
Definition: exponential_moving_variance.hpp:86