knitr::opts_chunk$set(collapse=TRUE,                # hadley
                      comment = "#>",               # hadley
                      error=TRUE, purl=FALSE,       # to be able to see errors
                      fig.width=7.25, fig.height=6) # nice-sized pictures
library(ustreasuries)
# Hull 7th edition Ch 13 P 294
Stock     <- 42    # S_0
Exercise  <- 40    # K
Time      <- 0.50  # T
Interest  <- 0.10  # r
Yield     <- 0     # q
sigma     <- 0.20  

ans_c <- EuroCall(Stock, Exercise, Time, Interest, Yield, sigma)
ans_p <- EuroPut(Stock, Exercise, Time, Interest, Yield, sigma)

writeLines(paste0("c = ", round(ans_c,2), ", p = ", round(ans_p,2)))
# Hull, 7th edition ch 13 p296
Stock      <- 21     # S_0
Exercise   <- 20     # K
Time       <- 0.25   # T
Interest   <- 0.10   # r
Yield      <- 0      # q
Call_price <- 1.875
Put_price <- 1.875

callvol <- EuroCallVol(Stock, Exercise, Time, Interest, Yield, Call_price)
putvol  <- EuroPutVol(Stock, Exercise, Time, Interest, Yield, Put_price)
writeLines(paste0("Implied Call Volatility: ", round(callvol*100, 1), "% per annum\n",
                  "Implied Put Volatility:  ", round(putvol*100, 1), "% per annum"))

Greeks

Relationship between Delta, Theta and Gamma

$\Theta + rS\Delta + \frac{1}{2} \sigma^2 S^2 \Gamma = r \Pi$

# Hull, 7th edition Ch 17 p 363
library(ustreasuries)
Stock    <- 49
Exercise <- 50
Time     <- 20/52
Interest <- 0.05
Yield    <- 0
sigma    <- 0.20
ans <- DeltaCall(Stock, Exercise, Time, Interest, Yield, sigma)
writeLines(paste0("Delta call: when the asset price changes by Delta_S,\n",
                  "                the option price changes by Delta_S*",round(ans, 3)))
# Hull, 7th edition Ch 17 p 362,3
Stock    <- 49     # S_0
Exercise <- 50     # K
Time     <- 20/52  # T
Interest <- 0.05   # r
Yield    <- 0      # q
sigma    <- 0.20

dcall <- DeltaCall(Stock, Exercise, Time, Interest, Yield, sigma)
dput  <- DeltaPut(Stock, Exercise, Time, Interest, Yield, sigma)

writeLines(paste0("Delta put: when the asset price changes by Delta_S,\n",
               "               the option price changes by Delta_S*",round(dput, 3),
               "\nDelta put = Delta call - 1? ", dput == dcall-1))
x_axis      <- c()
y_axis_call <- c()
y_axis_put  <- c()
for (Stock in seq(0, 100)) {
    x_axis <- c(x_axis, Stock)
    y_axis_call <- c(y_axis_call, DeltaCall(Stock, Exercise, Time, Interest, Yield, sigma))
    y_axis_put  <- c(y_axis_put, DeltaPut(Stock, Exercise, Time, Interest, Yield, sigma))
}
plot(x_axis, y_axis_call, ylim=c(-1.5,1.5), type="l",col="red",
     xlab="Stock Price", ylab="Delta",
     main="Variation of Delta with Asset Price",
     sub="K = 50, r = 5%, q = 0, sigma = 20%")
lines(x_axis, y_axis_put,col="blue")

abline(h=0)
abline(h=-1, lty=2)
abline(h=1, lty=2)
legend("topright",c("DeltaCall","DeltaPut"),lty=c(1,1),col=c("red","blue"))
abline(v=Exercise)
text(x=Exercise, y=-1.25, labels="K", cex=1.25)
#dev.copy(png,filename="../Wiki_images/deltaplot.png");
#dev.off ();
# Hull, 7th edition Ch 17 p 367
Stock    <- 49     # S_0
Exercise <- 50     # K
Time     <- 20/52  # T
Interest <- 0.05   # r
Yield    <- 0      # q
sigma    <- 0.20

thcall <- ThetaCall(Stock, Exercise, Time, Interest, Yield, sigma)
thput  <- ThetaPut(Stock, Exercise, Time, Interest, Yield, sigma)

rKe    <- Interest * Exercise * exp(-Interest*Time)
#--------------------------------------------------------------------

writeLines(paste0("ThetaCall:        ", round(thcall, 2), "\n",
                  "ThetaPut:         ", round(thput, 2),   "\n",
                  "per calendar day: ", round(thcall/365, 4), " (call)", "\n",
                  "per trading day:  ", round(thcall/252, 4), " (call)", "\n\n",
    "ThetaPut is always greater than ThetaCall by an amount rKe:", "\n",
    "Diff: ",thput-thcall,"\n",
    "rKe:  ",rKe))
x_axis      <- c()
y_axis_call <- c()
y_axis_put  <- c()
for (Stock in seq(0, 100)) {
    x_axis <- c(x_axis, Stock)
    y_axis_call <- c(y_axis_call, ThetaCall(Stock, Exercise, Time, Interest, Yield, sigma))
    y_axis_put  <- c(y_axis_put, ThetaPut(Stock, Exercise, Time, Interest, Yield, sigma))
}

plot(x_axis, y_axis_call, type="l", col="black",
     xlab="Stock Price", ylab="Theta",
     ylim=c(floor(min(y_axis_call)), ceiling(max(y_axis_put))),
     main="Variation in Theta as Stock Price Changes",
     sub=expression(paste0("K = 50, r = 5%, q = 0 ", sigma, " = 20%")))

