Libraries to load

1 Evaluate an integral

produce a plot of 5 different sigmas:

fx <- function(x) {
    x^3 * sin((x + 3.4)/2)
}
# using the Monte Carlo integration method
n = 10000
x1 <- runif(n, min = 0, max = 3)
sx <- fx(x1)
ans.mc <- mean(sx * 3)
ans.mc
## [1] 4.684397

# using the integrate() function in R
Fx <- integrate(fx, lower = 0, upper = 3)
Fx$value
## [1] 4.652802

2 Exotic Financial option simulation

stock.sim = function(sigma, ndays = 90, S0 = 100, mu = 0.05) {
    N = rnorm(ndays)
    S = S0
    dT = 1/365
    Sres = S
    for (i in 1:ndays) {
        dS = S * (mu * dT + sigma * sqrt(dT) * N[i])
        S = S + dS
        Sres = c(Sres, S)
    }
    return(Sres)
}

sigmas = c(0.1, 0.25, 0.5, 0.75, 1)
price = sapply(sigmas, stock.sim)
matplot(price, type = "l", xlab = "Day", ylab = "Stock price")
legend("topright", legend = sigmas, col = 1:5, lty = 1:5, cex = 0.8, title = "sigma")

2.1 Stock price simulation

Produce a plot of 5 different simulations(niters:1:5):


stock.sim = function(niters, sigma = 0.5, ndays = 90, S0 = 100, mu = 0.05) {
    N = rnorm(ndays)
    S = S0
    dT = 1/365
    Sres = S
    for (i in 1:ndays) {
        dS = S * (mu * dT + sigma * sqrt(dT) * N[i])
        S = S + dS
        Sres = c(Sres, S)
    }
    return(Sres)
}

palette <- brewer.pal(7, "Dark2")
niters = seq(5)
price = sapply(niters, stock.sim)
matplot(price, type = "l", xlab = "Day", ylab = "Stock price", col = palette[1:5])
legend("topright", legend = niters, col = palette[1:5], lty = 1:5, cex = 0.8, title = "simulations")

2.2 Compute the exotice option pay-offs

# exotice option payoff simulation
set.seed(500316995)
exo_stock.payoff = function(niters, sigma = 0.5, ndays = 90, S0 = 100, mu = 0.05, 
    strike.price = 105, knockout.price = 130) {
    N = rnorm(ndays)
    S = S0
    dT = 1/365
    Sres = S
    for (i in 1:ndays) {
        dS = S * (mu * dT + sigma * sqrt(dT) * N[i])
        S = S + dS
        Sres = c(Sres, S)
    }
    option.payoff = if (any(Sres >= knockout.price)) 
        0 else max(tail(Sres, 1) - strike.price, 0)
    
    return(option.payoff)
}

niters = seq(1000)
exo.payoff = sapply(niters, exo_stock.payoff)

exo.density <- density(exo.payoff)

2.3 Conduct Monte carlo simulation of pay-offs

# vanilla call option payoff simulation
set.seed(500316995)
van_stock.payoff = function(niters, sigma = 0.5, ndays = 90, S0 = 100, mu = 0.05, 
    strike.price = 105) {
    N = rnorm(ndays)
    S = S0
    dT = 1/365
    Sres = S
    for (i in 1:ndays) {
        dS = S * (mu * dT + sigma * sqrt(dT) * N[i])
        S = S + dS
        Sres = c(Sres, S)
    }
    option.payoff = max(tail(Sres, 1) - strike.price, 0)
    
    return(option.payoff)
}

niters = seq(1000)
van.payoff = sapply(niters, van_stock.payoff)

van.density <- density(van.payoff)

A graphic to compare these two options:

fig <- plot_ly(x = ~exo.density$x, y = ~exo.density$y, name = "exotice option", type = "scatter", 
    mode = "lines", fill = "tozeroy")
fig <- fig %>% add_trace(x = ~van.density$x, y = ~van.density$y, name = "vanilla call option", 
    fill = "tozeroy")
fig <- fig %>% layout(title = "Option Pay-offs", xaxis = list(title = "Payoff"), 
    yaxis = list(title = "Density"))

fig
fig <- plot_ly(y = exo.payoff, type = "box", boxpoints = "suspectedoutliers", name = "exotice option")
fig <- fig %>% add_trace(y = van.payoff, boxpoints = "suspectedoutliers", name = "vanilla call option")
fig <- fig %>% layout(title = "Option Pay-offs", xaxis = list(title = "Option Type"), 
    yaxis = list(title = "Payoff"))

fig
# Price of $105 ($130) exotice options is the expected value of the payoffs
exo.price <- mean(exo.payoff)

# Price of $105 vanilla call options is the expected value of the payoffs
van.price <- mean(van.payoff)

Price_gap <- van.price - exo.price
compare_price <- data.frame(exotice_price = exo.price, vanilla_price = van.price, 
    Price_gap = Price_gap)
knitr::kable(compare_price)
exotice_price vanilla_price Price_gap
1.553575 8.489406 6.93583

We can see exotice_price(the appropriate price for such an exotic option) is 1.553575 and the price gap(how much cheaper is this knock-out call option compared to a vanilla call option) is 6.93583 in this table.

APP. Student Info

Course: STAT5003_Computational Statistical Methods
Assignment: Lab Week 10
Student Name: Yujun Yao(June Yao)
SID: 500316995
Email: