-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapter4.3Code.R
168 lines (138 loc) · 4.71 KB
/
Chapter4.3Code.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Chapter 4.3 code --------------------------------------------------------
# Last updated: Aug 24, 2017
set.seed(19930225)
numTime = 50 # number of time points
t0 = 0
t = 1
kappa2 = 0.6 # parameter values from Duffie and Garleanu (2001)
sigma2 = 0.14
theta2 = 0.02
lambda0 = 0.01
npath = 1000 # number of sample paths
cir_paths = replicate(npath,
EM_cir(
n = numTime,
kappa = kappa2,
sigma = sigma2,
theta = theta2,
start = lambda0,
t0 = t0,
t = t))
t2 = 2
theta3 = 0.8 # Increase theta to 0.8
cir_paths_paramchange = t(matrix(unlist(lapply(
cir_paths[dim(cir_paths)[1],], EM_cir,
n = numTime+1,
kappa = kappa2,
sigma = sigma2,
theta = theta3,
t0 = t,
t = t2)),
ncol=numTime+2,
byrow=T))
cir_paths2 = rbind(cir_paths, cir_paths_paramchange[2:(numTime+1),])
t3 = 3
cir_paths_paramchange2 = t(matrix(unlist(lapply(
cir_paths2[dim(cir_paths2)[1],], EM_cir,
n = numTime+1,
kappa = kappa2,
sigma = sigma2,
theta = theta2, # Change theta back to 0.02
t0 = t2,
t = t3)),
ncol=numTime+2,
byrow=T))
cir_paths3 = rbind(cir_paths2, cir_paths_paramchange2[2:(numTime+1),])
# plot figure 4.3
yran <- range(cir_paths3,na.rm=T) # plot range
plot(0:(numTime*3),
cir_paths3[,1],
type = "l",
ylim = yran, # plot the first path
xlab = "Time",
ylab = expression(paste(lambda[t], " is a square-root diffusion")),
main = expression(paste(
"Square-Root Diffusion; Parameter ",
bar(theta),
" Changes Value at Time 50 and 100")))
for(k in 2:100) # plot the other paths
lines(0:(numTime*3), cir_paths3[,k], col = adjustcolor("black", alpha.f = 1/(1+100/14)))
mtext(expression(paste(lambda[0], " = 0.01, ",
kappa, " = 0.6, ",
bar(theta)[0-50], " = 0.02, ",
bar(theta)[50-100], " = 0.8, ",
bar(theta)[100-150], " = 0.02, ",
sigma, " = 0.14, npath = 100")),
side = 3)
abline(v=50, col="red")
abline(v=100, col="red")
# end of plotting figure 4.3
# use calculate the default probability using closed-form solution
# first, between time 0 to 50
a1 = 1
timestep = seq(from=t0,to=t,by=(t-t0)/numTime)
C_tT1 = C_tT.fun2(t_i = timestep,
a1 = a1,
kappa = kappa2,
theta = theta2,
sigma = sigma2)
A_tT1 = A_tT.fun2(t_i = timestep,
a1 = a1,
kappa = kappa2,
theta = theta2,
sigma = sigma2)
DefaultProb_01 <- 1-exp(lambda0*C_tT1 + A_tT1)
# second, between time 51 to 100
C_tT2 = C_tT.fun2(t_i = timestep,
a1 = a1,
kappa = kappa2,
theta = theta3,
sigma = sigma2)
A_tT2 = A_tT.fun2(t_i = timestep,
a1 = a1,
kappa = kappa2,
theta = theta3,
sigma = sigma2)
phi = phi.fun(q = -C_tT2,
a1 = a1,
t = 1,
kappa = kappa2,
theta = theta2,
sigma = sigma2)
psi = psi.fun(q = -C_tT2,
a1 = a1,
t = 1,
kappa = kappa2,
theta = theta2,
sigma = sigma2)
DefaultProb_12 <- 1-exp(A_tT2)*exp(lambda0*psi + phi)
DefaultProb_02 = c(DefaultProb_01[1:numTime],DefaultProb_12)
# use calculate the default probability by averaging simulations
Bond_tT_Euler_path2 = exp(apply(
-cir_paths2[1:(numTime*2),]*((t-t0)/numTime),2,cumsum))
Bond_tT_Euler_path_avg2 = apply(Bond_tT_Euler_path2,1,mean,na.rm=T)
DefaultProb_02_icir = 1-Bond_tT_Euler_path_avg2
DefaultProb_02_icir = c(0,DefaultProb_02_icir)
# plot figure 4.4
yran <- range(DefaultProb_02) # plot range
plot(0:(numTime*2),
DefaultProb_02,
type = "l",
ylim = yran, # plot the first path
xlab = "Time",
ylab = expression("Probability of Default"),
main = expression(paste(
"Probability of Default; Square-Root Diffusion Parameter ",
bar(theta),
" Changes Value at Time 50")))
lines(x=0:(numTime*2), y=DefaultProb_02_icir[1:101],col="green")
abline(v=50, col="red")
mtext(expression(paste(lambda[0], " = 0.01, ",
kappa, " = 0.6, ",
bar(theta), " = 0.02, ",
bar(theta)["new"], " = 0.8, ",
sigma, " = 0.14, npath = 1000")),
side = 3)
legend("topleft",inset=0.01,legend=c("Closed-form solution", "Mean of simulations"),
col=c("black","green"), lty=1:1,cex=0.9,box.lty=0, y.intersp=0.8)
# end of plotting figure 4.4