3
3
Wikipedia Reference: https://en.wikipedia.org/wiki/Exponential_smoothing
4
4
Reference: https://www.investopedia.com/terms/e/ema.asp#toc-what-is-an-exponential-moving-average-ema
5
5
6
- Exponential moving average is used in financial field to analyze changes stock prices.
7
- EMA is used in conjuction with Simple moving average (SMA), EMA reacts to the changes in
8
- the value quicker than SMA, which is one of the advantages of using EMA.
6
+ Exponential moving average is used in finance to analyze changes stock prices.
7
+ EMA is used in conjuction with Simple moving average (SMA), EMA reacts to the
8
+ changes inthe value quicker than SMA, which is one of the advantages of using EMA.
9
9
"""
10
10
11
- import numpy as np
12
11
13
- def exponential_moving_average (series : list [float ], window_size : int ) -> list [float ]:
14
- '''
15
- :param series: Array of numbers (Time series data)
16
- :param window_size: Window size for calculating average (window_size > 0 )
17
- :return: Resulting array of exponentially averaged numbers
12
+ def exponential_moving_average (series : list [float ], window_size : int ) -> list [float ]:
13
+ """
14
+ Returns the exponential moving average of the given array list
15
+ >>> exponential_moving_average([2, 5, 3, 8.2, 6, 9, 10], 3 )
16
+ [2.0, 3.5, 3.25, 5.725, 5.8625, 7.43125, 8.715625]
18
17
19
- Formula:
18
+ :param series: Array of numbers (Time series data)
19
+ :param window_size: Window size for calculating average (window_size > 0)
20
+ :return: Resulting array of exponentially averaged numbers
20
21
21
- st = alpha * xt + (1 - alpha) * st_prev
22
- alpha = 2/(1 + window_size) - smoothing factor
22
+ Formula:
23
23
24
- Exponential moving average (EMA) is a rule of thumb technique for
25
- smoothing time series data using the exponential window function.
26
- '''
24
+ st = alpha * xt + (1 - alpha) * st_prev
25
+ alpha = 2/(1 + window_size) - smoothing factor
27
26
28
- if (window_size <= 0 ):
27
+ Exponential moving average (EMA) is a rule of thumb technique for
28
+ smoothing time series data using the exponential window function.
29
+ """
30
+
31
+ if window_size <= 0 :
29
32
raise ValueError ("window_size must be > 0" )
30
- elif ( window_size >= len (series ) ):
33
+ elif window_size >= len (series ):
31
34
raise ValueError ("window_size must be < length of series" )
32
35
33
36
# Resultent array
34
- exp_averaged_arr : list [float ] = []
37
+ exp_averaged_arr : list [float ] = []
35
38
36
39
# Calculating smoothing factor
37
40
alpha = 2 / (1 + window_size )
@@ -50,18 +53,18 @@ def exponential_moving_average(series : list[float], window_size : int) -> list
50
53
# point and previous exponential average value
51
54
st = (alpha * series [t ]) + ((1 - alpha ) * st )
52
55
exp_averaged_arr .append (st )
53
-
56
+
54
57
return exp_averaged_arr
55
58
59
+
56
60
if __name__ == "__main__" :
57
61
import doctest
58
62
59
63
doctest .testmod ()
60
64
61
- test_series = [2 ,5 , 3 , 8.2 ,6 , 9 , 10 ]
65
+ test_series = [2 , 5 , 3 , 8.2 , 6 , 9 , 10 ]
62
66
test_window_size = 3
63
67
result = exponential_moving_average (test_series , test_window_size )
64
68
print ("Test series: " , test_series )
65
69
print ("Window size: " , test_window_size )
66
70
print ("Result: " , result )
67
-
0 commit comments