【統計学】[時系列解析] ARMAモデルをひたすらプロットして傾向をつかむ。

ARMAモデルをひたすらプロットしてみる記事です。

${\rm ARMA}(p,q)$のパラメータによってどのようにグラフが変化するかを視覚的に理解するためにグラフを描きまくります。計49本ありますw ずっと眺めていたら、グラフを見てパラメーターが見分けられるようにならないかなー、という淡い期待から書いてみた記事です :grin: 
グラフを並べて眺めた後、これらを生成したPythonコードを記載します。

## 自己回帰移動平均(ARMA)モデル

ARMAモデルの式は下記になります。本記事では $p=0,1,2,\ q=0,1,2$のパターンで、かつ各パラメータの符号のバリエーションの数だけグラフを描いていきます。

```math
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　　　　　　　　　　　　　
```

# グラフを描画する

## ARMA(0,0)

```math
 y_t = \varepsilon_t 
```
![ARMA_1.png](https://qiita-image-store.s3.amazonaws.com/0/50670/d5de2ef4-9474-9ccd-50d2-cddbbb731584.png)



## ARMA(0,1)

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

パラメータ：
$\theta_1=0.7$

![ARMA_2.png](https://qiita-image-store.s3.amazonaws.com/0/50670/365e8fb0-295a-e9a5-ffff-b389c2213a9d.png)

![ARMA_3.png](https://qiita-image-store.s3.amazonaws.com/0/50670/30a1fe0c-bffa-3c49-ce34-33eed0a262e6.png)

## ARMA(0,2)

```math
 y_t = \varepsilon_t + \theta_1 \varepsilon_{t-1} + \theta_2 \varepsilon_{t-2}
```
パラメータ：
$\theta_1=0.7,\ \theta_2=0.3$
![ARMA_4.png](https://qiita-image-store.s3.amazonaws.com/0/50670/fa3c80c5-2c38-161d-0f09-240827d138d1.png)
![ARMA_5.png](https://qiita-image-store.s3.amazonaws.com/0/50670/4ea64bae-c452-68fb-6cc8-c937500e87a1.png)
![ARMA_6.png](https://qiita-image-store.s3.amazonaws.com/0/50670/57ab0b2c-802a-92fa-5106-47ddd0674260.png)
![ARMA_7.png](https://qiita-image-store.s3.amazonaws.com/0/50670/79b5e0fa-b3bd-293f-830b-6cc4e8eb39b7.png)


## ARMA(1,0)

```math
y_t = \varepsilon_t +  \phi_1 y_{t-1} 
```
パラメータ：
$\phi_1=0.7$
![ARMA_8.png](https://qiita-image-store.s3.amazonaws.com/0/50670/9f78e7c6-f8fe-2756-2ca1-bc4d61bf86a2.png)
![ARMA_9.png](https://qiita-image-store.s3.amazonaws.com/0/50670/e3d9a458-9d9a-0ec6-edf2-94231a19b7b8.png)


## ARMA(1,1)

```math
y_t = \varepsilon_t +  \phi_1 y_{t-1} + \theta_1 \varepsilon_{t-1}
```
パラメータ：
$\phi_1=0.7,\ \theta_1=0.7$
![ARMA_10.png](https://qiita-image-store.s3.amazonaws.com/0/50670/d2ed3d65-0cde-6828-1b03-c2f134c2e744.png)
![ARMA_11.png](https://qiita-image-store.s3.amazonaws.com/0/50670/ca80699f-3e94-0fb6-0bf1-dc5c0c92ad53.png)
![ARMA_12.png](https://qiita-image-store.s3.amazonaws.com/0/50670/059d82b4-8561-208a-718a-8d7484986bdd.png)
![ARMA_13.png](https://qiita-image-store.s3.amazonaws.com/0/50670/fe25a10a-a0e2-9435-0f4e-103ef2429c5d.png)




## ARMA(1,2)

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

パラメータ：
$\phi_1=0.7,\ \theta_1=0.7,\ \theta_2=0.3$

![ARMA_14.png](https://qiita-image-store.s3.amazonaws.com/0/50670/7dbfa11d-3621-8425-6916-b5b0e156c90f.png)
![ARMA_15.png](https://qiita-image-store.s3.amazonaws.com/0/50670/fae85c7a-1f12-aca1-cae0-7fe460b8ec47.png)
![ARMA_16.png](https://qiita-image-store.s3.amazonaws.com/0/50670/9852848d-20d3-44d8-a48c-17ec284672dd.png)
![ARMA_17.png](https://qiita-image-store.s3.amazonaws.com/0/50670/7404cc5c-8cdc-9a01-e6c0-820ebf3594f7.png)
![ARMA_18.png](https://qiita-image-store.s3.amazonaws.com/0/50670/2a0e815c-7ec2-7afb-bfed-58c77d8d791d.png)
![ARMA_19.png](https://qiita-image-store.s3.amazonaws.com/0/50670/6922843a-5b7a-a7a8-c310-6587da8c00ac.png)
![ARMA_20.png](https://qiita-image-store.s3.amazonaws.com/0/50670/338348b1-a61d-a748-1cf8-ac8e44562e56.png)
![ARMA_21.png](https://qiita-image-store.s3.amazonaws.com/0/50670/8572b56e-5268-8373-3ec3-f80c9d783ad1.png)


## ARMA(2,0)

```math
y_t = \varepsilon_t + \phi_1 y_{t-1} + \phi_2 y_{t-2} 
```
パラメータ：
$\phi_1=0.7,\ \phi_2=0.3$

![ARMA_22.png](https://qiita-image-store.s3.amazonaws.com/0/50670/39ee78eb-8cd2-2d0d-3903-532118180162.png)
![ARMA_23.png](https://qiita-image-store.s3.amazonaws.com/0/50670/79830362-52fe-6dd9-c07f-ff8bd28b8223.png)
![ARMA_24.png](https://qiita-image-store.s3.amazonaws.com/0/50670/8db1a045-5667-ea6d-e593-9166e7ef0647.png)
![ARMA_25.png](https://qiita-image-store.s3.amazonaws.com/0/50670/6a84dd96-6983-046a-303c-44d2f1087608.png)


## ARMA(2,1)

```math
y_t = \varepsilon_t + \phi_1 y_{t-1} + \phi_2 y_{t-2} + \theta_1 \varepsilon_{t-1} 
```
パラメータ：
$\phi_1=0.7,\ \phi_2=0.3\ \theta_1=0.7$

![ARMA_26.png](https://qiita-image-store.s3.amazonaws.com/0/50670/9b740e07-a85b-e672-4693-bed31e19bf0f.png)
![ARMA_27.png](https://qiita-image-store.s3.amazonaws.com/0/50670/f7843842-3ca9-3901-f7e4-fdbc7c744a45.png)
![ARMA_28.png](https://qiita-image-store.s3.amazonaws.com/0/50670/dc50f51a-c5e3-691e-246d-f40f9f9d8976.png)
![ARMA_29.png](https://qiita-image-store.s3.amazonaws.com/0/50670/baeeb7f3-0074-82e8-6854-7a20cfe6838c.png)
![ARMA_30.png](https://qiita-image-store.s3.amazonaws.com/0/50670/f701868c-9ba8-f068-1dbb-e3c9f5fbd7c6.png)
![ARMA_31.png](https://qiita-image-store.s3.amazonaws.com/0/50670/2ab9db60-27f2-a235-9109-c0192f0fd0c8.png)
![ARMA_32.png](https://qiita-image-store.s3.amazonaws.com/0/50670/e4d5ef13-0ef2-e024-4a1b-8f32eb9174f4.png)
![ARMA_33.png](https://qiita-image-store.s3.amazonaws.com/0/50670/08fbe17a-d0c7-8b80-3014-aa32c1f56c6a.png)


## ARMA(2,2)

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

パラメータ：
$\phi_1=0.7,\ \phi_2=0.3\ \theta_1=0.7,\ \theta_2=0.3$

![ARMA_34.png](https://qiita-image-store.s3.amazonaws.com/0/50670/b4a3c741-3aa6-ab03-a80a-20f0e2c42a93.png)
![ARMA_35.png](https://qiita-image-store.s3.amazonaws.com/0/50670/5160673b-7271-93c8-bdc6-95d1d673be1f.png)
![ARMA_36.png](https://qiita-image-store.s3.amazonaws.com/0/50670/02fca0ec-f128-b8a5-019b-27b25a9d91d8.png)
![ARMA_37.png](https://qiita-image-store.s3.amazonaws.com/0/50670/a5c840cf-20f3-9405-d2ff-160d32ed7219.png)
![ARMA_38.png](https://qiita-image-store.s3.amazonaws.com/0/50670/953b6a3c-c504-22da-1b34-6d3b25c12a51.png)
![ARMA_39.png](https://qiita-image-store.s3.amazonaws.com/0/50670/fd7d0a8d-fa46-e788-e11c-8f6414f25c5c.png)
![ARMA_40.png](https://qiita-image-store.s3.amazonaws.com/0/50670/58320aa4-55ec-b3bd-87ee-4aadfbbca47e.png)
![ARMA_41.png](https://qiita-image-store.s3.amazonaws.com/0/50670/c3044681-a8ef-80e5-e30f-50039ed5d923.png)
![ARMA_42.png](https://qiita-image-store.s3.amazonaws.com/0/50670/58313e4d-351f-a63d-8f72-893827dc08b2.png)
![ARMA_43.png](https://qiita-image-store.s3.amazonaws.com/0/50670/29d6336e-9cf8-813f-c710-81a1a70741eb.png)
![uploading ARMA_44.png...]()
![ARMA_44.png](https://qiita-image-store.s3.amazonaws.com/0/50670/064bea9f-9faf-a9c3-6902-4b46b5f1c7f7.png)
![ARMA_45.png](https://qiita-image-store.s3.amazonaws.com/0/50670/605beef2-320e-d364-9cb5-539312723c7f.png)
![ARMA_46.png](https://qiita-image-store.s3.amazonaws.com/0/50670/aa01d336-e8ef-47ed-15eb-a08a43a46771.png)
![ARMA_47.png](https://qiita-image-store.s3.amazonaws.com/0/50670/0a45b948-68a1-25fa-aa5b-6249d13bce8a.png)
![ARMA_48.png](https://qiita-image-store.s3.amazonaws.com/0/50670/80cf991f-d5d4-66c8-73b4-f159b5c41446.png)
![ARMA_49.png](https://qiita-image-store.s3.amazonaws.com/0/50670/11dfa241-5241-ae4b-ef65-114ad7d3fc51.png)


# 描画用コード
PythonでARMAの人工データが作れる`statsmodels`を利用しました。

```py
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()
```

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