(Source: https://ei.marketwatch.com/Multimedia/2018/03/30/Photos/ZH/MW-GG561_DJI_20_20180330105510_ZH.jpg?uuid=57e356ee-342a-11e8-978d-ac162d7bc1f7)

Stock Market Analysis Using ARIMA

Time Series Data

Time Series is a big component of our everyday lives. They are in fact used in medicine (EEG analysis), finance (Stock Prices) and electronics (Sensor Data Analysis). Many Machine Learning models have been created in order to tackle these types of tasks, two examples are ARIMA (AutoRegressive Integrated Moving Average) models and RNNs (Recurrent Neural Networks).


I have been recently working on a Stock Market Dataset on Kaggle. This dataset provides all US-based stocks daily price and volume data. If you want to find out more about it, all my code is freely available on my Kaggle and GitHub profiles.

ARIMA (AutoRegressive Integrated Moving Average)

The acronym of ARIMA stands for [1]:

  • Integrated = differencing between raw observations (eg. subtracting observations at different time steps).
  • Moving Average = the model takes advantage of the relationship between the residual error and the observations.
  • d = the degree of differencing.
  • q = the size of the moving average window.


In order to realise the following code exercise, I made use of the following libraries and dependencies.

import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
from pandas.plotting import lag_plot
from pandas import datetime
from statsmodels.tsa.arima_model import ARIMA
from sklearn.metrics import mean_squared_error
df = pd.read_csv("../input/Data/Stocks/msft.us.txt").fillna(0)
Figure 1: Dataset Head
lag_plot(df['Open'], lag=5)
plt.title('Microsoft Autocorrelation plot')
Figure 2: Autocorrelation plot using a Lag of 5
train_data, test_data = df[0:int(len(df)*0.8)], df[int(len(df)*0.8):]
plt.title('Microsoft Prices')
plt.plot(df['Open'], 'blue', label='Training Data')
plt.plot(test_data['Open'], 'green', label='Testing Data')
plt.xticks(np.arange(0,7982, 1300), df['Date'][0:7982:1300])
Figure 3: Graphical Representation of Train/Test Split
def smape_kun(y_true, y_pred):
return np.mean((np.abs(y_pred - y_true) * 200/ (np.abs(y_pred) + np.abs(y_true))))
Figure 4: SMAPE (Symmetric mean absolute percentage error) [2]
train_ar = train_data['Open'].values
test_ar = test_data['Open'].values
history = [x for x in train_ar]
predictions = list()
for t in range(len(test_ar)):
model = ARIMA(history, order=(5,1,0))
model_fit = model.fit(disp=0)
output = model_fit.forecast()
yhat = output[0]
obs = test_ar[t]
error = mean_squared_error(test_ar, predictions)
print('Testing Mean Squared Error: %.3f' % error)
error2 = smape_kun(test_ar, predictions)
print('Symmetric mean absolute percentage error: %.3f' % error2)
Testing Mean Squared Error: 0.343
Symmetric mean absolute percentage error: 40.776
plt.plot(df['Open'], 'green', color='blue', label='Training Data')
plt.plot(test_data.index, predictions, color='green', marker='o', linestyle='dashed',
label='Predicted Price')
plt.plot(test_data.index, test_data['Open'], color='red', label='Actual Price')
plt.title('Microsoft Prices Prediction')
plt.xticks(np.arange(0,7982, 1300), df['Date'][0:7982:1300])
Figure 5: Microsoft Price Prediction
plt.plot(test_data.index, predictions, color='green', marker='o', linestyle='dashed',label='Predicted Price')
plt.plot(test_data.index, test_data['Open'], color='red', label='Actual Price')
plt.title('Microsoft Prices Prediction')
plt.xticks(np.arange(6386,7982, 300), df['Date'][6386:7982:300])
Figure 6: Prediction vs Actual Price Comparison


In case you have any questions or any suggestions on what my next article should be about, please just leave a comment below.


[1] How to Create an ARIMA Model for Time Series Forecasting in Python, Jason Brownlee, Machine Learning Mastery. Accessed at: https://machinelearningmastery.com/arima-for-time-series-forecasting-with-python/

Data Scientist at SAS, TDS Associate Editor and Freelancer. https://pierpaolo28.github.io/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store