0% found this document useful (0 votes)
34 views

Part 4.1 R and MC Simulation

1. Brownian motion is a continuous-time stochastic process used to model random behavior over time. 2. Brownian motion can be simulated by treating each time step as a normal random variable based on the previous value. 3. Brownian motion is used in financial models like the Black-Scholes model to represent random fluctuations in stock prices over time.

Uploaded by

Nguyễn Oanh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Part 4.1 R and MC Simulation

1. Brownian motion is a continuous-time stochastic process used to model random behavior over time. 2. Brownian motion can be simulated by treating each time step as a normal random variable based on the previous value. 3. Brownian motion is used in financial models like the Black-Scholes model to represent random fluctuations in stock prices over time.

Uploaded by

Nguyễn Oanh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Part 4 Simualtion and stochastic process

Dr. Nguyen Quang Huy

Dec 08, 2020

Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 1 / 34
Brownian motion

Brownian motion: named after a Scottish botanist and paleobotanist


Robert Brown (1773-1858)
Brownian motion is also called Weiner process (named after a
mathematician Norbert Weiner(1894-1964))
In mathematics, 1 dimensional-Brownian motion denoted by (Bt )t≥0 is
a continuous-time stochastic process characterized by three facts
1. B0 = 0
2. Bt2 − Bt1 Bs2 − Bs1 where t2 ≥ t1 ≥ s2 ≥ s1 t1,t2,s1,s2: mốc tgian
|=

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

0.00 0.25 0.50 0.75 1.00

Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 3 / 34
Brownian motion

mô phỏng 1 quá trình ngẫu nhiên Xt

To simulate a stochastic process (in general) (X )t≤s≤T from t to T


T −t
Let ∆ =
n
For i = 1, 2, · · · , n
Determine the conditional
 distribution of Xi∆ |X(i−1)∆ (or
Xi∆ − X(i−1)∆ |X(i−1)∆ )
Simulate Xi∆ based on the conditional distribution of Xi∆ |X(i−1)∆

If Xt is a Brownian motion (Bt ), what is conditional distribution of


Bi∆ |B(i−1)∆ ?

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

0.00 0.25 0.50 0.75 1.00


Time

Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 7 / 34
Brownian motion

Write a code to simulate 100 Brownian motion from time 0 to time 5?

Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 8 / 34
Application of brownian motion

Given (Xt )t≥0 where X0 = 0 and

Xt+∆ = Xt + µ × ∆ + σ × (Bt+∆ − Bt )

or we can write (in a stochastic differential equation)

dXt = µdt + σdBt

where

∆

 → dt
X
t+∆ t −X → dXt

−B → dBt

B
t+∆ t

What is the distribution function of dXt |Xt ?

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

Given (Xt )t≥0 where X0 = 0 and

Xt+∆ = Xt + µ × ∆ + σ × (Bt+∆ − Bt )

Prove that XT has a normal distribution with mean µT and variance


σ2T .
Simulate m = 100 processes Xt from 0 with T = 4, n = 1000,
µ = 0.25 and σ = 0.2

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

A well-known assumption of Black-Scholes model: stock price return


satisfies the following SDE

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

A well-known assumption of Black-Scholes model: stock price return


satisfies the following SDE

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 ?

dSt = St µdt + St σdBt


 
dSt ∼ N dt(St µ), dt(St σ)2

Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 13 / 34
Application of brownian motion

Write a function of parameters (S0 , T , n, µ, σ) to simulate a stock price


from t to T satisfying the SDE

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

If f (ST ) is the pay-off of a derivative product based on an underlying asset


ST then the price (fair price) of this product can be calculated by

f0 = e −rT EQ (f (ST ))

where f0 is the fair price of the derivative at time 0, r is the continuous


risk-free interest rate, and Q is the risk neutral probability measure.
In the case that St is a stock price, St satisfies the following SDE

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

When f (ST ) = max (ST − K , 0) = (ST − K )+ we have an european


Call option on stock S with strike price K .
When f (ST ) = max (K − ST , 0) = (K − ST )+ we have an european
Put option on stock S with strike price K .
The fair price of Call and Put option can be written as follows:

c0 = e −rT EQ (ST − K )+


p0 = e −rT EQ (K − ST )+


where St satisfies the SDE

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

2 Calculate the option payoff


CT = (ST − K )+


PT = (K − ST )+


3 Repeat step 1. and 2. (N times) to obtain a vector of Call option


payoff VCT and a vector of Put option payoff VPT
4 Return
c0 = exp(−rT ) × mean (VCT )
p0 = exp(−rT ) × mean (VPT )
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 19 / 34
Option pricing and MC simulation
Using N = 104 simulation, calculate the price of an european call option
with S0 = 100, T = 1, r = 2%, σ = 20%, K = 100.
T<-1
r<-0.02
n<-52
sigma<-0.2
S0<-100
K<-100
N<-10ˆ5
CT<-rep(0,N)
for (i in 1:N){
ST<-StockPriceMC(S0,T,n,r,sigma)[n+1]
CT[i]<-max(ST-K,0)
}
mean(CT)*exp(-r*T)
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 20 / 34
Option pricing and MC simulation
Using N = 104 simulation, calculate the price of an european put option
with S0 = 100, T = 1, r = 2%, σ = 20%, K = 100.
T<-1
r<-0.02
n<-52
sigma<-0.2
S0<-100
K<-100
N<-10ˆ5
PT<-rep(0,N)
for (i in 1:N){
ST<-StockPriceMC(S0,T,n,r,sigma)[n+1]
PT[i]<-max(K-ST,0)
}
mean(PT)*exp(-r*T)
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 21 / 34
Option pricing and MC simulation

There are analytic formulas for european Call and european Put option as
follow:

c0 = S0 Φ(d1 ) − e −rT K Φ(d2 )


p0 = e −rT K Φ(−d2 ) − S0 Φ(−d1 )

where Φ is the distribution function of standard normal random variable and


log(S0 /K ) + r + σ 2 /2 T

d1 = √
σ T

d2 = d1 − σ T

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

The homogeneous Poisson process is a stochastic process N(t),(t ≥ 0)


has the following three properties:
1. N(0) = 0
2. N(t2 ) − N(t1 ) and N(s2 ) − N(s1 ) are independent with
t2 ≥ t1 ≥ s2 ≥ s1
3. N(t + ∆) − N(t) is a Poisson r.v with mean λ∆
Write R code to simulate a Poisson process from 0 to T = 4 with λ = 5
using n = 1000

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

where Xi are i.i.d random variables and N(t) is an homogeneous


Poisson process.


0
 if dN(t) = 0
dY (t) = dN(t)
P

 Xi , otherwise
i=1

Write R code to simulate a Compound poisson process from 0 to


T = 4 with λ = 5, and Xi ∼ exp(1) using n = 1000
Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 32 / 34
Compound poisson process
20 paths of Compound poisson process − exponential claim mean 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

An insurance company has initial capital u0 . Suppose that the premium is


paid at a constant rate c i.e. from time t to time (t + ∆), the premium
collected is c∆. Number of claim from time t to time (t + ∆) is a Poisson
r.v with mean λ∆ and claim amount is an exponential random variable with
mean µ. The capital process of the insurance company can be write as
follow:

ut = u0 + ct − Y (t)

where Y (t) is a compound poisson process.


With u0 = 3, c = 2, λ = 10 and µ = 0.1, simulate a path of ut from 0 to
T =4

Dr. Nguyen Quang Huy Part 4 Simualtion and stochastic process Dec 08, 2020 34 / 34

You might also like