def testIborSwaptionModels(): ########################################################################## # COMPARISON OF MODELS ########################################################################## valuation_date = Date(1, 1, 2011) libor_curve = test_ibor_depositsAndSwaps(valuation_date) exercise_date = Date(1, 1, 2012) swapMaturityDate = Date(1, 1, 2017) swapFixedFrequencyType = FrequencyTypes.SEMI_ANNUAL swapFixedDayCountType = DayCountTypes.ACT_365F strikes = np.linspace(0.02, 0.08, 5) testCases.header("LAB", "STRIKE", "BLK", "BLK_SHFT", "SABR", "SABR_SHFT", "HW", "BK") model1 = Black(0.00001) model2 = BlackShifted(0.00001, 0.0) model3 = SABR(0.013, 0.5, 0.5, 0.5) model4 = SABRShifted(0.013, 0.5, 0.5, 0.5, -0.008) model5 = HWTree(0.00001, 0.00001) model6 = BKTree(0.01, 0.01) settlement_date = valuation_date.add_weekdays(2) for k in strikes: swaptionType = SwapTypes.PAY swaption = IborSwaption(settlement_date, exercise_date, swapMaturityDate, swaptionType, k, swapFixedFrequencyType, swapFixedDayCountType) swap1 = swaption.value(valuation_date, libor_curve, model1) swap2 = swaption.value(valuation_date, libor_curve, model2) swap3 = swaption.value(valuation_date, libor_curve, model3) swap4 = swaption.value(valuation_date, libor_curve, model4) swap5 = swaption.value(valuation_date, libor_curve, model5) swap6 = swaption.value(valuation_date, libor_curve, model6) testCases.print("PAY", k, swap1, swap2, swap3, swap4, swap5, swap6) testCases.header("LABEL", "STRIKE", "BLK", "BLK_SHFTD", "SABR", "SABR_SHFTD", "HW", "BK") for k in strikes: swaptionType = SwapTypes.RECEIVE swaption = IborSwaption(settlement_date, exercise_date, swapMaturityDate, swaptionType, k, swapFixedFrequencyType, swapFixedDayCountType) swap1 = swaption.value(valuation_date, libor_curve, model1) swap2 = swaption.value(valuation_date, libor_curve, model2) swap3 = swaption.value(valuation_date, libor_curve, model3) swap4 = swaption.value(valuation_date, libor_curve, model4) swap5 = swaption.value(valuation_date, libor_curve, model5) swap6 = swaption.value(valuation_date, libor_curve, model6) testCases.print("REC", k, swap1, swap2, swap3, swap4, swap5, swap6)
def test_FinShiftedSABR(): strikes = np.linspace(-0.006, 0.016, 10) alpha = 0.013345 beta = 0.5 rho = 0.46698 nu = 0.49861 shift = 0.008 model = SABRShifted(alpha, beta, rho, nu, shift) f = 0.0006384 T = 1.0 vols = model.black_vol(f, strikes, T) if PLOT_GRAPHS: plt.figure() plt.plot(strikes, vols) plt.title("SHIFTED SABR")
def test_FinShiftedSABRSimple(): strikes = np.linspace(0.01, 0.06, 10) alpha = 0.060277 beta = 0.5 rho = 0.2097 nu = 0.75091 model1 = SABRShifted(alpha, beta, rho, nu, 0.0) alpha = 0.058484 beta = 0.5 rho = 0.20568 nu = 0.79647 model2 = SABRShifted(alpha, beta, rho, nu, 0.0) f = 0.0350 T = 1.0 vols1 = model1.black_vol(f, strikes, T) vols2 = model2.black_vol(f, strikes, T) if PLOT_GRAPHS: plt.figure() plt.plot(strikes, vols1) plt.plot(strikes, vols2) plt.title("Shifted SIMPLE SABR")
def test_IborSwaptionQLExample(): valuation_date = Date(4, 3, 2014) settlement_date = Date(4, 3, 2014) depoDCCType = DayCountTypes.THIRTY_E_360_ISDA depos = [] depo = IborDeposit(settlement_date, "1W", 0.0023, depoDCCType) depos.append(depo) depo = IborDeposit(settlement_date, "1M", 0.0023, depoDCCType) depos.append(depo) depo = IborDeposit(settlement_date, "3M", 0.0023, depoDCCType) depos.append(depo) depo = IborDeposit(settlement_date, "6M", 0.0023, depoDCCType) depos.append(depo) # No convexity correction provided so I omit interest rate futures swaps = [] accType = DayCountTypes.ACT_365F fixedFreqType = FrequencyTypes.SEMI_ANNUAL fixed_leg_type = SwapTypes.PAY swap = IborSwap(settlement_date, "3Y", fixed_leg_type, 0.00790, fixedFreqType, accType) swaps.append(swap) swap = IborSwap(settlement_date, "4Y", fixed_leg_type, 0.01200, fixedFreqType, accType) swaps.append(swap) swap = IborSwap(settlement_date, "5Y", fixed_leg_type, 0.01570, fixedFreqType, accType) swaps.append(swap) swap = IborSwap(settlement_date, "6Y", fixed_leg_type, 0.01865, fixedFreqType, accType) swaps.append(swap) swap = IborSwap(settlement_date, "7Y", fixed_leg_type, 0.02160, fixedFreqType, accType) swaps.append(swap) swap = IborSwap(settlement_date, "8Y", fixed_leg_type, 0.02350, fixedFreqType, accType) swaps.append(swap) swap = IborSwap(settlement_date, "9Y", fixed_leg_type, 0.02540, fixedFreqType, accType) swaps.append(swap) swap = IborSwap(settlement_date, "10Y", fixed_leg_type, 0.0273, fixedFreqType, accType) swaps.append(swap) swap = IborSwap(settlement_date, "15Y", fixed_leg_type, 0.0297, fixedFreqType, accType) swaps.append(swap) swap = IborSwap(settlement_date, "20Y", fixed_leg_type, 0.0316, fixedFreqType, accType) swaps.append(swap) swap = IborSwap(settlement_date, "25Y", fixed_leg_type, 0.0335, fixedFreqType, accType) swaps.append(swap) swap = IborSwap(settlement_date, "30Y", fixed_leg_type, 0.0354, fixedFreqType, accType) swaps.append(swap) libor_curve = IborSingleCurve(valuation_date, depos, [], swaps, InterpTypes.LINEAR_ZERO_RATES) exercise_date = settlement_date.add_tenor("5Y") swapMaturityDate = exercise_date.add_tenor("5Y") swapFixedCoupon = 0.040852 swapFixedFrequencyType = FrequencyTypes.SEMI_ANNUAL swapFixedDayCountType = DayCountTypes.THIRTY_E_360_ISDA swapFloatFrequencyType = FrequencyTypes.QUARTERLY swapFloatDayCountType = DayCountTypes.ACT_360 swapNotional = 1000000 swaptionType = SwapTypes.PAY swaption = IborSwaption(settlement_date, exercise_date, swapMaturityDate, swaptionType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType, swapNotional, swapFloatFrequencyType, swapFloatDayCountType) testCases.header("MODEL", "VALUE") model = Black(0.1533) v = swaption.value(settlement_date, libor_curve, model) testCases.print(model.__class__, v) model = BlackShifted(0.1533, -0.008) v = swaption.value(settlement_date, libor_curve, model) testCases.print(model.__class__, v) model = SABR(0.132, 0.5, 0.5, 0.5) v = swaption.value(settlement_date, libor_curve, model) testCases.print(model.__class__, v) model = SABRShifted(0.352, 0.5, 0.15, 0.15, -0.005) v = swaption.value(settlement_date, libor_curve, model) testCases.print(model.__class__, v) model = HWTree(0.010000000, 0.00000000001) v = swaption.value(settlement_date, libor_curve, model) testCases.print(model.__class__, v)
def test_FinIborCapFloor(): todayDate = Date(20, 6, 2019) valuation_date = todayDate start_date = todayDate.add_weekdays(2) maturity_date = start_date.add_tenor("1Y") libor_curve = test_ibor_depositsAndSwaps(todayDate) # The capfloor has begun # lastFixing = 0.028 ########################################################################## # COMPARISON OF MODELS ########################################################################## strikes = np.linspace(0.02, 0.08, 5) testCases.header("LABEL", "STRIKE", "BLK", "BLK_SHFTD", "SABR", "SABR_SHFTD", "HW", "BACH") model1 = Black(0.20) model2 = BlackShifted(0.25, 0.0) model3 = SABR(0.013, 0.5, 0.5, 0.5) model4 = SABRShifted(0.013, 0.5, 0.5, 0.5, -0.008) model5 = HWTree(0.30, 0.01) model6 = Bachelier(0.01) for k in strikes: capFloorType = FinCapFloorTypes.CAP capfloor = IborCapFloor(start_date, maturity_date, capFloorType, k) cvalue1 = capfloor.value(valuation_date, libor_curve, model1) cvalue2 = capfloor.value(valuation_date, libor_curve, model2) cvalue3 = capfloor.value(valuation_date, libor_curve, model3) cvalue4 = capfloor.value(valuation_date, libor_curve, model4) cvalue5 = capfloor.value(valuation_date, libor_curve, model5) cvalue6 = capfloor.value(valuation_date, libor_curve, model6) testCases.print("CAP", k, cvalue1, cvalue2, cvalue3, cvalue4, cvalue5, cvalue6) testCases.header("LABEL", "STRIKE", "BLK", "BLK_SHFTD", "SABR", "SABR_SHFTD", "HW", "BACH") for k in strikes: capFloorType = FinCapFloorTypes.FLOOR capfloor = IborCapFloor(start_date, maturity_date, capFloorType, k) fvalue1 = capfloor.value(valuation_date, libor_curve, model1) fvalue2 = capfloor.value(valuation_date, libor_curve, model2) fvalue3 = capfloor.value(valuation_date, libor_curve, model3) fvalue4 = capfloor.value(valuation_date, libor_curve, model4) fvalue5 = capfloor.value(valuation_date, libor_curve, model5) fvalue6 = capfloor.value(valuation_date, libor_curve, model6) testCases.print("FLR", k, fvalue1, fvalue2, fvalue3, fvalue4, fvalue5, fvalue6) ############################################################################### # PUT CALL CHECK ############################################################################### testCases.header("LABEL", "STRIKE", "BLK", "BLK_SHFTD", "SABR", "SABR SHFTD", "HW", "BACH") for k in strikes: capFloorType = FinCapFloorTypes.CAP capfloor = IborCapFloor(start_date, maturity_date, capFloorType, k) cvalue1 = capfloor.value(valuation_date, libor_curve, model1) cvalue2 = capfloor.value(valuation_date, libor_curve, model2) cvalue3 = capfloor.value(valuation_date, libor_curve, model3) cvalue4 = capfloor.value(valuation_date, libor_curve, model4) cvalue5 = capfloor.value(valuation_date, libor_curve, model5) cvalue6 = capfloor.value(valuation_date, libor_curve, model6) capFloorType = FinCapFloorTypes.FLOOR capfloor = IborCapFloor(start_date, maturity_date, capFloorType, k) fvalue1 = capfloor.value(valuation_date, libor_curve, model1) fvalue2 = capfloor.value(valuation_date, libor_curve, model2) fvalue3 = capfloor.value(valuation_date, libor_curve, model3) fvalue4 = capfloor.value(valuation_date, libor_curve, model4) fvalue5 = capfloor.value(valuation_date, libor_curve, model5) fvalue6 = capfloor.value(valuation_date, libor_curve, model6) pcvalue1 = cvalue1 - fvalue1 pcvalue2 = cvalue2 - fvalue2 pcvalue3 = cvalue3 - fvalue3 pcvalue4 = cvalue4 - fvalue4 pcvalue5 = cvalue5 - fvalue5 pcvalue6 = cvalue6 - fvalue6 testCases.print("PUT_CALL", k, pcvalue1, pcvalue2, pcvalue3, pcvalue4, pcvalue5, pcvalue6)
def test_ShiftedSABR(): testCases.header("TEST", "CALIBRATION ERROR") alpha = 0.0 beta = 0.5 rho = -0.09 nu = 0.1 shift = 0.02 strikeVol = 0.1 f = 0.043 k = 0.050 r = 0.03 texp = 2.0 call_optionType = FinOptionTypes.EUROPEAN_CALL put_optionType = FinOptionTypes.EUROPEAN_PUT df = np.exp(-r * texp) # SABR equivalent to lognormal (Black) model (i.e. beta = 1, rho = 0, nu = 0, shift = 0) modelSABR_01 = SABRShifted(0.0, 1.0, 0.0, 0.0, 0.0) modelSABR_01.set_alpha_from_black_vol(strikeVol, f, k, texp) impliedLognormalVol = modelSABR_01.black_vol(f, k, texp) impliedATMLognormalVol = modelSABR_01.black_vol(k, k, texp) impliedLognormalSmile = impliedLognormalVol - impliedATMLognormalVol assert impliedLognormalSmile == 0.0, "In lognormal model, smile should be flat" calibrationError = round(strikeVol - impliedLognormalVol, 12) testCases.print("LOGNORMAL CASE", calibrationError) # Volatility: pure SABR dynamics modelSABR_02 = SABRShifted(alpha, beta, rho, nu, shift) modelSABR_02.set_alpha_from_black_vol(strikeVol, f, k, texp) impliedLognormalVol = modelSABR_02.black_vol(f, k, texp) impliedATMLognormalVol = modelSABR_02.black_vol(k, k, texp) impliedLognormalSmile = impliedLognormalVol - impliedATMLognormalVol calibrationError = round(strikeVol - impliedLognormalVol, 12) testCases.print("SABR CASE", calibrationError) # Valuation: pure SABR dynamics valueCall = modelSABR_02.value(f, k, texp, df, call_optionType) valuePut = modelSABR_02.value(f, k, texp, df, put_optionType) assert round((valueCall - valuePut), 12) == round(df*(f - k), 12), \ "The method called 'value()' doesn't comply with Call-Put parity"
libor_curve = IborSingleCurve(valuation_date, depos, fras, swaps) return libor_curve todayDate = Date(20, 6, 2019) valuation_date = todayDate start_date = todayDate.add_weekdays(2) maturity_date = start_date.add_tenor("1Y") libor_curve = build_curve(todayDate) model1 = Black(0.20) model2 = BlackShifted(0.25, 0.0) model3 = SABR(0.013, 0.5, 0.5, 0.5) model4 = SABRShifted(0.013, 0.5, 0.5, 0.5, -0.008) model5 = HWTree(0.30, 0.01) model6 = Bachelier(0.01) def test_cap(): capFloorType = FinCapFloorTypes.CAP k = 0.02 capfloor = IborCapFloor(start_date, maturity_date, capFloorType, k) cvalue1 = capfloor.value(valuation_date, libor_curve, model1) cvalue2 = capfloor.value(valuation_date, libor_curve, model2) cvalue3 = capfloor.value(valuation_date, libor_curve, model3) cvalue4 = capfloor.value(valuation_date, libor_curve, model4) cvalue5 = capfloor.value(valuation_date, libor_curve, model5) cvalue6 = capfloor.value(valuation_date, libor_curve, model6)