from unittest import TestCase from parameters import xR, simNumber, t_step, trim_start, trim_end, freq, periods, referenceDate, start, inArrears, WORKING_DIR from MonteCarloSimulators.Vasicek.vasicekMCSim import MC_Vasicek_Sim from Scheduler.Scheduler import Scheduler mySchedule = Scheduler() # Global Variables mySchedule = mySchedule.getSchedule(start=trim_start, end=trim_end, freq="M") mySimulator = MC_Vasicek_Sim(datelist=mySchedule, x=xR, simNumber=simNumber, t_step=t_step) mySimulator.getLibor() mySimulator.getSmallLibor() a = 1 class TestMC_Vasicek_Sim(TestCase): def test_getLibor(self): mySimulator.getLibor() def test_getSmallLibor(self): # Monte Carlo trajectories creation - R Libor = mySimulator.getSmallLibor(mySchedule) return Libor
#SDE parameter t_step = 1.0/365 r0 = 0.08 sigmaR = 0.09 sigmaRef = 0.03 muR = 0.05 alphaR=3.0 simNumber=1000 muHazardRate = 0.005 k = 0.1 recovery = 0.4 #Bond parameters coupon = 0.08 #Monte Carlo trajectories creation t1 = datetime.datetime.now() myVasicek = MC_Vasicek_Sim(datelist, r0,sigmaR, sigmaRef, muR, muHazardRate,alphaR, k, simNumber,t_step) longLibor = myVasicek.getLibor() libor = myVasicek.getSmallLibor() longsurvival = myVasicek.getSurvival() survival = myVasicek.getSmallSurvival() #myVasicek.saveMeExcel() #Bond Pricing myBond = bond(libor,coupon,datelist,survival, recovery) myCDS = CDS(libor, datelist, survival, recovery) print('Risky Bond Price = ', str(1000*myBond.pv().sum())) print('Riskless Bond Price = ', str(1000*myBond.riskless().sum() )) print ('5 years Par Spread for each quarter= ', str(myCDS.parSpread())) print ('5 years Mark to Market Value for each quarter = ', myCDS.MTM())
for i in range(len(startDates)): notional = (-1.0)**i myPortfolio[i] = CouponBond(fee=1.0, start=startDates[i], coupon=coupon, notional=notional, maturity=maturities[i], freq="3M", referencedate=referenceDate) portfolioScheduleOfCF = set(ReferenceDateList) for i in range(len(myPortfolio)): portfolioScheduleOfCF = portfolioScheduleOfCF.union( myPortfolio[i].getScheduleComplete()[0]) portfolioScheduleOfCF = sorted(portfolioScheduleOfCF.union(ReferenceDateList)) OIS = myVasicek.getSmallLibor(datelist=portfolioScheduleOfCF) # at this point OIS contains all dates for which the discount curve should be known. # If the OIS doesn't contain that date, it would not be able to discount the cashflows and the calcualtion would faill. pvs = {} for t in portfolioScheduleOfCF: pvs[t] = np.zeros([1, simNumber]) for i in range(len(myPortfolio)): myPortfolio[i].setLibor(OIS) pvs[t] = pvs[t] + myPortfolio[i].getExposure(referencedate=t).values pvsPlot = pd.DataFrame.from_dict(list(pvs.items())) pvsPlot.index = list(pvs.keys()) pvs1 = {} for i, t in zip(pvsPlot.values, pvsPlot.index): pvs1[t] = i[1][0]
return results.x def fCurve(self, x): calCurve = self.getLiborAvg(x, self.datelist) thisPV = np.multiple(self.cashFlows, calcCurve).mean(axis=1).sum(axis=0) error = 1e4 * (self.price - thisPV)**2 return if (__name__ == "__main__"): coupon = 0.03 myscheduler = Scheduler() datelist = myscheduler(start=trim_start, end=trim_end, freq="3M", referencedate=trim_start) myMC = MC_Vasicek_Sim(x=XR, datelist=datelist, simNumber=simnumber, t_step=t_step) libor = myMC.getSmallLibor(datelist=datelist) myBond = Bond(libor=libor, start=trim_start, maturity=trim_end, coupon=coupon, freq="3M", referencedate=trim_start) myPV = myBond.PV() print(myPV)
myBond = CouponBond(fee=fee, start=start, maturity=maturity, coupon=coupon, freq="3M", referencedate=referenceDate, observationdate=observationdate) fulllist, datelist = myBond.getScheduleComplete() myMC = MC_Vasicek_Sim() myMC.setVasicek(x=xR, minDay=minDay, maxDay=maxDay, simNumber=simNumber, t_step=t_step) myMC.getLibor() libor = myMC.getSmallLibor(datelist=fulllist) myBond.setLibor(libor) class TestBond(TestCase): def test01_PV(self): myPV = myBond.getPV(referencedate=referenceDate) print(myPV) print(myBond.pv) def test00_getLiborAvg(self): print(myBond.getLiborAvg(yieldIn=0.05, datelist=datelist)) def test02_getYield(self): x = [] y = []
myBond = CouponBond(fee=fee, start=start, maturity=maturity, coupon=coupon, freq="3M", referencedate=referenceDate, observationdate=observationdate) fulllist, datelist = myBond.getScheduleComplete() myMC = MC_Vasicek_Sim(datelist=[minDay, maxDay], x=xR, simNumber=simNumber, t_step=t_step) #myMC.setVasicek(x=xR, minDay=minDay, maxDay=maxDay, simNumber=simNumber, t_step=t_step) myMC.getLibor() libor = myMC.getSmallLibor(tenors=fulllist) myBond.setLibor(libor) class TestBond(TestCase): def test01_PV(self): myPV = myBond.getPV(referencedate=referenceDate) print(myPV) print(myBond.pv) def test00_getLiborAvg(self): print(myBond.getLiborAvg(yieldIn=0.05, datelist=datelist)) def test02_getYield(self): x = [] y = []