/
Exponential Smoothing
36 lines (28 loc) · 1.14 KB
/
Exponential Smoothing
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
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# lambda function that converts a string into datetime
dateparse = lambda dates: pd.datetime.strptime(dates, '%m-%d-%Y')
# reads the csv into
df = pd.read_csv(r"C:\TBLAMEY\UH\FA2020\Covid-19\Reported_maui.csv", parse_dates=['Date'], date_parser=dateparse)
# sets the date column as the index
#df = df.set_index('Date')
period = pd.Series(df['Date'])
ts = df['Reported']
# defines the function for exponential smoothing
def exp_smooth(y_true, alpha):
y_pred = [y_true[0]]
for n in range(1, len(y_true)-1):
y_pred.append(alpha * y_true[n] + (1 - alpha) * y_pred[n - 1])
y_pred = [np.NaN] + y_pred
return y_pred
# plots the exponential smoothing forecast for alpha = 0.05, 0.1
plt.plot(period, ts,label = "Signal - Obs Values ");
plt.plot(period, exp_smooth(ts, 0.05),label = "Exp Smoothing (α = 0.05)");
plt.plot(period, exp_smooth(ts, 0.2),label = "Exp Smoothing (α = 0.1)");
plt.xlabel('Period', fontsize = 12);
plt.xticks(rotation='vertical')
plt.margins(0.12)
plt.subplots_adjust(bottom=0.05)
plt.ylabel('Reported', fontsize = 12);
plt.legend(loc='upper left');