-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathregularizer.py
More file actions
132 lines (96 loc) · 4.42 KB
/
regularizer.py
File metadata and controls
132 lines (96 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import abc # Abstract Basic Class
class Regularizer() :
""" Regularize a signal """
value = None
@abc.abstractmethod
def regularize(self, value : float) :
pass
def __call__(self, value : float) :
return self.regularize(value)
class Regularizer_EMA(Regularizer) :
""" Exponential Moving Average as defined here : https://www.investopedia.com/terms/e/ema.asp """
def __init__(self, period : float = 10, smoothing : float = 2) :
# Check type and value
assert isinstance(period, (int, float)), f"[Type Error] :: <period> should be a float or an integer (got '{type(period)}' instead)."
assert period >= 0, f"[Value Error] :: <period> should be >= 0 (got '{period}' instead)."
assert isinstance(smoothing, (int, float)), f"[Type Error] :: <smoothing> should be a float or an integer (got '{type(smoothing)}' instead)."
assert smoothing >= 0, f"[Value Error] :: <smoothing> should be >= 0 (got '{smoothing}' instead)."
# Store values
self.period = period
self.smoothing = smoothing
self.value = None
def regularize(self, newValue : float) :
if self.value is None :
self.value = newValue
else :
proportion = self.smoothing / (1 + self.period)
self.value = newValue * proportion + self.value * (1 - proportion)
return self.value
def __str__(self) :
return f"[Regularizer_EMA] :: period={self.period} :: smoothing={self.smoothing} :: value={self.value}"
class Regularizer_AvgStd(Regularizer) :
""" Regularise a value stream using average and standart deviation """
def __init__(self, theta : float = 0.99) :
# Check type and value
assert isinstance(theta, (int, float)), f"[Type Error] :: <theta> should be a float or an integer (got '{type(theta)}' instead)."
assert theta >= 0, f"[Value Error] :: <theta> should be >= 0 (got '{theta}' instead)."
assert theta <= 1, f"[Value Error] :: <theta> should be <= 1 (got '{theta}' instead)."
# Store values
self.theta = theta
self.estimatedAverage = None
self.estimatedStd = None # Average distance to average
def regularize(self, value : float) :
# Update estimated average
if self.estimatedAverage is None :
self.estimatedAverage = value
else :
self.estimatedAverage = self.theta * self.estimatedAverage + (1 - self.theta) * value
# Update estimated std
currentStd = abs(value - self.estimatedAverage)
if self.estimatedStd is None :
self.estimatedStd = currentStd
else :
self.estimatedStd = self.theta * self.estimatedStd + (1 - self.theta) * currentStd
# Regularize value
if (self.estimatedStd == 0) :
return 0
else :
return (value - self.estimatedAverage) / self.estimatedStd
def __str__(self) :
return f"[Regularizer_AvgStd] :: Avg={self.estimatedAverage} :: Std={self.estimatedStd}"
class Derivator() :
""" Derivate a value stream """
def __init__(self) :
self.previousValue = None
self.derivative = None
def derivate(self, value : float) :
if self.previousValue is None :
self.derivative = 1
else :
self.derivative = value - self.previousValue
self.previousValue = value
return self.derivative
def __str__(self) :
return f"[Derivator] :: PreviousValue={self.previousValue} :: Derivative={self.derivative}"
def __call__(self, value : float) :
return self.derivate(value)
if __name__ == "__main__" :
# Run tests
import random
regularizer_AvgStd = Regularizer_AvgStd()
regularizer_EMA = Regularizer_EMA()
derivator = Derivator()
printStep = 1
for _ in range(int(1e9)) :
value = 1000 * random.random()
regularizedValue_AvgStd = regularizer_AvgStd.regularize(value)
regularizedValue_EMA = regularizer_EMA(value)
derivative = derivator(value)
if _ >= printStep :
printStep *= 1.3
print(f"Value {_} : {value}")
print(f"\t{regularizer_AvgStd}")
print("\tregularizedValue_AvgStd : ", regularizedValue_AvgStd)
print(f"\t{regularizer_EMA}")
print("\tregularizedValue_EMA : ", regularizedValue_EMA)
print("\tderivative : ", derivative)