Crash Course in Forecasting: Apple Stock price analysis and Prediction using ARIMA

Anvi Jain
AI Skunks
Published in
3 min readApr 10, 2023

Introduction

What is Time series forecasting? Time series forecasting is the process of predicting future values of a time series based on its historical behavior using statistical and machine learning approaches. The goal of time series forecasting is to understand the patterns and trends in historical data and use that information to make predictions about future values.

Time series forecasting has various uses, including anticipating sales, stock prices, product demand, and weather patterns, among others. Businesses and organizations may make better decisions, streamline operations, and plan for the future with the aid of accurate forecasting.

ARIMA

ARIMA stands for Autoregressive Integrated Moving Average. It is a popular statistical model used for time series analysis and forecasting. ARIMA models are widely used in fields such as economics, finance, and engineering to make predictions about the future values of a time series based on its past behavior.

The ARIMA model is made up of three components: the autoregressive (AR) component, the integrated (I) component, and the moving average (MA) component.

  • The AR component models the dependence of the current value on previous values of the time series.
  • The I component represents the degree of differencing required to make the time series stationary. Differencing involves taking the difference between consecutive values of the time series to remove any trend or seasonality.
  • The MA component models the dependence of the current value on past errors (residuals) of the time series.

ARIMA models are widely used in time series forecasting due to their flexibility and ability to capture complex patterns in the data. However, they can be sensitive to outliers and require careful analysis and tuning to produce accurate forecasts.

Visualizing the Time series

Closing price of Apple stock from 2017 to March 2023
Histogram of Apple stock prices

Calculating the moving average

simple_ma = df["Close"].rolling(window=100).mean()

plt.figure(figsize=(14,8))
simple_ma.plot(label="Simple Moving Average")
df["Close"].plot(label="Closing Price")
plt.xticks(rotation=0)
plt.title("Moving Average of Closing Price", size=15)
plt.legend()
plt.show()

On average, there are 252 trading days in a year so it can be seen that there is a yearly seasonality when I pass in 252 to the parameter “period”.

Applying ARIMA:

l_param = []
l_param_seasonal=[]
l_results_aic=[]
for param in pdq:
for param_seasonal in seasonal_pdq:
try:
mod = sm.tsa.statespace.SARIMAX(monthly_mean,
order=param,
seasonal_order=param_seasonal,
enforce_stationarity=False,
enforce_invertibility=False)

results = mod.fit()

print('ARIMA{}x{}12 - AIC:{}'.format(param, param_seasonal, results.aic))

l_param.append(param)
l_param_seasonal.append(param_seasonal)
l_results_aic.append(results.aic)
except:
continue

Results:

pred = results.get_prediction(start=pd.to_datetime('2017-12-31'), dynamic=False)
pred_ci = pred.conf_int()

ax = monthly_mean['2014':].plot(label='observed')
pred.predicted_mean.plot(ax=ax, label='One-step ahead Forecast', alpha=.7, figsize=(14, 7))

ax.fill_between(pred_ci.index,
pred_ci.iloc[:, 0],
pred_ci.iloc[:, 1], color='k', alpha=.2)

ax.set_xlabel('Date')
ax.set_ylabel('close price')
plt.legend()

plt.show()
One step ahead forecast
Final Predications

References:
https://builtin.com/data-science/time-series-forecasting-python
https://medium.com/@stallonejacob/time-series-forecast-a-basic-introduction-using-python-414fcb963000

--

--