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

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

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

今回、多変量GARCHモデルを扱えるpythonのモジュールが見当たらず、7.5をpythonで解答することを断念しました。もし、多変量GARCHモデルを扱えるモジュールをご存知の方がいましたらご教授いただけますと有り難いです。

また、第8章の章末問題にも挑戦しましたが、閾値モデルを扱えるモジュールが見当たらないこと及びマルコフ転換モデルをベクトルに対して扱えるモジュールが見当たらないことから、pythonで解答することを断念しました。もし、閾値モデルやマルコフ転換モデルを扱えるモジュールをご存知の方がいましたらご教授いただけますと有り難いです。

この辺まで来るとRには敵わないという感じでしょうか...

兎も角、今回で『pythonで「経済・ファイナンスデータの計量時系列分析」の章末問題を解く』シリーズは最後となります。ここまで読んでいただき、ありがとうございました。



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


```python
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import acf
from statsmodels.stats.diagnostic import acorr_ljungbox
from arch import arch_model

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

# 7.4

## (1)

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


```python
msci = pd.read_csv('./input/msci_day.csv')
print(msci.shape)
msci.head()
```

    (1391, 8)





<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Date</th>
      <th>ca</th>
      <th>fr</th>
      <th>ge</th>
      <th>it</th>
      <th>jp</th>
      <th>uk</th>
      <th>us</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2003/1/1</td>
      <td>560.099</td>
      <td>902.081</td>
      <td>724.932</td>
      <td>290.187</td>
      <td>1593.175</td>
      <td>791.076</td>
      <td>824.583</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2003/1/2</td>
      <td>574.701</td>
      <td>927.206</td>
      <td>768.150</td>
      <td>296.963</td>
      <td>1578.214</td>
      <td>797.813</td>
      <td>852.219</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2003/1/3</td>
      <td>580.212</td>
      <td>929.297</td>
      <td>768.411</td>
      <td>298.757</td>
      <td>1578.411</td>
      <td>800.175</td>
      <td>851.935</td>
    </tr>
    <tr>
      <th>3</th>
      <td>2003/1/6</td>
      <td>589.619</td>
      <td>943.002</td>
      <td>788.164</td>
      <td>303.273</td>
      <td>1619.700</td>
      <td>803.966</td>
      <td>871.515</td>
    </tr>
    <tr>
      <th>4</th>
      <td>2003/1/7</td>
      <td>585.822</td>
      <td>923.785</td>
      <td>774.054</td>
      <td>297.892</td>
      <td>1590.951</td>
      <td>793.625</td>
      <td>865.992</td>
    </tr>
  </tbody>
</table>
</div>



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


```python
msci.index=pd.to_datetime(msci.Date.values)
msci.drop('Date',axis=1,inplace=True)
msci.head()
```




<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>ca</th>
      <th>fr</th>
      <th>ge</th>
      <th>it</th>
      <th>jp</th>
      <th>uk</th>
      <th>us</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2003-01-01</th>
      <td>560.099</td>
      <td>902.081</td>
      <td>724.932</td>
      <td>290.187</td>
      <td>1593.175</td>
      <td>791.076</td>
      <td>824.583</td>
    </tr>
    <tr>
      <th>2003-01-02</th>
      <td>574.701</td>
      <td>927.206</td>
      <td>768.150</td>
      <td>296.963</td>
      <td>1578.214</td>
      <td>797.813</td>
      <td>852.219</td>
    </tr>
    <tr>
      <th>2003-01-03</th>
      <td>580.212</td>
      <td>929.297</td>
      <td>768.411</td>
      <td>298.757</td>
      <td>1578.411</td>
      <td>800.175</td>
      <td>851.935</td>
    </tr>
    <tr>
      <th>2003-01-06</th>
      <td>589.619</td>
      <td>943.002</td>
      <td>788.164</td>
      <td>303.273</td>
      <td>1619.700</td>
      <td>803.966</td>
      <td>871.515</td>
    </tr>
    <tr>
      <th>2003-01-07</th>
      <td>585.822</td>
      <td>923.785</td>
      <td>774.054</td>
      <td>297.892</td>
      <td>1590.951</td>
      <td>793.625</td>
      <td>865.992</td>
    </tr>
  </tbody>
</table>
</div>



欠損データ(e.g. 2005-01-01)があるので、グラフの目盛用にその年で1番最初の日かどうかをカラムに加える


```python
msci['is_year_start']=[msci.index[i].year!=msci.index[i-1].year for i in range(msci.shape[0])]
```

株式収益率(%)の系列を対数差分を用いて作成する


```python
title=['ca','fr','ge','it','jp','uk','us']
for i in range(len(title)):
    msci['per_%s'%title[i]]=np.log(msci[title[i]]).diff()
