You are on page 1of 4

GEOMETRIC BROWNIAN MOTION SIMULATION

ELVIS LIKOKO

2022-07-12

#loading the packages required


library(ggplot2)#data viz
library(reshape2)#convert data from wide to long format for easier plotting
library(knitr) #using kable to display tables

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.

n <- 100 #number of days


S_0 <- 100 #initial stock price
sigma <- 0.02 #volatility
dt <- 1/365
mu <- 0.13
#Let's create a dataframe that shows the day and the
#stock price
df <- data.frame(day=1:n,stock_price= rep(0,n))
df$stock_price[1] <- S_0
library(knitr)
kable(head(df))

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")

Single Trajectory GBM


103.5

102.5
stock_price

101.5

100.5

99.5
0 25 50 75 100
day

Several Sample Paths


I will simulate 50 sample paths for 100 days. The assumptions will be the same as in the single trajectory.

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

#Brownian motion simulation


set.seed(12)
for (i in 2:n){
x = df[(i-1),-1]#previous stock price
df[i,-1]<- x + mu*x*dt+ sigma*x*sqrt(dt)*rnorm(nsim)
}
kable(head(df[,1:11]))#first 10 sample paths

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

#drawing the graphs


#I will use reshape 2 to convert the data to a long format
df_1 <- melt(df,id="day")
kable(head(df_1))

day variable value


1 S_1 100.00000
2 S_1 99.88062
3 S_1 99.93958
4 S_1 99.98616

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")

Several Trajectories for GBM

104
stock price

102

100

0 25 50 75 100
day

You might also like