Build Status

library(knitr)
opts_chunk$set(comment=NA,
               collapse=TRUE)
library(derivmkts)

This is a collection of option pricing functions for a course in financial derivatives. The names of the functions mostly match those in my book Derivatives Markets, which explains the package name. This should be useful for students comfortable with R. In any case, I hope it will be helpful for instructors.

There are of course other option pricing packages in R, notably RQuantLib and fOptions. I don't add significant pricing functionality to those. This package does, however, have a few aspects that might be unique, which I describe below: calculation of greeks; display of binomial formula output and trees; and a quincunx function.

The package includes functions for computing

Calculation of Greeks

The function greeks() accepts an option pricing function call as an argument, and returns a vectorized set of greeks for any pricing function that uses the input names standard in the package (i.e., the asset price is s, the volatility is v, the interest rate is r, the dividend yield is d, and the time to maturity is tt).

As an example, the following calculation will produce the full complement of greeks for a call, for each of three strike prices. You can access the delta values, for example, using x['Delta', ].

x1 <- greeks(bscall(s=40, k=c(35, 40, 45), v=0.3, r=0.08, tt=0.25, d=0))
x1

There is a new complete option which produces one row containing all inputs and outputs for each case. The option long=TRUE produces the same output in long form.

x2 <- greeks(bscall(s=40, k=c(35, 40, 45), v=0.3, r=0.08, tt=0.25, d=0),
             complete=TRUE)
x2

x3 <- greeks(bscall(s=40, k=c(35, 40, 45), v=0.3, r=0.08, tt=0.25, d=0),
             long=TRUE)
x3

\normalsize

k <- 100; r <- 0.08; v <- 0.30; tt <- 2; d <- 0
S <- seq(.5, 250, by=.5)
y <- list(Call=greeks(bscall(S, k, v, r, tt, d)),
          Put=greeks(bsput(S, k, v, r, tt, d))
          )
par(mfrow=c(4, 4))  ## create a 4x4 plot
par(mar=c(2,2,2,2)) ## shrink some of the margins
for (i in names(y)) {
    for (j in rownames(y[[i]])) {  ## loop over greeks
        plot(S, y[[i]][j, ], main=paste(i, j), ylab=j, type='l')
    }
}

The following computes and plots all call and put Greeks for 500 options:

library(derivmkts)
library(ggplot2)
s <- 100; r <- 0.08; v <- 0.30; tt <- 2; d <- 0
k <- seq(.5, 250, by=.5)
yc <- greeks(bscall(s, k, v, r, tt, d), long=TRUE)
yp <- greeks(bsput(s, k, v, r, tt, d), long=TRUE)
ggplot(rbind(yc, yp), aes(x=k, y=value, color=funcname)) +
    geom_line() +
    labs(x='Strike price') +
    facet_wrap(~ greek, scales='free_y')

Simulating stock prices

The simprice function produces simulated price paths, with output either in wide or long form.

s <- simprice(s0 = 100, v = 0.3, r = 0.08, tt = 1, d = 0, trials = 5,
              periods = 365, jump = FALSE, long = TRUE, seed = 1)
ggplot(s, aes(x = period, y = price, color = trial)) +
    geom_line()

Binomial calculations

binomopt

By default the binomopt function returns the price of an American call. In adddition:

Here is an example illustrating everything that the binomopt function can return:

x <- binomopt(41, 40, .3, .08, 1, 0, 3, putopt=TRUE, american=TRUE,
              returntrees=TRUE)
x

binomplot

This function plots the binomial tree, providing a visual depiction of the nodes, the probability of reaching each node, and whether exercise occurs at that node.

binomplot(41, 40, .3, .08, 1, 0, 3, putopt=TRUE, american=TRUE)

Galton board or quincunx

The Galton board is a pegboard that illustrates the central limit theorem. Balls drop from the top and randomly fall right or left, providing a physical simulation of a binomial distribution. (My physicist brother-in-law tells me that real-life Galton boards don't typically generate a normal distribution because, among other things, balls acquire momentum in the direction of their original travel. The distribution is thus likely to be fatter-tailed than normal.)

You can see the Galton board in action with quincunx():

par(mar=c(2, 2, 2, 2))
quincunx(n=11, numballs=250, delay=0, probright=0.5)

Feedback

Please feel free to contact me with bug reports or suggestions. Best would be to file an issue on Github, but email is fine as well.



rmcd1024/derivmkts documentation built on June 11, 2022, 9:22 a.m.