-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathforecast_arma.py
90 lines (73 loc) · 2.6 KB
/
forecast_arma.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
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
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 6 00:46:34 2020
@author: Yu Zhe
"""
import pandas as pd
import numpy as np
#df = pd.read_csv("../monthly-car-sales.csv")
#test = Predictor_ARMA(df)
#eval_result = test.evaluate_model()
#print(eval_result)
#result = test.outsample_forecast()
#result.plot()
#%%
"""
Autoregressive Moving Average (ARMA)
The Autoregressive Moving Average (ARMA) method models the next step in the sequence
as a linear function of the observations and resiudal errors at prior time steps.
It combines both Autoregression (AR) and Moving Average (MA) models.
The method is suitable for univariate time series without trend and seasonal components.
"""
#from statsmodels.tsa.arima_model import ARMA
from sklearn.metrics import mean_absolute_error
import statsmodels.api as sm
import warnings
warnings.filterwarnings('ignore', 'statsmodels.tsa.arima_model.ARMA',
FutureWarning)
warnings.filterwarnings('ignore', 'statsmodels.tsa.arima_model.ARIMA',
FutureWarning)
class Predictor_ARMA:
def __init__(self, df):
df.columns = ['ds', 'y']
df['ds']= pd.to_datetime(df['ds'])
self.df = df.set_index('ds')
self.y = self.df['y']
"""
Fit ARIMA Model
"""
def fit_model(self, df):
model = sm.tsa.ARMA(df, order=(2, 1))
model_fit = model.fit()
return model_fit
"""
Evaluate Forecast Model
"""
def evaluate_model(self, output=True):
# Predict last 12 months of data
train = self.df[:-12]
y_truth = self.y[-12:]
model_fit = self.fit_model(train)
y_forecasted = model_fit.predict(len(train), len(train)+11)
mse = ((y_forecasted - y_truth) ** 2).mean()
rounded_mse = round(mse, 2)
rmse = round(np.sqrt(mse), 2)
# calculate MAE between expected and predicted values
mae = mean_absolute_error(y_truth, y_forecasted)
if (output==True):
print('The Mean Squared Error of our forecasts is {}'.format(rounded_mse))
print('The Root Mean Squared Error of our forecasts is {}'.format(rmse))
print('MAE: %.3f' % mae)
print(model_fit.summary())
return rounded_mse, rmse, mae
"""
Out-of-Sample Forecast
Producing and visualizing forecasts
"""
def outsample_forecast(self, output=True):
model_fit = self.fit_model(self.df)
yhat = model_fit.predict(len(self.df), len(self.df)+11)
if (output==True):
print(yhat)
yhat.plot()
return yhat