Momentum Scripting v1
Loading...
Searching...
No Matches
Statistic.h
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material without having a written signed agreement with Algoryx Simulation AB.
9
10Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
11from using this software, unless otherwise stated in written agreements with
12Algoryx Simulation AB.
13*/
14
15#ifndef MOMENTUM_STATISTIC_H
16#define MOMENTUM_STATISTIC_H
17
18#include "momentum_export.h"
19#include "momentum_namespace.h"
20#include <agx/Vector.h>
21
22namespace v1
23{
27 class MOMENTUM_EXPORT Statistic
28 {
29 public:
35 virtual double update(double observation) = 0;
36
40 virtual double get() const = 0;
41
42 virtual ~Statistic() {}
43 };
44
45
52 class MOMENTUM_EXPORT ExponentialMovingAverageStatistic : public Statistic
53 {
54 public:
59 ExponentialMovingAverageStatistic(double smoothFactor, double initialObservation = 0)
60 : m_smoothFactor(smoothFactor), m_s(initialObservation) {}
61
62 virtual inline double update(double observation)
63 {
64 m_s += m_smoothFactor * (observation - m_s);
65 return m_s;
66 }
67
73 void set(double val) { m_s = val; }
74
75 inline double get() const { return m_s; }
76
80 inline double getSmoothFactor() const { return m_smoothFactor; }
81
85 inline void setSmoothFactor(double smoothFactor) { m_smoothFactor = smoothFactor; }
86
88
89 protected:
91 double m_s;
92 };
93
98 class MOMENTUM_EXPORT MedianStatistic : public Statistic
99 {
100 public:
104 MedianStatistic(size_t maxHistorySize)
105 : m_maxHistorySize(maxHistorySize)
106 {
107 m_historyByAge.reserve(m_maxHistorySize);
108 m_historyByValue.reserve(m_maxHistorySize);
109 }
110
114 virtual double get() const override
115 {
116 size_t n = this->getCurrentHistorySize();
117
118 if (n == 0) {
120 return double(0.0);
121 }
122
123 size_t middle = n / 2;
124 if (n % 2 == 0) {
125 // Two in the middle, return their average.
126 return (m_historyByValue[middle - 1] + m_historyByValue[middle]) / double(2.0);
127 }
128 else {
129 return m_historyByValue[middle];
130 }
131 }
132
138 virtual double update(double observation) override
139 {
140 // Remove old values.
141 while (this->getCurrentHistorySize() >= this->getMaxHistorySize()) {
142 double v = m_historyByAge.back();
143 m_historyByAge.pop_back();
144 m_historyByValue.findAndErase(v, false);
145 }
146
147 // Insert new value.
148 using namespace std;
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);
152
153 return this->get();
154 }
155
157 void clear()
158 {
159 m_historyByAge.clear();
160 m_historyByValue.clear();
161 }
162
163
164 size_t getMaxHistorySize() const
165 {
166 return m_maxHistorySize;
167 }
168
170 {
171 agxAssert(m_historyByAge.size() == m_historyByValue.size());
172 size_t n = m_historyByAge.size();
173 return n;
174 }
175
176 private:
177 size_t m_maxHistorySize;
178 agx::RealVector m_historyByAge;
179 agx::RealVector m_historyByValue;
180 };
181
182}
183#endif
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
Median statistic that keeps a history of a number of observations and reports the median,...
Definition: Statistic.h:99
MedianStatistic(size_t maxHistorySize)
Definition: Statistic.h:104
void clear()
Reset all history.
Definition: Statistic.h:157
size_t getMaxHistorySize() const
Definition: Statistic.h:164
virtual double update(double observation) override
Add a new observation to the history, removing the oldest if the maximum history size has been reache...
Definition: Statistic.h:138
virtual double get() const override
Definition: Statistic.h:114
size_t getCurrentHistorySize() const
Definition: Statistic.h:169
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