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 American option. | |
genlattice.american <- function(Asset, Volatility, IntRate, DividendRate, Strike, Expiry, NoSteps, Payoff) { | |
# 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. | |
# The early exercise flag is also stored. |
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
# Generates a graph specification that can be fed into graphviz. | |
# Input: the binomial lattice produced by one of genlattice family functions. | |
dotlattice <- function(S, digits=2) { | |
shape <- "plaintext" | |
cat("digraph G {", "\n", sep="") | |
cat("node[shape=",shape,"];","\n", sep="") | |
cat("rankdir=LR;","\n") | |
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
genlattice.vanilla.european.call <- function(Asset, Volatility, IntRate, DividendRate, Strike, Expiry, NoSteps) { | |
return (genlattice.european(Asset=Asset, Volatility=Volatility, IntRate=IntRate, DividendRate=DividendRate, Strike=Strike, Expiry=Expiry, NoSteps=NoSteps, Payoff=payoff.vanilla.call)) | |
} | |
genlattice.vanilla.european.put <- function(Asset, Volatility, IntRate, DividendRate, Strike, Expiry, NoSteps) { | |
return (genlattice.european(Asset=Asset, Volatility=Volatility, IntRate=IntRate, DividendRate=DividendRate, Strike=Strike, Expiry=Expiry, NoSteps=NoSteps, Payoff=payoff.vanilla.put)) | |
} |
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)) |
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
payoff.vanilla.call <- function(Asset, Strike) { | |
return( max(0, Asset - Strike) ) | |
} | |
payoff.vanilla.put <- function(Asset, Strike) { | |
return( max(0, Strike - Asset) ) | |
} |
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 graph specification that can be fed into graphviz | |
# This function was borrowed Rory Winston: | |
# http://www.theresearchkitchen.com/archives/738 | |
dotlattice <- function(S, labels=FALSE) { | |
shape <- ifelse(labels == TRUE, "plaintext", "point") | |
cat("digraph G {", "\n", sep="") | |
cat("node[shape=",shape,", samehead, sametail];","\n", sep="") | |
cat("rankdir=LR;","\n") |
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 a given up, down, start value and number of steps | |
# This function was borrowed Rory Winston: | |
# http://www.theresearchkitchen.com/archives/738 | |
genlattice <- function(X0=100, u=1.1, d=.75, N=5) { | |
X <- c() | |
X[1] <- X0 | |
count <- 2 | |
for (i in 1:N) { |
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
// c++ -std=c++11 -Wall binomial-sum.cc -o binomial-sum | |
#include <cstdlib> | |
#include <iostream> | |
#include <numeric> | |
#include <random> | |
#include <vector> | |
using namespace std; |
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
m <- 400 * 0.5 #mean | |
v <- 400 * 0.25 #variance | |
s <- sqrt(v) #stdev | |
p <- 1 - pnorm(219, m, s) | |
print(round(p, digits=2)) |
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
n <- 400 # number of tosses in a single trial | |
m <- 220 # threshold of interest | |
s <- 0 # the sum of binomial coefficients | |
for (k in m:n) { | |
s = s + choose(n,k) | |
} | |
p = s / 2^n | |
print(round(p, digits=2)) |