2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-02-17

自分の勉強用に、沖本竜義「経済・ファイナンスデータの計量経済分析」の章末問題をpythonで解いてみました。
jupyter notebook上で記述したものをほぼそのまま載せてあります。
ソースコードの細かい説明は省いてありますが、今後余裕があれば説明を加えようと思います。

こちらの記事は第5章の章末問題を解いたものになります。
第1章第2章第4章第6章第7章も公開しています。

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

%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)

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

eco = pd.read_csv('./input/economicdata.csv')
print(eco.shape)
eco.head()
(364, 7)
date topix exrate indprod cpi saunemp intrate
0 Jan-75 276.09 29.13 47.33 52.625 1.7 12.67
1 Feb-75 299.81 29.70 46.86 52.723 1.8 13.00
2 Mar-75 313.50 29.98 46.24 53.114 1.8 12.92
3 Apr-75 320.57 29.80 47.33 54.092 1.8 12.02
4 May-75 329.65 29.79 47.33 54.385 1.8 11.06

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

eco.index=pd.to_datetime(eco.date.values,format='%b-%y')
eco.drop('date',axis=1,inplace=True)
eco.head()
topix exrate indprod cpi saunemp intrate
1975-01-01 276.09 29.13 47.33 52.625 1.7 12.67
1975-02-01 299.81 29.70 46.86 52.723 1.8 13.00
1975-03-01 313.50 29.98 46.24 53.114 1.8 12.92
1975-04-01 320.57 29.80 47.33 54.092 1.8 12.02
1975-05-01 329.65 29.79 47.33 54.385 1.8 11.06

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

logs=['topix','exrate','indprod','cpi'] 
for i in range(len(logs)):
    eco['%s_log'%logs[i]]=np.log(eco[logs[i]])
eco.head()
topix exrate indprod cpi saunemp intrate topix_log exrate_log indprod_log cpi_log
1975-01-01 276.09 29.13 47.33 52.625 1.7 12.67 5.620727 3.371769 3.857144 3.963191
1975-02-01 299.81 29.70 46.86 52.723 1.8 13.00 5.703149 3.391147 3.847164 3.965052
1975-03-01 313.50 29.98 46.24 53.114 1.8 12.92 5.747799 3.400530 3.833845 3.972441
1975-04-01 320.57 29.80 47.33 54.092 1.8 12.02 5.770101 3.394508 3.857144 3.990686
1975-05-01 329.65 29.79 47.33 54.385 1.8 11.06 5.798031 3.394173 3.857144 3.996088

プロットする

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

5.2の解答に従い、exrate_log,saunemp,intrateは場合2、topix_log,indeprod_log,cpi_logは場合3を仮定する

  • 場合1...トレンドがなく、期待値が0
  • 場合2...トレンドがなく、期待値が0ではない
  • 場合3...トレンドがある
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検定の結果を表示する

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検定の結果を表示する

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以外の系列の差分系列を作成する

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

プロットする

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

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

  • 場合1...トレンドがなく、期待値が0
  • 場合2...トレンドがなく、期待値が0ではない
  • 場合3...トレンドがある
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検定の結果を表示する

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検定の結果を表示する

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

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?