-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtestSABRModelStaticSmile.py
More file actions
103 lines (93 loc) · 3.27 KB
/
testSABRModelStaticSmile.py
File metadata and controls
103 lines (93 loc) · 3.27 KB
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
import numpy as np
import matplotlib.pyplot as plt
import pandas
from QuantLibWrapper.SabrModel import SabrModel
from QuantLibWrapper.MCSimulation import MCSimulation
# SabrModel( S(t), T, alpha, beta, nu, rho )
model1 = SabrModel(0.05,1.0,0.0100,0.0001,0.0001,0.0,shift=0.1)
model2 = SabrModel(0.05,1.0,0.0450,0.5000,0.0001,0.0,shift=0.1)
model3 = SabrModel(0.05,1.0,0.0405,0.5000,0.5000,0.0,shift=0.1)
model4 = SabrModel(0.05,1.0,0.0420,0.5000,0.5000,0.7,shift=0.1)
# ATM calibration
print(model1.calibrateATM(0.01), model2.calibrateATM(0.01), model3.calibrateATM(0.01), model4.calibrateATM(0.01))
# Strikes
strikes = [ (i+1)/1000 for i in range(100) ]
# implied volatility
vols1 = [model1.normalVolatility(strike) for strike in strikes]
vols2 = [model2.normalVolatility(strike) for strike in strikes]
vols3 = [model3.normalVolatility(strike) for strike in strikes]
vols4 = [model4.normalVolatility(strike) for strike in strikes]
# implied density
dens1 = [model1.density(strike) for strike in strikes]
dens2 = [model2.density(strike) for strike in strikes]
dens3 = [model3.density(strike) for strike in strikes]
dens4 = [model4.density(strike) for strike in strikes]
plt.figure()
plt.plot(strikes,vols1, 'b-', label='Normal')
plt.plot(strikes,vols2, 'r-', label='CEV')
plt.plot(strikes,vols3, 'g-', label='CEV+SV')
plt.plot(strikes,vols4, 'y-', label='CEV+SV+Corr')
plt.legend()
plt.xlabel('Strike')
plt.ylabel('Normal Volatility')
plt.figure()
plt.plot(strikes,dens1, 'b-', label='Normal')
plt.plot(strikes,dens2, 'r-', label='CEV')
plt.plot(strikes,dens3, 'g-', label='CEV+SV')
plt.plot(strikes,dens4, 'y-', label='CEV+SV+Corr')
plt.legend()
plt.xlabel('Rate')
plt.ylabel('Density')
plt.show()
table = pandas.DataFrame( [ strikes, vols1, vols2, vols3, vols4 ] ).T
table.columns = [ 'Strikes', 'Normal', 'CEV', 'CEV+SV', 'CEV+SV+Corr' ]
# print(table)
table.to_csv('SABRVolsAnalytic.csv')
# MC simulation
print('Start MC simulation...')
times = np.array([k*0.01 for k in range(501)])
nPaths = 10000
mcSim1 = MCSimulation(model1,times,nPaths)
print('.')
mcSim2 = MCSimulation(model2,times,nPaths)
print('.')
mcSim3 = MCSimulation(model3,times,nPaths)
print('.')
mcSim4 = MCSimulation(model4,times,nPaths)
print('Done.')
# payoffs and normal vols
print('Start MC payoff calculation...')
mcStrikes = np.array([ (i+1)/100 for i in range(10) ])
mcVols1 = model1.monteCarloImpliedNormalVol(mcSim1,mcStrikes)
print('.')
mcVols2 = model2.monteCarloImpliedNormalVol(mcSim2,mcStrikes)
print('.')
mcVols3 = model3.monteCarloImpliedNormalVol(mcSim3,mcStrikes)
print('.')
mcVols4 = model4.monteCarloImpliedNormalVol(mcSim4,mcStrikes)
print('Done.')
# output
plt.figure()
#
plt.plot(strikes,vols1, 'b-', label='Normal')
plt.plot(mcStrikes,mcVols1, 'b*')
#
plt.plot(strikes,vols2, 'r-', label='CEV')
plt.plot(mcStrikes,mcVols2, 'r*')
#
plt.plot(strikes,vols3, 'g-', label='CEV+SV')
plt.plot(mcStrikes,mcVols3, 'g*')
#
plt.plot(strikes,vols4, 'y-', label='CEV+SV+Corr')
plt.plot(mcStrikes,mcVols4, 'y*')
#
plt.legend()
plt.xlabel('Strike')
plt.ylabel('Normal Volatility')
plt.xlim((0.0, 0.101))
plt.ylim((0.005, 0.025))
plt.show()
table = pandas.DataFrame( [ mcStrikes, mcVols1, mcVols2, mcVols3, mcVols4 ] ).T
table.columns = [ 'Strikes', 'Normal', 'CEV', 'CEV+SV', 'CEV+SV+Corr' ]
# print(table)
table.to_csv('SABRVolsMonteCarlo.csv')