Loading of Libraries and Classes
In [1]:
%matplotlib inline
import numpy as np
import pandas as pd
from datetime import datetime, timedelta, date
import matplotlib.pyplot as plt
from MonteCarloSimulators.CIR.CIRMCSim import MC_CIR_Sim
from Scheduler.Scheduler import Scheduler
from Products.Credit.CDS import CDS
from Curves.OIS.OIS import OIS
from Calibration.CIR_calibrate import CIRLambdaCalibration
from Products.Credit.CDS_Swaption import CDSSwaption
from Products.Credit.CDS_exposure import CDS_exposure
Parameters
In [ ]:
option_start = date(2010,1,10)
cds_start = date(2011,1,10)
cds_end = date(2016,1,10)
Recovery = 0.4
initial_lambda = 0.03
# initial_guess = np.array([1,1,0.13])
initial_guess = np.array([1,0.1,0.03])
market_tenorList = np.array([6/12,1,2,3,4,5,7,10])
# market_spread = 0.0001*np.array([39.3, 40.40, 71.465, 130, 202.25, 265.433, 346.885,
378.250])
# market_spread = 0.0001*np.array([43.2, 40.40, 71.465, 130, 202.25, 265.433, 346.885,
378.250])
# market_spread = 0.0001*np.array([39.3, 40.40, 71.465, 143, 202.25, 265.433, 346.885,
378.250])
market_spread = 0.0001*np.array([39.3, 40.40, 71.465, 130, 202.25, 265.433, 346.885, 41
6.075])
# market_spread = 0.0001 * np.array([12.1, 13.6, 20.0, 24.8, 31.1, 35, 72.9, 121.1])
t_step = 1.0/365
simNumber = 30
————————————————————————–
–
ModuleNotFoundError t) |
Traceback (most recent call las |
<ipython-input-1-ec000c344b83> in <module>
4 from datetime import datetime, timedelta, date
5 import matplotlib.pyplot as plt
—-> 6 from MonteCarloSimulators.CIR.CIRMCSim import MC_CIR_Sim
7 from Scheduler.Scheduler import Scheduler
8 from Products.Credit.CDS import CDS
ModuleNotFoundError: No module named ‘MonteCarloSimulators’
OIS
In [ ]:
myois = OIS(option_start, cds_end)
mymthois = myois.getDiscountFactor()
df1 = pd.DataFrame(mymthois.loc[option_start], index=mymthois.columns)
df2 = pd.DataFrame(mymthois.loc[cds_start], index=mymthois.columns)
df3 = pd.DataFrame(mymthois.loc[cds_end], index=mymthois.columns)
df4 = pd.concat([df1,df2,df3],axis=1)
df_plot = df4.plot()
df_plot.set_xlabel(‘tenor (months)’)
df_plot.set_ylabel(‘discount rate’)
df_plot.set_title(‘OIS discount Rate’)
Calibrate hazard rate using CIR
In [ ]:
MyCal = CIRLambdaCalibration(option_start, cds_end)
MyPara = MyCal.getCalibratedParameters(initial_lambda, initial_guess, market_tenorList,
market_spread)
MyPara
Hazard rate and Survival curve single path
simulation
In [ ]:
Mysim_SinglePath = MC_CIR_Sim()
initial_guess_new = np.append(MyPara, [initial_lambda])
Mysim_SinglePath.setCIR(option_start, cds_end, initial_guess_new, 1, t_step)
MyQ_SinglePath = Mysim_SinglePath.getSurvival_daily()
hazard_SinglePath = Mysim_SinglePath.getHazard_daily()
hazard_graph_S = hazard_SinglePath.plot(legend=False)
hazard_graph_S.set_xlabel(‘Year’)
hazard_graph_S.set_ylabel(‘Hazard Rate’)
hazard_graph_S.set_title(‘Simulated Hazard Rate (Single Path)’)
MyQ_graph_S = MyQ_SinglePath.plot(legend=False)
MyQ_graph_S.set_xlabel(‘Year’)
MyQ_graph_S.set_ylabel(‘Survival Probability’)
MyQ_graph_S.set_title(‘Survival Curve (Single Path)’)
Hazard rate and Survival curve multi paths simulation
(Sim_Number = 30)
In [ ]:
Mysim = MC_CIR_Sim()
initial_guess_new = np.append(MyPara, [initial_lambda])
Mysim.setCIR(option_start, cds_end, initial_guess_new, simNumber, t_step)
MyQ = Mysim.getSurvival_daily()
hazard = Mysim.getHazard_daily()
# MyQ
In [ ]:
hazard_graph = hazard.plot(legend=False)
hazard_graph.set_xlabel(‘Year’)
hazard_graph.set_ylabel(‘Hazard Rate’)
hazard_graph.set_title(‘Simulated Hazard Rate’)
MyQ_graph = MyQ.plot(legend=False)
MyQ_graph.set_xlabel(‘Year’)
MyQ_graph.set_ylabel(‘Survival Probability’)
MyQ_graph.set_title(‘Survival Curve’)
Pricing default swaption
In [ ]:
MySwaptionPayer_price = np.array([])
MySwaptionReceiver_price = np.array([])
strikes = np.array([0,100,200,300,400,500,600])*0.0001
for i in strikes:
MySwaptionPayer = CDSSwaption(option_start, cds_start, cds_end, i, “3M”, 1, Recover
y, MyQ)
MySwaptionReceiver = CDSSwaption(option_start, cds_start, cds_end, i, “3M”, 0, Reco
very, MyQ)
#tempSwaptionValuePayer = tempSwaptionPayer.getCDSSwaption(i)
#tempSwaptionValueReceiver = tempSwaptionReceiver.getCDSSwaption(i)
tempPayer = MySwaptionPayer.getCDSSwaption(i)
tempReceiver = MySwaptionReceiver.getCDSSwaption(i)
MySwaptionPayer_price = np.append(MySwaptionPayer_price, tempPayer)
MySwaptionReceiver_price = np.append(MySwaptionReceiver_price, tempReceiver)
In [ ]:
Swaption_prices = np.array([MySwaptionPayer_price, MySwaptionReceiver_price])
Swaption_prices = pd.DataFrame(data=Swaption_prices.transpose(), index=strikes*10000, c
olumns=[“payer”,“receiver”])
Swaption_prices_Graph = Swaption_prices.plot()
Swaption_prices_Graph.set_xlabel(‘Strikes (bp)’)
Swaption_prices_Graph.set_ylabel(”)
Swaption_prices_Graph.set_title(‘Prices of Default Swaption’)
In [ ]:
Swaption_prices
exposures for CDS
In [ ]:
Mysim_exposure = MC_CIR_Sim()
initial_guess_new = np.append(MyPara, [initial_lambda])
# initial_guess_new = np.append(initial_guess, [initial_lambda])
Mysim_exposure.setCIR(option_start, cds_end, initial_guess_new, 30, t_step)
MyQ_exposure = Mysim_exposure.getSurvival_daily()
myexposure = CDS_exposure(MyQ_exposure, cds_start, cds_end, “3M”, 1, recovery=0.4)
Exposure = [myexposure.getEE(),myexposure.getEPE(), myexposure.getENE(), myexposure.get
PFE(0.9), myexposure.getPFE(0.1)]
Exp = pd.concat(Exposure,axis=1)
Exp.plot()
In [ ]:
myexposure.getCVA()
In [ ]:
myexposure.getParspread_single(MyQ_exposure[0])
In [ ]:
myexposure.getParspread_ave()