15#ifndef MOMENTUM_STATISTIC_H
16#define MOMENTUM_STATISTIC_H
18#include "momentum_export.h"
19#include "momentum_namespace.h"
20#include <agx/Vector.h>
35 virtual double update(
double observation) = 0;
40 virtual double get()
const = 0;
60 : m_smoothFactor(smoothFactor), m_s(initialObservation) {}
62 virtual inline double update(
double observation)
64 m_s += m_smoothFactor * (observation - m_s);
73 void set(
double val) { m_s = val; }
75 inline double get()
const {
return m_s; }
85 inline void setSmoothFactor(
double smoothFactor) { m_smoothFactor = smoothFactor; }
105 : m_maxHistorySize(maxHistorySize)
107 m_historyByAge.reserve(m_maxHistorySize);
108 m_historyByValue.reserve(m_maxHistorySize);
114 virtual double get()
const override
116 size_t n = this->getCurrentHistorySize();
123 size_t middle = n / 2;
126 return (m_historyByValue[middle - 1] + m_historyByValue[middle]) / double(2.0);
129 return m_historyByValue[middle];
138 virtual double update(
double observation)
override
141 while (this->getCurrentHistorySize() >= this->getMaxHistorySize()) {
142 double v = m_historyByAge.back();
143 m_historyByAge.pop_back();
144 m_historyByValue.findAndErase(v,
false);
149 auto insertAt = lower_bound(begin(m_historyByValue), end(m_historyByValue), observation);
150 m_historyByValue.insert(insertAt, observation);
151 m_historyByAge.insert(
size_t(0), observation);
159 m_historyByAge.clear();
160 m_historyByValue.clear();
166 return m_maxHistorySize;
171 agxAssert(m_historyByAge.size() == m_historyByValue.size());
172 size_t n = m_historyByAge.size();
177 size_t m_maxHistorySize;
178 agx::RealVector m_historyByAge;
179 agx::RealVector m_historyByValue;
Exponential moving average statistic.
Definition: Statistic.h:53
double get() const
Definition: Statistic.h:75
void set(double val)
Set the raw value of the statistic scalar.
Definition: Statistic.h:73
double getSmoothFactor() const
Definition: Statistic.h:80
virtual ~ExponentialMovingAverageStatistic()
Definition: Statistic.h:87
virtual double update(double observation)
Updates the statistic given the current observation and returns the current value of this statistic.
Definition: Statistic.h:62
ExponentialMovingAverageStatistic(double smoothFactor, double initialObservation=0)
Definition: Statistic.h:59
double m_s
Definition: Statistic.h:91
void setSmoothFactor(double smoothFactor)
Definition: Statistic.h:85
double m_smoothFactor
Definition: Statistic.h:90
Abstract base class for handling smoothing of data points.
Definition: Statistic.h:28
virtual ~Statistic()
Definition: Statistic.h:42
virtual double get() const =0
virtual double update(double observation)=0
Updates the statistic given the current observation and returns the current value of this statistic.
Definition: Statistic.h:23