Professional Documents
Culture Documents
ELVIS LIKOKO
2022-07-12
Numerical Solution
√
If we discretize the time intervals, St+1 = St + µSt dt + σSt dtz. The
√ Z is standard normal random variable
i.e. Z~N(0,1) and can be simulated in R using the rnorm() function. t × z has the same distribution as dWt
hence it can simulate the Brownian motion part.
Single Trajectory
I will do a simulation for 100 days. I will assume the stock price on day one is 100, sigma is 0.02, mu = 0.13
and dt = 1/365.
day stock_price
1 100
2 0
3 0
4 0
5 0
6 0
1
#simulating using GBM numerical solution
set.seed(12) #ensuring reproducibility
for(i in 2:n){
x= df$stock_price[i-1]#previous stock price
df$stock_price[i]<- x+ mu*x*dt+ sigma*x*sqrt(dt)*rnorm(1)
}
kable(head(df))
day stock_price
1 100.00000
2 99.88062
3 100.08111
4 100.01651
5 99.95581
6 99.78238
#visualization
ggplot(df,aes(x=day,y=stock_price))+geom_line()+
theme_bw()+ggtitle("Single Trajectory GBM")
102.5
stock_price
101.5
100.5
99.5
0 25 50 75 100
day
2
nsim <- 20 #number of sample paths
#just as in the previous example, I will use a data frame to store the results
n <- 100 #number of days
S_0 <- 100 #initial stock price
sigma <- 0.02 #volatility
dt <- 1/365
mu <- 0.13
df <- data.frame(day = 1:n)
#sample path names: S_1, S_2,....,S_50
df[,2:(nsim+1)]<-0
names(df)[2:(nsim+1)]<-paste0("S_",1:20)
df[1,-1]<-S_0#initializing
kable(head(df))
day S_1 S_2 S_3 S_4 S_5 S_6 S_7 S_8 S_9 S_10 S_11 S_12 S_13 S_14 S_15 S_16 S_17 S_18 S_19 S_20
1 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
day S_1 S_2 S_3 S_4 S_5 S_6 S_7 S_8 S_9 S_10
1 100.00000 100.0000 100.00000 100.00000 100.00000 100.0000 100.0000 100.00000 100.0000 100.0000
2 99.88062 100.2007 99.93546 99.93931 99.82649 100.0071 100.0026 99.96985 100.0245 100.0804
3 99.93958 100.4470 100.07692 99.94326 99.75491 100.0147 100.0174 100.01918 100.0754 100.1540
4 99.98616 100.3612 100.17314 99.81191 99.75822 100.0974 99.9507 100.07469 100.1876 100.1380
5 100.06432 100.5014 100.29856 99.86806 99.88088 100.2218 100.1908 99.88517 100.3252 100.2937
6 100.10041 100.4031 100.31306 100.02537 99.91401 100.3516 100.2079 100.03720 100.3040 100.2283
3
day variable value
5 S_1 100.06432
6 S_1 100.10041
ggplot(df_1,aes(x=day,y=value,color=variable))+
geom_line()+theme_bw()+ggtitle("Several Trajectories for GBM")+theme(legend.position="none")+
labs(y="stock price")
104
stock price
102
100
0 25 50 75 100
day