[R] 3D Black-Scholes Graph Help!
David Winsemius
dwinsemius at comcast.net
Tue Mar 13 21:40:44 CET 2012
On Mar 13, 2012, at 4:24 PM, Anna Dunietz wrote:
> 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!
>
The usual problem with lattice calls that you "can't quite get ... to
work" is failure to read the R-FAQ ( or the help(lattice) page) where
it is explained that you need to print() the function result. At the
moment my R-machine is tied up with a long process but try wrapping
print() around that wireframe call.
--
David.
> 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),)
> }
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list