Skip to content

Instantly share code, notes, and snippets.

View olliefr's full-sized avatar
🌩️

Oliver Frolovs olliefr

🌩️
View GitHub Profile
@olliefr
olliefr / genlattice.american.R
Created May 26, 2016 14:20
Generates a binomial lattice for American option.
# 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.
@olliefr
olliefr / dotlattice.R
Created May 26, 2016 14:19
Dotlattice function which can plot European and American trees.
# 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")
@olliefr
olliefr / genlattice.european.public.R
Created May 26, 2016 13:59
The public interface to genlattice.european.
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))
}
@olliefr
olliefr / genlattice.european.R
Created May 26, 2016 13:57
Generates a binomial lattice for European option.
# 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))
@olliefr
olliefr / payoff.R
Created May 26, 2016 13:51
A fistful of option payoff functions of different types.
payoff.vanilla.call <- function(Asset, Strike) {
return( max(0, Asset - Strike) )
}
payoff.vanilla.put <- function(Asset, Strike) {
return( max(0, Strike - Asset) )
}
@olliefr
olliefr / dotlattice.R
Created May 25, 2016 15:01
Generate a graph specification that can be fed into graphviz. A function by Rory Winston.
# 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")
@olliefr
olliefr / genlattice.R
Last active May 25, 2016 15:00
Generate a binomial lattice for a given up, down, start value and number of steps. A function by Rory Winston.
# 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) {
@olliefr
olliefr / binomial-sum.cc
Last active May 20, 2016 15:17
Brain teaser - a simulation approach (C++).
// c++ -std=c++11 -Wall binomial-sum.cc -o binomial-sum
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <random>
#include <vector>
using namespace std;
@olliefr
olliefr / binomial-sum-3.R
Last active May 20, 2016 13:19
Brain teaser - using the Central Limit Theorem.
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))
@olliefr
olliefr / binomial-sum-2.R
Last active May 20, 2016 11:33
Brain teaser - summing the PMFs approach.
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))