def loadHomogeneousSpreadCurves(valuationDate, liborCurve, cdsSpread3Y, cdsSpread5Y, cdsSpread7Y, cdsSpread10Y, numCredits): maturity3Y = valuationDate.nextCDSDate(36) maturity5Y = valuationDate.nextCDSDate(60) maturity7Y = valuationDate.nextCDSDate(84) maturity10Y = valuationDate.nextCDSDate(120) recoveryRate = 0.40 cds3Y = FinCDS(valuationDate, maturity3Y, cdsSpread3Y) cds5Y = FinCDS(valuationDate, maturity5Y, cdsSpread5Y) cds7Y = FinCDS(valuationDate, maturity7Y, cdsSpread7Y) cds10Y = FinCDS(valuationDate, maturity10Y, cdsSpread10Y) contracts = [cds3Y, cds5Y, cds7Y, cds10Y] issuerCurve = FinCDSCurve(valuationDate, contracts, liborCurve, recoveryRate) issuerCurves = [] for _ in range(0, numCredits): issuerCurves.append(issuerCurve) return issuerCurves
def loadHeterogeneousSpreadCurves(valuationDate, liborCurve): maturity3Y = valuationDate.nextCDSDate(36) maturity5Y = valuationDate.nextCDSDate(60) maturity7Y = valuationDate.nextCDSDate(84) maturity10Y = valuationDate.nextCDSDate(120) path = os.path.join(os.path.dirname(__file__), './/data//CDX_NA_IG_S7_SPREADS.csv') f = open(path, 'r') data = f.readlines() f.close() issuerCurves = [] for row in data[1:]: splitRow = row.split(",") spd3Y = float(splitRow[1]) / 10000.0 spd5Y = float(splitRow[2]) / 10000.0 spd7Y = float(splitRow[3]) / 10000.0 spd10Y = float(splitRow[4]) / 10000.0 recoveryRate = float(splitRow[5]) cds3Y = FinCDS(valuationDate, maturity3Y, spd3Y) cds5Y = FinCDS(valuationDate, maturity5Y, spd5Y) cds7Y = FinCDS(valuationDate, maturity7Y, spd7Y) cds10Y = FinCDS(valuationDate, maturity10Y, spd10Y) cdsContracts = [cds3Y, cds5Y, cds7Y, cds10Y] issuerCurve = FinCDSCurve(valuationDate, cdsContracts, liborCurve, recoveryRate) issuerCurves.append(issuerCurve) return issuerCurves
def test_IssuerCurveBuild(): ''' Test issuer curve build with simple libor curve to isolate cds curve building time cost. ''' valuationDate = FinDate(20, 6, 2018) times = np.linspace(0.0, 10.0, 11) r = 0.05 discountFactors = np.power((1.0 + r), -times) dates = valuationDate.addYears(times) liborCurve = FinDiscountCurve(valuationDate, dates, discountFactors, FinInterpTypes.FLAT_FWD_RATES) recoveryRate = 0.40 cdsContracts = [] cdsCoupon = 0.005 # 50 bps maturityDate = valuationDate.addMonths(12) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsContracts.append(cds) cdsCoupon = 0.0055 maturityDate = valuationDate.addMonths(24) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsContracts.append(cds) cdsCoupon = 0.0060 maturityDate = valuationDate.addMonths(36) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsContracts.append(cds) cdsCoupon = 0.0065 maturityDate = valuationDate.addMonths(60) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsContracts.append(cds) cdsCoupon = 0.0070 maturityDate = valuationDate.addMonths(84) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsContracts.append(cds) cdsCoupon = 0.0073 maturityDate = valuationDate.addMonths(120) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsContracts.append(cds) issuerCurve = FinCDSCurve(valuationDate, cdsContracts, liborCurve, recoveryRate) return cdsContracts, issuerCurve
def buildFlatIssuerCurve(tradeDate, liborCurve, spread, recoveryRate): valuationDate = tradeDate.addDays(1) cdsMarketContracts = [] maturityDate = FinDate(29, 6, 2010) cds = FinCDS(valuationDate, maturityDate, spread) cdsMarketContracts.append(cds) issuerCurve = FinCDSCurve(valuationDate, cdsMarketContracts, liborCurve, recoveryRate) return issuerCurve
def buildIssuerCurve(valuationDate, liborCurve): cdsMarketContracts = [] cdsCoupon = 0.0048375 maturityDate = FinDate(2010, 6, 29) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) recoveryRate = 0.40 issuerCurve = FinCDSCurve(valuationDate, cdsMarketContracts, liborCurve, recoveryRate) return issuerCurve
def test_CDSFastApproximation(): valueDate = FinDate(2018, 6, 20) # I build a discount curve that requires no bootstrap times = np.linspace(0, 10.0, 11) r = 0.05 discountFactors = np.power((1.0 + r), -times) dates = valueDate.addYears(times) liborCurve = FinDiscountCurve(valueDate, dates, discountFactors, FinInterpTypes.FLAT_FWD_RATES) ########################################################################## maturityDate = valueDate.nextCDSDate(120) t = (maturityDate - valueDate) / 365.242 z = liborCurve.df(maturityDate) r = -np.log(z) / t recoveryRate = 0.40 contractCoupon = 0.010 testCases.header("MKT_SPD", "EXACT_VALUE", "APPROX_VALUE", "DIFF(%NOT)") for mktCoupon in np.linspace(0.000, 0.05, 21): cdsContracts = [] cdsMkt = FinCDS(valueDate, maturityDate, mktCoupon, ONE_MILLION) cdsContracts.append(cdsMkt) issuerCurve = FinCDSCurve(valueDate, cdsContracts, liborCurve, recoveryRate) cdsContract = FinCDS(valueDate, maturityDate, contractCoupon) v_exact = cdsContract.value( valueDate, issuerCurve, recoveryRate)['full_pv'] v_approx = cdsContract.valueFastApprox( valueDate, r, mktCoupon, recoveryRate)[0] pctdiff = (v_exact - v_approx) / ONE_MILLION * 100.0 testCases.print(mktCoupon * 10000, v_exact, v_approx, pctdiff)
def test_CDSIndexPortfolio(): tradeDate = FinDate(1, 8, 2007) stepInDate = tradeDate.addDays(1) valuationDate = stepInDate liborCurve = buildIborCurve(tradeDate) maturity3Y = tradeDate.nextCDSDate(36) maturity5Y = tradeDate.nextCDSDate(60) maturity7Y = tradeDate.nextCDSDate(84) maturity10Y = tradeDate.nextCDSDate(120) path = os.path.join(os.path.dirname(__file__), './/data//CDX_NA_IG_S7_SPREADS.csv') f = open(path, 'r') data = f.readlines() f.close() issuerCurves = [] for row in data[1:]: splitRow = row.split(",") spd3Y = float(splitRow[1]) / 10000.0 spd5Y = float(splitRow[2]) / 10000.0 spd7Y = float(splitRow[3]) / 10000.0 spd10Y = float(splitRow[4]) / 10000.0 recoveryRate = float(splitRow[5]) cds3Y = FinCDS(stepInDate, maturity3Y, spd3Y) cds5Y = FinCDS(stepInDate, maturity5Y, spd5Y) cds7Y = FinCDS(stepInDate, maturity7Y, spd7Y) cds10Y = FinCDS(stepInDate, maturity10Y, spd10Y) cdsContracts = [cds3Y, cds5Y, cds7Y, cds10Y] issuerCurve = FinCDSCurve(valuationDate, cdsContracts, liborCurve, recoveryRate) issuerCurves.append(issuerCurve) ########################################################################## # Now determine the average spread of the index ########################################################################## cdsIndex = FinCDSIndexPortfolio() averageSpd3Y = cdsIndex.averageSpread(valuationDate, stepInDate, maturity3Y, issuerCurves) * 10000.0 averageSpd5Y = cdsIndex.averageSpread(valuationDate, stepInDate, maturity5Y, issuerCurves) * 10000.0 averageSpd7Y = cdsIndex.averageSpread(valuationDate, stepInDate, maturity7Y, issuerCurves) * 10000.0 averageSpd10Y = cdsIndex.averageSpread(valuationDate, stepInDate, maturity10Y, issuerCurves) * 10000.0 testCases.header("LABEL", "VALUE") testCases.print("AVERAGE SPD 3Y", averageSpd3Y) testCases.print("AVERAGE SPD 5Y", averageSpd5Y) testCases.print("AVERAGE SPD 7Y", averageSpd7Y) testCases.print("AVERAGE SPD 10Y", averageSpd10Y) ########################################################################## # Now determine the intrinsic spread of the index to the same maturity # dates. As the single name CDS contracts ########################################################################## cdsIndex = FinCDSIndexPortfolio() intrinsicSpd3Y = cdsIndex.intrinsicSpread( valuationDate, stepInDate, maturity3Y, issuerCurves) * 10000.0 intrinsicSpd5Y = cdsIndex.intrinsicSpread( valuationDate, stepInDate, maturity5Y, issuerCurves) * 10000.0 intrinsicSpd7Y = cdsIndex.intrinsicSpread( valuationDate, stepInDate, maturity7Y, issuerCurves) * 10000.0 intrinsicSpd10Y = cdsIndex.intrinsicSpread( valuationDate, stepInDate, maturity10Y, issuerCurves) * 10000.0 ########################################################################## ########################################################################## testCases.header("LABEL", "VALUE") testCases.print("INTRINSIC SPD 3Y", intrinsicSpd3Y) testCases.print("INTRINSIC SPD 5Y", intrinsicSpd5Y) testCases.print("INTRINSIC SPD 7Y", intrinsicSpd7Y) testCases.print("INTRINSIC SPD 10Y", intrinsicSpd10Y)
def test_FinCDSCurve(): curveDate = FinDate(2018, 12, 20) swaps = [] depos = [] fras = [] fixedDCC = FinDayCountTypes.ACT_365F fixedFreq = FinFrequencyTypes.SEMI_ANNUAL fixedCoupon = 0.05 for i in range(1, 11): maturityDate = curveDate.addMonths(12 * i) swap = FinIborSwap(curveDate, maturityDate, FinSwapTypes.PAYER, fixedCoupon, fixedFreq, fixedDCC) swaps.append(swap) libor_curve = FinIborCurve(curveDate, depos, fras, swaps) cdsContracts = [] for i in range(1, 11): maturityDate = curveDate.addMonths(12 * i) cds = FinCDS(curveDate, maturityDate, 0.005 + 0.001 * (i - 1)) cdsContracts.append(cds) issuerCurve = FinCDSCurve(curveDate, cdsContracts, libor_curve, recoveryRate=0.40, useCache=False) testCases.header("T", "Q") n = len(issuerCurve._times) for i in range(0, n): testCases.print(issuerCurve._times[i], issuerCurve._values[i]) testCases.header("CONTRACT", "VALUE") for i in range(1, 11): maturityDate = curveDate.addMonths(12 * i) cds = FinCDS(curveDate, maturityDate, 0.005 + 0.001 * (i - 1)) v = cds.value(curveDate, issuerCurve) testCases.print(i, v) if 1 == 0: x = [0.0, 1.2, 1.6, 1.7, 10.0] qs = issuerCurve.survProb(x) print("===>", qs) x = [0.3, 1.2, 1.6, 1.7, 10.0] xx = np.array(x) qs = issuerCurve.survProb(xx) print("===>", qs) x = [0.3, 1.2, 1.6, 1.7, 10.0] dfs = issuerCurve.df(x) print("===>", dfs) x = [0.3, 1.2, 1.6, 1.7, 10.0] xx = np.array(x) dfs = issuerCurve.df(xx) print("===>", dfs)
def buildFullIssuerCurve(valuationDate): dcType = FinDayCountTypes.ACT_360 depos = [] irBump = 0.0 m = 1.0 # 0.00000000000 spotDays = 0 settlementDate = valuationDate.addDays(spotDays) maturityDate = settlementDate.addMonths(1) depo1 = FinIborDeposit(settlementDate, maturityDate, m * 0.0016, dcType) maturityDate = settlementDate.addMonths(2) depo2 = FinIborDeposit(settlementDate, maturityDate, m * 0.0020, dcType) maturityDate = settlementDate.addMonths(3) depo3 = FinIborDeposit(settlementDate, maturityDate, m * 0.0024, dcType) maturityDate = settlementDate.addMonths(6) depo4 = FinIborDeposit(settlementDate, maturityDate, m * 0.0033, dcType) maturityDate = settlementDate.addMonths(12) depo5 = FinIborDeposit(settlementDate, maturityDate, m * 0.0056, dcType) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) fras = [] spotDays = 2 settlementDate = valuationDate.addDays(spotDays) swaps = [] dcType = FinDayCountTypes.THIRTY_E_360_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL maturityDate = settlementDate.addMonths(24) swap1 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0044 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0078 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0119 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0158 + irBump, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0192 + irBump, fixedFreq, dcType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0219 + irBump, fixedFreq, dcType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0242 + irBump, fixedFreq, dcType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0261 + irBump, fixedFreq, dcType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0276 + irBump, fixedFreq, dcType) swaps.append(swap9) liborCurve = FinIborCurve(valuationDate, depos, fras, swaps) cdsMarketContracts = [] cdsCoupon = 0.005743 maturityDate = valuationDate.nextCDSDate(6) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.007497 maturityDate = valuationDate.nextCDSDate(12) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.011132 maturityDate = valuationDate.nextCDSDate(24) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.013932 maturityDate = valuationDate.nextCDSDate(36) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.015764 maturityDate = valuationDate.nextCDSDate(48) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.017366 maturityDate = valuationDate.nextCDSDate(60) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.020928 maturityDate = valuationDate.nextCDSDate(84) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.022835 maturityDate = valuationDate.nextCDSDate(120) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) recoveryRate = 0.40 issuerCurve = FinCDSCurve(valuationDate, cdsMarketContracts, liborCurve, recoveryRate) return liborCurve, issuerCurve
def test_performCDSIndexHazardRateAdjustment(): tradeDate = FinDate(2007, 8, 1) stepInDate = tradeDate.addDays(1) valuationDate = stepInDate liborCurve = buildIborCurve(tradeDate) maturity3Y = tradeDate.nextCDSDate(36) maturity5Y = tradeDate.nextCDSDate(60) maturity7Y = tradeDate.nextCDSDate(84) maturity10Y = tradeDate.nextCDSDate(120) path = dirname(__file__) filename = "CDX_NA_IG_S7_SPREADS.csv" full_filename_path = join(path, "data", filename) f = open(full_filename_path, 'r') data = f.readlines() issuerCurves = [] for row in data[1:]: splitRow = row.split(",") spd3Y = float(splitRow[1]) / 10000.0 spd5Y = float(splitRow[2]) / 10000.0 spd7Y = float(splitRow[3]) / 10000.0 spd10Y = float(splitRow[4]) / 10000.0 recoveryRate = float(splitRow[5]) cds3Y = FinCDS(stepInDate, maturity3Y, spd3Y) cds5Y = FinCDS(stepInDate, maturity5Y, spd5Y) cds7Y = FinCDS(stepInDate, maturity7Y, spd7Y) cds10Y = FinCDS(stepInDate, maturity10Y, spd10Y) cdsContracts = [cds3Y, cds5Y, cds7Y, cds10Y] issuerCurve = FinCDSCurve(valuationDate, cdsContracts, liborCurve, recoveryRate) issuerCurves.append(issuerCurve) ########################################################################## # Now determine the average spread of the index ########################################################################## cdsIndex = FinCDSIndexPortfolio() averageSpd3Y = cdsIndex.averageSpread(valuationDate, stepInDate, maturity3Y, issuerCurves) * 10000.0 averageSpd5Y = cdsIndex.averageSpread(valuationDate, stepInDate, maturity5Y, issuerCurves) * 10000.0 averageSpd7Y = cdsIndex.averageSpread(valuationDate, stepInDate, maturity7Y, issuerCurves) * 10000.0 averageSpd10Y = cdsIndex.averageSpread(valuationDate, stepInDate, maturity10Y, issuerCurves) * 10000.0 testCases.header("LABEL", "VALUE") testCases.print("AVERAGE SPD 3Y", averageSpd3Y) testCases.print("AVERAGE SPD 5Y", averageSpd5Y) testCases.print("AVERAGE SPD 7Y", averageSpd7Y) testCases.print("AVERAGE SPD 10Y", averageSpd10Y) testCases.banner( "===================================================================") ########################################################################## # Now determine the intrinsic spread of the index to the same maturity dates # As the single name CDS contracts ########################################################################## cdsIndex = FinCDSIndexPortfolio() intrinsicSpd3Y = cdsIndex.intrinsicSpread( valuationDate, stepInDate, maturity3Y, issuerCurves) * 10000.0 intrinsicSpd5Y = cdsIndex.intrinsicSpread( valuationDate, stepInDate, maturity5Y, issuerCurves) * 10000.0 intrinsicSpd7Y = cdsIndex.intrinsicSpread( valuationDate, stepInDate, maturity7Y, issuerCurves) * 10000.0 intrinsicSpd10Y = cdsIndex.intrinsicSpread( valuationDate, stepInDate, maturity10Y, issuerCurves) * 10000.0 ########################################################################## ########################################################################## testCases.header("LABEL", "VALUE") testCases.print("INTRINSIC SPD 3Y", intrinsicSpd3Y) testCases.print("INTRINSIC SPD 5Y", intrinsicSpd5Y) testCases.print("INTRINSIC SPD 7Y", intrinsicSpd7Y) testCases.print("INTRINSIC SPD 10Y", intrinsicSpd10Y) testCases.banner( "===================================================================") ########################################################################## ########################################################################## indexCoupons = [0.002, 0.0037, 0.0050, 0.0063] indexUpfronts = [0.0, 0.0, 0.0, 0.0] indexMaturityDates = [ FinDate(2009, 12, 20), FinDate(2011, 12, 20), FinDate(2013, 12, 20), FinDate(2016, 12, 20) ] indexRecoveryRate = 0.40 tolerance = 1e-6 import time start = time.time() indexPortfolio = FinCDSIndexPortfolio() adjustedIssuerCurves = indexPortfolio.hazardRateAdjustIntrinsic( valuationDate, issuerCurves, indexCoupons, indexUpfronts, indexMaturityDates, indexRecoveryRate, tolerance) end = time.time() testCases.header("TIME") testCases.print(end - start) # numCredits = len(issuerCurves) # testCases.print("#","MATURITY","CDS_UNADJ","CDS_ADJ") # for m in range(0,numCredits): # for cds in cdsContracts: # unadjustedSpread = cds.parSpread(valuationDate,issuerCurves[m]) # adjustedSpread = cds.parSpread(valuationDate,adjustedIssuerCurves[m]) # testCases.print(m,str(cds._maturityDate),"%10.3f"%(unadjustedSpread*10000),"%10.3f" %(adjustedSpread*10000)) cdsIndex = FinCDSIndexPortfolio() intrinsicSpd3Y = cdsIndex.intrinsicSpread(valuationDate, stepInDate, indexMaturityDates[0], adjustedIssuerCurves) * 10000.0 intrinsicSpd5Y = cdsIndex.intrinsicSpread(valuationDate, stepInDate, indexMaturityDates[1], adjustedIssuerCurves) * 10000.0 intrinsicSpd7Y = cdsIndex.intrinsicSpread(valuationDate, stepInDate, indexMaturityDates[2], adjustedIssuerCurves) * 10000.0 intrinsicSpd10Y = cdsIndex.intrinsicSpread(valuationDate, stepInDate, indexMaturityDates[3], adjustedIssuerCurves) * 10000.0 # If the adjustment works then this should equal the index spreads testCases.header("LABEL", "VALUE") testCases.print("ADJUSTED INTRINSIC SPD 3Y", intrinsicSpd3Y) testCases.print("ADJUSTED INTRINSIC SPD 5Y", intrinsicSpd5Y) testCases.print("ADJUSTED INTRINSIC SPD 7Y", intrinsicSpd7Y) testCases.print("ADJUSTED INTRINSIC SPD 10Y", intrinsicSpd10Y)
def test_fullPriceCDSIndexOption(): tradeDate = FinDate(1, 8, 2007) stepInDate = tradeDate.addDays(1) valuationDate = stepInDate liborCurve = buildIborCurve(tradeDate) maturity3Y = tradeDate.nextCDSDate(36) maturity5Y = tradeDate.nextCDSDate(60) maturity7Y = tradeDate.nextCDSDate(84) maturity10Y = tradeDate.nextCDSDate(120) path = os.path.join(os.path.dirname(__file__), './/data//CDX_NA_IG_S7_SPREADS.csv') f = open(path, 'r') data = f.readlines() f.close() issuerCurves = [] for row in data[1:]: splitRow = row.split(",") creditName = splitRow[0] spd3Y = float(splitRow[1]) / 10000.0 spd5Y = float(splitRow[2]) / 10000.0 spd7Y = float(splitRow[3]) / 10000.0 spd10Y = float(splitRow[4]) / 10000.0 recoveryRate = float(splitRow[5]) cds3Y = FinCDS(stepInDate, maturity3Y, spd3Y) cds5Y = FinCDS(stepInDate, maturity5Y, spd5Y) cds7Y = FinCDS(stepInDate, maturity7Y, spd7Y) cds10Y = FinCDS(stepInDate, maturity10Y, spd10Y) cdsContracts = [cds3Y, cds5Y, cds7Y, cds10Y] issuerCurve = FinCDSCurve(valuationDate, cdsContracts, liborCurve, recoveryRate) issuerCurves.append(issuerCurve) ########################################################################## ########################################################################## indexUpfronts = [0.0, 0.0, 0.0, 0.0] indexMaturityDates = [ FinDate(20, 12, 2009), FinDate(20, 12, 2011), FinDate(20, 12, 2013), FinDate(20, 12, 2016) ] indexRecovery = 0.40 testCases.banner( "======================= CDS INDEX OPTION ==========================") indexCoupon = 0.004 volatility = 0.50 expiryDate = FinDate(1, 2, 2008) maturityDate = FinDate(20, 12, 2011) notional = 10000.0 tolerance = 1e-6 testCases.header("TIME", "STRIKE", "INDEX", "PAY", "RECEIVER", "G(K)", "X", "EXPH", "ABPAY", "ABREC") for index in np.linspace(20, 60, 10): ####################################################################### cdsContracts = [] for dt in indexMaturityDates: cds = FinCDS(valuationDate, dt, index / 10000.0) cdsContracts.append(cds) indexCurve = FinCDSCurve(valuationDate, cdsContracts, liborCurve, indexRecovery) if 1 == 1: indexSpreads = [index / 10000.0] * 4 indexPortfolio = FinCDSIndexPortfolio() adjustedIssuerCurves = indexPortfolio.hazardRateAdjustIntrinsic( valuationDate, issuerCurves, indexSpreads, indexUpfronts, indexMaturityDates, indexRecovery, tolerance) else: indexSpread = index / 10000.0 issuerCurve = buildFlatIssuerCurve(tradeDate, liborCurve, indexSpread, indexRecovery) adjustedIssuerCurves = [] for iCredit in range(0, 125): adjustedIssuerCurves.append(issuerCurve) ####################################################################### for strike in np.linspace(20, 60, 20): start = time.time() option = FinCDSIndexOption(expiryDate, maturityDate, indexCoupon, strike / 10000.0, notional) v_pay_1, v_rec_1, strikeValue, mu, expH = option.valueAnderson( valuationDate, adjustedIssuerCurves, indexRecovery, volatility) end = time.time() elapsed = end - start end = time.time() v_pay_2, v_rec_2 = option.valueAdjustedBlack( valuationDate, indexCurve, indexRecovery, liborCurve, volatility) elapsed = end - start testCases.print(elapsed, strike, index, v_pay_1, v_rec_1, strikeValue, mu, expH, v_pay_2, v_rec_2)
def buildFullIssuerCurve2(mktSpreadBump, irBump): # https://www.markit.com/markit.jsp?jsppage=pv.jsp # YIELD CURVE 20 August 2020 SNAP AT 1600 m = 1.0 settlementDate = FinDate(24, 8, 2020) dcType = FinDayCountTypes.ACT_360 depos = [] maturityDate = settlementDate.addMonths(1) depo1 = FinLiborDeposit(settlementDate, maturityDate, m * 0.001709, dcType) maturityDate = settlementDate.addMonths(2) depo2 = FinLiborDeposit(settlementDate, maturityDate, m * 0.002123, dcType) maturityDate = settlementDate.addMonths(3) depo3 = FinLiborDeposit(settlementDate, maturityDate, m * 0.002469, dcType) maturityDate = settlementDate.addMonths(6) depo4 = FinLiborDeposit(settlementDate, maturityDate, m * 0.003045, dcType) maturityDate = settlementDate.addMonths(12) depo5 = FinLiborDeposit(settlementDate, maturityDate, m * 0.004449, dcType) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) swaps = [] dcType = FinDayCountTypes.THIRTY_E_360_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL maturityDate = settlementDate.addMonths(24) swap1 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.002155 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.002305 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.002665 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.003290 + irBump, fixedFreq, dcType) swaps.append(swap4) liborCurve = FinLiborCurve(settlementDate, depos, [], swaps) cdsCoupon = 0.01 + mktSpreadBump cdsMarketContracts = [] effectiveDate = FinDate(21, 8, 2020) cds = FinCDS(effectiveDate, "6M", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "1Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "2Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "3Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "4Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "5Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "7Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "10Y", cdsCoupon) cdsMarketContracts.append(cds) recoveryRate = 0.40 issuerCurve = FinCDSCurve(settlementDate, cdsMarketContracts, liborCurve, recoveryRate) testCases.header("DATE", "DISCOUNT_FACTOR", "SURV_PROB") years = np.linspace(0.0, 10.0, 20) dates = settlementDate.addYears(years) for dt in dates: df = liborCurve.df(dt) q = issuerCurve.survProb(dt) testCases.print("%16s" % dt, "%12.8f" % df, "%12.8f" % q) return liborCurve, issuerCurve
def buildFullIssuerCurve1(mktSpreadBump, irBump): # https://www.markit.com/markit.jsp?jsppage=pv.jsp # YIELD CURVE 8-AUG-2019 SNAP AT 1600 tradeDate = FinDate(2019, 8, 9) valuationDate = tradeDate.addDays(1) dcType = FinDayCountTypes.ACT_360 depos = [] m = 1.0 # 0.00000000000 spotDays = 2 settlementDate = valuationDate.addDays(spotDays) maturityDate = settlementDate.addMonths(1) depo1 = FinLiborDeposit(settlementDate, maturityDate, m * 0.022009, dcType) maturityDate = settlementDate.addMonths(2) depo2 = FinLiborDeposit(settlementDate, maturityDate, m * 0.022138, dcType) maturityDate = settlementDate.addMonths(3) depo3 = FinLiborDeposit(settlementDate, maturityDate, m * 0.021810, dcType) maturityDate = settlementDate.addMonths(6) depo4 = FinLiborDeposit(settlementDate, maturityDate, m * 0.020503, dcType) maturityDate = settlementDate.addMonths(12) depo5 = FinLiborDeposit(settlementDate, maturityDate, m * 0.019930, dcType) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) fras = [] swaps = [] dcType = FinDayCountTypes.THIRTY_E_360_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL maturityDate = settlementDate.addMonths(24) swap1 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.015910 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.014990 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.014725 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.014640 + irBump, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.014800 + irBump, fixedFreq, dcType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.014995 + irBump, fixedFreq, dcType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.015180 + irBump, fixedFreq, dcType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.015610 + irBump, fixedFreq, dcType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.015880 + irBump, fixedFreq, dcType) swaps.append(swap9) maturityDate = settlementDate.addMonths(144) swap10 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.016430 + irBump, fixedFreq, dcType) swaps.append(swap10) liborCurve = FinLiborCurve(settlementDate, depos, fras, swaps) cdsMarketContracts = [] cdsCoupon = 0.04 + mktSpreadBump maturityDate = valuationDate.nextCDSDate(6) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(12) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(24) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(36) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(48) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(60) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(84) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(120) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(180) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) recoveryRate = 0.40 issuerCurve = FinCDSCurve(settlementDate, cdsMarketContracts, liborCurve, recoveryRate) return liborCurve, issuerCurve