Authors@R: person(“Annice”, “Najafi”, email = “annicenajafi27@gmail.com”)
Summer 2024
This is an R implementation of the stock option model from Chapter 1, section 3 (“A Stock-Option Model”) of the “Introduction to Stochastic Dynamic Programming” book by Sheldon Ross.
An option for a stock is the ability to buy a stock for a pre-determined price. That means you can pay some money to have the right to buy a specific share of a company for a pre-determined price even if the stock price increases later on.
First let’s start by loading relevant libraries
#Load libraries
library(stats)
library(latex2exp)
library(ggplot2)
Define the maximum expected return function
# Define the probability density function
f <- function(x) {
dnorm(x, mean = 0, sd = 1)
}
#Define V_n
#' Recursive function returns the maximal expected return
#'
#' @param n number of days
#' @param s current price of the stock
#' @param c past price that we buy the stock at
#' @return maximal expected return value
#' @export
V <- function(n, s, c) {
if (n == 0) {
return(max((s-c), 0))
}
#Remember F(x) is f(x)dx so we can multiply by the pdf and integrate with
#respect to x.
hold.integrand <- function(x) {V(n - 1, s + x, c) * f(x)}
expected_future_value <- integrate(hold.integrand, -Inf, Inf)$value
# Two possible scenarios here
# Case I) immediately sell the option
# Case II) wait and see what happens, hoping for better future value
return(max(s - c, expected_future_value))
}
Let’s test
# Parameters
initial_stock_price <- 10 # Initial stock price
c <- 8 # stock purchase price
n <- 3 # Number of days
result <- V(n, initial_stock_price, c)
Proving Lemma 3.1
s_values <- seq(0, 100, by = 5)
V_n_s <- c()
for(i in 1:length(s_values)) {
s <- s_values[i]
V_n_s_value <- V(n, s, c)
V_n_s <- c(V_n_s , V_n_s_value)
}
hold.df <- data.frame(s = s_values, V_n_s = (V_n_s_value - s_values))
ggplot(hold.df, aes(s, V_n_s))+
geom_line(size=2, color = "#538392")+
labs(x="s", y=TeX("$V_n(s) - s$"))+
ggtitle(TeX("$V_n(s) - s$ is decreasing in s"))+
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
plot.title = element_text(hjust = 0.5, size=20),
axis.text = element_text(size = 15),
text = element_text(size=18))+labs(tag="A")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
We just proved Lemma 3.1 using R!
Annice
Summer 2024
References:
Ross, S. M. (2014). Introduction to stochastic dynamic programming. Academic press.