[Statistics] [Time series analysis] Plot the ARMA model and grasp the tendency.

This is an article about plotting the ARMA model.

Draw a graph to visually understand how the $ {\ rm ARMA} (p, q) $ parameter changes the graph. There are 49 books in total w I wrote this article from the faint expectation that if you look at it all the time, you will be able to distinguish the parameters by looking at the graph: grin: After looking at the graphs side by side, write the Python code that generated them.

Autoregressive-moving average (ARMA) model

The formula for the ARMA model is as follows. In this article, we will draw a graph with the pattern of $ p = 0,1,2, \ q = 0,1,2 $ and the number of variations of the sign of each parameter.

y_t = \varepsilon_t +  \sum_{i=1}^p \phi_i y_{t-i} + \sum_{i=1}^q \theta_i \varepsilon_{t-i} \\
\varepsilon_t \sim N(0,\sigma^2)                \\
t = 1, 2, \cdots, T             

Draw a graph

ARMA(0,0)

 y_t = \varepsilon_t 

ARMA_1.png

ARMA(0,1)

 y_t = \varepsilon_t + \theta_1 \varepsilon_{t-1}

Parameters: \theta_1=0.7

ARMA_2.png

ARMA_3.png

ARMA(0,2)

 y_t = \varepsilon_t + \theta_1 \varepsilon_{t-1} + \theta_2 \varepsilon_{t-2}

Parameters: \theta_1=0.7,\ \theta_2=0.3 ARMA_4.png ARMA_5.png ARMA_6.png ARMA_7.png

ARMA(1,0)

y_t = \varepsilon_t +  \phi_1 y_{t-1} 

Parameters: \phi_1=0.7 ARMA_8.png ARMA_9.png

ARMA(1,1)

y_t = \varepsilon_t +  \phi_1 y_{t-1} + \theta_1 \varepsilon_{t-1}

Parameters: \phi_1=0.7,\ \theta_1=0.7 ARMA_10.png ARMA_11.png ARMA_12.png ARMA_13.png

ARMA(1,2)

y_t = \varepsilon_t +  \phi_1 y_{t-1} + \theta_1 \varepsilon_{t-1} + \theta_2 \varepsilon_{t-2}

Parameters: \phi_1=0.7,\ \theta_1=0.7,\ \theta_2=0.3

ARMA_14.png ARMA_15.png ARMA_16.png ARMA_17.png ARMA_18.png ARMA_19.png ARMA_20.png ARMA_21.png

ARMA(2,0)

y_t = \varepsilon_t + \phi_1 y_{t-1} + \phi_2 y_{t-2} 

Parameters: \phi_1=0.7,\ \phi_2=0.3

ARMA_22.png ARMA_23.png ARMA_24.png ARMA_25.png

ARMA(2,1)

y_t = \varepsilon_t + \phi_1 y_{t-1} + \phi_2 y_{t-2} + \theta_1 \varepsilon_{t-1} 

Parameters: \phi_1=0.7,\ \phi_2=0.3\ \theta_1=0.7

ARMA_26.png ARMA_27.png ARMA_28.png ARMA_29.png ARMA_30.png ARMA_31.png ARMA_32.png ARMA_33.png

ARMA(2,2)

y_t = \varepsilon_t + \phi_1 y_{t-1} + \phi_2 y_{t-2} + \theta_1 \varepsilon_{t-1} + \theta_2 \varepsilon_{t-2}

Parameters: \phi_1=0.7,\ \phi_2=0.3\ \theta_1=0.7,\ \theta_2=0.3

ARMA_34.png ARMA_35.png ARMA_36.png ARMA_37.png ARMA_38.png ARMA_39.png ARMA_40.png ARMA_41.png ARMA_42.png ARMA_43.png uploading ARMA_44.png... ARMA_44.png ARMA_45.png ARMA_46.png ARMA_47.png ARMA_48.png ARMA_49.png

Drawing code

I used statsmodels which can create ARMA artificial data with Python.

import numpy as np
import pandas as pd
import numpy.random as rd
import itertools, sys

%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import gridspec
plt.style.use('ggplot')

