Re: [Quantlib-users] Binomial Engines - Greeks

Posted by Krishna Kumar-2 on
URL: http://quantlib.414.s1.nabble.com/Re-Quantlib-users-Binomial-Engines-Greeks-tp11199p11200.html

Here is an R implementation of the  Pelseer/Vorst method for computing
greeks on the tree as in the paper
"The Binomial Model and the Greeks." /Journal of Derivatives/, Spring
1994, 45-49


Best,
Kris


greek.binomial<- function(imod=1, TypeFlag="ce", S=100, X=110, r=0.05, q=0, tyr=1, sigma=0.3, nstep=100)
{
# greek.binomial(imod=1,TypeFlag= "pa", S = 50, X = 50, tyr=5/12, r = 0.1, q = 0, sigma = 0.4, nstep = 50)
# the extended CRR tree with node extensions!.
   if (imod == 2)   nstep <- nstep + nstep%%2
    mstep<-nstep+2
    TypeFlag = TypeFlag[1]
   
    z = NA
   
    if (TypeFlag == "ce" || TypeFlag == "ca")
        z <- +1
    if (TypeFlag == "pe" || TypeFlag == "pa")
        z <- -1
    if (is.na(z)) stop("TypeFlag misspecified: ce|ca|pe|pa")
   dtval = tyr/nstep
   udpvec <- BinTreeParams(imod, S, X, r, q, tyr, sigma, nstep)
   u <- udpvec[1]
   d <- udpvec[2]
   p <- udpvec[3]
   Df = exp(-r * dtval)
   OptionValue = z * (S * u^(0:mstep) * d^(mstep:0) - X)
   OptionValue = (abs(OptionValue) + OptionValue)/2
    if (TypeFlag == "ce" || TypeFlag == "pe") {
        for (j in seq(from = mstep - 1, to = 2, by = -1))
                for (i in 0:j)
                {
        { OptionValue[i + 1] = (p * OptionValue[i + 2] + (1 - p) * OptionValue[i +
            1]) * Df }
                        }
    }
    if (TypeFlag == "ca" || TypeFlag == "pa") {
        for (j in seq(from = mstep - 1, to = 2, by = -1)) for (i in 0:j) OptionValue[i +
            1] = max((z * (S * u^i * d^(abs(i - j)) - X)), (p *
            OptionValue[i + 2] + (1 - p) * OptionValue[i + 1]) *
            Df)
    }
#cat(OptionValue[1]," ", OptionValue[3]," ",OptionValue[2],"\n")
    greek.binomial<-(OptionValue[1]-OptionValue[3])/(S*u^2-S*d^2)


}