Code
# Separate Train set and Test set
Ntest = 30
train = df.iloc[:-Ntest]
test = df.iloc[-Ntest:]
# Use auto_arima model to find hyperparameter
model = pm.auto_arima(
train,
error_action='ignore',
trace=True,
suppress_warnings=True,
maxiter=10,
seasonal=False
)
# Plot predit result function
def plot_result(model, fulldata, train, test):
params = model.get_params()
d = params['order'][1]
train_pred = model.predict_in_sample(start=d, end=-1)
test_pred, confint = model.predict(n_periods=Ntest, return_conf_int=True)
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(fulldata.index, fulldata, label='data')
ax.plot(train.index[d:], train_pred, label='fitted')
ax.plot(test.index, test_pred, label='forecast')
ax.fill_between(test.index, confint[:,0], confint[:,1], color='red', alpha=0.3)
ax.legend();
# Plot test result
def plot_test(model, test):
test_pred, confint = model.predict(n_periods=Ntest, return_conf_int=True)
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(test.index, test, label='true')
ax.plot(test.index, test_pred, label='forecast')
ax.fill_between(test.index, confint[:,0], confint[:,1], color='red', alpha=0.3)
ax.legend();
# RMSE function
def rmse(y, t):
return np.sqrt(np.mean((t - y)**2))
# ---- output ----
# output model summary
model.summary()
# Plot Result
plot_result(model, df, train, test)
# Plot Test
plot_test(model, test)
# Print RMSE
print("RMSE ARIMA:", rmse(model.predict(Ntest), test))
print("RMSE Naive:", rmse(train.iloc[-1], test))
GOOG




AAPL




IBM




SBUX



