SIMULATION EXERCISES IN FINANCE KARTHIK IYER • • • • • knitr 1. Exercises (1) Write a Monte Carlo simulation to generate 10000 samples of a Poisson random variable. Take λ = 1, and report the mean and variance from your samples. Solution: We use the inverse transform method to generate samples of a Poisson random variable with mean 1. (See page 128 in Monte Carlo Methods in Financinal Engineering by Paul Glasserman for the algorithm.) rm(list=ls()) #Removes all objects MonteCarloPoisson <- function(lambda,u) { j <- 0 p <- exp(-lambda) f <- p while(u > f) { p <- (lambda * p)/(j+1) f <- f+p j <- j+1 } return(j) } set.seed(123)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
SIMULATION EXERCISES IN FINANCE
KARTHIK IYER
This collection of solutions is based on assignments for a graduate level course on simulation of models of equity and �xed-incomemarkets applied to derivative security pricing and risk management that I attended. The following topics were covered in thecourse:
• Generating random variables: inverse transform method, composition method, acceptance-rejection method. Generat-ing correlated random variables: correlated normal copula method
• Simulating stochastic processes;
• Simulation output analysis; Variance reduction techniques: control variates, antithetics, conditional Monte Carlo, strat-i�ed sampling, and importance sampling;
• Pricing of �nancial derivatives, including European, American, and exotic derivatives; Price sensitivity (Greeks) estima-tion;
• Risk estimation via simulation, e.g. loss probability, value at risk risk measures, as well as credit portfolio default risks;
The assignments were a mix of written and programming questions. R programming language was used for implementationof the simulation procedures. This document was typeset using LaTex with all of the R code examples embedded into thedocument and evaluated using the wonderful package knitr (written by Yihui Xie).
All errors are entirely mine. Please let me know if you spot any. Comments, suggestions and ideas are always welcome.
1. Exercises
(1) Write a Monte Carlo simulation to generate 10000 samples of a Poisson random variable. Take λ = 1, and reportthe mean and variance from your samples.
Solution: We use the inverse transform method to generate samples of a Poisson random variable with mean 1.(See page 128 in Monte Carlo Methods in Financinal Engineering by Paul Glasserman for the algorithm.)
Step 1 First generate U ∼ U (0,1). Let Y = −log(1−U ). We note that Y is a non-negative random variable takingvalues in [0,∞). Moreover,
P(Y ≤ x) = P(log(1−U ) ≥ −x)
= P(U ≤ 1− e−x) = 1− e−x.
Thus the probability density function of Y , is fY (y) = e−y .
Step 2 Condition on Y = y to simulate a random variable Xy with the cumulative distribution function xy and setX = Xy . In other words, P(Xy ≤ x) = P(X ≤ x|Y = y) = xy . The random variable Xy can be simulated byinverse transform method similar to as done in Step 1.
Step 3 We now show that X has the distribution F(x). Let us first observe that
P(X ≤ x|Y = y) = xy =
x∫0
yty−1 dt. (0.1)
Thus the conditional probability density of the random variable Xy is fX |Y (x|y) = yxy−1. By Step 1, the jointprobability density function of (X,Y ) is
f (x,y) = fY (y) · fX |Y (x|y) = ye−yxy−1, (x,y) ∈ [0,1]× [0,∞)
Hence,
P(X ≤ x) =
∞∫0
x∫0
ye−yty−1 dt dy for 0 ≤ x ≤ 1
=
∞∫0
xye−y dy
= F(x).
�
(3) Give an acceptance-rejection algorithm to generate a random variable with pdf:
f (x) = 2(1− x) for 0 ≤ x ≤ 1. (0.2)
Describe your choice of the density function g(x) and constant a. Run your simulation code to generate 10000samples, and compute the average number of iterations that were required until each sample was accepted.
Solution: We choose g(x) = 1 on [0,1]. Hencemaxx∈[0,1]
(f (x)g(x)
)= 2. We choose a = 2 and run the acceptance-rejection
algorithm.
rm(list=ls())
AcceptanceRejection<-function(U1)
{
#R.V corresponding to g=1
U2 <- runif(1,min=0,max=1)
#count the number of attempts to generate
#the desired R.V with pdf f(x)=2(1-x)
i <- 1
while(U2 >1-U1)
{
U2 <- runif(1,min=0,max=1)
U1 <- runif(1,min=0,max=1)
i <- i+1
}
return(c(U1,i))
}
#-----------------------------
U1 <- runif(10000,min=0,max=1)
results <- sapply(U1,FUN=AcceptanceRejection)
cat("Average number of iterations till a sample with
pdf f(x)=2(1-x) was accepted:", mean(results[2,]))
## Average number of iterations till a sample with
## pdf f(x)=2(1-x) was accepted: 1.9749
#mean(results[1,])
#mean close to 1/3 which is mean of R.V with pdf f
This is correct, because on an average we require a tries before we accept a sample. (See page 59 in Monte CarloMethods in Financinal Engineering by Paul Glasserman for a reference to this fact.) �
(4) Let (V1,V2) in Cartesian co-ordinates, be uniformly distributed in a circle about the origin. Show that V1V2
hasCauchy distribution, i.e
P(V1
V2≤ x) =
1πarctan(x) +
12. (0.3)
Use this fact to construct an algorithm to generate Cauchy variables.
Proof. We first generate a random uniform variable U ∼U (−π,π). Define (V1,V2) = (sin(U ),cos(U )). It is easy tosee that (V1,V2) is uniformly distributed on the circle. Let S denote an arc within the circle and parametrize S by
S = {θ ∈ [−π,π] : θ1 ≤ θ ≤ θ2}. Then
P[(X,Y ) ∈ S] =1
2π
θ2∫θ1
√cos2(θ) + sin2(θ)dθ =
12π
(θ2 −θ1)
We now note that by symmetry of V2 about the origin,
P[V1
V2≤ x] = 2P[
V1
V2≤ x∩V2 > 0] (0.4)
= 2P[(tan(U ) ≤ x∩U ∈ [−π2,π2
]]
The last equality follows as the region V2 > 0 corresponds to cos(U ) > 0 which happens precisely when U ∈ [−π2 ,π2 ].
We thus have,
2P[(tan(U ) ≤ x∩U ∈ [−π2,π2
]] (0.5)
= 2P[(U ≤ arctan(x)∩U ∈ [−π2,π2
]]
=2
2π(arctan(x)− (−π
2))
=1π
arctan(x) +12.
We now propose the following algorithm to generate a random variable V with Cauchy distribution.
Step 1 Generate uniform random U ∼U (−π,π)
Step 2 Let (X,Y ) = (sin(U ),cos(U )).
Step 3 Set V = XY .
Attached is the code for the above algorithm along with a histogram.
rm(list=ls())
Cauchy <- function()
{
U <- runif(100000,min=0,max=1)
X <- cos(2*pi*U)
Y <- sin(2*pi*U)
v <- X/Y
}
V <- Cauchy()
hist(V[abs(V)<25], freq=F, xlab="", main="Random draws from V")
Random draws from VD
ensi
ty
−20 −10 0 10 20
0.00
0.05
0.10
0.15
#Try out V[abs(V)<c] for different values of c to see that
#V indeed has a Cauchy distribution.
�
(5) Recall the acceptance-rejection algorithm for simulating X ∼N (0,1) that utilizes
g(x) = 0.5ex1(−∞,0](x) + 0.5e−x1(0,∞)(x) (0.6)
Write a simulation code to implement this algorithm to generate 10000 samples of X. Show the histogram of thesamples. Estimate E[X] and V ar(X). How many uniforms are used (on average) to generate one sample of X?
Solution: The code below uses acceptance-rejection algorithm to generate standard normal random variable usingdouble exponential random variable. (See page 61 in Monte Carlo Methods in Financinal Engineering by PaulGlasserman for the acceptance rejection method of generating normal random variable from a double exponential.)
main="Histogram of 10000 simulated normal random variabe
using acceptance-rejection method")
Histogram of 10000 simulated normal random variabe using acceptance−rejection method
x
Fre
quen
cy
−4 −2 0 2 4
050
010
0015
0020
00
mean(results[2, ]) #Mean of the number of loops required to
## [1] 1.3082
#generate one standard normal.
mean(results[1, ]) #Mean of the simulated standard normal rv.
## [1] 0.01578049
var(results[1, ]) #Variance of the simulated standard normal rv.
## [1] 1.005786
To count the average number of uniforms we need to generate 1 sample of N (0,1), we multiply the expected no. ofloops by 3 (which is the number of uniforms used in each loop): 3E[N ] = 3×mean(numberof loops) ≈ 4. �
(6) Write a simulation code to implement the Box-Muller algorithm for generating two rv’s (X,Y ) that are IIDN (0,1). Use this to estimate E[X], E[Y ], V ar(X), V ar(Y ), and cov(X,Y ). You should check the estimates with thetheoretical values (which are obvious without computation).
Solution: We implement the Box-Muller algorithm to generate a pair of independent standard normal randomvariables. (See page 66 in Monte Carlo methods in Financial engineering by Paul Glasserman for the Box-Mulleralgorithm.)
#Results match pretty well with theoretical values.
�
(7) Consider the CIR processdXt = (µ−Xt)dt + σ
√XtdWt , t ≥ 0 (1.1)
with positive constants µ,σ and initial value X0 > 0. Define a new process V (Xt) =√Xt , t ≥ 0. There are two ways
to simulate this.
(a) Write down the SDE for V (Xt), and then use this SDE to simulate Vt over time using Euler’s method.
Proof. Using Itô’s lemma, we get the following SDE for Yt =√Xt
dYt = [µ
2Yt− Yt
2− σ
2
8Yt]dt +
σ2dWt , t ≥ 0. (1.2)
with Y0 =√X0. �
(b) You can simulate X first and then apply the function V (x) =√X.
Write the simulation codes for both methods. Take µ = 2,σ = 0.3,T = 1,N = 200,X0 = 1. Compare the twosimulated paths (based on the same generated Brownian motion increments) in one figure. To understandtheir difference, compute the maximum absolute difference between the two path (over all time points).
Solution. We have simulated V (Xt) with both the methods outlined in part (a) and part (b) above. Forsimulating V (Xt) in part (a), we used the SDE (1.2).
Attached is the R code for one simulated path using the algorithm in part (a) and part (b).
rm(list=ls())
#Intialization of variables
N <- 200
mu <- 2
sigma <- 0.3
T <- 1
dt <- T/N
t <- seq(0,T,by = T/N)
Y0 <- 1
Y <- rep(Y0,N) #Simulated path vector for part (a) algorithm initialized all to 1
X0 <- 1
X <- rep(X0,N) #Simulated path vector for part (b) algorithm initialized all to 1
#Generate path according to either algorithm (a) or algorithm (b)
for(i in (1:N))
{
temp <- rnorm(1, mean=0, sd=1)
Y[i+1] = Y[i] + ((mu/(2*Y[i])) - Y[i]/2 -
sigma^2/(8*Y[i]))*dt +
(sigma/2)*sqrt(dt)*temp
X[i+1] = X[i] + (mu - X[i])*dt +
sigma*sqrt(X[i])*sqrt(dt)*temp
}
#----------------------------------------------
plot(t, Y, type="l", lwd=2, col="blue", xlab="time",
ylab="")
lines(t, sqrt(X), col="green")
legend("topleft",
inset=.05,
cex = 0.5,
title="",
c("Part (a) path","Part (b) path"),
horiz=TRUE,
lty=c(1,1),
lwd=c(2,2),
col=c("blue","green"),
bg="grey96")
0.0 0.2 0.4 0.6 0.8 1.0
1.0
1.1
1.2
1.3
time
Part (a) path Part (b) path
cat("Max difference is ", abs(max(sqrt(X) - Y)))
## Max difference is 0.000497556
�
(8) Suppose a store opens for business between t = 0 and t = 10 and that arrivals to the store during [0,10] constitute
a non-homogeneous Poisson process with intensity function λ(t) = (2+t+t2)100 . (The fact that the intensity function is
increasing might reflect the fact that rush hour occurs at the end of the time period.) Each arrival is equally likelyto spend $100, $400 or $900.
(a,c) Use the thinning algorithm to simulate arrivals to the store and to estimate the average amount of moneyand the variance of the amount of money that is spent in [0,10]. (Simulate at least 10000 samples for yourestimate.)
Solution. We use the thinning algorithm and estimate the average amount of money spent and the varianceof the amount of money spent by customers in the store.
rm(list=ls())
Poissonnonhomo <- function(U1, final_t)
{
t <- 0
I <- 0 #counts the number of arrivals
lambda_max <- 1.12 #max value of lambda(t) on [0,final_t]
t <- t - log(U1)/lambda_max
amount_spent <- 0
while(t < final_t)
{
U2 <- runif(1, min=0, max=1)
if (U2 <= lambda(t)/lambda_max)
{
I <- I+1
#We now introduce another uniform rv U3
#to take care of the amount spent by each customer
cat("Average number of customers in [0,10] is ", mean(results[1,]))
## Average number of customers in [0,10] is 4.0561
cat("Average amount of money spent in store in [0,10]
is $", mean(results[2,]))
## Average amount of money spent in store in [0,10]
## is $ 1897.25
cat("Variance of amount of money spent in store in [0,10]
is $", var(results[2,]))
## Variance of amount of money spent in store in [0,10]
## is $ 1352607
�
(b) Now compute analytically the expected amount of money that is spent in [0,10] and compare it to yourestimate.
Proof. Let X denote the number of arrivals to the store during [0,10]. We know that X is a non-homogeneousPoisson random variable with intensity λ(t) = 2+t+t2
100 .
Let Y denote amount each arrival spends in the store. We know Y is a discrete random variable taking values100,400,900 with equal probability 1/3.
We need to find E[XY ]. Becuase X and Y are independent, E[XY ] = E[X]E[Y ]. It is easy to see thatE[Y ] = 100+400+900
3 = 14003 .
Since X is a non-homogeneous Poisson process, we know that E[X] =∫ 10
02+t+t2
100 = 12130 . Hence, E[XY ] =
12130
14003 = 1882.22. This analytical value is in close agreement with the ’simulated’ value found in part (a).
�
(9) Let T be the investment horizon. You can invest in a stock S and the riskless money market account. Denote by Stand Bt the prices at time t of the stock and the money market account, respectively. Assume that St ∼ GBM(µ,σ )and that Bt = exp(rt), t ∈ [0,T ]. Suppose you can trade at the m equally spaced time points: {ti = iT /m : i =0, ...,m− 1}.
Our trading strategy is a constant proportion trading strategy. That is, at each trading point ti , we re-balanceour portfolio so that α% of our wealth Wt i is invested in the stock, and (1−α)% is invested in the money marketaccount.
You may assume that T = 1 years and that m = 12 so that we re-balance our portfolio every month. AssumeW0 = $100,000,S0 = $100,B0 = $1, r = 5%,µ = 15%,σ = 20% and α = 60%.
Write a simulation algorithm to (1) show the distribution of WT (say, in a histogram) and estimate its mean andvariance; (2) estimate the probability that WT /W0 ≤ p where p is some fixed constant. Run your program for
values of p = .8, .9,1.0,1.1,1.2,1.3,1.4,1.5. You should use at least 20,000 samples and plot P(WT /W0 ≤ p) againstp.
Solution. We run the simulation with the code provided below and have produced the desired plot and histogram.As instructed, we have used the "constant proportion strategy". Let us outline in words what our code essentiallydoes.
To wit, at time t = 0, we know 60% of the initial wealth is invested in the stocks, which means $6000 is initiallyinvested in the stocks, which means at time t = 0, the number of shares we have in the stock is na = 60 sinceS0 = 100. Similary, at time t = 0, we own nm = 4000 shares in the money market account. We now simulate thestock price using GBM and estimate St1. Similarly we evaluate Bt1 . Using the values of na and nb calculated attime t = 0, we compute Wt1 .
The next (and crucial) step is to rebalance at time t1 so that proportion of money in stocks is α% and proportionof money in money market account is (1−α)%. This amounts to updating na and nm.
We continue like this for times t2, .., tm = T and thus estimate WT .
rm(list=ls())
S0 <- 100
B0 <- 1
r <- 0.05
mu <- 0.15
sigma <- 0.2
final_t <- 1
m <- 12 #number of times when portfolio is re-balanced
W0 <- 10000 #initial wealth
alpha <- 0.6 #weight in stock
na <- alpha*W0/S0 #initial number of shares in stock
nm <- (1- alpha)*W0/B0 #initial number of shares in money market
#Displaying mean and variance of simulated terminal wealth
cat("Mean of terminal wealth of portfolio is $ ",
mean(simulated_wealth_terminal_time))
## Mean of terminal wealth of portfolio is $ 11066.92
cat("Variance of terminal wealth of portfolio is $ ",
var(simulated_wealth_terminal_time))
## Variance of terminal wealth of portfolio is $ 1660351
�
(10) Consider a price process St , t ≥ 0. The running maximum of S at time t is defined by St :=max0≤u≤tSu . To measurethe distance between the current price and its running maximum, we consider the difference
Dt =StSt− 1.
which is called the relative drawdown of S at time t. This is an essential concept used in evaluating investmentmanagers and their strategies. For many hedge funds, the managers would try to avoid a large drawdown.
Suppose S is a GBM, with parameters T = 1,µ = 5%,σ = 15%,S0 = 1. Simulate a path of S together with theassociated path of S over the period [0,T ].
Next, plot the path of the relative drawdown D (in percentage) over time. This is typically called the "underwaterchart".
In addition, the maximum relative drawdown over [0,T ] is defined by
D∗T =max0≤t≤TDt
For D∗T , plot its (estimated) expected value when σ = 10%,12%, ...,40%, using at least 10000 samples each andholding other parameters fixed. How do the expected values vary with the volatility parameter σ?
Solution. Attached is the R code along with the "underwater chart" for σ = 0.15. Here we have used m = 255independent Brownian motions to simulate the stock price path for t ∈ [0,1].
Underwater chart for one stock price simulated path
time
rela
tive
draw
dow
n %
Next is the R code and the plot for maximum relative drawdown vs the standard deviation σ keeping otherparamters fixed. As we can see, the maximum relative drawdown increases linearly with σ . For this code, wechose 10000 sample stock price path with each path having m = 255 Brownian motions.
where X ′i s are independent and Xi ∼ exp(λi), i = 1,2,3,4. The objective is to estimate the probability P{V > v}for a large constant v. Describe a simulation algorithm that uses importance sampling with a new distributionXi ∼ exp(νi), i = 1,2,3,4 (all independent).
Proof. We are given that Xi ∼ exp(λi) for i = 1, ..,4 with probability density function fi(y) = λie−λiy for y ≥ 0 andfi(y) = 0 otherwise. Moreover we note that
where Et[.] denotes expectation with respect to the X ′i s under the tilted densities, fi,t(), and Mi(t) is the moment
generating function of Xi . Our likelihood ratio will thus be∏4i=1
fi (xi )ft,i (Xi )
.
Let i = 1, .., r. Since v is large, we need to sample more often from the regions where Xi is large and hence we usetilted densitites fi,t where t > 0. As mentioned before, we choose fi,t(xi) = (λi − t)e−(λi−t)xi for xi ≥ 0. We will hencesample from a new distribution Yi ∼ exp(νi) where νi = λi − t where Yi are independent. To ensure, νi > 0, wechoose t to be small enough. For instance, t < min{λi : i = 1,2,3,4} or t = v
2 . �
(12) Black-Scholes Gammas and Deltas
Estimate the Delta and Gamma of Black-Scholes Puts. For Deltas, use the pathwise (PW) and likelihood ratio (LR)methods. For Gammas, use the pure LR method, the combined LR-PW method, as well as the combined PW-LRmethod.
(a) Give the Deltas and Gammas from the Black-Scholes formula, and use them to check/compare with yourestimates.
(b) Report the variances of your estimators.
Parameters: S0 = 100,σ = 25%, r = 3%,T = 0.5, at strikes K = 90,100,110. Use at least 100,000 samples (use up to1 million if feasible). Summarize your answers in a table.
Solution. The closed form expression for Black Scholes Put Delta and Gamma are
∆ =N (d1)− 1
Γ =N′(d1)
S0σ√T
where S0 is the current stock price, σ is the volatility of the stock, T is the expiration date, N (x) is the cumulativestandard normal distribution and
d1 =log(S0/K) + (r + 1
2σ2)T
σ√T
Note that ST = S0e(r− 1
2σ2)T+σ
√TZ where Z ∼ N (0,1). For estimating the delta and gamma, we use the following
estimators
– Delta using PW −e−rT 1K>STSTS0
.
– Delta using LR −e−rT (K − ST )+ ZS0σ√T
.
– Gamma using LR e−rT (K − ST )+( Z2−1
S20σ
2T− ZS2
0σ√T
).
– Gamma using LR-PW −Ke−rT ZS2
0σ√T1ST >K .
– Gamma using PW-LR e−rT STS2
01K>ST (− Z
σ√T
+ 1).
(See page 404 in Monte Carlo Methods in Financinal Engineering by Paul Glasserman for a derivation of theestimators.)
Attached is the R code for Delta and Gamma put using various methods (PW, LR, LR-PW and PW-LR). Note thatall the estimators for Delta and Gamma that we use are unbiased.
## Var of GammaLR 0.0056849867 0.0111751281 0.0192520653
## Var of GammaLRPW 0.0009185158 0.0010642075 0.0015127723
## Var of GammaPWLR 0.0008059532 0.0007401201 0.0008120036
We observe that variance of the PW-LR estimator for Gamma is the least among all considered estimators forGamma and the variance of the PW estimator is lesser than that for the LR estimator for Delta. �
(13) Consider the terminal payoff of an exotic option
h(ST2,ST ) := (ST −K1)+
1S T2≤L + (ST −K2)+
1S T2>L
Apply the likelihood ratio method to estimate the delta and vega of this option with parameters
S0 = 100,σ = 25%, r = 4%,T = 0.5,K1 = 100,K2 = 110,L = 90. Use at least 100,000 runs.
Using the expression for Black-Scholes Delta using LR method, the estimator for Delta for the option consideredin this problem is
e−rT /2f (ST /2)Z
S0σ√T /2
The pseudo-code would for estimatiing Delta would thus be
– Simulate ST /2 using Z.
– Compute e−rT /2f (ST /2) ZS0σ√T /2
– Take expectation.
Estimating vega for this option by LR method is similar. Since the discounted payoff e−rT h(ST /2,ST ) does notexplicitly depend on σ and depends on the value of stock price at T /2, we only need to find the score functionat T /2. Once the score is calculated it can be multiplied by discounted payoff to estimate the delta. (This isone advantage of using Likelihood Ratio method. The form of the estimator does not depend on details of thediscounted payoff.)
The score function of estimating the vega at T /2 is known to be Z2−1σ −Z
√T2 (See page 404 in Monte Carlo Methods
in Financinal Engineering by Paul Glasserman for a derivation of this fact.) The pseudo-code would for estimatiingvega for this option thus takes the form
– Simulate ST /2 using Z.
– Compute e−rT /2f (ST /2)[Z2−1σ −Z
√T /2]
– Take expectation.
The R code for estimating Delta and Vega for this exotic option is attached below.
cat("Estimated Delta using LR method is", mean(sapply(Z, FUN=deltaestimation)))
## Estimated Delta using LR method is 0.3800503
cat("Estimated Vega using LR method is", mean(sapply(Z, FUN=vegaestimation)))
## Estimated Vega using LR method is 13.70004
�
(14) (Portfolio Loss Probability) Suppose your portfolio consists of n shares of stock S and m units of a put optionwritten on S with strike K and expiration date T . Assume the usual GBM model for the stock price S.
(a) First, write down the delta-gamma approximation Q of the portfolio loss over a small time ∆t. The portfolioloss can also be computed analytically in terms of the Black-Scholes formula.
Proof. Let Wt denote the value of the given portfolio at time t. Note that Wt = nSt +mPt where Pt denotes thevalue of the put option at time t and St denotes the value of stock at time t. By Delta-Gamma approximation,
∆W =W∆t −W0 (2.1)
≈ n∆S +m∆P
≈ n∆S +m(∂P∂t
∆t +∂P∂S
∆S +12∂2P
∂S2 ∆S2).
Note that,
∂P∂t
= θP (θ of put option)
∂P∂S
= δP (δ of put option)
∂2P
∂S2 = γP (γ of put option)
Under the Geometric Brownian motion (GBM) model,
∆S ≈ S0(µ̂∆t + σ√∆tZ).
where µ̂ = r − 12σ
2 and Z ∼N (0,1).
Hence by Ito’s lemma the last line in (2.1) is approximately equal to
nS0(µ̂∆t + σ√∆tZ) +m(bp0 + bp1Z + bp2Z
2).
where
bp0 = θP∆t + δP S0µ̂∆t (2.2)
bp1 = δP S0σ
√∆t
bp2 =
γP2σ2S2
0∆t.
Under the constant volatility assumption in the GBM model, the analytic expressions for the put Greeks takethe following form
θP = −S0φ(d1)σ
2√T
+ rKe−rTΦ(−d2) (2.3)
δP = −Φ(−d1)
γP = Ke−rTφ(d2)
S2σ√T.
where d1 = log(S0/K)+(r+0.5σ2)Tσ√T
, d2 = d1 − σ√T , φ(x) is the density for N (0,1) and Φ(x) is the cumulative
distribution function for N (0,1).
Hence our loss L over the time horizon ∆t can be approximated as
−L ≈ nS0(µ̂∆t + σ√∆tZ) +m(bp0 + bp1Z + bp2Z
2). (2.4)
where bpj , j = 1,2,3 are as in (2.2) and (2.3). �
(b) Write a script to generate samples of the portfolio loss at time ∆t using (i) the analytical formula and (ii)the delta-gamma approximation. Then, let’s compare (i) and (ii). For each pair of realizations, show in ascatter plot to illustrate how well/poorly the delta-gamma approximation works compared to the samplesfrom the analytical formula. You may take parameters: S0 = 100,σ = 30%, r = 5%, T = 0.5,K = 100,∆t =14/365,n = 2,m = 3. (Vary ∆t or σ , and observe the performance of the delta-gamma approximation - whatdo you observe?)
Solution: Let us first anayltically compute the portfolio loss. Note that for any 0 ≤ t ≤ T , we have
Wt = nSt +mPt = nS0eµ̂t+σ
√tZ +m(e−r(T−t)KΦ(−d2)− S0Φ(−d1)).
where d1 and d2 are evaluted at T − t. The portfolio loss over the time horizon ∆t is thus −L =W∆t −W0.
We get the following comparison of Anayltical Loss vs Simulated Delta-Gamma Approximation loss fordifferent values of σ and for one relaization of driving Brownian motion. In our code, we take σ varyingfrom 0.01 to 0.4 in steps of 0.01.
(c) Estimate the probabilities of losing x% of the initial portfolio value for σ ranging from 10% to 40%. In otherwords, show a 3-D plot where the z-axis is the loss probability, x-axis is loss level x, and y-axis is σ .
rm(list=ls())
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: ’plotly’
## The following object is masked from ’package:ggplot2’:
##
## last_plot
## The following object is masked from ’package:stats’:
##
## filter
## The following object is masked from ’package:graphics’:
main="3D plot of loss probability vs volatility vs loss level",
ticktype="detailed",
xlab="Loss level",
ylab="Volatility",
zlab = "Loss probability",
theta = 40, phi = 15,
col = "springgreen", shade = 0.5)
Loss level
0.0
0.1
0.2
0.3
0.4Vo
latilit
y
0.10
0.15
0.20
0.25
0.300.35
0.40
Loss probability
0.3
0.4
0.5
0.6
3D plot of loss probability vs volatility vs loss level
(15) (Continued) Since the loss approxmization is of the form: L ∼ a+∑mj=1(bjZj +λjZ
2j ) :=Q.The moment generating
function of Q is E[eθQ] = eaθ∏mj=1E[eθ(bjZj+λjZ
2j )],θ ∈R. Compute E[eθQ]. You can take m = 1.
Proof. For m = 1, the approximate loss in (2.4) takes the following form
−L ≈ nS0(µ̂∆t + σ√∆tZ) +m(bp0 + bp1Z + bp2Z
2)
L ≈ (−nS0µ̂∆t − bp0) + (−bp1 −nS0σ
√∆t)Z − bp2Z
2.
where bpj , j = 1,2,3 are as in (2.2) and (2.3) and Z ∼N (0,1)
Denote h0 = −nS0µ̂∆t − bp0 , h1 = −bp1 −nS0σ
√∆t and h2 = −bp2 . We can succintly (and approximately) write our loss
asL ≈ h0 + h1Z + h2Z
2 :=Q.
Thus for any θ ∈R,
E[eθQ] =eθh0
√2π
∫R
eθh1z+(θh2−0.5)z2dz
=eθh0+0.5θ2h2
1σ̂2
√2π
∫R
e− 1
2σ̂2 (z−θh1σ̂2)2dz (where σ̂2 = 1
1−2θh2)
= σ̂ eθh0+0.5θ2h21σ̂
2
1√
2πσ̂2
∫R
e− 1
2σ̂2 (z−θh1σ̂2)2dz
= σ̂ eθh0+0.5θ2h2
1σ̂2
(The expression inside the big brackets integrates to 1)
�
(16a) Write a script to implement the loss probability formula
P(Y ≤ y) ≈ Φ
Φ−1(1− p)−√
1− ρΦ−1(1− y)√ρ
. (2.5)
Fix p = 1%. Plot P(Y ≤ y) as a function of y, for ρ = 0.2;0.5;0.8.
Solution: We first note that the approximation (2.5) holds for a portfolio where we have credit risk exposure on mdiferent firms and m is very large. Y denotes the random variable L
m where L is the number of defaults from mfirms. Φ of course denotes the standard normal cumulative distribution function and ρ incorporates dependenceamong the defaults.
rm(list=ls())
y <- seq(0,0.4, by=0.001)
p <- 0.01
prob_loss_formula <- function(y, rho)
{
numerator <- qnorm(1-p) - sqrt(1-rho)*qnorm(1-y)
denominator <- sqrt(rho)
return(pnorm(numerator/denominator))
}
y1 <- sapply(y, FUN=prob_loss_formula, rho=0.2)
y2 <- sapply(y, FUN=prob_loss_formula, rho=0.5)
y3 <- sapply(y, FUN=prob_loss_formula, rho=0.8)
df <- data.frame(x=rep(y,3), y=c(y1, y2,y3),
class=c(rep("rho=0.2", length(y)),
rep("rho=0.5", length(y)),
rep("rho=0.8", length(y))))
library(ggplot2)
ggplot(data = df, aes(x=x, y=y, color=class)) +
geom_point()+
xlab("y") +
ylab("Pr(Y<y)") +
ggtitle("Loss probability vs y")
0.00
0.25
0.50
0.75
1.00
0.0 0.1 0.2 0.3 0.4
y
Pr(
Y<
y)
class
rho=0.2
rho=0.5
rho=0.8
Loss probability vs y
�
(16b) Alternatively, can numerically compute the PMF:
P(L = l) =∫R
(ml
)p̃(z)l(1− p̃(z))m−lfZ (z)dz
where fZ is the pdf of a N (0,1) randdom variable and p̃(z) is defined as p̃(z) = 1−Φ(x−√ρz√
1−ρ) where x = Φ−1(1− p).
Take m = 10, 25, 50. It will become more computationally expensive as m increases. Again, take p = 1%. Plot thePMF P(L = l) as a function of l, for ρ = 0.2;0.5;0.8.
Proof. For the sake of brevity, we have only attached the R code m = 10.