# Tietokoneharjoitus 4

## Tehtävä 5

```
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+choose(n,j)*q^j*(1-q)^(n-j)*Hn
}
C<-(1+r*dt/n)^(-n)*C

return(C)
}

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

moneyness<-seq(0.9,0.95,1)
S<-100
sigma<-0.1
r<-0
dt<-1/12

neet<-seq(100,1000,10)
ratios<-matrix(0,length(neet),length(moneyness))
for (i in 1:length(neet)){
for (j in 1:length(moneyness)){
n<-neet[i]
K<-S/moneyness[j]
a<-bs.binomial(S,K,sigma,r,dt,n,type="call")
b<-bs(S,K,sigma,r,dt,type="call")
ratios[i,j]<-a/b
}
}
matplot(neet,ratios,type="l")
text(900,0.974,"0.9")
text(850,0.994,"0.95")
text(800,1,"1")

```