abline(v=Exercise)
text(x=Exercise, y=0, labels="K", cex=1.5)

abline(h=0, lty=2)

abline(h=-Interest*Exercise*exp(-Interest*Time), lty=2)
text(x=5, y=-Interest*Exercise*exp(-Interest*Time), pos=3, labels=expression(-rKe^{-rT}))

lines(x=x_axis, y=y_axis_put, lty=5, col="red")

legend("topright",c("ThetaPut","ThetaCall"),lty=c(5,1),col=c("red","black"))
#dev.copy(png,filename="../Wiki_images/thetacall.png");
#dev.off ();
# Hull, 7th edition Ch 17 p 371,2
Stock    <- 49     # S_0
Exercise <- 50     # K
Time     <- 20/52  # T
Interest <- 0.05   # r
Yield    <- 0      # q
sigma    <- 0.20

gamma <- Gamma(Stock, Exercise, Time, Interest, Yield, sigma)
round(gamma, 3) # 0.066
x_axis <- c()
y_axis <- c()
for (Stock in seq(0, 100)) {
    x_axis <- c(x_axis, Stock)
    y_axis <- c(y_axis, Gamma(Stock, Exercise, Time, Interest, Yield, sigma))
}

plot(x_axis, y_axis, type="l", col="black",
     xlab="Stock Price", ylab="Gamma",
     main="Variation in Gamma as Stock Price Changes",
     sub="K = 50, r = 5%, q = 0, sigma = 20%")

abline(v=Exercise)
text(x=Exercise, y=0.01, labels="K", cex=1.5)

#dev.copy(png,filename="../Wiki_images/gamma.png");
#dev.off ();

# Hull, 7th edition Ch 17 p 375
Stock    <- 49     # S_0
Exercise <- 50     # K
Time     <- 20/52  # T
Interest <- 0.05   # r
Yield    <- 0      # q
sigma    <- 0.20

vega <- Vega(Stock, Exercise, Time, Interest, Yield, sigma)

writeLines(paste0("The value of Vega is ", round(vega,1),   "\n",
"Therefore, a 1% change in the volatility from 20% to 21%", "\n",
"will raise the price of the option by this amount:",       "\n",
"1% x ", round(vega,1), " = ", round((0.01 * vega), 3),
", from ", Stock, " to ", Stock+round((0.01 * vega), 3)))
x_axis <- c()
y_axis <- c()
for (Stock in seq(0, 100,0.01)) {
    x_axis <- c(x_axis, Stock)
    y_axis <- c(y_axis, Vega(Stock, Exercise, Time, Interest, Yield, sigma))
}

plot(x_axis, y_axis, type="l", col="black",
     xlab="Stock Price", ylab="Vega",
     main="Variation in Vega as Stock Price Changes",
     sub="K = 50, r = 5%, q = 0, sigma = 20%")

abline(v=Exercise)
text(x=Exercise, y=0.01, labels="K", cex=1.5)

#dev.copy(png,filename="../Wiki_images/vegaprice.png");
#dev.off ();
Stock    <- 49     # S_0
x_axis <- c()
y_axis <- c()
for (sigma in seq(0.05, 0.75, 0.01)) {
    x_axis <- c(x_axis, sigma)
    y_axis <- c(y_axis, Vega(Stock, Exercise, Time, Interest, Yield, sigma))
}

plot(x_axis, y_axis, type="l", col="black",
     xlab="sigma", ylab="Vega",
     main="Variation in Vega as Volatility Changes",
     sub="K = 50, r = 5%, q = 0, S = 49")
abline(h=12.10548, lty=2)
abline(v=0.20,     lty=2)

#dev.copy(png,filename="../Wiki_images/vegasigma.png")
#dev.off ()
# Hull, 7th edition Ch 17 p 376
Stock    <- 49     # S_0
Exercise <- 50     # K
Time     <- 20/52  # T
Interest <- 0.05   # r
Yield    <- 0      # q
sigma    <- 0.20

rhocall <- RhoCall(Stock, Exercise, Time, Interest, Yield, sigma)
rhoput  <- RhoPut(Stock, Exercise, Time, Interest, Yield, sigma)

writeLines(paste0("RhoCall:  ", round(rhocall, 2), "\n",
                  "RhoPut:  ", round(rhoput, 2)))
x_axis      <- c()
y_axis_call <- c()
y_axis_put  <- c()
for (Stock in seq(0, 100, 0.01)) {
    x_axis <- c(x_axis, Stock)
    y_axis_call <- c(y_axis_call, RhoCall(Stock, Exercise, Time, Interest, Yield, sigma))
    y_axis_put  <- c(y_axis_put,  RhoPut(Stock, Exercise, Time, Interest, Yield, sigma))
}

plot(x_axis, y_axis_call, type="l", col="black",
     xlab="Stock Price", ylab="Rho",
     ylim=c(floor(min(y_axis_put)),ceiling(max(y_axis_call))),
     main="Variation in Rho as Stock Price Changes",
     sub="K = 50, r = 5%, q = 0, sigma = 20%")

lines(x=x_axis, y=y_axis_put, lty=5, col="red")

abline(v=Exercise)
text(x=Exercise, y=0.01, labels="K", cex=1.5)

abline(h=0, lty=2)

legend("topleft", c("RhoCall","RhoPut"), lty=c(1,5), col=c("black","red"))

#dev.copy(png,filename="../Wiki_images/rhoprice.png");
#dev.off ()


grfiv/ustreasuries documentation built on May 17, 2019, 8:36 a.m.