Created
May 26, 2016 13:57
-
-
Save olliefr/177e8b41c5829cce4a4b0d4d219bf99d to your computer and use it in GitHub Desktop.
Generates a binomial lattice for European option.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate a binomial lattice for European option. | |
genlattice.european <- function(Asset, Volatility, IntRate, DividendRate, Strike, Expiry, NoSteps, Payoff, Type) { | |
# The number of tree nodes to process. | |
count <- sum(1 : (NoSteps+1)) | |
# This data frame will store asset and option prices. | |
# The mapping from tree node (i,j) to linear index | |
# inside the data frame will have to be computed. | |
X <- data.frame(matrix(NA, nrow=count, ncol=2)) | |
names(X) <- c("asset", "option") | |
# Time between price movements. | |
dt = Expiry / NoSteps | |
# Option price discount factor. | |
DiscountFactor <- exp(-IntRate * dt) | |
# The up and down jump factors and | |
# corresponding (synthetic) probabilities using | |
# Cox, Ross, and Rubinstein (1979) method. | |
u = exp(Volatility * sqrt(dt)) | |
d = 1/u | |
a = exp((IntRate - DividendRate) * dt) | |
p = (a - d) / (u - d) | |
# Compute the asset and option prices, starting | |
# from the last node of the tree, which is | |
# its bottom right corner when viewed as a graph. | |
# Work up and backwards. Backwards, comrades! | |
for (i in NoSteps:0) { | |
for (j in i:0) { | |
X$asset[count] <- Asset * u^(i-j) * d^j | |
# Compute the payoff directly for the last step's nodes, | |
# otherwise use a formula. | |
if (i == NoSteps) { | |
X$option[count] <- Payoff(X$asset[count], Strike) | |
} else { | |
up <- X$option[sum(1:(i+1), j, 1)] | |
down <- X$option[sum(1:(i+1), j+1, 1)] | |
X$option[count] <- DiscountFactor * (p * up + (1-p) * down) | |
} | |
count <- count - 1 | |
} | |
} | |
return(X) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment