Time Series Modeling

ALL ABOUT AR, MA, ARMA, ARIMA, ACF AND PACF:

Kartikeya Mishra
5 min readMay 24, 2022

Time Series can be defined as a set of measurements of certain
variable made at regular time intervals. Time acts as an independent variable for estimation. A time series defined by the values Y1, Y2.. of a variable Y
at times t1, t2, t3.. is given by : Y = F (t)

Application of Time Series Analysis :

Weekly production of a shoe manufacturing company.

Monthly tickets sold by an airline.

Yearly GDP of a developing country.

Need For Time Series Analysis :

Evaluate current progress.

Understand seasonal patterns.

Detect unusual events.

Forecasting.

Time Series Models :

1. Auto Regressive or (AR Model).

2. Moving Average, or (MA model).

3. Autoregressive and Moving Average, or (ARMA model).

4. Autoregressive Integrated Moving Average, or (ARIMA model).

Auto Regressive (AR) Model :

In an AR model, you predict future values based on a weighted sum of past values. Equation for the auto regressive model :

Yt = c + φ1Yt−1 + φ2yt−2 + ⋯ + φpYt−p + et

Yt : is the function of different past values of the same variable.
et : is the error term.
c : is a constant.
φ1 to φp are the parameters.

AR(1) is a model whose current value is based on the preceding
value. AR(2) is based on the preceding two values.

Auto Regressive (AR) Model

Moving Average (MA) Model :

MA model is used to forecast time series if Yt depends only on the random error terms. Equation for the MA model :

Yt = μ + φ1 Et−1 + φ2 Et−2 + ⋯ + φp E t−p

Yt : is the function of different past error terms.
μ : is the mean of the series.
Et : is the error term.
φ1 to φp are the parameters.
The error terms here are assumed to be white noise processes with mean zero and constant variance.

Moving Average (MA) Model

ARMA (Auto Regressive Moving Average) Model :

ARMA model is used to forecast time series using both the past values and the error terms. Equation for the ARMA model :

Yt = c + φ1Yt−1 + φ2yt−2 + ⋯ + φpYt−p + e + μ + Et + φ1Et−1 + φ2Et−2 + — — — — -+ φp Et−p

It is referred as ARMA ( p, q ), where p is autoregressive terms and q is moving average terms.

ARMA (Auto Regressive Moving Average) Model

ARIMA (Auto Regressive Integrated Moving Average) Model:

ARIMA model predicts a value in a response time series as a linear combination of its own past values, past errors, also current and past values of other time series.

ARIMA ( p, d, q ) :

p : is the order of the autoregressive part.
d : is the order of the differencing.
q : is the order of the moving-average process.

If no differencing is done (d = 0), the models are usually referred to as ARMA(p, q) models.

ARIMA (Auto Regressive Integrated Moving Average) Model

ACF (Autocorrelation Function (ACF) and PACF(Partial Autocorrelation Function (PACF) :

ACF (Autocorrelation Function (ACF):

ACF is the coefficient of correlation between the value of a point at a current time and its value at lag p, that is, correlation between Y(t) and Y(t-p).

ACF will identify the order of MA process.

ACF (Autocorrelation Function (ACF)

PACF(Partial Autocorrelation Function (PACF):

PACF is similar to ACF, but the intermediate lags between t and t-p are removed, that is, correlation between Y(t) and Y(t-p) with p-1 lags excluded.

PACF will identify the order of AR process.

PACF(Partial Autocorrelation Function (PACF)

ACF and PACF are used to determine the value of p and q.

Steps in Time Series Forecasting :

Step 01 : Visualize the time series — check for trend, seasonality, or random patterns.

Step 02: Stationarize the series using decomposition or differencing techniques.

Step 03 : Plot ACF / PACF and find ( p, d, q ) parameters.

Step 04 : Build ARIMA model.

Step 05 : Make predictions using final ARIMA model.

Code ACF and PACF :

plt.plot(np.arange(0,11), acf(ts_log_mv_diff, nlags = 10))
plt.axhline(y=0,linestyle=’ — ‘,color=’gray’)
plt.axhline(y=-7.96/np.sqrt(len(ts_log_mv_diff)),linestyle=’ — ‘,color=’gray’)
plt.axhline(y=7.96/np.sqrt(len(ts_log_mv_diff)),linestyle=’ — ‘,color=’gray’)
plt.title(‘Autocorrelation Function’)
plt.show()

plt.plot(np.arange(0,11), pacf(ts_log_mv_diff, nlags = 10))
plt.axhline(y=0,linestyle=’ — ‘,color=’gray’)
plt.axhline(y=-7.96/np.sqrt(len(ts_log_mv_diff)),linestyle=’ — ‘,color=’gray’)
plt.axhline(y=7.96/np.sqrt(len(ts_log_mv_diff)),linestyle=’ — ‘,color=’gray’)
plt.title(‘Partial Autocorrelation Function’)
plt.show()

Code ARIMA (Auto Regressive Integrated Moving Average) Model :

model = ARIMA(ts_log, order=(1, 1, 0)) results_ARIMA = model.fit(disp=-1)
plt.plot(ts_log_mv_diff) plt.plot(results_ARIMA.fittedvalues, color=’red’)
plt.title(‘RSS: %.4f’% (((results_ARIMA.fittedvalues[1:] -
ts_log_mv_diff)**2).mean()))
predictions_ARIMA_diff = pd.Series(results_ARIMA.fittedvalues, copy=True)
predictions_ARIMA_diff.head()
predictions_ARIMA_diff_cumsum = predictions_ARIMA_diff.cumsum()
predictions_ARIMA_diff_cumsum.head()
predictions_ARIMA_log = pd.Series(ts_log.ix[0], index=ts_log.index)
predictions_ARIMA_log =
predictions_ARIMA_log.add(predictions_ARIMA_diff_cumsum,fill_value=0)
predictions_ARIMA_log.head()

predictions_ARIMA = np.exp(predictions_ARIMA_log)
plt.plot(ts)
plt.plot(predictions_ARIMA)
plt.title(‘RMSE: %.4f’% np.sqrt(((predictions_ARIMA-ts)**2)/(ts)).mean())

EXTRA

Hope you all are doing great,
I was going through the internet and found that people were demanding ‘Time Series Analysis (Machine Learning) python’ so I created a series on time series analysis. These are the first few explanation video. If you are into video learning I am also providing a link to the video. Hope you like it.

Video link : https://bit.ly/3pJDihL

--

--

Kartikeya Mishra

All about new technology in fun and easy way so that you can be confident in it and make your own piece of work using this knowledge !