Tietokoneharjoitus 7

Tehtävä 5

Sovita GARCH(1,1) malli SP500 tuottoihin, piirra hat(sigma)_t ja residuaalit.

file<-"http://cc.oulu.fi/~jklemela/timeseries/sp500.csv"
data<-read.csv(file=file)
sp500<-data[,7]                # otetaan kunkin paivan lopetuskurssi
sp500<-sp500[length(sp500):1]  # aloitetaan aikasarja vanhimmasta havainnosta

# piiretaan aikasarja
plot(sp500,type="l")

# tulostetaan tuotot
y<-sp500[2:length(sp500)]-sp500[1:(length(sp500)-1)]
r<-y/sp500[1:(length(sp500)-1)]
plot(r,type="l")

# Estimointi GARCH(1,1) #######################################

library(tseries)

x<-100*r
order<-c(1,1)
ga<-garch(x,order=order)
summary(ga)

Call:
garch(x = x, order = order)

Model:
GARCH(1,1)

Residuals:
      Min        1Q    Median        3Q       Max 
-10.65981  -0.52453   0.06144   0.64030   6.44386 

Coefficient(s):
    Estimate  Std. Error  t value Pr(>|t|)    
a0 0.0074141   0.0006603    11.23   <2e-16 ***
a1 0.0775141   0.0016973    45.67   <2e-16 ***
b1 0.9164638   0.0022025   416.10   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Diagnostic Tests:
	Jarque Bera Test

data:  Residuals
X-squared = 9478.852, df = 2, p-value < 2.2e-16


	Box-Ljung test

data:  Squared.Residuals
X-squared = 7.7792, df = 1, p-value = 0.005285

# Estimointi GARCH(1,3) ###################################


# 'order[2]' corresponds to the ARCH part and 'order[1]’ to the GARCH part
# GARCH(p,q): p on ARCH-part ja q on GARCH-part

x<-100*r
order<-c(3,1)
ga<-garch(x,order=order)
summary(ga)

Call:
garch(x = x, order = order)

Model:
GARCH(3,1)

Residuals:
      Min        1Q    Median        3Q       Max 
-10.60827  -0.52684   0.06149   0.63883   6.52770 

Coefficient(s):
    Estimate  Std. Error  t value Pr(>|t|)    
a0 9.410e-03   9.158e-04   10.275  < 2e-16 ***
a1 1.055e-01   3.925e-03   26.881  < 2e-16 ***
b1 6.520e-01   5.869e-02   11.110  < 2e-16 ***
b2 4.893e-13   8.861e-02    0.000        1    
b3 2.350e-01   5.634e-02    4.171 3.03e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Diagnostic Tests:
	Jarque Bera Test

data:  Residuals
X-squared = 8724.943, df = 2, p-value < 2.2e-16


	Box-Ljung test

data:  Squared.Residuals
X-squared = 1.0553, df = 1, p-value = 0.3043


# Fan & Yao, 1974-1999 s. 174: #################################
# a0  0.015
# a1  0.112
# b1  0.492
# b2 -0.034
# b3  0.420


# hat(sigma)_t ######################################

a0<-0.0074141   
a1<-0.0775141   
b1<-0.9164638 

sig<-sd(x)                  # [1] 0.9729996
sig<-sqrt(a0/(1-a1-b1))     # [1] 1.109571
T<-length(x)

hatsigma2<-matrix(sig^2,T,1)
for (i in 2:T) hatsigma2[i]<-a0+a1*x[i-1]^2+b1*hatsigma2[i-1]

plot(sqrt(hatsigma2),type="l")

# residuaalit #######################################

hateps<-matrix(0,T,1)
for (i in 1:T) hateps[i]<-x[i]/sqrt(hatsigma2[i])
plot(hateps,type="l")




# Akaiken informaatiokriteeri ##########################

AIC(ga)

# [1] 38653.25

aics<-matrix(0,4,1)
for (i in 1:4){
    order<-c(1,i)
    ga<-garch(r,order=order)
    aics[i]<-AIC(ga)
}
plot(aics)
aics
         [,1]
[1,] -106356.3
[2,] -106292.0
[3,] -106136.5
[4,] -105892.5


##########################

library(fSeries)


#######################################################
# kokeillaan aikaa 1972-2000, vrt. s.171, GARCH(1,3)

time<-data[,1]
time<-time[length(time):1] 
time[1]
time[length(time)]

# [1] 1950-01-03
# [1] 2013-10-11

t0<-5500
t1<-12550
time[t0]
time[t1]
x<-100*r[t0:t1]

order<-c(3,1)
ga<-garch(x,order=order)

summary(ga)

Call:
garch(x = x, order = order)

Model:
GARCH(3,1)

Residuals:
      Min        1Q    Median        3Q       Max 
-10.11524  -0.53561   0.04789   0.64116   5.50138 

Coefficient(s):
    Estimate  Std. Error  t value Pr(>|t|)    
a0 1.221e-02   1.879e-03    6.497 8.18e-11 ***
a1 9.651e-02   3.730e-03   25.878  < 2e-16 ***
b1 4.718e-01   7.539e-02    6.258 3.90e-10 ***
b2 5.700e-14   9.047e-02    0.000        1    
b3 4.205e-01   6.333e-02    6.640 3.14e-11 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Diagnostic Tests:
	Jarque Bera Test

data:  Residuals
X-squared = 4537.4, df = 2, p-value < 2.2e-16


	Box-Ljung test

data:  Squared.Residuals
X-squared = 1.8779, df = 1, p-value = 0.1706


##### hat(sigma)_t


a0<-1.221e-02     # 0.015
a1<-9.651e-02     # 0.112
b1<-4.718e-01     # 0.492
b2<-3.094e-14     #-0.034
b3<-4.205e-01     # 0.420

sig<-sd(x)
T<-length(x)

hatsig2<-matrix(sig^2,T,1)
for (i in 4:T) hatsig2[i]<-a0+a1*x[i-1]^2+b1*hatsig2[i-1]+b2*hatsig2[i-2]+b3*hatsig2[i-3]

plot(sqrt(hatsig2),type="l")

#### residuaalit

hateps<-matrix(0,T,1)
for (i in 1:T) hateps[i]<-x[i]/sqrt(hatsigma2[i])
plot(hateps,type="l")

plot(hateps,type="l")