from statsmodels.tsa.arima_process import arma_generate_sample
import statsmodels.api as sm
import statsmodels.tsa.stattools as stt
import statsmodels.graphics.tsaplots as tsaplots



def select_negative(l):
    res = []
    l = np.array(l)
    n = len(l)
    res.append(l)
    l = np.array(l)
    for i in range(n):
        for j in itertools.combinations(range(n),i+1):
            _l = l.copy()
            _l[list(j)] = _l[list(j)] * -1
            res.append(_l)
    return res

cnt = 0
n = 3
nobs = 500
itrvl = 28

for len_ar in range(n):
    for len_ma in range(n):
        
        _ar_params = [.7, .3][:len_ar]
        _ma_params = [.7, .3][:len_ma]
        
        _ar_params = select_negative(_ar_params)
        _ma_params = select_negative(_ma_params)
        for i in _ar_params:
            for j in _ma_params:
                cnt += 1

                ar_params = np.r_[1, -i]
                ma_params = np.r_[1, j]
                
                yy = arma_generate_sample(ar_params, ma_params, nobs)
                ts = pd.Series(yy, index=pd.date_range('2010/1/1', periods=nobs))
                ar_sign = ['+' if val >= 0 else '-' for val in i]
                ma_sign = ['+' if val >= 0 else '-' for val in j]

                plt.subplots(2, 1, sharex=True, figsize=(15,7)) 
                gs = gridspec.GridSpec(2, 1, height_ratios=[5,2])
                ax1 = plt.subplot(gs[0])
                ax2 = plt.subplot(gs[1])

                # ax1 --------
                ts.plot(color="b", alpha=0.4, lw=1, ax=ax1,
                        title="ARMA({0},{1}). ar:{2},ma:{3}, ar:{4},ma:{5}".format(len_ar, len_ma, i, j, ar_sign, ma_sign))
                ax1.set_title(ax1.get_title(), fontsize=16)

                ts_mean = pd.rolling_mean(ts,itrvl)
                ts_std = pd.rolling_std(ts,itrvl) 
                upper = ts_mean + ts_std * 1.96
                lower = ts_mean - ts_std * 1.96

                ts_mean.plot(ax=ax1)
                upper.plot(figsize=(15,7), c="purple", alpha=.6, ax=ax1, linestyle='--')
                lower.plot(figsize=(15,7), c="purple", alpha=.6, ax=ax1, linestyle='--')

                # ax2 --------
                tsaplots.plot_acf(ts ,ax=ax2, color="g", lags=300, lw=2)
                plt.subplots_adjust(hspace=0)

                plt.savefig('./ARMA_fig/ARMA_{}.png'.format(cnt))
                plt.clf()

reference

StatsModels : Autoregressive Moving Average (ARMA): Artificial data  http://statsmodels.sourceforge.net/stable/examples/notebooks/generated/tsa_arma_1.html

Recommended Posts

[Statistics] [Time series analysis] Plot the ARMA model and grasp the tendency.
Time series analysis 2 Stationary, ARMA / ARIMA model
Python: Time Series Analysis: Stationarity, ARMA / ARIMA Model
I tried time series analysis! (AR model)
Time series analysis Part 2 AR / MA / ARMA
Time series analysis 4 Construction of SARIMA model
Time series analysis # 6 Spurious regression and cointegration
Introduction to Time Series Analysis ~ Seasonal Adjustment Model ~ Implemented in R and Python
Python: Time Series Analysis
I implemented "Basics of Time Series Analysis and State Space Model" (Hayamoto) with pystan
RNN_LSTM1 Time series analysis
Time series analysis 1 Basics
Time series plot / Matplotlib
Reformat the timeline of the pandas time series plot with matplotlib
Time series analysis related memo
Time series analysis part 4 VAR
Time series analysis Part 3 Forecast
[Python] Plot time series data
Time series analysis Part 1 Autocorrelation
Time series analysis practice sales forecast
Time series plot started ~ python edition ~
About time series data and overfitting
Movement statistics for time series forecasting
Time series analysis 3 Preprocessing of time series data
Instantly illustrate the predominant period in time series data using spectrum analysis
Plot multiple maps and data at the same time with Python's matplotlib