Skip to content

Commit acdc0a2

Browse files
committed
code clean up
1 parent a33bc05 commit acdc0a2

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

Diff for: financial/exponential_moving_average.py

+24-21
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,38 @@
33
Wikipedia Reference: https://en.wikipedia.org/wiki/Exponential_smoothing
44
Reference: https://www.investopedia.com/terms/e/ema.asp#toc-what-is-an-exponential-moving-average-ema
55
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.
99
"""
1010

11-
import numpy as np
1211

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]
1817
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
2021
21-
st = alpha * xt + (1 - alpha) * st_prev
22-
alpha = 2/(1 + window_size) - smoothing factor
22+
Formula:
2323
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
2726
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:
2932
raise ValueError("window_size must be > 0")
30-
elif (window_size >= len(series)):
33+
elif window_size >= len(series):
3134
raise ValueError("window_size must be < length of series")
3235

3336
# Resultent array
34-
exp_averaged_arr : list[float] = []
37+
exp_averaged_arr: list[float] = []
3538

3639
# Calculating smoothing factor
3740
alpha = 2 / (1 + window_size)
@@ -50,18 +53,18 @@ def exponential_moving_average(series : list[float], window_size : int) -> list
5053
# point and previous exponential average value
5154
st = (alpha * series[t]) + ((1 - alpha) * st)
5255
exp_averaged_arr.append(st)
53-
56+
5457
return exp_averaged_arr
5558

59+
5660
if __name__ == "__main__":
5761
import doctest
5862

5963
doctest.testmod()
6064

61-
test_series = [2,5,3,8.2,6,9,10]
65+
test_series = [2, 5, 3, 8.2, 6, 9, 10]
6266
test_window_size = 3
6367
result = exponential_moving_average(test_series, test_window_size)
6468
print("Test series: ", test_series)
6569
print("Window size: ", test_window_size)
6670
print("Result: ", result)
67-

0 commit comments

Comments
 (0)