-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPricingSwaptionLiborMarketModelMCV2.py
216 lines (164 loc) · 6.65 KB
/
PricingSwaptionLiborMarketModelMCV2.py
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
210
211
212
213
214
215
216
'''
Created on Jan 21, 2016
port of following:
http://www.quantcode.com/modules/mydownloads/singlefile.php?lid=63
[1] Linking Caplet and Swaption Volatilities in a BGM/J Framework: Approximate Solutions by Peter Jackel, Riccardo Rebonato
@author: David Dallaire
Purpose is to learn pricing swaptions using Libor Market Model with Monte Carlo
'''
import numpy as np
import math as math
import sys
import random
import csv
def GetForwardRatesAndTerms(P,tau):
theRates = np.zeros(P.shape[0]-1)
theTerms = np.zeros(P.shape[0]-1)
#lose one observation at end
for i in range(0,P.shape[0]-1):
#print(i)
val = (P[i,]/P[i+1,]-1)/tau
theRates[i,] = val
theTerms[i,] = i*tau
#print(val)
return theTerms,theRates
def CreateCorrelation(beta,terms):
numberOfTerms= len(terms)
corrMatrix = np.zeros((numberOfTerms,numberOfTerms))
countX = 0
for i in terms:
countY = 0
for j in terms:
val = math.exp(-beta * abs(i - j))
#print(str(i) + " " + str(j) + " " + str(val))
corrMatrix[countX,countY] = val
countY += 1
countX += 1
return corrMatrix
#equation 19 in [1]
def corr(Tj,Tk,beta):
return math.exp(-beta * abs(Tj - Tk))
#see equation 18 in [1]
def vol(Tj,T_o):
a = -0.05
b = 0.5
c = 1.5
d = 0.15
return ((a + b * (Tj - T_o)) * math.exp(-c * (Tj - T_o)) + d)
def GetSwapRate(F,alpha,beta):
alpha = int(alpha)
beta = int(beta)
#print(alpha)
#print(beta)
tmp = 1
tmpSum = 0
swapRate = 1
for j in range(alpha,beta):
tmp *= (1/(1 + tau * F[j,]))
#Floating Leg
SR = 1 - tmp
for i in range(alpha,beta):
tmp = 1
for j in range(alpha,i+1):
tmp *= (1/(1+tau*F[j,]))
tmpSum += tau * tmp
SR = SR/tmpSum
return SR
def MonteCarloLiborMarketModel(beta,tau,end,swapPrices,numberOfScenarios):
terms,forwardRates = GetForwardRatesAndTerms(swapPrices,tau)
swapRate = GetSwapRate(forwardRates,g_alpha,g_beta)
terms = np.arange(0,end,tau)
corrMatrix = CreateCorrelation(beta,terms)
cholMatrix = (np.linalg.cholesky(corrMatrix)).T
#anti Thetic Flag starts at false
antiTheticFlag = False
timePoints = ((g_alpha * tau) / dt)
rand_nos_mat=np.zeros((timePoints,g_beta)) #store cholRand for use in anti
#initialize arrays
libor_simulations = np.zeros((numberOfScenarios,g_beta))
finalFVec = np.zeros((1,swapPrices.shape[0]))
discountCurve = np.zeros((1,swapPrices.shape[0]))
sqrDt = math.sqrt(dt)
payoff_sum = 0
useRandom = True #This is used for debuging. When false, random numbers are ones.
np.random.seed(100000)
tmpCount = 0
for scenario in range(0,numberOfScenarios):
currLog = np.log(forwardRates)
currLog_t = np.log(forwardRates)
if antiTheticFlag == True:
antiTheticFlag = False
else:
antiTheticFlag = True
mOnes = np.ones((1,14))
t = 0
count = 0
while(t < (g_alpha * tau)):
if(antiTheticFlag):
randNumbers = np.random.randn(g_beta)
if (useRandom):
randChol = np.dot(randNumbers,cholMatrix)
else:
randChol = np.dot(mOnes,cholMatrix)
rand_nos_mat[count,:] = randChol
else:
randChol = -1 * rand_nos_mat[count,:]
nextResetIdx = math.floor(t/tau) + 1
for k in range(int(nextResetIdx),int(g_beta)):
driftSum = 0
for j in range(nextResetIdx,k + 1):
tmp = corr(terms[k],terms[j],beta) * tau * vol(terms[j],t) * math.exp(currLog_t[j])
tmp = tmp / ( 1 + tau * math.exp(currLog_t[j]))
driftSum += tmp
dLogF=0
vol_Tk_t = vol(terms[k],t)
dLogF += vol_Tk_t*driftSum*dt
dLogF -= 0.5*vol_Tk_t*vol_Tk_t * dt
if (useRandom):
dLogF = dLogF + vol_Tk_t * randChol[k] * sqrDt
else:
dLogF += vol_Tk_t*mOnes[0,k] * sqrDt
currLog[k] += dLogF
for i in range(0,currLog_t.shape[0]):
currLog_t[i] = currLog[i]
count += 1
t = t + dt
for i in range(0,int(g_beta)):
libor_simulations[scenario,i] = math.exp(currLog_t[i])
for i in range(0,int(g_beta)):
finalFVec[0,i] = math.exp(currLog_t[i])
discountCurve[0,0] = 1 / (1 + tau * finalFVec[0,0])
for i in range(1,int(g_beta)):
discountCurve[0,i] = discountCurve[0,i-1] / (1 + tau * finalFVec[0,i])
payoff = 0
for i in range(int(g_alpha),int(g_beta)):
payoff += (swapRate - finalFVec[0,i]) * tau * discountCurve[0,i]
payoff_sum += max(payoff,0)
return 100*payoff_sum/numberOfScenarios
if __name__=='__main__':
numberOfScenarios = 1000
start = 2.5 #in years
end = 7 #in years
print("Swap start = " + str(start) + " year")
print("Swap end = " + str(end) + " year")
beta = 0.1
tau = 0.5 #term
g_alpha = start / tau
g_beta = end / tau
dt = 0.1
swapPrices = np.array([1.000000,0.969514,0.939441,0.909913,0.881024,0.852807,0.825482,0.799100,
0.773438,0.749042,0.725408,0.702527,0.680361,0.659402,0.639171,0.619580,
0.600668,0.582455,0.564873,0.547888,0.531492,0.515651,0.500360,0.485543,
0.471240,0.457861,0.444977,0.432554,0.420575,0.409019,0.397888,0.387341,
0.377196,0.367435,0.358056,0.348978,0.340292,0.331614,0.323265,0.315460,
0.307945,0.300321])
#print(P.shape)
#flatCurve = np.array([1.000000,0.966736,0.934579,0.903492,0.873439,0.844385,0.816298,
# 0.789145,0.762895,0.737519,0.712986,0.689270,0.666342,0.644177,
# 0.622750,0.602035,0.582009,0.562649,0.543934,0.525841,0.508349,
# 0.491440,0.475093,0.459290,0.444012,0.429243,0.414964,0.401161,
# 0.387817,0.374917,0.362446,0.350390,0.338735,0.327467,0.316574,
# 0.306044,0.295864,0.286022,0.276508,0.267311,0.258419,0.249823])
#print(flatCurve.shape)
swaptionPrice = MonteCarloLiborMarketModel(beta,tau,end,swapPrices,numberOfScenarios)
print("swaption price = " + str(swaptionPrice))