1

I am trying to use an ARIMA model to predict stock price data, specifically, I am using auto_arima. My goal is to predict the next 30 days of stock prices and compare it to the test data.

I am unable to predict the data correctly as seen in the graph below. Unable to predict correctly

Here is the code I used:

#general
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
%matplotlib inline
import yfinance as yf

ticker = "0118.KL"
data = yf.Ticker(ticker)
df = data.history(start="2019-01-01",end="2020-04-30")

df = df.filter(items=['Close'])
train = df[:-30]
test = df[-30:]

from pmdarima import auto_arima

model = auto_arima(train,trace=True,m=7,error_action='ignore', suppress_warnings=True)
model.fit(train)

forecast = model.predict(n_periods=30)
forecast = pd.DataFrame(forecast,index = test.index,columns=['Prediction'])

plt.plot(test, label='Valid')
plt.plot(forecast, label='Prediction')
plt.show()

Any idea on how to get a better fit? Thank you for reading.

diggledoot
  • 691
  • 8
  • 22

1 Answers1

0

before predict you need to check whether your model is ready for prediction. 1) check diagnostics of your model

model.plot_diagnostics(figsize=(8,8))

check whether residuals are normally distributed and is there any correlation in residuals.

2) get the statistics of the model

model.summary()

this shows the AIC scores and Ljung-Box(Prob(Q)) and Jarque-Bera (Prob(JB)) statistics.You can confirm the residual distribution and correlation in residuals using p values.If all these conditions are met,you can use the model for prediction.otherwise you need to tune the parameters.

Rajith Thennakoon
  • 3,975
  • 2
  • 14
  • 24