【第5章】pythonで「経済・ファイナンスデータの計量時系列分析」の章末問題を解く

自分の勉強用に、[沖本竜義「経済・ファイナンスデータの計量経済分析」](https://www.asakura.co.jp/G_12.php?isbn=ISBN978-4-254-12792-8)の章末問題をpythonで解いてみました。
jupyter notebook上で記述したものをほぼそのまま載せてあります。
ソースコードの細かい説明は省いてありますが、今後余裕があれば説明を加えようと思います。

こちらの記事は第5章の章末問題を解いたものになります。
[第1章](https://qiita.com/mckeeeen/items/666e14c94e1548130646#15-5)、[第2章](https://qiita.com/mckeeeen/items/666e14c94e1548130646)、[第4章](https://qiita.com/mckeeeen/items/4afa1167008c1f315d0b)、[第6章](https://qiita.com/mckeeeen/items/55cb0130e5a7fa035e1e)、[第7章](https://qiita.com/mckeeeen/items/ce000b00b0c725503072)も公開しています。



各種設定・モジュールのインポート


```python
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from arch.unitroot import ADF
from arch.unitroot import PhillipsPerron

import matplotlib as mpl
font = {"family":"IPAexGothic"}
mpl.rc('font', **font)
plt.rcParams["font.size"] = 12
```



# 5.5

## (1)

ファイルをインポートする


```python
eco = pd.read_csv('./input/economicdata.csv')
print(eco.shape)
eco.head()
```

    (364, 7)





<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>date</th>
      <th>topix</th>
      <th>exrate</th>
      <th>indprod</th>
      <th>cpi</th>
      <th>saunemp</th>
      <th>intrate</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>Jan-75</td>
      <td>276.09</td>
      <td>29.13</td>
      <td>47.33</td>
      <td>52.625</td>
      <td>1.7</td>
      <td>12.67</td>
    </tr>
    <tr>
      <th>1</th>
      <td>Feb-75</td>
      <td>299.81</td>
      <td>29.70</td>
      <td>46.86</td>
      <td>52.723</td>
      <td>1.8</td>
      <td>13.00</td>
    </tr>
    <tr>
      <th>2</th>
      <td>Mar-75</td>
      <td>313.50</td>
      <td>29.98</td>
      <td>46.24</td>
      <td>53.114</td>
      <td>1.8</td>
      <td>12.92</td>
    </tr>
    <tr>
      <th>3</th>
      <td>Apr-75</td>
      <td>320.57</td>
      <td>29.80</td>
      <td>47.33</td>
      <td>54.092</td>
      <td>1.8</td>
      <td>12.02</td>
    </tr>
    <tr>
      <th>4</th>
      <td>May-75</td>
      <td>329.65</td>
      <td>29.79</td>
      <td>47.33</td>
      <td>54.385</td>
      <td>1.8</td>
      <td>11.06</td>
    </tr>
  </tbody>
</table>
</div>



Date列をdatetime型に変換してecoのDatetimeIndexとする


```python
eco.index=pd.to_datetime(eco.date.values,format='%b-%y')
eco.drop('date',axis=1,inplace=True)
eco.head()
```




<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>topix</th>
      <th>exrate</th>
      <th>indprod</th>
      <th>cpi</th>
      <th>saunemp</th>
      <th>intrate</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1975-01-01</th>
      <td>276.09</td>
      <td>29.13</td>
      <td>47.33</td>
      <td>52.625</td>
      <td>1.7</td>
      <td>12.67</td>
    </tr>
    <tr>
      <th>1975-02-01</th>
      <td>299.81</td>
      <td>29.70</td>
      <td>46.86</td>
      <td>52.723</td>
      <td>1.8</td>
      <td>13.00</td>
    </tr>
    <tr>
      <th>1975-03-01</th>
      <td>313.50</td>
      <td>29.98</td>
      <td>46.24</td>
      <td>53.114</td>
      <td>1.8</td>
      <td>12.92</td>
    </tr>
    <tr>
      <th>1975-04-01</th>
      <td>320.57</td>
      <td>29.80</td>
      <td>47.33</td>
      <td>54.092</td>
      <td>1.8</td>
      <td>12.02</td>
    </tr>
    <tr>
      <th>1975-05-01</th>
      <td>329.65</td>
      <td>29.79</td>
      <td>47.33</td>
      <td>54.385</td>
      <td>1.8</td>
      <td>11.06</td>
    </tr>
  </tbody>
</table>
</div>



topix,exrate,indprod,cpiの対数系列を追加する


```python
logs=['topix','exrate','indprod','cpi'] 
for i in range(len(logs)):
    eco['%s_log'%logs[i]]=np.log(eco[logs[i]])
eco.head()
```




<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>topix</th>
      <th>exrate</th>
      <th>indprod</th>
      <th>cpi</th>
      <th>saunemp</th>
      <th>intrate</th>
      <th>topix_log</th>
      <th>exrate_log</th>
      <th>indprod_log</th>
      <th>cpi_log</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1975-01-01</th>
      <td>276.09</td>
      <td>29.13</td>
      <td>47.33</td>
      <td>52.625</td>
      <td>1.7</td>
      <td>12.67</td>
      <td>5.620727</td>
      <td>3.371769</td>
      <td>3.857144</td>
      <td>3.963191</td>
    </tr>
    <tr>
      <th>1975-02-01</th>
      <td>299.81</td>
      <td>29.70</td>
      <td>46.86</td>
      <td>52.723</td>
      <td>1.8</td>
      <td>13.00</td>
      <td>5.703149</td>
      <td>3.391147</td>
      <td>3.847164</td>
      <td>3.965052</td>
    </tr>
    <tr>
      <th>1975-03-01</th>
      <td>313.50</td>
      <td>29.98</td>
      <td>46.24</td>
      <td>53.114</td>
      <td>1.8</td>
      <td>12.92</td>
      <td>5.747799</td>
      <td>3.400530</td>
      <td>3.833845</td>
      <td>3.972441</td>
    </tr>
    <tr>
      <th>1975-04-01</th>
      <td>320.57</td>
      <td>29.80</td>
      <td>47.33</td>
      <td>54.092</td>
      <td>1.8</td>
      <td>12.02</td>
      <td>5.770101</td>
      <td>3.394508</td>
      <td>3.857144</td>
      <td>3.990686</td>
    </tr>
    <tr>
      <th>1975-05-01</th>
      <td>329.65</td>
      <td>29.79</td>
      <td>47.33</td>
      <td>54.385</td>
      <td>1.8</td>
      <td>11.06</td>
      <td>5.798031</td>
      <td>3.394173</td>
      <td>3.857144</td>
      <td>3.996088</td>
    </tr>
  </tbody>
</table>
</div>



プロットする


```python
title=['topix_log','exrate_log','indprod_log','cpi_log','saunemp','intrate']

fig,ax = plt.subplots(nrows=3,ncols=2,figsize=[10,15])
for i in range(3):
    for j in range(2):
        ax[i,j].plot(eco[title].iloc[:,2*i+j])
        ax[i,j].set_title(title[2*i+j])
        ax[i,j].set_xlim(eco.index[0],eco.index[-1])
        ax[i,j].set_xticks(eco.index[eco.index.is_year_start][::3])
        ax[i,j].set_xticklabels(eco.index[eco.index.is_year_start][::3].strftime('%y'))
        ax[i,j].set_xlabel('year')
plt.subplots_adjust(wspace=0.2,hspace=0.3)
plt.show()
```


![output_11_0.png](https://qiita-image-store.s3.amazonaws.com/0/235450/5c8af2a4-8ca7-0b8b-6ca7-5643a4e65d49.png)



5.2の解答に従い、exrate_log,saunemp,intrateは場合2、topix_log,indeprod_log,cpi_logは場合3を仮定する
* 場合1...トレンドがなく、期待値が0
* 場合2...トレンドがなく、期待値が0ではない
* 場合3...トレンドがある


```python
trend=['ct','c','ct','ct','c','c']
adf=[]
pp=[]
for i in range(len(title)):
    adf.append(ADF(eco[title[i]],trend=trend[i],max_lags=10,method='AIC'))
    pp.append(PhillipsPerron(eco[title[i]],trend=trend[i]))
```

ADF検定の結果を表示する


```python
for i in range(len(title)):
    print(title[i])
    print(adf[i])
    print('')
```

    topix_log
       Augmented Dickey-Fuller Results   
    =====================================
    Test Statistic                 -1.149
    P-value                         0.921
    Lags                                1
    -------------------------------------
    
    Trend: Constant and Linear Time Trend
    Critical Values: -3.98 (1%), -3.42 (5%), -3.13 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    exrate_log
       Augmented Dickey-Fuller Results   
    =====================================
    Test Statistic                 -1.839
    P-value                         0.361
    Lags                                3
    -------------------------------------
    
    Trend: Constant
    Critical Values: -3.45 (1%), -2.87 (5%), -2.57 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    indprod_log
       Augmented Dickey-Fuller Results   
    =====================================
    Test Statistic                 -2.299
    P-value                         0.435
    Lags                                4
    -------------------------------------
    
    Trend: Constant and Linear Time Trend
    Critical Values: -3.98 (1%), -3.42 (5%), -3.13 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    cpi_log
       Augmented Dickey-Fuller Results   
    =====================================
    Test Statistic                 -3.824
    P-value                         0.015
    Lags                               10
    -------------------------------------
    
    Trend: Constant and Linear Time Trend
    Critical Values: -3.98 (1%), -3.42 (5%), -3.13 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    saunemp
       Augmented Dickey-Fuller Results   
    =====================================
    Test Statistic                 -0.846
    P-value                         0.805
    Lags                               10
    -------------------------------------
    
    Trend: Constant
    Critical Values: -3.45 (1%), -2.87 (5%), -2.57 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    intrate
       Augmented Dickey-Fuller Results   
    =====================================
    Test Statistic                 -2.226
    P-value                         0.197
    Lags                                3
    -------------------------------------
    
    Trend: Constant
    Critical Values: -3.45 (1%), -2.87 (5%), -2.57 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    


ADF検定では、cpi_logのみ単位根の帰無仮説が棄却された

PP検定の結果を表示する


```python
for i in range(len(title)):
    print(title[i])
    print(pp[i])
    print('')
```

    topix_log
         Phillips-Perron Test (Z-tau)    
    =====================================
    Test Statistic                 -1.269
    P-value                         0.895
    Lags                               17
    -------------------------------------
    
    Trend: Constant and Linear Time Trend
    Critical Values: -3.98 (1%), -3.42 (5%), -3.13 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    exrate_log
         Phillips-Perron Test (Z-tau)    
    =====================================
    Test Statistic                 -1.856
    P-value                         0.353
    Lags                               17
    -------------------------------------
    
    Trend: Constant
    Critical Values: -3.45 (1%), -2.87 (5%), -2.57 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    indprod_log
         Phillips-Perron Test (Z-tau)    
    =====================================
    Test Statistic                 -2.012
    P-value                         0.595
    Lags                               17
    -------------------------------------
    
    Trend: Constant and Linear Time Trend
    Critical Values: -3.98 (1%), -3.42 (5%), -3.13 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    cpi_log
         Phillips-Perron Test (Z-tau)    
    =====================================
    Test Statistic                 -3.898
    P-value                         0.012
    Lags                               17
    -------------------------------------
    
    Trend: Constant and Linear Time Trend
    Critical Values: -3.98 (1%), -3.42 (5%), -3.13 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    saunemp
         Phillips-Perron Test (Z-tau)    
    =====================================
    Test Statistic                 -0.733
    P-value                         0.838
    Lags                               17
    -------------------------------------
    
    Trend: Constant
    Critical Values: -3.45 (1%), -2.87 (5%), -2.57 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    intrate
         Phillips-Perron Test (Z-tau)    
    =====================================
    Test Statistic                 -2.417
    P-value                         0.137
    Lags                               17
    -------------------------------------
    
    Trend: Constant
    Critical Values: -3.45 (1%), -2.87 (5%), -2.57 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    


ADF検定と同じくPP検定でも、cpi_logのみ単位根の帰無仮説が棄却された

## (2)

cpi_log以外の系列の差分系列を作成する


```python
for i in range(len(title)):
    if title[i]!='cpi_log':
        eco['%s_diff'%title[i]]=eco[title[i]].diff()
    else:
        pass
```

プロットする


```python
diff=['topix_log_diff','exrate_log_diff','indprod_log_diff','saunemp_diff','intrate_diff']

fig,ax = plt.subplots(nrows=3,ncols=2,figsize=[10,15])
for i in range(3):
    for j in range(2):
        if i!=2 or j!=1:
            ax[i,j].plot(eco[diff].iloc[:,2*i+j])
            ax[i,j].set_title(diff[2*i+j])
            ax[i,j].set_xlim(eco.index[0],eco.index[-1])
            ax[i,j].set_xticks(eco.index[eco.index.is_year_start][::3])
            ax[i,j].set_xticklabels(eco.index[eco.index.is_year_start][::3].strftime('%y'))
            ax[i,j].set_xlabel('year')
        else:
            pass
plt.subplots_adjust(wspace=0.2,hspace=0.3)
plt.show()
```


![output_24_0.png](https://qiita-image-store.s3.amazonaws.com/0/235450/dcb9f819-422c-46c9-457f-7b68136aac54.png)



全ての系列でトレンドがなく期待値が0であるように見えるので、全ての系列について場合1を仮定してADF検定及びPP検定を行う
* 場合1...トレンドがなく、期待値が0
* 場合2...トレンドがなく、期待値が0ではない
* 場合3...トレンドがある


```python
trend=['nc','nc','nc','nc','nc']
adf=[]
pp=[]
for i in range(len(diff)):
    adf.append(ADF(eco[diff[i]][1:],trend=trend[i],max_lags=10,method='AIC'))
    pp.append(PhillipsPerron(eco[diff[i]][1:],trend=trend[i]))
```

ADF検定の結果を表示する


```python
for i in range(len(diff)):
    print(diff[i])
    print(adf[i])
    print('')
```

    topix_log_diff
       Augmented Dickey-Fuller Results   
    =====================================
    Test Statistic                -13.799
    P-value                         0.000
    Lags                                0
    -------------------------------------
    
    Trend: No Trend
    Critical Values: -2.57 (1%), -1.94 (5%), -1.62 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    exrate_log_diff
       Augmented Dickey-Fuller Results   
    =====================================
    Test Statistic                 -8.762
    P-value                         0.000
    Lags                                2
    -------------------------------------
    
    Trend: No Trend
    Critical Values: -2.57 (1%), -1.94 (5%), -1.62 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    indprod_log_diff
       Augmented Dickey-Fuller Results   
    =====================================
    Test Statistic                 -6.117
    P-value                         0.000
    Lags                                3
    -------------------------------------
    
    Trend: No Trend
    Critical Values: -2.57 (1%), -1.94 (5%), -1.62 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    saunemp_diff
       Augmented Dickey-Fuller Results   
    =====================================
    Test Statistic                 -3.864
    P-value                         0.000
    Lags                                9
    -------------------------------------
    
    Trend: No Trend
    Critical Values: -2.57 (1%), -1.94 (5%), -1.62 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    intrate_diff
       Augmented Dickey-Fuller Results   
    =====================================
    Test Statistic                 -7.681
    P-value                         0.000
    Lags                                2
    -------------------------------------
    
    Trend: No Trend
    Critical Values: -2.57 (1%), -1.94 (5%), -1.62 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    


ADF検定では、全ての系列で単位根の帰無仮説が棄却された

PP検定の結果を表示する


```python
for i in range(len(diff)):
    print(diff[i])
    print(pp[i])
    print('')
```

    topix_log_diff
         Phillips-Perron Test (Z-tau)    
    =====================================
    Test Statistic                -14.137
    P-value                         0.000
    Lags                               17
    -------------------------------------
    
    Trend: No Trend
    Critical Values: -2.57 (1%), -1.94 (5%), -1.62 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    exrate_log_diff
         Phillips-Perron Test (Z-tau)    
    =====================================
    Test Statistic                -13.692
    P-value                         0.000
    Lags                               17
    -------------------------------------
    
    Trend: No Trend
    Critical Values: -2.57 (1%), -1.94 (5%), -1.62 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    indprod_log_diff
         Phillips-Perron Test (Z-tau)    
    =====================================
    Test Statistic                -24.889
    P-value                         0.000
    Lags                               17
    -------------------------------------
    
    Trend: No Trend
    Critical Values: -2.57 (1%), -1.94 (5%), -1.62 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    saunemp_diff
         Phillips-Perron Test (Z-tau)    
    =====================================
    Test Statistic                -21.391
    P-value                         0.000
    Lags                               17
    -------------------------------------
    
    Trend: No Trend
    Critical Values: -2.57 (1%), -1.94 (5%), -1.62 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    
    intrate_diff
         Phillips-Perron Test (Z-tau)    
    =====================================
    Test Statistic                -14.303
    P-value                         0.000
    Lags                               17
    -------------------------------------
    
    Trend: No Trend
    Critical Values: -2.57 (1%), -1.94 (5%), -1.62 (10%)
    Null Hypothesis: The process contains a unit root.
    Alternative Hypothesis: The process is weakly stationary.
    


ADF検定と同じくPP検定でも、全ての系列で単位根の帰無仮説が棄却された
結論として、cpi_logは定常過程に従い、その他5つの系列は全て単位根過程（1次和分過程）に従うことが示唆された

# 参考サイト

ARCHのドキュメント: http://arch.readthedocs.io/en/latest/index.html