Plaquette
 
Loading...
Searching...
No Matches
RobustScaler.h
1/*
2 * RobustScaler.h
3 *
4 * (c) 2025 Sofian Audry :: info(@)sofianaudry(.)com
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 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * (c) 2025 Sofian Audry
20 *
21 * Adaptive quantile-based scaler using Robbins–Monro updates.
22 */
23
24#ifndef SCALER_H_
25#define SCALER_H_
26
27#include "PqCore.h"
28#include "MovingFilter.h"
29#include "MovingAverage.h"
30
31namespace pq {
32
33// Default low quantile level (corresponds to 1% coverage of value in [0, 1]).
34#define ROBUST_SCALER_DEFAULT_SPAN 0.99f
35
36// Minimum value of base eta.
37#define ROBUST_SCALER_MIN_ETA 1e-4f
38
40class RobustScaler : public MovingFilter {
41public:
44
50
57
58 virtual ~RobustScaler() {}
59
61 virtual void span(float span);
62
64 virtual float span() const;
65
67 virtual void lowQuantileLevel(float level);
68
70 virtual float lowQuantileLevel() const { return _quantileLevel; }
71
73 virtual void highQuantileLevel(float level);
74
76 virtual float highQuantileLevel() const { return (1 - _quantileLevel); }
77
79 virtual float lowQuantile() const { return _lowQuantile; }
80
82 virtual float highQuantile() const { return _highQuantile; }
83
85 virtual float stdDev() const { return _stdDev.constGet(); }
86
88 virtual void reset();
89
91 virtual void reset(float estimatedMeanValue);
92
94 virtual void reset(float estimatedMinValue, float estimatedMaxValue);
95
97 virtual float get() { return _value; }
98
100 virtual float put(float value);
101
103 virtual float filter(float value);
104
105protected:
106 virtual void step();
107
108 // Internal quantile update (Robbins–Monro).
109 inline void _updateQuantile(float& q, float level, float eta, float x);
110
111 // Helper function to initialize range.
112 void _initializeRange(float minValue, float maxValue);
113
114protected:
115 // Low quantile level (in [0, 0.5]).
116 float _quantileLevel;
117
118 // Quantile estimators.
119 float _lowQuantile; // q_low
120 float _highQuantile; // q_high
121
122 // Stddev estimator.
123 MovingAverage _stdDev;
124
125 // Variables used to compute current value average during a step (in case of multiple calls to put()).
126 float _currentStdDevStep;
127};
128
129}
130
131#endif
The main Plaquette static class containing all the units.
Definition PqCore.h:63
static Engine & primary()
Returns the main instance of Plaquette.
Definition PqCore.cpp:30
An exponential moving average class.
Definition MovingAverage.h:34
Definition MovingFilter.h:39
Regularizes signal into [0,1] using adaptive quantile tracking (robust to outliers).
Definition RobustScaler.h:40
virtual float highQuantileLevel() const
Returns the current high quantile level.
Definition RobustScaler.h:76
virtual void reset()
Resets the filter.
Definition RobustScaler.cpp:122
virtual float put(float value)
Pushes a new value and returns the scaled output.
Definition RobustScaler.cpp:152
virtual float lowQuantile() const
Returns the current low quantile.
Definition RobustScaler.h:79
virtual float highQuantile() const
Returns the current high quantile.
Definition RobustScaler.h:82
virtual float filter(float value)
Returns the filtered value (without calibrating).
Definition RobustScaler.cpp:231
virtual float lowQuantileLevel() const
Returns the current low quantile level.
Definition RobustScaler.h:70
virtual float get()
Returns value of scaler.
Definition RobustScaler.h:97
virtual float stdDev() const
Returns the current standard deviation.
Definition RobustScaler.h:85
virtual float span() const
Returns the current span.
Definition RobustScaler.cpp:61
virtual float timeWindow() const
Returns the time window (expressed in seconds).
Definition TimeWindowable.h:45
Engine * engine() const
Returns the engine that owns this unit.
Definition PqCore.h:420