USING ARIMA(Auto regressive integrated moving average) model to predict the stock price
ARIMA is a popular time series model used for forecasting. In this article we will use arima model to predict the stock prices of microsoft.
The stock price data is downloaded from Yahoo Finance website. You can open the website, search for microsoft stocks. Go to history and there you will find the download option to download the stock prices.
I used daily stock data of microsoft from 01-01-2010 to 11-06-2021.
PS: To remove warnings I used :
from warnings import simplefilter
simplefilter(action='ignore', category = FutureWarning)
Also use 'from datatime import datetime' instead of 'from pandas import datetime'
from warnings import simplefilter
simplefilter(action='ignore', category = FutureWarning)
Also use 'from datatime import datetime' instead of 'from pandas import datetime'
So here's the code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas.plotting import lag_plot
from datetime import datetime
from statsmodels.tsa.arima_model import ARIMA
from sklearn.metrics import mean_squared_error
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
data = pd.read_csv('microsoft stock data.csv')
data.head()
data.shape
plt.figure()
lag_plot(data['Open'], lag=3)
plt.title('MICROSOFT Stock - Autocorrelation plot with lag = 3')
plt.show()
#For modeling, we train the model using 70 percent of data, And test the model using 30 percent of data
train_data, test_data = data[0:int(len(data)*0.7)], data[int(len(data)*0.7):]
training_data = train_data['Close'].values
test_data = test_data['Close'].values
history = [x for x in training_data]
model_predictions = []
N_test_observations = len(test_data)
for time_point in range(N_test_observations):
model = ARIMA(history, order=(4,1,0))
model_fit = model.fit(disp=0)
output = model_fit.forecast()
yhat = output[0]
model_predictions.append(yhat)
true_test_value = test_data[time_point]
history.append(true_test_value)
MSE_error = mean_squared_error(test_data, model_predictions)
print('Testing Mean Squared Error is {}'.format(MSE_error))
test_set_range = data[int(len(data)*0.7):].index
plt.plot(test_set_range, model_predictions, color='blue', marker='o', linestyle='solid',label='Predicted Price')
plt.plot(test_set_range, test_data, color='red', label='Actual Price')
plt.title('Microsoft Prices Prediction')
plt.xlabel('Date')
plt.ylabel('Prices')
plt.xticks(np.arange(2000,2990,400), df.Date[2000:2990:400])
plt.legend()
plt.show()
Conclusion: The error is less and thus ARIMA seems to be a good predictive model.



Comments
Post a Comment