Tietokoneharjoitus 5
Tehtävä 5
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)
}
bs.binomial<-function(S,K,sigma,r,dt,n,type="call")
{
# S stock price
# K strike
# sigma annualized volatility
# r interest rate (annual)
# dt time to expiration (fractions of year)
q<-1/2+r*sqrt(dt/n)/(2*sigma)
u<-1+sigma*sqrt(dt/n)
C<-0
for (j in 0:n){
snj<-u^j*(2-u)^(n-j)*S
if (type=="call") Hn<-max(snj-K,0) else Hn<-max(K-snj,0)
C<-C+factorial(n)/(factorial(n-j)*factorial(j))*q^j*(1-q)^(n-j)*Hn
}
C<-(1+r*dt/n)^(-n)*C
return(C)
}
bs.recursive<-function(S,K,sigma,r,dt,n)
{
# S = stock price
# K = strike price
# sigma = annualized volatility
# r = interest rate (annual)
# dt = time to expiration (fractions of year)
# n = the number of steps
u<-1+sigma*sqrt(dt/n)
q<-1/2+r*sqrt(dt/n)/(2*sigma)
stock.prices<-matrix(0,n+1,1)
for (i in 0:n) stock.prices[i+1]<-u^i*(2-u)^(n-i)*S
prev<-pmax(K-stock.prices,0)
for (k in (n-1):0){
deriv.prices<-matrix(0,k+1,1)
for (i in 1:(k+1))
deriv.prices[i]<-(1+r*dt/n)^(-1)*((1-q)*prev[i]+q*prev[i+1])
prev<-deriv.prices
}
return(prev[1])
}
bs.recursive.amerput<-function(S,K,sigma,r,dt,n)
{
# S stock price
# K strike
# sigma annualized volatility in percents
# r interest rate (annual)
# dt time to expiration (fractions of year)
# n is the number of steps
u<-1+sigma*sqrt(dt/n)
q<-1/2+r*sqrt(dt/n)/(2*sigma)
stock.prices<-matrix(0,n+1,1)
for (i in 0:n) stock.prices[i+1]<-u^i*(2-u)^(n-i)*S
prev<-pmax(K-stock.prices,0)
for (k in (n-1):0){
deriv.prices<-matrix(0,k+1,1)
stock.prices<-matrix(0,k+1,1)
for (i in 0:k) stock.prices[i+1]<-u^i*(2-u)^(k-i)*S
for (i in 1:(k+1)) deriv.prices[i]<-
max(K-stock.prices[i],(1+r*dt/n)^(-1)*((1-q)*prev[i]+q*prev[i+1]))
prev<-deriv.prices
}
return(prev[1])
}
S<-105
K<-100
sigma<-0.2
r<-0.01
dt<-0.25
n<-10
bs(S,K,sigma,r,dt,type="put")
bs.binomial(S,K,sigma,r,dt,n,type="put")
bs.recursive(S,K,sigma,r,dt,n)
bs.recursive.amerput(S,K,sigma,r,dt,n)
[1] 1.982567
[1] 2.049282
[1] 2.049282
[1] 2.060766
##################
r<-0.01
sgrid<-seq(99,101,0.2)
pgrid<-matrix(0,length(sgrid),1)
pgrid.am<-matrix(0,length(sgrid),1)
for (i in 1:length(sgrid)){
pgrid[i]<-bs.recursive(sgrid[i],K,sigma,r,dt,n)
pgrid.am[i]<-bs.recursive.amerput(sgrid[i],K,sigma,r,dt,n)
}
matplot(sgrid,pgrid,type="l")
matplot(sgrid,pgrid.am,type="l",add=TRUE,col="red")
#####################
r<-0
sgrid<-seq(99,101,1)
pgrid<-matrix(0,length(sgrid),1)
pgrid.am<-matrix(0,length(sgrid),1)
for (i in 1:length(sgrid)){
pgrid[i]<-bs.recursive(sgrid[i],K,sigma,r,dt,n)
pgrid.am[i]<-bs.recursive.amerput(sgrid[i],K,sigma,r,dt,n)
}
matplot(sgrid,pgrid,type="l")
matplot(sgrid,pgrid.am,type="l",add=TRUE,col="red")
pgrid
pgrid.am
Tehtävä 6
http://de.finance.yahoo.com/q?s=NOA3.DE
http://www.eurexchange.com/action/exchange-en/155392-35362/35364/quotesSingleViewOption.do?callPut=Put&maturityDate=201212
nokia optiot
file<-"http://cc.oulu.fi/~jklemela/stafin/nokia.csv"
data<-read.csv(file=file)
nokia<-data[,7]
nokia<-nokia[length(nokia):1]
plot(nokia,type="l")
nokia<-nokia[1000:length(nokia)]
pituus<-length(nokia)
tuotto<-log(nokia[2:pituus])-log(nokia[1:(pituus-1)])
ikkuna<-50
otos<-tuotto[(length(tuotto)-ikkuna):length(tuotto)]
sigma<-sqrt(250)*sd(otos)
sigma
[1] 0.91
S<-2.13
K<-2.00
sigma<-0.91
r<-0.01
dt<-(21/360+11/12)-(1/360+9/12)
n<-40
bs.recursive.amerput(S,K,sigma,r,dt,n)
# [1] 0.2885211
bs(S,K,sigma,r,dt,type="put")
# [1] 0.2870861
Tehtävä 7
# (a) implikoitu volatiliteetti
http://www.eurexchange.com/exchange-en/products/idx/dax/17252/
http://www1.deutsche-boerse.com/parkett/parkett2.jpg
S<-7286
K<-7400
sigma<-0.15
r<-0.01
dt<-(21/360+11/12)-(9/360+9/12)
bs(S,K,sigma,r,dt)
# [1] 150.6448
# 162
#################################
bs.implied<-function(P,S,K,r,t,type="call",vlow=0.01,vupp=0.9,tol=0.01)
{
# P is the price of the option
# S stock price
# K strike
# r interest rate (annual)
# t time to expiration (fractions of year)
# returns implied volatility in percents
vcurlow<-vlow
vcurupp<-vupp
vcur<-(vcurlow+vcurupp)/2
diff<-vupp-vlow
while (diff>=tol){
cur<-bs(S,K,vcur,r,t,type=type)
if (cur>=P) vcurupp<-vcur else vcurlow<-vcur
vcur<-(vcurlow+vcurupp)/2
diff<-vcurupp-vcurlow
}
return(vcur)
}
http://www.eurexchange.com/exchange-en/products/idx/dax/17252/
http://www1.deutsche-boerse.com/parkett/parkett2.jpg
P<-162
S<-7286
K<-7400
r<-0.01
t<-(21/360+11/12)-(9/360+9/12)
bs.implied(P,S,K,r,t)
# [1] 0.1594922
# (b) historiallinen volatiliteetti
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)])
sigma<-sqrt(250)*sd(tuotto)
sigma
# [1] 0.2327807
ikkuna<-50
otos<-tuotto[(length(tuotto)-ikkuna):length(tuotto)]
sigma<-sqrt(250)*sd(otos)
sigma
[1] 0.2016796