The
A time series is a collection of data points collected at constant time intervals. Time series are used to forecast future values based on previous values.
A stationary time series is one whose statistical properties (mean, variance, autocorrelation, etc.) are all constant over time. A non-stationary series is one whose statistical properties change over time.
An ARIMA model is characterized by three terms: p, d, q.
p is the order of the
q is the order of the
d is the number of
In this example, we will predict the next 10 days of stock prices from a given data of 100 days.
Import the relevant libraries to perform time series forecasting:
import numpy as np, pandas as pd
import statsmodels.tsa.stattools as ts
from statsmodels.tsa.arima_model import ARIMA
import matplotlib.pyplot as plt
Upload the relevant dataset using pandas.read_csv()
method:
file = pd.read_csv("data.csv")
// prices is a field in .csv file containing all stock prices.
stock_price = df['prices']
You can view this data in stock_price using the plt.plot()
method:
plt.plot(stock_price)
Below is the code to an output the variation in stock price for the last 100 days. It also contains a .csv
file with sample stock prices.
Initialize the ARIMA model and set the values of p, d, and q as 1, 1, and 2.
model = ARIMA(stock_price, order=(1,1,2))
model_fit = model.fit(disp=0)
// summary provides a detailed summary of the time series model
print(model_fit.summary())
Let’s predict the next 10 values and plot them on a graph:
pred = model_fit.predict(100,109,typ='levels')
// 100-109, refers to the next 10 values after the value at 99th index.
newarr = []
for i in price:
newarr.append(i)
for x in pred:
newarr.append(x)
plt.plot(newarr)
import numpy as np, pandas as pdimport matplotlib.pyplot as pltimport statsmodels.tsa.stattools as tsfrom statsmodels.tsa.arima_model import ARIMAdf = pd.read_csv("data.csv")# prices is a field in .csv file containing all stock prices.stock_price = df['prices']plt.plot(stock_price)# ARIMA modelmodel = ARIMA(stock_price, order=(1,1,2))model_fit = model.fit(disp=0)# summary provides a detailed summary of the time series modelprint(model_fit.summary())# Predicting valuespred = model_fit.predict(100,109,typ='levels')# 100-109, refers to the next 10 values after the value at 99th index.# newarr array combines the predicted the stock values in one arraynewarr = []for i in price:newarr.append(i)for x in pred:newarr.append(x)plt.plot(newarr)
Free Resources