Tietokoneharjoitus 3

Tehtävä 5a

file<-"http://cc.oulu.fi/~jklemela/stafin/dax.csv"
data<-read.csv(file=file)
dax<-data[,7]
dax<-dax[length(dax):1]
plot(dax,type="l")
pituus<-length(dax)
tuotto<-log(dax[2:pituus])-log(dax[1:(pituus-1)])

# A
sigma<-sqrt(250)*sd(tuotto)
sigma

# [1] 0.2378647

# B 

ikkuna<-50
otos<-tuotto[(length(tuotto)-ikkuna):length(tuotto)]
sigma<-sqrt(250)*sd(otos)
sigma

# [1] 0.1843388

# C) GARCH(1,1)

library(tseries)
ga<-garch(tuotto)
ga$coef

#          a0           a1           b1 
# 1.703149e-06 7.575368e-02 9.167762e-01 

alpha0<-1.703149e-06
alpha1<-7.575368e-02
beta<-9.167762e-01 

sigmat<-matrix(0,length(tuotto),1)
sigmat[1]<-sd(tuotto)
for (t in 2:length(tuotto)){
   sigmat[t]<-sqrt(alpha0+alpha1*tuotto[t-1]^2+beta*sigmat[t-1]^2)
}
sigma<-
sqrt(alpha0+alpha1*tuotto[length(tuotto)]^2+beta*sigmat[length(sigmat)]^2)
sigma<-sqrt(250)*sigma
sigma

# [1] 0.2199405

Tehtävä 5c

bs<-function(S,K,sigma,r,dt,type="call")
{
# S stock price
# K strike
# sigma annualized volatility
# r interest rate (annual)
# dt time to expiration (fractions of year)

if (K==0) C<-S
else{
  zplus<-(log(S/K)+(r+sigma^2/2)*dt)/(sigma*sqrt(dt))
  zminus<-zplus-sigma*sqrt(dt)
  C<-S*pnorm(zplus)-K*exp(-r*dt)*pnorm(zminus)
}

if (type=="put") C<-C-S+K*exp(-r*dt)

return(C)
}

Tehtävä 5b

# http://www1.deutsche-boerse.com/parkett/parkett2.jpg

S<-9372
K<-9500
sigma<-0.2378647
sigma<-0.1843388
sigma<-0.2199405
r<-0.01
dt<-(19+19)/365
bs(S,K,sigma,r,dt)

# [1] 233.4709
# [1] 169.636
# [1] 212.0372


# http://www.eurexchange.com/exchange-en/products/idx/dax/17252/

# 235