-
Notifications
You must be signed in to change notification settings - Fork 1
/
cdoValue_22Aug.R
executable file
·209 lines (157 loc) · 9.44 KB
/
cdoValue_22Aug.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
CDOValue <- function(N,R,lambdaf,rho,n,c,a,d,r,No,flag)
{
TM <- 5 # length of the CDO
tstep <- 0.5 # the coupon payments
nn = n * N # the total notional
loss = n * ( 1 - R) # the total loss where R is recovery Rate
T <- seq(0,TM,tstep) # vector for the fixed coupon dates
Tmod <- t(array(rep(T,N), dim=c(length(T),N))) # matrix of fixed coupon dates for all N companies
discount <- exp(-r*T[2:length(T)]) # discounted fixed coupon vector
# just to initialize the generator
MRho <- repmat(rho,N,N) # initializing the correlation matrix
for (i in 1:length(MRho[1,])) { MRho[i,i] <- 1 } # filling diagonal entries with 1
MRho <- chol(MRho) # doing the Cholesky factorization
fixedtot <- 0 # initializing for fixed leg total
floattot <- 0 # initializing for floating leg total
sqfixtot <- 0 # for standard error estimate
sqfltot <- 0 # for standard error estimate
Values <- N * No
PMat <- array(rnorm(Values),dim=c(N,No)) # N rows, No columns
PMat1 <- t(MRho) %*% PMat # to get the correlated Gaussian matrix
PMat11 <- pnorm(PMat1) # take the CDF to make them a copula
PMat2 <- -log(1 - PMat11)/ lambdaf # inverse function to get the default time
# Begin MC simulations
for (i in 1:No) # loop for different paths of MC
{
PMat3 <- PMat2[,i] # getting the i'th path
Pmatmod <-repmat(PMat3,1,2*TM+1) # getting it for the fixed coupon dates
Temp1 <- Pmatmod < Tmod # keeping default times that are only within the CDS maturity
Lmat <- loss*Temp1 # getting the losses matrix by multiplying with the defaults
Tloss <- colSums(Lmat) # summing up the losses
Ploss <- Tloss/nn # getting the loss percentages
PlossSum <- rep(0,length(Tloss))
for (j in 1:length(Ploss))
{ PlossSum[j] <- max(Ploss[j]-a,0) - max(Ploss[j]-d,0) }
#Plossum <- max(Ploss-a,0)-max(Ploss-d,0) # getting the loss percentage in the tranche
Lossum <- PlossSum*nn # getting the absolute loss in the tranche
tempplos <- PlossSum[2:length(PlossSum)]-PlossSum[1:length(PlossSum)-1] #
temp2 <- nn * tempplos
temp <- nn*(d-a-PlossSum) # getting the notional left in the tranche
coupon <- tstep*(temp[1:length(temp)-1] + temp[2:length(temp)])/2 # getting the fixed coupons
fl_flows <- discount*temp2 # getting the discounted floating flows
fx_flows <- discount*coupon*c # getting the discounted fixed flows
Vfloat <- sum(fl_flows) # the total floating flows for this path
floattot <- floattot + Vfloat # the total floating flows until now
sqfltot <- sqfltot + (Vfloat^2) # to get the standard error square term
Vfixed <- sum(fx_flows) # the total fixed flows for this path
fixedtot <- fixedtot + Vfixed # the total fixed flows until now
sqfixtot <- sqfixtot + (Vfixed^2) # to get the standard error square term
}
# End MC simulations
result <- 0
#disp('Monte Carlo fixed leg value estimate:')
result[1] <- fixedtot/No
#disp('Monte Carlo floating leg value estimate:')
result[2] <- floattot/No
#disp('Monte Carlo CDO Value estimate:')
#if flag == 1 # if flag is set the buyer's value is displayed
result[3] <- (floattot - fixedtot)/No
#else
#result(1,3) <- (fixedtot - floattot)/No # if flag is not set the seller's value is displayed
#end
#}
#disp('Monte Carlo Breakeven Spread estimate:')
result[4] <- floattot/(fixedtot/c)
#disp('Monte Carlo Fixed leg standard error:')
result[5] <- (1/No) * sqrt(sqfixtot - ((1/No) * (fixedtot^2)))
#disp('Monte Carlo Floating leg standard error:')
result[6] <- (1/No) * sqrt(sqfltot - ((1/No) * (floattot^2)))
# return the breakeven spread ALONE for simulation purposes
return(result)
}
getCDOValue <- function(mktSpread,correl,trancheHigh)
{
# default intensity is set to 0.01 by default
# For base correlation lower tranche is always 0.00
result<-CDOValue(100,0.4,0.01,correl,1,1,0.00,trancheHigh,0.04,10000,1)
return((mktSpread*result[1] - result[2]))
#return(result[1] - result[2])
}
getBaseSpread <- function(prevTranchePV,correl,trancheHigh)
{
# default intensity is set to 0.01 by default
# For base correlation lower tranche is always 0.00
return(CDOBaseSpread(100,0.4,0.01,correl,1,1,0.00,trancheHigh,0.04,10000,1,prevTranchePV))
}
CDOBaseSpread <- function(N,R,lambdaf,rho,n,c,a,d,r,No,flag,prevTranchePV)
{
TM <- 5 # length of the CDO
tstep <- 0.5 # the coupon payments
nn = n * N # the total notional
loss = n * ( 1 - R) # the total loss where R is recovery Rate
T <- seq(0,TM,tstep) # vector for the fixed coupon dates
Tmod <- t(array(rep(T,N), dim=c(length(T),N))) # matrix of fixed coupon dates for all N companies
discount <- exp(-r*T[2:length(T)]) # discounted fixed coupon vector
# just to initialize the generator
MRho <- repmat(rho,N,N) # initializing the correlation matrix
for (i in 1:length(MRho[1,])) { MRho[i,i] <- 1 } # filling diagonal entries with 1
MRho <- chol(MRho) # doing the Cholesky factorization
fixedtot <- 0 # initializing for fixed leg total
floattot <- 0 # initializing for floating leg total
sqfixtot <- 0 # for standard error estimate
sqfltot <- 0 # for standard error estimate
Values <- N * No
PMat <- array(rnorm(Values),dim=c(N,No)) # N rows, No columns
PMat1 <- t(MRho) %*% PMat # to get the correlated Gaussian matrix
PMat11 <- pnorm(PMat1) # take the CDF to make them a copula
PMat2 <- -log(1 - PMat11)/ lambdaf # inverse function to get the default time
# Begin MC simulations
for (i in 1:No) # loop for different paths of MC
{
PMat3 <- PMat2[,i] # getting the i'th path
Pmatmod <-repmat(PMat3,1,2*TM+1) # getting it for the fixed coupon dates
Temp1 <- Pmatmod < Tmod # keeping default times that are only within the CDS maturity
Lmat <- loss*Temp1 # getting the losses matrix by multiplying with the defaults
Tloss <- colSums(Lmat) # summing up the losses
Ploss <- Tloss/nn # getting the loss percentages
PlossSum <- rep(0,length(Tloss))
for (j in 1:length(Ploss))
{ PlossSum[j] <- max(Ploss[j]-a,0) - max(Ploss[j]-d,0) }
#Plossum <- max(Ploss-a,0)-max(Ploss-d,0) # getting the loss percentage in the tranche
Lossum <- PlossSum*nn # getting the absolute loss in the tranche
tempplos <- PlossSum[2:length(PlossSum)]-PlossSum[1:length(PlossSum)-1] #
temp2 <- nn * tempplos
temp <- nn*(d-a-PlossSum) # getting the notional left in the tranche
coupon <- tstep*(temp[1:length(temp)-1] + temp[2:length(temp)])/2 # getting the fixed coupons
fl_flows <- discount*temp2 # getting the discounted floating flows
fx_flows <- discount*coupon*c # getting the discounted fixed flows
Vfloat <- sum(fl_flows) # the total floating flows for this path
floattot <- floattot + Vfloat # the total floating flows until now
sqfltot <- sqfltot + (Vfloat^2) # to get the standard error square term
Vfixed <- sum(fx_flows) # the total fixed flows for this path
fixedtot <- fixedtot + Vfixed # the total fixed flows until now
sqfixtot <- sqfixtot + (Vfixed^2) # to get the standard error square term
}
# End MC simulations
result <- 0
#disp('Monte Carlo fixed leg value estimate:')
result[1] <- fixedtot/No
#disp('Monte Carlo floating leg value estimate:')
result[2] <- floattot/No
#disp('Monte Carlo CDO Value estimate:')
#if flag == 1 # if flag is set the buyer's value is displayed
result[3] <- (floattot - fixedtot)/No
#else
#result(1,3) <- (fixedtot - floattot)/No # if flag is not set the seller's value is displayed
#end
#}
#disp('Monte Carlo Breakeven Spread estimate:')
result[4] <- (floattot + prevTranchePV*No)/(fixedtot/c)
#result[4] <- (floattot)/(fixedtot + prevTranchePV/c)
#disp('Monte Carlo Fixed leg standard error:')
result[5] <- (1/No) * sqrt(sqfixtot - ((1/No) * (fixedtot^2)))
#disp('Monte Carlo Floating leg standard error:')
result[6] <- (1/No) * sqrt(sqfltot - ((1/No) * (floattot^2)))
# return the breakeven spread ALONE for simulation purposes
return(result[4])
}