def test_flat_hazard_with_quote(self): Settings.instance().evaluation_date = self.todays_date hazard_rate = SimpleQuote() flat_curve = FlatHazardRate(2, self.calendar, hazard_rate, Actual365Fixed()) for h in [0.01, 0.02, 0.03]: hazard_rate.value = h self.assertAlmostEqual(flat_curve.survival_probability(self.d), math.exp(-h * flat_curve.time_from_reference(self.d)))
def test_create_flat_hazard(self): Settings.instance().evaluation_date = self.todays_date flat_curve = FlatHazardRate(2, self.calendar, 0.05, Actual365Fixed()) flat_curve_from_reference_date = FlatHazardRate.from_reference_date( self.calendar.advance(self.todays_date, 2, Days), 0.05, Actual365Fixed()) self.assertIsNotNone(flat_curve) self.assertIsNotNone(flat_curve_from_reference_date) self.assertEqual(flat_curve.time_from_reference(self.d), flat_curve_from_reference_date.time_from_reference(self.d)) self.assertAlmostEqual(flat_curve.hazard_rate(self.d), 0.05) self.assertAlmostEqual(flat_curve.survival_probability(self.d), math.exp(-0.05*flat_curve.time_from_reference(self.d)))
def example02(): print("example 2:\n") todays_date = Date(25, 9, 2014) Settings.instance().evaluation_date = todays_date calendar = TARGET() term_date = calendar.adjust(todays_date + Period(2, Years), Following) cds_schedule = Schedule(todays_date, term_date, Period(Quarterly), WeekendsOnly(), ModifiedFollowing, ModifiedFollowing, date_generation_rule=Rule.CDS) for date in cds_schedule: print(date) print() todays_date = Date(21, 10, 2014) Settings.instance().evaluation_date = todays_date quotes = [0.00006, 0.00045, 0.00081, 0.001840, 0.00256, 0.00337] tenors = [1, 2, 3, 6, 9, 12] deps = [DepositRateHelper(q, Period(t, Months), 2, calendar, ModifiedFollowing, False, Actual360()) for q, t in zip(quotes, tenors)] tenors = [2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20, 30] quotes = [0.00223, 0.002760, 0.003530, 0.004520, 0.005720, 0.007050, 0.008420, 0.009720, 0.010900, 0.012870, 0.014970, 0.017, 0.01821] swaps = [SwapRateHelper.from_tenor(q, Period(t, Years), calendar, Annual, ModifiedFollowing, Thirty360(), Euribor6M(), SimpleQuote(0)) for q, t in zip(quotes, tenors)] helpers = deps + swaps YC = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount, Interpolator.LogLinear, todays_date, helpers, Actual365Fixed()) YC.extrapolation = True print("ISDA rate curve:") for h in helpers: print("{0}: {1:.6f}\t{2:.6f}".format(h.latest_date, YC.zero_rate(h.latest_date, Actual365Fixed(), 2).rate, YC.discount(h.latest_date))) defaultTs0 = FlatHazardRate(0, WeekendsOnly(), 0.016739207493630, Actual365Fixed()) cds_schedule = Schedule.from_rule(Date(22, 9, 2014), Date(20, 12, 2019), Period(3, Months), WeekendsOnly(), Following, Unadjusted, Rule.CDS, False) nominal = 100000000 trade = CreditDefaultSwap(Side.Buyer, nominal, 0.01, cds_schedule, Following, Actual360(), True, True, Date(22, 10, 2014), Actual360(True), True) engine = IsdaCdsEngine(defaultTs0, 0.4, YC, False) trade.set_pricing_engine(engine) print("reference trade NPV = {0}\n".format(trade.npv))
def setUp(self): calendar = TARGET() today_date = today() Settings().evaluation_date = today_date hazard_rate = SimpleQuote(0.01234) probability_curve = FlatHazardRate(0, calendar, hazard_rate, Actual360()) discount_curve = FlatForward(today_date, 0.06, Actual360()) issue_date = today_date #calendar.advance(today_date, -1, Years) maturity = calendar.advance(issue_date, 10, Years) self.convention = Following self.schedule = Schedule(issue_date, maturity, Period("3M"), calendar, self.convention, self.convention, Rule.TwentiethIMM) recovery_rate = 0.4 self.engine = MidPointCdsEngine(probability_curve, recovery_rate, discount_curve, True)
class CreditDefaultSwapTest(unittest.TestCase): calendar = TARGET() today_date = today() Settings().evaluation_date = today_date hazard_rate = SimpleQuote(0.01234) probability_curve = FlatHazardRate(0, calendar, hazard_rate, Actual360()) discount_curve = FlatForward(today_date, 0.06, Actual360()) issue_date = today_date #calendar.advance(today_date, -1, Years) maturity = calendar.advance(issue_date, 10, Years) convention = Following schedule = Schedule(issue_date, maturity, Period("3M"), calendar, convention, convention, TwentiethIMM) recovery_rate = 0.4 engine = MidPointCdsEngine(probability_curve, recovery_rate, discount_curve, True) def test_fair_spread(self): fixed_rate = 0.001 day_count = Actual360() notional = 10000 cds = CreditDefaultSwap(SELLER, notional, fixed_rate, self.schedule, self.convention, day_count, True, True) cds.set_pricing_engine(self.engine) fair_rate = cds.fair_spread fair_cds = CreditDefaultSwap(SELLER, notional, fair_rate, self.schedule, self.convention, day_count, True, True) fair_cds.set_pricing_engine(self.engine) self.assertAlmostEqual(fair_cds.npv, 0.) def test_fair_upfront(self): fixed_rate = 0.05 upfront = 0.001 day_count = Actual360() notional = 10000 cds = CreditDefaultSwap.from_upfront(SELLER, notional, upfront, fixed_rate, self.schedule, self.convention, day_count, True, True) cds.set_pricing_engine(self.engine) fair_upfront = cds.fair_upfront fair_cds = CreditDefaultSwap.from_upfront(SELLER, notional, fair_upfront, fixed_rate, self.schedule, self.convention, day_count, True, True) fair_cds.set_pricing_engine(self.engine) self.assertAlmostEqual(fair_cds.npv, 0.) # same with null upfront upfront = 0. cds2 = CreditDefaultSwap.from_upfront(SELLER, notional, upfront, fixed_rate, self.schedule, self.convention, day_count, True, True) cds2.set_pricing_engine(self.engine) fair_upfront2 = cds.fair_upfront fair_cds2 = CreditDefaultSwap.from_upfront(SELLER, notional, fair_upfront, fixed_rate, self.schedule, self.convention, day_count, True, True) fair_cds2.set_pricing_engine(self.engine) self.assertAlmostEqual(fair_cds2.npv, 0.)