Part 4.1 R and MC Simulation
Part 4.1 R and MC Simulation
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 1 / 34
Brownian motion
3. Bt − Bs ∼ N (0, t − s) where t ≥ s
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 2 / 34
Brownian motion
50 Brownian motion simulations from 0 to 1
−1
−2
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 3 / 34
Brownian motion
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 4 / 34
Brownian motion
Bh-Bo là hàm pp chuẩn
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 5 / 34
Brownian motion
T<-1
n<-250
delta<-T/n
time<-seq(0,T,length=(n+1))
# Vector BM contains simulated value of a BM
BM<-rep(0,(n+1))
for (i in 2:(n+1)){
# (BM[i] - BM[i-1]) is a normal r.v (0,delta)
Ni<-rnorm(1,0,1)[1]*sqrt(delta)
BM[i]<-BM[i-1]+Ni
}
dat<-data.frame("Time"=time,"BM"=BM)
dat%>%ggplot(aes(Time,BM))+geom_point()+geom_line()
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 6 / 34
Brownian motion
1.2
0.8
0.4
BM
0.0
−0.4
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 7 / 34
Brownian motion
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 8 / 34
Application of brownian motion
Xt+∆ = Xt + µ × ∆ + σ × (Bt+∆ − Bt )
where
∆
→ dt
X
t+∆ t −X → dXt
−B → dBt
B
t+∆ t
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 9 / 34
Application of brownian motion
0.75
0.50
Xt
0.25
0.00
0 1 2 3 4
Time
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 10 / 34
Application of brownian motion
Xt+∆ = Xt + µ × ∆ + σ × (Bt+∆ − Bt )
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 11 / 34
Application of brownian motion
1
Value
0 1 2 3 4
Time
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 12 / 34
Application of brownian motion
dSt
= µdt + σdBt
St
where µ is the expected return of stock price under the real world
probability measure. σ is the standard deviation of stock price
return. (σ is called stock price return’s volatility).
Given St , what is the distribution function of dSt ?
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 13 / 34
Application of brownian motion
dSt
= µdt + σdBt
St
where µ is the expected return of stock price under the real world
probability measure. σ is the standard deviation of stock price
return. (σ is called stock price return’s volatility).
Given St , what is the distribution function of dSt ?
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 13 / 34
Application of brownian motion
dSt
= µdt + σdBt
St
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 14 / 34
Application of brownian motion
Write a code to simulate 10 stock price from time t = 0 to time T = 4
with S0 = 100, µ = 0.1, σ = 0.2, and n = 1000
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 15 / 34
Application of brownian motion
Write a code to simulate 10 stock price from time t = 0 to time T = 4
with S0 = 100, µ = 0.1, σ = 0.2, and n = 1000
T<-4
mu<-0.1
sigma<-0.2
S0<-100
time<-seq(0,T,length=(n+1))
n<-1000
dat<-data.frame("Time"=time)
for (i in 1:100){
St<-StockPriceMC(S0,T,n,mu,sigma)
dat<-mutate(dat,St)
names(dat)[(i+1)]<-paste0("S",i)
}
dat%>%gather("StockPrice",Value,"S1":"S100")%>%
ggplot(aes(Time,Value,group=StockPrice,color=StockPrice))+ge
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 15 / 34
Application of brownian motion
250
200
Value
150
100
50
0 1 2 3 4
Time
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 16 / 34
Option pricing and MC simulation
f0 = e −rT EQ (f (ST ))
dSt
= rdt + σdBt
St
where Bt is a Brownian motion under Q.
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 17 / 34
Option pricing and MC simulation
c0 = e −rT EQ (ST − K )+
p0 = e −rT EQ (K − ST )+
dSt
= rdt + σdBt
St
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 18 / 34
Option pricing and MC simulation
1 Simulate (St )t≥0 using the following SDE to obtain ST .
dSt
= rdt + σdBt
St
PT = (K − ST )+
There are analytic formulas for european Call and european Put option as
follow:
Write functions to calculate the fair price of european call and european put
option using these folmulas. Compare the results with MC simulation.
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 22 / 34
Option pricing and MC simulation
EuCallPrice<-function(S0,K,T,r,sigma){
d1<-(log(S0/K)+(r+sigmaˆ2/2)*T)/(sigma*sqrt(T))
d2<-d1-sigma*sqrt(T)
EuCallPrice<-S0 * pnorm(d1) - exp(-r*T)*K*pnorm(d2)
}
c0<-EuCallPrice(S0,K,T,r,sigma)
EuPutPrice<-function(S0,K,T,r,sigma){
d1<-(log(S0/K)+(r+sigmaˆ2/2)*T)/(sigma*sqrt(T))
d2<-d1-sigma*sqrt(T)
EuPutPrice<- - S0 * pnorm(-d1) + exp(-r*T)*K*pnorm(-d2)
}
p0<-EuPutPrice(S0,K,T,r,sigma)
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 23 / 34
Option pricing and MC simulation
8.0
8.5
CallPrice
9.0
9.5
10.0
4000 6000 8000 10000
N
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 24 / 34
Option pricing and MC simulation
6.0
6.5
PutPrice
7.0
7.5
8.0
4000 6000 8000 10000
N
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 25 / 34
Option pricing and MC simulation
1 Barrier call option: Tính giá của một barrier call option trên cổ phiếu
S với S0 = 100, σ = 20%, r = 2%, T = 1 và K = 100 biết rằng payoff
của barrier call option là (ST − K )+ tuy nhiên người sở hữu quyền
chọn bán chỉ được chi trả với điều kiện ST lớn hơn một mức giá
barrier (PB = 105)
2 Asian call option: Khác với quyền chọn kiểu châu Âu sử dụng giá cổ
phiếu tại thời điểm đáo hạn T , quyền chọn kiểu châu Á sử dụng giá trị
trung bình của cổ phiếu S trong khoảng thời gian từ 0 đến T . Mức giá
trung bình của cổ phiếu được tính bằng trung bình cộng của giá cổ
phiếu tại các thời điểm được xác định trước. Tính giá của quyền chọn
kiểu châu Á với S0 = 100, σ = 20%, r = 2%, T = 1 và K = 100 với
payoff (S̄T − K )+ trong đó
ST /4 + S2T /4 + S3T /4 + ST
S̄T =
4
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 26 / 34
Option pricing and MC simulation
# BARRIER CALL OPTION
T<-1
r<-0.02
n<-52
sigma<-0.2
S0<-100
K<-100
PB<-105
N<-10ˆ4
CT<-rep(0,N)
for (i in 1:N){
ST<-StockPriceMC(S0,T,n,r,sigma)[n+1]
CT[i]<-ifelse(ST>105,max(ST-K,0),0)
}
mean(CT)*exp(-r*T)
## Dr.
[1] 8.976387
Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 27 / 34
Option pricing and MC simulation
# ASIAN CALL OPTION
T<-1
r<-0.02
n<-52
sigma<-0.2
S0<-100
K<-100
N<-10ˆ4
CT<-rep(0,N)
for (i in 1:N){
St<-StockPriceMC(S0,T,n,r,sigma)
CT[i]<-max((St[n/4+1]+St[2*n/4+1]+St[3*n/4+1]+St[4*n/4+1])/4
}
mean(CT)*exp(-r*T)
## [1] 5.857523
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 28 / 34
Poisson process
qtr ngẫu nhiên liên tục
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 29 / 34
Poisson process
k<-20 # number of Nt
T<-4
lambda<-5
n<-1000
delta<-T/n
Nt<-matrix(0,k,(n+1))
for (i in 2:(n+1)){
dNt<-rpois(k,lambda*delta)
Nt[,i]<-Nt[,(i-1)]+dNt
}
dat<-as.data.frame(t(Nt))
names(dat)<-paste0("Nt",1:k)
dat<-dat%>%mutate(Time=seq(0,T,T/n))%>%gather(Nt,Value,Nt1:pas
dat%>%ggplot(aes(Time,Value,group=Nt,col=Nt))+geom_line()+
theme(legend.position = "none")+ggtitle(paste0(k, " paths of
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 30 / 34
Poisson process
20 paths of poisson process
20
Value
10
0 1 2 3 4
Time
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 31 / 34
Compound Poisson process
A compound Poisson process is a process (Y (t)) given by
0
if N(t) = 0
Y (t) = N(t)
P
Xi , otherwise
i=1
0
if dN(t) = 0
dY (t) = dN(t)
P
Xi , otherwise
i=1
20
Value
10
0 1 2 3 4
Time
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 33 / 34
Application of Poisson process
ut = u0 + ct − Y (t)
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 34 / 34