[R] 3D Black-Scholes Graph Help!

Anna Dunietz anna.dunietz at gmail.com
Tue Mar 13 21:24:23 CET 2012


Hello all!

I would like to create a 3d plot, with the option price explained by  
the underlying price and time.  Unfortunately, I can't quite get it to  
work.  I would very much appreciate your help!

Thanks,
Anna

# Black-Scholes Option Graph
library(lattice)


blackscholes <- function(s, k, r=.1, t=5, sigma=.9,call=TRUE) {
     #calculate call/put option
     d1 <- (log(s/k)+(r+sigma^2/2)*t)/(sigma*sqrt(t))
     d2 <- d1 - sigma * sqrt(t)
     ifelse(call==TRUE,s*pnorm(d1) - k*exp(-r*t)*pnorm(d2),k*exp(-r*t)  
* pnorm(-d2) - s*pnorm(-d1))
     }

plotbs <- function(price){
     #create
     s<-seq(0,price,len=price/2)
     k<-s

     t<-0:5
     sigma<-seq(0,0.9,by=.1)

     #expand information
     OptionPrice<-matrix(nrow=length(s),ncol=length(k))
     for(i in 1:length(s)) OptionPrice[i,]<-mapply(blackscholes,s[i],k)
     grid <- expand.grid(list(Time=t, UnderlyingPrice=s))

     #plot
     wireframe(OptionPrice~Time*UnderlyingPrice,data=grid,main="3D  
Option",
               drape=T,col.regions=heat.colors(100),scales =  
list(arrows=FALSE),)
     }



More information about the R-help mailing list