-
Notifications
You must be signed in to change notification settings - Fork 1
/
EWMA.py
58 lines (38 loc) · 1.15 KB
/
EWMA.py
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
"""
Exponentially Weighted Moving Average (EWMA) function
By: Rui Nian
"""
import numpy as np
import matplotlib.pyplot as plt
def ewma(beta, vector):
"""
Description
---
Exponential smoothing
Note: Exponentially smoothed vector has 0 for the first value. During implementation, omit first value.
Inputs
---
beta: Smoothing factor. Higher = more smoothing
vector: Vector of numbers to be smoothed
Returns
---
ewma_num: Exponentially smoothed vector
"""
ewma_num = np.zeros(len(vector))
for j in range(len(ewma_num)):
if j == (len(ewma_num) - 1):
break
ewma_num[j + 1] = beta * ewma_num[j] + (1 - beta) * vector[j + 1]
# Bias correction
if j == 0:
ewma_num[j + 1] = ewma_num[j + 1] / (1 - np.power(beta, j + 1))
return ewma_num
if __name__ == "__main__":
np.random.seed(1)
numbers = np.zeros(500)
for i, number in enumerate(numbers):
numbers[i] = i + np.random.uniform(-i / 5, i / 5)
ewma_values = ewma(0.9, numbers)
plt.plot(numbers)
plt.plot(ewma_values)
plt.show()