msci.head()
```




<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>ca</th>
      <th>fr</th>
      <th>ge</th>
      <th>it</th>
      <th>jp</th>
      <th>uk</th>
      <th>us</th>
      <th>is_year_start</th>
      <th>per_ca</th>
      <th>per_fr</th>
      <th>per_ge</th>
      <th>per_it</th>
      <th>per_jp</th>
      <th>per_uk</th>
      <th>per_us</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2003-01-01</th>
      <td>560.099</td>
      <td>902.081</td>
      <td>724.932</td>
      <td>290.187</td>
      <td>1593.175</td>
      <td>791.076</td>
      <td>824.583</td>
      <td>True</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>NaN</td>
    </tr>
    <tr>
      <th>2003-01-02</th>
      <td>574.701</td>
      <td>927.206</td>
      <td>768.150</td>
      <td>296.963</td>
      <td>1578.214</td>
      <td>797.813</td>
      <td>852.219</td>
      <td>False</td>
      <td>0.025736</td>
      <td>0.027471</td>
      <td>0.057907</td>
      <td>0.023082</td>
      <td>-0.009435</td>
      <td>0.008480</td>
      <td>0.032966</td>
    </tr>
    <tr>
      <th>2003-01-03</th>
      <td>580.212</td>
      <td>929.297</td>
      <td>768.411</td>
      <td>298.757</td>
      <td>1578.411</td>
      <td>800.175</td>
      <td>851.935</td>
      <td>False</td>
      <td>0.009544</td>
      <td>0.002253</td>
      <td>0.000340</td>
      <td>0.006023</td>
      <td>0.000125</td>
      <td>0.002956</td>
      <td>-0.000333</td>
    </tr>
    <tr>
      <th>2003-01-06</th>
      <td>589.619</td>
      <td>943.002</td>
      <td>788.164</td>
      <td>303.273</td>
      <td>1619.700</td>
      <td>803.966</td>
      <td>871.515</td>
      <td>False</td>
      <td>0.016083</td>
      <td>0.014640</td>
      <td>0.025381</td>
      <td>0.015003</td>
      <td>0.025822</td>
      <td>0.004727</td>
      <td>0.022723</td>
    </tr>
    <tr>
      <th>2003-01-07</th>
      <td>585.822</td>
      <td>923.785</td>
      <td>774.054</td>
      <td>297.892</td>
      <td>1590.951</td>
      <td>793.625</td>
      <td>865.992</td>
      <td>False</td>
      <td>-0.006461</td>
      <td>-0.020589</td>
      <td>-0.018065</td>
      <td>-0.017902</td>
      <td>-0.017909</td>
      <td>-0.012946</td>
      <td>-0.006357</td>
    </tr>
  </tbody>
</table>
</div>



アメリカの株式収益率のコレログラムをプロットする


```python
fig,ax=plt.subplots(1,1,figsize=[8,5])
ax.bar(range(1,21),acf(msci['per_us'][1:])[1:21])
ax.set_ylim(-0.2,0.2)
ax.set_xticks([1,5,10,15,20])
ax.set_yticks(np.asarray(range(-2,3))*0.1)
ax.hlines(0,0,21,linewidth=1)
ax.hlines([1.96/np.sqrt(msci.shape[0]-1),-1.96/np.sqrt(msci.shape[0]-1)],0,21,linestyles='dashed',linewidth=1)
ax.set_xlabel('lag')
ax.set_title('acf')
plt.show()
```


![output_13_0.png](https://qiita-image-store.s3.amazonaws.com/0/235450/9da75e81-0624-4209-8f2a-00915ce0bd30.png)



1期（と7期と19期）に大きな自己相関がみられる

## (2)

アメリカの株式収益率の2乗の系列を作成する


```python
msci['per_sq_us']=msci['per_us']**2
```

アメリカの株式収益率の2乗のコレログラムをプロットする


```python
fig,ax=plt.subplots(1,1,figsize=[8,5])
ax.bar(range(1,21),acf(msci['per_sq_us'][1:])[1:21])
ax.set_ylim(-0.1,0.3)
ax.set_xticks([1,5,10,15,20])
ax.set_yticks(np.asarray(range(-1,4))*0.1)
ax.hlines(0,0,21,linewidth=1)
ax.hlines([1.96/np.sqrt(msci.shape[0]-1),-1.96/np.sqrt(msci.shape[0]-1)],0,21,linestyles='dashed',linewidth=1)
ax.set_xlabel('lag')
ax.set_title('acf')
plt.show()
```


![output_19_0.png](https://qiita-image-store.s3.amazonaws.com/0/235450/76c0b35b-35a7-a80e-7660-778ec53ff122.png)



20期までの全てで大きな自己相関がみられる

## (3)

アメリカの株式収益率のデータに対し、AR(1)-GARCH(1,1)モデルを推定する
（尚、系列の値を100倍しているのは、最適化を収束させるためです:https://quant.stackexchange.com/questions/32350/fitting-garch1-1-in-python-for-moderately-large-data-sets）


```python
garch=arch_model(100*msci['per_us'][1:],mean='AR',lags=1,vol='GARCH',p=1,o=0,q=1,dist='studentst').fit()
```

    Iteration:      1,   Func. Count:      8,   Neg. LLF: 1626.1493851808236
    Iteration:      2,   Func. Count:     20,   Neg. LLF: 1624.472806709472
    Iteration:      3,   Func. Count:     31,   Neg. LLF: 1624.1048230411657
    Iteration:      4,   Func. Count:     42,   Neg. LLF: 1623.8180932803173
    Iteration:      5,   Func. Count:     52,   Neg. LLF: 1623.5731061263423
    Iteration:      6,   Func. Count:     61,   Neg. LLF: 1621.7085398302058
    Iteration:      7,   Func. Count:     70,   Neg. LLF: 1621.477983394893
    Iteration:      8,   Func. Count:     79,   Neg. LLF: 1621.1965432813513
    Iteration:      9,   Func. Count:     88,   Neg. LLF: 1621.154364449716
    Iteration:     10,   Func. Count:     97,   Neg. LLF: 1621.1441778563244
    Iteration:     11,   Func. Count:    105,   Neg. LLF: 1621.1435809913812
    Iteration:     12,   Func. Count:    113,   Neg. LLF: 1621.1434081176585
    Iteration:     13,   Func. Count:    121,   Neg. LLF: 1621.1433861157093
    Optimization terminated successfully.    (Exit mode 0)
                Current function value: 1621.1433861157084
                Iterations: 13
                Function evaluations: 121
                Gradient evaluations: 13


結果を表示する


```python
garch.summary()
```




<table class="simpletable">
<caption>AR - GARCH Model Results</caption>
<tr>
  <th>Dep. Variable:</th>          <td>per_us</td>          <th>  R-squared:         </th>  <td>   0.010</td> 
</tr>
<tr>
  <th>Mean Model:</th>               <td>AR</td>            <th>  Adj. R-squared:    </th>  <td>   0.009</td> 
</tr>
<tr>
  <th>Vol Model:</th>               <td>GARCH</td>          <th>  Log-Likelihood:    </th> <td>  -1621.14</td>
</tr>
<tr>
  <th>Distribution:</th>  <td>Standardized Student's t</td> <th>  AIC:               </th> <td>   3254.29</td>
</tr>
<tr>
  <th>Method:</th>           <td>Maximum Likelihood</td>    <th>  BIC:               </th> <td>   3285.70</td>
</tr>
<tr>
  <th></th>                           <td></td>             <th>  No. Observations:  </th>    <td>1389</td>   
</tr>
<tr>
  <th>Date:</th>              <td>Sun, Feb 18 2018</td>     <th>  Df Residuals:      </th>    <td>1383</td>   
</tr>
<tr>
  <th>Time:</th>                  <td>23:55:20</td>         <th>  Df Model:          </th>      <td>6</td>    
</tr>
</table>
<table class="simpletable">
<caption>Mean Model</caption>
<tr>
      <td></td>         <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>     <th>95.0% Conf. Int.</th>   
</tr>
<tr>
  <th>Const</th>     <td>    0.0595</td> <td>1.904e-02</td> <td>    3.127</td> <td>1.765e-03</td> <td>[2.222e-02,9.684e-02]</td>
</tr>
<tr>
  <th>per_us[1]</th> <td>   -0.0734</td> <td>2.386e-02</td> <td>   -3.076</td> <td>2.101e-03</td> <td>[ -0.120,-2.661e-02]</td> 
</tr>
</table>
<table class="simpletable">
<caption>Volatility Model</caption>
<tr>
      <td></td>        <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>      <th>95.0% Conf. Int.</th>   
</tr>
<tr>
  <th>omega</th>    <td>5.7558e-03</td> <td>3.979e-03</td> <td>    1.446</td> <td>    0.148</td> <td>[-2.044e-03,1.356e-02]</td>
</tr>
<tr>
  <th>alpha[1]</th> <td>    0.0490</td> <td>1.471e-02</td> <td>    3.333</td> <td>8.582e-04</td>  <td>[2.020e-02,7.784e-02]</td>
</tr>
<tr>
  <th>beta[1]</th>  <td>    0.9432</td> <td>1.803e-02</td> <td>   52.322</td>   <td>0.000</td>      <td>[  0.908,  0.979]</td>  
</tr>
</table>
<table class="simpletable">
<caption>Distribution</caption>
<tr>
   <td></td>     <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>   <th>95.0% Conf. Int.</th> 
</tr>
<tr>
  <th>nu</th> <td>    7.9484</td> <td>    2.074</td> <td>    3.833</td> <td>1.267e-04</td> <td>[  3.884, 12.013]</td>
</tr>
</table>
<caption>Covariance estimator: robust</caption>



## (4)

アメリカの株式収益率のデータに対し、AR(1)-GJR(1,1)モデルを推定する


```python
gjr=arch_model(100*msci['per_us'][1:],mean='AR',lags=1,vol='GARCH',p=1,o=1,q=1,dist='studentst').fit()
```


    Iteration:      1,   Func. Count:      9,   Neg. LLF: 1608.311864669829
    Iteration:      2,   Func. Count:     21,   Neg. LLF: 1607.6177366714319
    Iteration:      3,   Func. Count:     34,   Neg. LLF: 1607.4674097311886
    Iteration:      4,   Func. Count:     46,   Neg. LLF: 1607.2432887204382
    Iteration:      5,   Func. Count:     58,   Neg. LLF: 1607.2321387222742
    Iteration:      6,   Func. Count:     68,   Neg. LLF: 1605.550623830057
    Iteration:      7,   Func. Count:     78,   Neg. LLF: 1605.0461425292215
    Iteration:      8,   Func. Count:     88,   Neg. LLF: 1604.6568827154686
    Iteration:      9,   Func. Count:     98,   Neg. LLF: 1604.4903663343728
    Iteration:     10,   Func. Count:    107,   Neg. LLF: 1603.7963336935013
    Iteration:     11,   Func. Count:    116,   Neg. LLF: 1603.7028334036809
    Iteration:     12,   Func. Count:    125,   Neg. LLF: 1603.6926759295156
    Iteration:     13,   Func. Count:    134,   Neg. LLF: 1603.6871967349107
    Iteration:     14,   Func. Count:    143,   Neg. LLF: 1603.6858039263102
    Iteration:     15,   Func. Count:    152,   Neg. LLF: 1603.6856369758025
    Iteration:     16,   Func. Count:    161,   Neg. LLF: 1603.6856308684942
    Optimization terminated successfully.    (Exit mode 0)
                Current function value: 1603.6856308684942
                Iterations: 16
                Function evaluations: 161
                Gradient evaluations: 16


結果を表示する


```python
gjr.summary()
```




<table class="simpletable">
<caption>AR - GJR-GARCH Model Results</caption>
<tr>
  <th>Dep. Variable:</th>          <td>per_us</td>          <th>  R-squared:         </th>  <td>   0.010</td> 
</tr>
<tr>
  <th>Mean Model:</th>               <td>AR</td>            <th>  Adj. R-squared:    </th>  <td>   0.009</td> 
</tr>
<tr>
  <th>Vol Model:</th>             <td>GJR-GARCH</td>        <th>  Log-Likelihood:    </th> <td>  -1603.69</td>
</tr>
<tr>
  <th>Distribution:</th>  <td>Standardized Student's t</td> <th>  AIC:               </th> <td>   3221.37</td>
</tr>
<tr>
  <th>Method:</th>           <td>Maximum Likelihood</td>    <th>  BIC:               </th> <td>   3258.03</td>
</tr>
<tr>
  <th></th>                           <td></td>             <th>  No. Observations:  </th>    <td>1389</td>   
</tr>
<tr>
  <th>Date:</th>              <td>Sun, Feb 18 2018</td>     <th>  Df Residuals:      </th>    <td>1382</td>   
</tr>
<tr>
  <th>Time:</th>                  <td>23:55:20</td>         <th>  Df Model:          </th>      <td>7</td>    
</tr>
</table>
<table class="simpletable">
<caption>Mean Model</caption>
<tr>
      <td></td>         <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>     <th>95.0% Conf. Int.</th>   
</tr>
<tr>
  <th>Const</th>     <td>    0.0429</td> <td>1.986e-02</td> <td>    2.158</td> <td>3.092e-02</td> <td>[3.934e-03,8.180e-02]</td>
</tr>
<tr>
  <th>per_us[1]</th> <td>   -0.0698</td> <td>2.380e-02</td> <td>   -2.933</td> <td>3.360e-03</td> <td>[ -0.116,-2.315e-02]</td> 
</tr>
</table>
<table class="simpletable">
<caption>Volatility Model</caption>
<tr>
      <td></td>        <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>      <th>95.0% Conf. Int.</th>   
</tr>
<tr>
  <th>omega</th>    <td>6.4152e-03</td> <td>7.926e-03</td> <td>    0.809</td> <td>    0.418</td> <td>[-9.119e-03,2.195e-02]</td>
</tr>
<tr>
  <th>alpha[1]</th> <td>1.2537e-10</td> <td>3.358e-02</td> <td>3.734e-09</td> <td>    1.000</td> <td>[-6.581e-02,6.581e-02]</td>
</tr>
<tr>
  <th>gamma[1]</th> <td>    0.0857</td> <td>3.043e-02</td> <td>    2.817</td> <td>4.848e-03</td>   <td>[2.608e-02,  0.145]</td> 
</tr>
<tr>
  <th>beta[1]</th>  <td>    0.9453</td> <td>5.263e-02</td> <td>   17.962</td> <td>3.868e-72</td>    <td>[  0.842,  1.048]</td>  
</tr>
</table>
<table class="simpletable">
<caption>Distribution</caption>
<tr>
   <td></td>     <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>   <th>95.0% Conf. Int.</th> 
</tr>
<tr>
  <th>nu</th> <td>    8.8703</td> <td>    2.844</td> <td>    3.119</td> <td>1.817e-03</td> <td>[  3.296, 14.445]</td>
</tr>
</table>
<caption>Covariance estimator: robust</caption>



レバレッジ効果を表すgammaが正なのでレバレッジ効果は有効だと考えられる

## (5)

残差について自己相関検定を行う


```python
acf_=acf(gjr.resid[1:])[1:]
q_m=acorr_ljungbox(gjr.resid[1:])[0]
p_value=acorr_ljungbox(gjr.resid[1:])[1]
lag=range(1,41)
test=pd.DataFrame({'lag':lag,'autocorr':acf_,'Q(m)':q_m,'P_value':p_value})
test[['lag','autocorr','Q(m)','P_value']].iloc[0:10,:].T
```




<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
      <th>5</th>
      <th>6</th>
      <th>7</th>
      <th>8</th>
      <th>9</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>lag</th>
      <td>1.000000</td>
      <td>2.000000</td>
      <td>3.000000</td>
      <td>4.000000</td>
      <td>5.000000</td>
      <td>6.000000</td>
      <td>7.000000</td>
      <td>8.000000</td>
      <td>9.000000</td>
      <td>10.000000</td>
    </tr>
    <tr>
      <th>autocorr</th>
      <td>-0.039489</td>
      <td>-0.019336</td>
      <td>0.013273</td>
      <td>-0.018904</td>
      <td>-0.023108</td>
      <td>-0.031006</td>
      <td>-0.062719</td>
      <td>-0.010821</td>
      <td>-0.006836</td>
      <td>0.033678</td>
    </tr>
    <tr>
      <th>Q(m)</th>
      <td>2.170625</td>
      <td>2.691466</td>
      <td>2.937043</td>
      <td>3.435585</td>
      <td>4.181064</td>
      <td>5.524131</td>
      <td>11.023673</td>
      <td>11.187490</td>
      <td>11.252913</td>
      <td>12.842065</td>
    </tr>
    <tr>
      <th>P_value</th>
      <td>0.140669</td>
      <td>0.260349</td>
      <td>0.401434</td>
      <td>0.487740</td>
      <td>0.523653</td>
      <td>0.478545</td>
      <td>0.137590</td>
      <td>0.191300</td>
      <td>0.258771</td>
      <td>0.232637</td>
    </tr>
  </tbody>
</table>
</div>



残差のコレログラムをプロットする


```python
fig,ax=plt.subplots(1,1,figsize=[8,5])
ax.bar(range(1,21),acf_[1:21])
ax.set_ylim(-0.1,0.1)
ax.set_xticks([1,5,10,15,20])
ax.set_yticks(np.asarray(range(-1,2))*0.1)
ax.hlines(0,0,21,linewidth=1)
ax.hlines([1.96/np.sqrt(gjr.resid.shape[0]-1),-1.96/np.sqrt(gjr.resid.shape[0]-1)],0,21,linestyles='dashed',linewidth=1)
ax.set_xlabel('lag')
ax.set_title('acf')
plt.show()
```


![output_36_0.png](https://qiita-image-store.s3.amazonaws.com/0/235450/3c018eaf-1c30-01e3-2389-3d10adbc58d7.png)



かばん検定の結果とコレログラムから、残差に大きな自己相関がないことが示唆された

## (6)

アメリカの株式収益率のデータに対し、AR(1)-EGARCH(1,1)モデルを推定する


```python
egarch=arch_model(100*msci['per_us'][1:],mean='AR',lags=1,vol='EGARCH',p=1,o=1,q=1,dist='studentst').fit()
```

    Iteration:      1,   Func. Count:      9,   Neg. LLF: 1606.2314288376633
    Iteration:      2,   Func. Count:     22,   Neg. LLF: 1604.722938615969
    Iteration:      3,   Func. Count:     35,   Neg. LLF: 1604.5984134498178
    Iteration:      4,   Func. Count:     48,   Neg. LLF: 1604.4955446095305
    Iteration:      5,   Func. Count:     61,   Neg. LLF: 1604.4129540561485
    Iteration:      6,   Func. Count:     73,   Neg. LLF: 1604.4099059723555
    Iteration:      7,   Func. Count:     83,   Neg. LLF: 1603.58238963428
    Iteration:      8,   Func. Count:     95,   Neg. LLF: 1603.5013215028691
    Iteration:      9,   Func. Count:    105,   Neg. LLF: 1603.1026401369263
    Iteration:     10,   Func. Count:    114,   Neg. LLF: 1603.0419397065198
    Iteration:     11,   Func. Count:    123,   Neg. LLF: 1603.023758905803
    Iteration:     12,   Func. Count:    132,   Neg. LLF: 1603.0226167218484
    Iteration:     13,   Func. Count:    141,   Neg. LLF: 1603.022421728096
    Iteration:     14,   Func. Count:    150,   Neg. LLF: 1603.0224056522998
    Optimization terminated successfully.    (Exit mode 0)
                Current function value: 1603.0224049277822
                Iterations: 14
                Function evaluations: 151
                Gradient evaluations: 14


結果を表示する


```python
egarch.summary()
```




<table class="simpletable">
<caption>AR - EGARCH Model Results</caption>
<tr>
  <th>Dep. Variable:</th>          <td>per_us</td>          <th>  R-squared:         </th>  <td>   0.010</td> 
</tr>
<tr>
  <th>Mean Model:</th>               <td>AR</td>            <th>  Adj. R-squared:    </th>  <td>   0.009</td> 
</tr>
<tr>
  <th>Vol Model:</th>              <td>EGARCH</td>          <th>  Log-Likelihood:    </th> <td>  -1603.02</td>
</tr>
<tr>
  <th>Distribution:</th>  <td>Standardized Student's t</td> <th>  AIC:               </th> <td>   3220.04</td>
</tr>
<tr>
  <th>Method:</th>           <td>Maximum Likelihood</td>    <th>  BIC:               </th> <td>   3256.70</td>
</tr>
<tr>
  <th></th>                           <td></td>             <th>  No. Observations:  </th>    <td>1389</td>   
</tr>
<tr>
  <th>Date:</th>              <td>Sun, Feb 18 2018</td>     <th>  Df Residuals:      </th>    <td>1382</td>   
</tr>
<tr>
  <th>Time:</th>                  <td>23:55:21</td>         <th>  Df Model:          </th>      <td>7</td>    
</tr>
</table>
<table class="simpletable">
<caption>Mean Model</caption>
<tr>
      <td></td>         <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>     <th>95.0% Conf. Int.</th>   
</tr>
<tr>
  <th>Const</th>     <td>    0.0423</td> <td>1.896e-02</td> <td>    2.230</td> <td>2.574e-02</td> <td>[5.121e-03,7.943e-02]</td>
</tr>
<tr>
  <th>per_us[1]</th> <td>   -0.0670</td> <td>2.389e-02</td> <td>   -2.803</td> <td>5.071e-03</td> <td>[ -0.114,-2.013e-02]</td> 
</tr>
</table>
<table class="simpletable">
<caption>Volatility Model</caption>
<tr>
      <td></td>        <th>coef</th>      <th>std err</th>      <th>t</th>       <th>P>|t|</th>      <th>95.0% Conf. Int.</th>   
</tr>
<tr>
  <th>omega</th>    <td>-5.1881e-03</td> <td>3.896e-03</td> <td>   -1.331</td> <td>    0.183</td> <td>[-1.282e-02,2.449e-03]</td>
</tr>
<tr>
  <th>alpha[1]</th> <td>    0.0639</td>  <td>1.808e-02</td> <td>    3.535</td> <td>4.071e-04</td>  <td>[2.848e-02,9.935e-02]</td>
</tr>
<tr>
  <th>gamma[1]</th> <td>   -0.0937</td>  <td>2.074e-02</td> <td>   -4.517</td> <td>6.271e-06</td>  <td>[ -0.134,-5.303e-02]</td> 
</tr>
<tr>
  <th>beta[1]</th>  <td>    0.9892</td>  <td>6.402e-03</td> <td>  154.511</td>   <td>0.000</td>      <td>[  0.977,  1.002]</td>  
</tr>
</table>
<table class="simpletable">
<caption>Distribution</caption>
<tr>
   <td></td>     <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>   <th>95.0% Conf. Int.</th> 
</tr>
<tr>
  <th>nu</th> <td>    8.3047</td> <td>    2.295</td> <td>    3.619</td> <td>2.957e-04</td> <td>[  3.807, 12.802]</td>
</tr>
</table>
<caption>Covariance estimator: robust</caption>


レバレッジ効果を表すgammaが負なのでレバレッジ効果は有効だと考えられる

## (7)

残差について自己相関検定を行う


```python
acf_=acf(egarch.resid[1:])[1:]
q_m=acorr_ljungbox(egarch.resid[1:])[0]
p_value=acorr_ljungbox(egarch.resid[1:])[1]
lag=range(1,41)
test=pd.DataFrame({'lag':lag,'autocorr':acf_,'Q(m)':q_m,'P_value':p_value})
test[['lag','autocorr','Q(m)','P_value']].iloc[0:10,:].T
```




<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
      <th>5</th>
      <th>6</th>
      <th>7</th>
      <th>8</th>
      <th>9</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>lag</th>
      <td>1.000000</td>
      <td>2.000000</td>
      <td>3.000000</td>
      <td>4.000000</td>
      <td>5.000000</td>
      <td>6.000000</td>
      <td>7.000000</td>
      <td>8.000000</td>
      <td>9.000000</td>
      <td>10.000000</td>
    </tr>
    <tr>
      <th>autocorr</th>
      <td>-0.042277</td>
      <td>-0.019060</td>
      <td>0.013367</td>
      <td>-0.018896</td>
      <td>-0.022973</td>
      <td>-0.030761</td>
      <td>-0.062600</td>
      <td>-0.010608</td>
      <td>-0.006909</td>
      <td>0.033732</td>
    </tr>
    <tr>
      <th>Q(m)</th>
      <td>2.488034</td>
      <td>2.994096</td>
      <td>3.243181</td>
      <td>3.741285</td>
      <td>4.478069</td>
      <td>5.799967</td>
      <td>11.278587</td>
      <td>11.436028</td>
      <td>11.502862</td>
      <td>13.097075</td>
    </tr>
    <tr>
      <th>P_value</th>
      <td>0.114715</td>
      <td>0.223790</td>
      <td>0.355629</td>
      <td>0.442150</td>
      <td>0.482823</td>
      <td>0.445967</td>
      <td>0.126920</td>
      <td>0.178196</td>
      <td>0.242807</td>
      <td>0.218295</td>
    </tr>
  </tbody>
</table>
</div>



残差のコレログラムをプロットする


```python
fig,ax=plt.subplots(1,1,figsize=[8,5])
ax.bar(range(1,21),acf_[1:21])
ax.set_ylim(-0.1,0.1)
ax.set_xticks([1,5,10,15,20])
ax.set_yticks(np.asarray(range(-1,2))*0.1)
ax.hlines(0,0,21,linewidth=1)
ax.hlines([1.96/np.sqrt(egarch.resid.shape[0]-1),-1.96/np.sqrt(egarch.resid.shape[0]-1)],0,21,linestyles='dashed',linewidth=1)
ax.set_xlabel('lag')
ax.set_title('acf')
plt.show()
```


![output_48_0.png](https://qiita-image-store.s3.amazonaws.com/0/235450/052f0f02-79a6-db24-ea98-aefe4a346cd2.png)



かばん検定の結果とコレログラムから、残差に大きな自己相関がないことが示唆された

## (8)

他国の例として、日本について同様の分析を行ってみる

日本の株式収益率のコレログラムをプロットする


```python
fig,ax=plt.subplots(1,1,figsize=[8,5])
ax.bar(range(1,21),acf(msci['per_jp'][1:])[1:21])
ax.set_ylim(-0.2,0.2)
ax.set_xticks([1,5,10,15,20])
ax.set_yticks(np.asarray(range(-2,3))*0.1)
ax.hlines(0,0,21,linewidth=1)
ax.hlines([1.96/np.sqrt(msci.shape[0]-1),-1.96/np.sqrt(msci.shape[0]-1)],0,21,linestyles='dashed',linewidth=1)
ax.set_xlabel('lag')
ax.set_title('acf')
plt.show()
```


![output_53_0.png](https://qiita-image-store.s3.amazonaws.com/0/235450/0ea89675-bb63-6fef-b570-6dd35160cb83.png)



大きな相関はほぼみられない

日本の株式収益率の2乗の系列を作成する


```python
msci['per_sq_jp']=msci['per_jp']**2
```

日本の株式収益率の2乗のコレログラムをプロットする


```python
fig,ax=plt.subplots(1,1,figsize=[8,5])
ax.bar(range(1,21),acf(msci['per_sq_jp'][1:])[1:21])
ax.set_ylim(-0.1,0.3)
ax.set_xticks([1,5,10,15,20])
ax.set_yticks(np.asarray(range(-1,4))*0.1)
ax.hlines(0,0,21,linewidth=1)
ax.hlines([1.96/np.sqrt(msci.shape[0]-1),-1.96/np.sqrt(msci.shape[0]-1)],0,21,linestyles='dashed',linewidth=1)
ax.set_xlabel('lag')
ax.set_title('acf')
plt.show()
```


![output_58_0.png](https://qiita-image-store.s3.amazonaws.com/0/235450/9fe1f0c7-ce5a-644a-3f64-2acb8680fd69.png)



20期までのほぼ全てで大きな自己相関がみられる

日本の株式収益率のデータに対し、AR(1)-GARCH(1,1)モデルを推定する


```python
garch=arch_model(100*msci['per_jp'][1:],mean='AR',lags=1,vol='GARCH',p=1,o=0,q=1,dist='studentst').fit()
```

    Iteration:      1,   Func. Count:      8,   Neg. LLF: 2241.7021893723913
    Iteration:      2,   Func. Count:     21,   Neg. LLF: 2239.6009631225484
    Iteration:      3,   Func. Count:     32,   Neg. LLF: 2239.5669687725062
    Iteration:      4,   Func. Count:     42,   Neg. LLF: 2239.41500078318
    Iteration:      5,   Func. Count:     53,   Neg. LLF: 2239.372629045961
    Iteration:      6,   Func. Count:     62,   Neg. LLF: 2238.6334134962735
    Iteration:      7,   Func. Count:     71,   Neg. LLF: 2238.2828616416014
    Iteration:      8,   Func. Count:     80,   Neg. LLF: 2237.9761251171585
    Iteration:      9,   Func. Count:     88,   Neg. LLF: 2237.9050544390084
    Iteration:     10,   Func. Count:     96,   Neg. LLF: 2237.891675885119
    Iteration:     11,   Func. Count:    104,   Neg. LLF: 2237.891298489836
    Iteration:     12,   Func. Count:    112,   Neg. LLF: 2237.8911050580737
    Iteration:     13,   Func. Count:    120,   Neg. LLF: 2237.890817179561
    Iteration:     14,   Func. Count:    128,   Neg. LLF: 2237.8907235573656
    Iteration:     15,   Func. Count:    136,   Neg. LLF: 2237.890709586687
    Optimization terminated successfully.    (Exit mode 0)
                Current function value: 2237.890708986084
                Iterations: 15
                Function evaluations: 137
                Gradient evaluations: 15


結果を表示する


```python
garch.summary()
```




<table class="simpletable">
<caption>AR - GARCH Model Results</caption>
<tr>
  <th>Dep. Variable:</th>          <td>per_jp</td>          <th>  R-squared:         </th>  <td>   0.001</td> 
</tr>
<tr>
  <th>Mean Model:</th>               <td>AR</td>            <th>  Adj. R-squared:    </th>  <td>   0.000</td> 
</tr>
<tr>
  <th>Vol Model:</th>               <td>GARCH</td>          <th>  Log-Likelihood:    </th> <td>  -2237.89</td>
</tr>
<tr>
  <th>Distribution:</th>  <td>Standardized Student's t</td> <th>  AIC:               </th> <td>   4487.78</td>
</tr>
<tr>
  <th>Method:</th>           <td>Maximum Likelihood</td>    <th>  BIC:               </th> <td>   4519.20</td>
</tr>
<tr>
  <th></th>                           <td></td>             <th>  No. Observations:  </th>    <td>1389</td>   
</tr>
<tr>
  <th>Date:</th>              <td>Sun, Feb 18 2018</td>     <th>  Df Residuals:      </th>    <td>1383</td>   
</tr>
<tr>
  <th>Time:</th>                  <td>23:55:22</td>         <th>  Df Model:          </th>      <td>6</td>    
</tr>
</table>
<table class="simpletable">
<caption>Mean Model</caption>
<tr>
      <td></td>         <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>      <th>95.0% Conf. Int.</th>   
</tr>
<tr>
  <th>Const</th>     <td>    0.0633</td> <td>3.015e-02</td> <td>    2.101</td> <td>3.565e-02</td>   <td>[4.249e-03,  0.122]</td> 
</tr>
<tr>
  <th>per_jp[1]</th> <td>   -0.0271</td> <td>2.663e-02</td> <td>   -1.017</td> <td>    0.309</td> <td>[-7.930e-02,2.510e-02]</td>
</tr>
</table>
<table class="simpletable">
<caption>Volatility Model</caption>
<tr>
      <td></td>        <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>     <th>95.0% Conf. Int.</th>   
</tr>
<tr>
  <th>omega</th>    <td>    0.0442</td> <td>1.557e-02</td> <td>    2.836</td> <td>4.566e-03</td> <td>[1.364e-02,7.467e-02]</td>
</tr>
<tr>
  <th>alpha[1]</th> <td>    0.0699</td> <td>1.421e-02</td> <td>    4.922</td> <td>8.573e-07</td> <td>[4.209e-02,9.780e-02]</td>
</tr>
<tr>
  <th>beta[1]</th>  <td>    0.9034</td> <td>1.776e-02</td> <td>   50.873</td>   <td>0.000</td>     <td>[  0.869,  0.938]</td>  
</tr>
</table>
<table class="simpletable">
<caption>Distribution</caption>
<tr>
   <td></td>     <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>   <th>95.0% Conf. Int.</th> 
</tr>
<tr>
  <th>nu</th> <td>   11.0611</td> <td>    3.152</td> <td>    3.509</td> <td>4.492e-04</td> <td>[  4.884, 17.239]</td>
</tr>
</table>
<caption>Covariance estimator: robust</caption>


日本の株式収益率のデータに対し、AR(1)-GJR(1,1)モデルを推定する


```python
gjr=arch_model(100*msci['per_jp'][1:],mean='AR',lags=1,vol='GARCH',p=1,o=1,q=1,dist='studentst').fit()
```

    Iteration:      1,   Func. Count:      9,   Neg. LLF: 2233.229679269623
    Iteration:      2,   Func. Count:     22,   Neg. LLF: 2232.928611427178
    Iteration:      3,   Func. Count:     35,   Neg. LLF: 2232.841708242234
    Iteration:      4,   Func. Count:     46,   Neg. LLF: 2232.6285781043343
    Iteration:      5,   Func. Count:     57,   Neg. LLF: 2232.520062528829
    Iteration:      6,   Func. Count:     68,   Neg. LLF: 2231.847921969732
    Iteration:      7,   Func. Count:     78,   Neg. LLF: 2230.185568521398
    Iteration:      8,   Func. Count:     88,   Neg. LLF: 2230.168176996417
    Iteration:      9,   Func. Count:     98,   Neg. LLF: 2230.0392175172383
    Iteration:     10,   Func. Count:    107,   Neg. LLF: 2230.019475088293
    Iteration:     11,   Func. Count:    116,   Neg. LLF: 2229.9967310548327
    Iteration:     12,   Func. Count:    125,   Neg. LLF: 2229.979300462734
    Iteration:     13,   Func. Count:    134,   Neg. LLF: 2229.958753628938
    Iteration:     14,   Func. Count:    143,   Neg. LLF: 2229.952478080927
    Iteration:     15,   Func. Count:    152,   Neg. LLF: 2229.9518094040345
    Iteration:     16,   Func. Count:    161,   Neg. LLF: 2229.951780382489
    Iteration:     17,   Func. Count:    170,   Neg. LLF: 2229.9517777982396
    Optimization terminated successfully.    (Exit mode 0)
                Current function value: 2229.9517777982396
                Iterations: 17
                Function evaluations: 170
                Gradient evaluations: 17


結果を表示する


```python
gjr.summary()
```




<table class="simpletable">
<caption>AR - GJR-GARCH Model Results</caption>
<tr>
  <th>Dep. Variable:</th>          <td>per_jp</td>          <th>  R-squared:         </th>  <td>   0.001</td> 
</tr>
<tr>
  <th>Mean Model:</th>               <td>AR</td>            <th>  Adj. R-squared:    </th>  <td>   0.000</td> 
</tr>
<tr>
  <th>Vol Model:</th>             <td>GJR-GARCH</td>        <th>  Log-Likelihood:    </th> <td>  -2229.95</td>
</tr>
<tr>
  <th>Distribution:</th>  <td>Standardized Student's t</td> <th>  AIC:               </th> <td>   4473.90</td>
</tr>
<tr>
  <th>Method:</th>           <td>Maximum Likelihood</td>    <th>  BIC:               </th> <td>   4510.56</td>
</tr>
<tr>
  <th></th>                           <td></td>             <th>  No. Observations:  </th>    <td>1389</td>   
</tr>
<tr>
  <th>Date:</th>              <td>Sun, Feb 18 2018</td>     <th>  Df Residuals:      </th>    <td>1382</td>   
</tr>
<tr>
  <th>Time:</th>                  <td>23:55:22</td>         <th>  Df Model:          </th>      <td>7</td>    
</tr>
</table>
<table class="simpletable">
<caption>Mean Model</caption>
<tr>
      <td></td>         <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>      <th>95.0% Conf. Int.</th>   
</tr>
<tr>
  <th>Const</th>     <td>    0.0439</td> <td>3.056e-02</td> <td>    1.435</td> <td>    0.151</td>  <td>[-1.605e-02,  0.104]</td> 
</tr>
<tr>
  <th>per_jp[1]</th> <td>   -0.0156</td> <td>2.777e-02</td> <td>   -0.562</td> <td>    0.574</td> <td>[-7.004e-02,3.883e-02]</td>
</tr>
</table>
<table class="simpletable">
<caption>Volatility Model</caption>
<tr>
      <td></td>        <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>      <th>95.0% Conf. Int.</th>   
</tr>
<tr>
  <th>omega</th>    <td>    0.0659</td> <td>2.402e-02</td> <td>    2.744</td> <td>6.064e-03</td>   <td>[1.884e-02,  0.113]</td> 
</tr>
<tr>
  <th>alpha[1]</th> <td>    0.0176</td> <td>1.494e-02</td> <td>    1.179</td> <td>    0.239</td> <td>[-1.167e-02,4.688e-02]</td>
</tr>
<tr>
  <th>gamma[1]</th> <td>    0.1083</td> <td>4.011e-02</td> <td>    2.700</td> <td>6.944e-03</td>   <td>[2.966e-02,  0.187]</td> 
</tr>
<tr>
  <th>beta[1]</th>  <td>    0.8861</td> <td>2.325e-02</td> <td>   38.110</td>   <td>0.000</td>      <td>[  0.841,  0.932]</td>  
</tr>
</table>
<table class="simpletable">
<caption>Distribution</caption>
<tr>
   <td></td>     <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>   <th>95.0% Conf. Int.</th> 
</tr>
<tr>
  <th>nu</th> <td>   13.3231</td> <td>    4.560</td> <td>    2.922</td> <td>3.480e-03</td> <td>[  4.386, 22.260]</td>
</tr>
</table>
<caption>Covariance estimator: robust</caption>


レバレッジ効果を表すgammaが正なのでレバレッジ効果は有効だと考えられる

残差について自己相関検定を行う


```python
acf_=acf(gjr.resid[1:])[1:]
q_m=acorr_ljungbox(gjr.resid[1:])[0]
p_value=acorr_ljungbox(gjr.resid[1:])[1]
lag=range(1,41)
test=pd.DataFrame({'lag':lag,'autocorr':acf_,'Q(m)':q_m,'P_value':p_value})
test[['lag','autocorr','Q(m)','P_value']].iloc[0:10,:].T
```




<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
      <th>5</th>
      <th>6</th>
      <th>7</th>
      <th>8</th>
      <th>9</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>lag</th>
      <td>1.000000</td>
      <td>2.000000</td>
      <td>3.000000</td>
      <td>4.000000</td>
      <td>5.000000</td>
      <td>6.000000</td>
      <td>7.000000</td>
      <td>8.000000</td>
      <td>9.000000</td>
      <td>10.000000</td>
    </tr>
    <tr>
      <th>autocorr</th>
      <td>-0.020387</td>
      <td>-0.047532</td>
      <td>-0.021293</td>
      <td>0.011971</td>
      <td>-0.006298</td>
      <td>0.005952</td>
      <td>-0.015552</td>
      <td>-0.042520</td>
      <td>-0.071958</td>
      <td>0.033569</td>
    </tr>
    <tr>
      <th>Q(m)</th>
      <td>0.578543</td>
      <td>3.725755</td>
      <td>4.357805</td>
      <td>4.557714</td>
      <td>4.613085</td>
      <td>4.662582</td>
      <td>5.000718</td>
      <td>7.530104</td>
      <td>14.779644</td>
      <td>16.358463</td>
    </tr>
    <tr>
      <th>P_value</th>
      <td>0.446884</td>
      <td>0.155225</td>
      <td>0.225330</td>
      <td>0.335759</td>
      <td>0.464897</td>
      <td>0.587758</td>
      <td>0.659876</td>
      <td>0.480661</td>
      <td>0.097169</td>
      <td>0.089821</td>
    </tr>
  </tbody>
</table>
</div>



残差のコレログラムをプロットする


```python
fig,ax=plt.subplots(1,1,figsize=[8,5])
ax.bar(range(1,21),acf_[1:21])
ax.set_ylim(-0.1,0.1)
ax.set_xticks([1,5,10,15,20])
ax.set_yticks(np.asarray(range(-1,2))*0.1)
ax.hlines(0,0,21,linewidth=1)
ax.hlines([1.96/np.sqrt(gjr.resid.shape[0]-1),-1.96/np.sqrt(gjr.resid.shape[0]-1)],0,21,linestyles='dashed',linewidth=1)
ax.set_xlabel('lag')
ax.set_title('acf')
plt.show()
```


![output_72_0.png](https://qiita-image-store.s3.amazonaws.com/0/235450/8aa7441c-a58f-0385-3dd1-030cbc482c12.png)



かばん検定の結果とコレログラムから、残差に大きな自己相関がないことが示唆された

日本の株式収益率のデータに対し、AR(1)-EGARCH(1,1)モデルを推定する


```python
egarch=arch_model(100*msci['per_jp'][1:],mean='AR',lags=1,vol='EGARCH',p=1,o=1,q=1,dist='studentst').fit()
```

    Iteration:      1,   Func. Count:      9,   Neg. LLF: 2235.3989385014665
    Iteration:      2,   Func. Count:     22,   Neg. LLF: 2232.6544335552594
    Iteration:      3,   Func. Count:     35,   Neg. LLF: 2232.024333146882
    Iteration:      4,   Func. Count:     49,   Neg. LLF: 2231.816316045613
    Iteration:      5,   Func. Count:     61,   Neg. LLF: 2230.9901837255084
    Iteration:      6,   Func. Count:     73,   Neg. LLF: 2230.5593467621834
    Iteration:      7,   Func. Count:     85,   Neg. LLF: 2230.544616375196
    Iteration:      8,   Func. Count:     94,   Neg. LLF: 2230.4151623044745
    Iteration:      9,   Func. Count:    103,   Neg. LLF: 2230.3997454528976
    Iteration:     10,   Func. Count:    112,   Neg. LLF: 2230.3800568523693
    Iteration:     11,   Func. Count:    121,   Neg. LLF: 2230.3655047749085
    Iteration:     12,   Func. Count:    130,   Neg. LLF: 2230.351205705787
    Iteration:     13,   Func. Count:    139,   Neg. LLF: 2230.346520815785
    Iteration:     14,   Func. Count:    148,   Neg. LLF: 2230.3463070242783
    Iteration:     15,   Func. Count:    157,   Neg. LLF: 2230.346284628481
    Optimization terminated successfully.    (Exit mode 0)
                Current function value: 2230.346284628483
                Iterations: 15
                Function evaluations: 157
                Gradient evaluations: 15


結果を表示する


```python
egarch.summary()
```




<table class="simpletable">
<caption>AR - EGARCH Model Results</caption>
<tr>
  <th>Dep. Variable:</th>          <td>per_jp</td>          <th>  R-squared:         </th>  <td>   0.001</td> 
</tr>
<tr>
  <th>Mean Model:</th>               <td>AR</td>            <th>  Adj. R-squared:    </th>  <td>   0.000</td> 
</tr>
<tr>
  <th>Vol Model:</th>              <td>EGARCH</td>          <th>  Log-Likelihood:    </th> <td>  -2230.35</td>
</tr>
<tr>
  <th>Distribution:</th>  <td>Standardized Student's t</td> <th>  AIC:               </th> <td>   4474.69</td>
</tr>
<tr>
  <th>Method:</th>           <td>Maximum Likelihood</td>    <th>  BIC:               </th> <td>   4511.35</td>
</tr>
<tr>
  <th></th>                           <td></td>             <th>  No. Observations:  </th>    <td>1389</td>   
</tr>
<tr>
  <th>Date:</th>              <td>Sun, Feb 18 2018</td>     <th>  Df Residuals:      </th>    <td>1382</td>   
</tr>
<tr>
  <th>Time:</th>                  <td>23:55:22</td>         <th>  Df Model:          </th>      <td>7</td>    
</tr>
</table>
<table class="simpletable">
<caption>Mean Model</caption>
<tr>
      <td></td>         <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>      <th>95.0% Conf. Int.</th>   
</tr>
<tr>
  <th>Const</th>     <td>    0.0329</td> <td>3.050e-02</td> <td>    1.079</td> <td>    0.281</td> <td>[-2.688e-02,9.270e-02]</td>
</tr>
<tr>
  <th>per_jp[1]</th> <td>   -0.0179</td> <td>2.713e-02</td> <td>   -0.661</td> <td>    0.509</td> <td>[-7.112e-02,3.524e-02]</td>
</tr>
</table>
<table class="simpletable">
<caption>Volatility Model</caption>
<tr>
      <td></td>        <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>     <th>95.0% Conf. Int.</th>   
</tr>
<tr>
  <th>omega</th>    <td>    0.0182</td> <td>7.770e-03</td> <td>    2.336</td> <td>1.949e-02</td> <td>[2.922e-03,3.338e-02]</td>
</tr>
<tr>
  <th>alpha[1]</th> <td>    0.1567</td> <td>2.346e-02</td> <td>    6.679</td> <td>2.400e-11</td>   <td>[  0.111,  0.203]</td>  
</tr>
<tr>
  <th>gamma[1]</th> <td>   -0.0824</td> <td>2.694e-02</td> <td>   -3.059</td> <td>2.220e-03</td> <td>[ -0.135,-2.961e-02]</td> 
</tr>
<tr>
  <th>beta[1]</th>  <td>    0.9618</td> <td>1.418e-02</td> <td>   67.822</td>   <td>0.000</td>     <td>[  0.934,  0.990]</td>  
</tr>
</table>
<table class="simpletable">
<caption>Distribution</caption>
<tr>
   <td></td>     <th>coef</th>     <th>std err</th>      <th>t</th>       <th>P>|t|</th>   <th>95.0% Conf. Int.</th> 
</tr>
<tr>
  <th>nu</th> <td>   13.4900</td> <td>    4.671</td> <td>    2.888</td> <td>3.878e-03</td> <td>[  4.335, 22.645]</td>
</tr>
</table>
<caption>Covariance estimator: robust</caption>


レバレッジ効果を表すgammaが負なのでレバレッジ効果は有効だと考えられる

残差について自己相関検定を行う


```python
acf_=acf(egarch.resid[1:])[1:]
q_m=acorr_ljungbox(egarch.resid[1:])[0]
p_value=acorr_ljungbox(egarch.resid[1:])[1]
lag=range(1,41)
test=pd.DataFrame({'lag':lag,'autocorr':acf_,'Q(m)':q_m,'P_value':p_value})
test[['lag','autocorr','Q(m)','P_value']].iloc[0:10,:].T
```




<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
      <th>5</th>
      <th>6</th>
      <th>7</th>
      <th>8</th>
      <th>9</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>lag</th>
      <td>1.000000</td>
      <td>2.000000</td>
      <td>3.000000</td>
      <td>4.000000</td>
      <td>5.000000</td>
      <td>6.000000</td>
      <td>7.000000</td>
      <td>8.000000</td>
      <td>9.000000</td>
      <td>10.000000</td>
    </tr>
    <tr>
      <th>autocorr</th>
      <td>-0.018168</td>
      <td>-0.047669</td>
      <td>-0.021375</td>
      <td>0.011909</td>
      <td>-0.006255</td>
      <td>0.005903</td>
      <td>-0.015637</td>
      <td>-0.042730</td>
      <td>-0.071984</td>
      <td>0.033357</td>
    </tr>
    <tr>
      <th>Q(m)</th>
      <td>0.459477</td>
      <td>3.624803</td>
      <td>4.261702</td>
      <td>4.459549</td>
      <td>4.514170</td>
      <td>4.562842</td>
      <td>4.904705</td>
      <td>7.459204</td>
      <td>14.714026</td>
      <td>16.272994</td>
    </tr>
    <tr>
      <th>P_value</th>
      <td>0.497869</td>
      <td>0.163262</td>
      <td>0.234556</td>
      <td>0.347371</td>
      <td>0.477990</td>
      <td>0.600971</td>
      <td>0.671591</td>
      <td>0.487992</td>
      <td>0.099097</td>
      <td>0.092080</td>
    </tr>
  </tbody>
</table>
</div>



残差のコレログラムをプロットする


```python
fig,ax=plt.subplots(1,1,figsize=[8,5])
ax.bar(range(1,21),acf_[1:21])
ax.set_ylim(-0.1,0.1)
ax.set_xticks([1,5,10,15,20])
ax.set_yticks(np.asarray(range(-1,2))*0.1)
ax.hlines(0,0,21,linewidth=1)
ax.hlines([1.96/np.sqrt(egarch.resid.shape[0]-1),-1.96/np.sqrt(egarch.resid.shape[0]-1)],0,21,linestyles='dashed',linewidth=1)
ax.set_xlabel('lag')
ax.set_title('acf')
plt.show()
```


![output_82_0.png](https://qiita-image-store.s3.amazonaws.com/0/235450/369a08af-fbf6-c4c1-e40b-86bdde719dcf.png)



かばん検定の結果とコレログラムから、残差に大きな自己相関がないことが示唆された

# 7.5

DVEC,DBEKK,CCCの推定を行えるpythonのモジュールを見つけられなかったため、解答を断念しました
ご存知でしたらご教授いただけますと有り難いです...

# 参考サイト

ARCHのドキュメント
: http://arch.readthedocs.io/en/latest/index.html
arch_modelをフィッティングさせるときに最適化が収束しない時の対処法
:https://quant.stackexchange.com/questions/32350/fitting-garch1-1-in-python-for-moderately-large-data-sets