def test_analytic_versus_black(self): settlement_date = today() self.settings.evaluation_date = settlement_date daycounter = ActualActual() exercise_date = settlement_date + 6 * Months payoff = PlainVanillaPayoff(Put, 30) exercise = EuropeanExercise(exercise_date) risk_free_ts = flat_rate(0.1, daycounter) dividend_ts = flat_rate(0.04, daycounter) s0 = SimpleQuote(32.0) v0 = 0.05 kappa = 5.0 theta = 0.05 sigma = 1.0e-4 rho = 0.0 process = HestonProcess( risk_free_ts, dividend_ts, s0, v0, kappa, theta, sigma, rho ) option = VanillaOption(payoff, exercise) engine = AnalyticHestonEngine(HestonModel(process), 144) option.set_pricing_engine(engine) calculated = option.net_present_value year_fraction = daycounter.year_fraction( settlement_date, exercise_date ) forward_price = 32 * np.exp((0.1 - 0.04) * year_fraction) expected = blackFormula( payoff.type, payoff.strike, forward_price, np.sqrt(0.05 * year_fraction) ) * np.exp(-0.1 * year_fraction) tolerance = 2.0e-7 self.assertAlmostEqual( calculated, expected, delta=tolerance )
def setUp(self): settlement_date = today() settings = Settings() settings.evaluation_date = settlement_date daycounter = ActualActual() self.calendar = NullCalendar() i_rate = .1 i_div = .04 self.risk_free_ts = flat_rate(i_rate, daycounter) self.dividend_ts = flat_rate(i_div, daycounter) self.s0 = SimpleQuote(32.0) # Bates model self.v0 = 0.05 self.kappa = 5.0 self.theta = 0.05 self.sigma = 1.0e-4 self.rho = 0.0 self.Lambda = .1 self.nu = .01 self.delta = .001
def _blsimpv(price, spot, strike, risk_free_rate, time, option_type='Call', dividend=0.0): spot = SimpleQuote(spot) daycounter = ActualActual() risk_free_ts = FlatForward(today(), risk_free_rate, daycounter) dividend_ts = FlatForward(today(), dividend, daycounter) volatility_ts = BlackConstantVol(today(), NullCalendar(), .3, daycounter) process = BlackScholesMertonProcess(spot, dividend_ts, risk_free_ts, volatility_ts) exercise_date = today() + Period(time * 365, Days) exercise = EuropeanExercise(exercise_date) payoff = PlainVanillaPayoff(option_type, strike) option = EuropeanOption(payoff, exercise) engine = AnalyticEuropeanEngine(process) option.set_pricing_engine(engine) accuracy = 0.001 max_evaluations = 1000 min_vol = 0.01 max_vol = 2 vol = option.implied_volatility(price, process, accuracy, max_evaluations, min_vol, max_vol) return vol
def test_extrapolation(self): rate_helpers = build_helpers() settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) print('dt Obs: %s\ndt Eval: %s\ndt Settle: %s' % (dtObs, eval_date, settlement_date)) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance) # max_date raises an exception without extrapolaiton... self.assertFalse(ts.extrapolation) with self.assertRaisesRegexp( RuntimeError, "1st iteration: failed at 2nd alive instrument"): dtMax = ts.max_date
def test_zero_curve(self): rate_helpers = build_helpers() settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance) # max_date raises an exception... ts.extrapolation = True zr = ts.zero_rate(Date(10, 5, 2027), ts_day_counter, 2) self.assertAlmostEqual(zr.rate, 0.0539332)
def test_smith(self): # test against result published in # Journal of Computational Finance Vol. 11/1 Fall 2007 # An almost exact simulation method for the heston model settlement_date = today() self.settings.evaluation_date = settlement_date daycounter = ActualActual() timeToMaturity = 4 exercise_date = settlement_date + timeToMaturity * 365 c_payoff = PlainVanillaPayoff(Call, 100) exercise = EuropeanExercise(exercise_date) risk_free_ts = flat_rate(0., daycounter) dividend_ts = flat_rate(0., daycounter) s0 = SimpleQuote(100.0) v0 = 0.0194 kappa = 1.0407 theta = 0.0586 sigma = 0.5196 rho = -.6747 nb_steps_a = 100 nb_paths = 20000 seed = 12347 process = HestonProcess(risk_free_ts, dividend_ts, s0, v0, kappa, theta, sigma, rho, QUADRATICEXPONENTIAL) model = HestonModel(process) option = VanillaOption(c_payoff, exercise) engine = AnalyticHestonEngine(model, 144) option.set_pricing_engine(engine) price_fft = option.net_present_value engine = MCEuropeanHestonEngine(process, antithetic_variate=True, steps_per_year=nb_steps_a, required_samples=nb_paths, seed=seed) option.set_pricing_engine(engine) price_mc = option.net_present_value expected = 15.1796 tolerance = .05 self.assertAlmostEqual(price_fft, expected, delta=tolerance) self.assertAlmostEqual(price_mc, expected, delta=tolerance)
def test_analytic_versus_black(self): settlement_date = today() self.settings.evaluation_date = settlement_date daycounter = ActualActual() exercise_date = settlement_date + 6 * Months payoff = PlainVanillaPayoff(Put, 30) exercise = EuropeanExercise(exercise_date) risk_free_ts = flat_rate(0.1, daycounter) dividend_ts = flat_rate(0.04, daycounter) s0 = SimpleQuote(32.0) v0 = 0.05 kappa = 5.0 theta = 0.05 sigma = 1.0e-4 rho = 0.0 process = HestonProcess(risk_free_ts, dividend_ts, s0, v0, kappa, theta, sigma, rho) option = VanillaOption(payoff, exercise) engine = AnalyticHestonEngine(HestonModel(process), 144) option.set_pricing_engine(engine) calculated = option.net_present_value year_fraction = daycounter.year_fraction(settlement_date, exercise_date) forward_price = 32 * np.exp((0.1 - 0.04) * year_fraction) expected = blackFormula(payoff.type, payoff.strike, forward_price, np.sqrt(0.05 * year_fraction)) * np.exp( -0.1 * year_fraction) tolerance = 2.0e-7 self.assertAlmostEqual(calculated, expected, delta=tolerance)
def test_creation(self): settings = Settings() # Market information calendar = TARGET() # must be a business day settings.evaluation_date = calendar.adjust(today()) settlement_date = Date(18, September, 2008) # must be a business day settlement_date = calendar.adjust(settlement_date); quotes = [SimpleQuote(0.0096), SimpleQuote(0.0145), SimpleQuote(0.0194)] tenors = [3, 6, 12] rate_helpers = [] calendar = TARGET() deposit_day_counter = Actual365Fixed() convention = ModifiedFollowing end_of_month = True for quote, month in zip(quotes, tenors): tenor = Period(month, Months) fixing_days = 3 helper = DepositRateHelper( quote, tenor, fixing_days, calendar, convention, end_of_month, deposit_day_counter ) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve[BootstrapTrait.Discount, LogLinear].from_reference_date( settlement_date, rate_helpers, ts_day_counter, accuracy=tolerance ) self.assertIsNotNone(ts) self.assertEqual( Date(18, September, 2008), ts.reference_date) # this is not a real test ... self.assertAlmostEqual(0.9975, ts.discount(Date(21, 12, 2008)), 4) self.assertAlmostEqual(0.9944, ts.discount(Date(21, 4, 2009)), 4) self.assertAlmostEqual(0.9904, ts.discount(Date(21, 9, 2009)), 4) dates, dfs = zip(*ts.nodes) self.assertAlmostEqual(list(dates), ts.dates) self.assertAlmostEqual(list(dfs), ts.data)
def test_creation(self): settings = Settings() # Market information calendar = TARGET() # must be a business day settings.evaluation_date = calendar.adjust(today()) settlement_date = Date(18, September, 2008) # must be a business day settlement_date = calendar.adjust(settlement_date); quotes = [0.0096, 0.0145, 0.0194] tenors = [3, 6, 12] rate_helpers = [] calendar = TARGET() deposit_day_counter = Actual365Fixed() convention = ModifiedFollowing end_of_month = True for quote, month in zip(quotes, tenors): tenor = Period(month, Months) fixing_days = 3 helper = DepositRateHelper( quote, tenor, fixing_days, calendar, convention, end_of_month, deposit_day_counter ) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = term_structure_factory( 'discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance ) self.assertIsNotNone(ts) self.assertEquals( Date(18, September, 2008), ts.reference_date) # this is not a real test ... self.assertAlmostEquals(0.9975, ts.discount(Date(21, 12, 2008)), 4) self.assertAlmostEquals(0.9944, ts.discount(Date(21, 4, 2009)), 4) self.assertAlmostEquals(0.9904, ts.discount(Date(21, 9, 2009)), 4)
def test_bucketanalysis_bond(self): face_amount = 100.0 redemption = 100.0 issue_date = Date(27, January, 2011) maturity_date = Date(1, January, 2021) coupon_rate = 0.055 fixed_bond_schedule = Schedule.from_rule( issue_date, maturity_date, Period(Semiannual), UnitedStates(market=GovernmentBond), Unadjusted, Unadjusted, Backward, False) bond = FixedRateBond( self.settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(Bond), Unadjusted, redemption, issue_date ) pricing_engine = DiscountingBondEngine(self.ts) bond.set_pricing_engine(pricing_engine) self.assertAlmostEqual(bond.npv, 100.82127876105724) quotes = [rh.quote for rh in self.rate_helpers] delta, gamma = bucket_analysis(quotes, [bond]) self.assertEqual(len(quotes), len(delta)) old_values = [q.value for q in quotes] delta_manual = [] gamma_manual = [] pv = bond.npv shift = 1e-4 for v, q in zip(old_values, quotes): q.value = v + shift pv_plus = bond.npv q.value = v - shift pv_minus = bond.npv delta_manual.append((pv_plus - pv_minus) * 0.5 / shift) gamma_manual.append((pv_plus - 2 * pv + pv_minus) / shift ** 2) q.value = v assert_allclose(delta, delta_manual) assert_allclose(gamma, gamma_manual, atol=1e-4)
def setUp(self): self.calendar = TARGET() self.settlement_days = 1 settlement_date = self.calendar.adjust(Date(28, January, 2011)) todays_date = self.calendar.advance( settlement_date, -self.settlement_days, Days ) Settings().evaluation_date = todays_date depositData = [[ 1, Months, 4.581 ], [ 2, Months, 4.573 ], [ 3, Months, 4.557 ], [ 6, Months, 4.496 ], [ 9, Months, 4.490 ]] swapData = [[ 1, Years, 4.54 ], [ 5, Years, 4.99 ], [ 10, Years, 5.47 ], [ 20, Years, 5.89 ], [ 30, Years, 5.96 ]] self.rate_helpers = [] end_of_month = True for m, period, rate in depositData: tenor = Period(m, Months) helper = DepositRateHelper(SimpleQuote(rate / 100), tenor, self.settlement_days, self.calendar, ModifiedFollowing, end_of_month, Actual360()) self.rate_helpers.append(helper) liborIndex = USDLibor(Period(6, Months)) for m, period, rate in swapData: sq_rate = SimpleQuote(rate/100) helper = SwapRateHelper.from_tenor( sq_rate, Period(m, Years), self.calendar, Annual, Unadjusted, Thirty360(), liborIndex ) self.rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 self.ts = PiecewiseYieldCurve( BootstrapTrait.Discount, Interpolator.LogLinear, self.settlement_days, self.calendar, self.rate_helpers, ts_day_counter, tolerance)
def _blsprice(spot, strike, risk_free_rate, time, volatility, option_type='Call', dividend=0.0, calc='price'): """ Black-Scholes option pricing model + greeks. """ _spot = SimpleQuote(spot) daycounter = ActualActual(ISMA) risk_free_ts = FlatForward(today(), risk_free_rate, daycounter) dividend_ts = FlatForward(today(), dividend, daycounter) volatility_ts = BlackConstantVol(today(), NullCalendar(), volatility, daycounter) process = BlackScholesMertonProcess(_spot, dividend_ts, risk_free_ts, volatility_ts) exercise_date = today() + Period(time * 365, Days) exercise = EuropeanExercise(exercise_date) payoff = PlainVanillaPayoff(option_type, strike) option = EuropeanOption(payoff, exercise) engine = AnalyticEuropeanEngine(process) option.set_pricing_engine(engine) if calc == 'price': res = option.npv elif calc == 'delta': res = option.delta elif calc == 'gamma': res = option.gamma elif calc == 'theta': res = option.theta elif calc == 'rho': res = option.rho elif calc == 'vega': res = option.vega elif calc == 'lambda': res = option.delta * spot / option.npv else: raise ValueError('calc type %s is unknown' % calc) return res
def setUp(self): self.settings = Settings() daycounter = ActualActual() interest_rate = .1 dividend_yield = .04 self.risk_free_ts = flat_rate(interest_rate, daycounter) self.dividend_ts = flat_rate(dividend_yield, daycounter) s0 = SimpleQuote(32.0) # Heston model v0 = 0.05 kappa = 5.0 theta = 0.05 sigma = 1.0e-4 rho = -0.5 self.heston_process = HestonProcess(self.risk_free_ts, self.dividend_ts, s0, v0, kappa, theta, sigma, rho, PartialTruncation) v0 = 0.05 ival = { 'v0': v0, 'kappa': 3.7, 'theta': v0, 'sigma': 1.0, 'rho': -.6, 'lambda': .1, 'nu': -.5, 'delta': 0.3 } spot = SimpleQuote(1200) self.bates_process = BatesProcess(self.risk_free_ts, self.dividend_ts, spot, ival['v0'], ival['kappa'], ival['theta'], ival['sigma'], ival['rho'], ival['lambda'], ival['nu'], ival['delta']) a = 0.376739 sigma = 0.0209 self.hullwhite_process = HullWhiteProcess(self.risk_free_ts, a, sigma)
def test_bond_schedule_anotherday(self): '''Test date calculations and role of settings when evaluation date set to arbitrary date. This test is known to fail with boost 1.42. ''' todays_date = Date(30, August, 2011) settings = Settings() settings.evaluation_date = todays_date calendar = TARGET() effective_date = Date(10, Jul, 2006) termination_date = calendar.advance( effective_date, 10, Years, convention=Unadjusted) settlement_days = 3 face_amount = 100.0 coupon_rate = 0.05 redemption = 100.0 fixed_bond_schedule = Schedule.from_rule( effective_date, termination_date, Period(Annual), calendar, ModifiedFollowing, ModifiedFollowing, Rule.Backward ) issue_date = effective_date bond = FixedRateBond( settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(ISMA), Following, redemption, issue_date ) self.assertEqual( calendar.advance(todays_date, 3, Days), bond.settlement_date())
def test_relative_yieldcurve(self): settings = Settings() settings.evaluation_date = Date(6, 10, 2016) # Market information calendar = TARGET() quotes = [0.0096, 0.0145, 0.0194] tenors = [3, 6, 12] deposit_day_counter = Actual365Fixed() convention = ModifiedFollowing end_of_month = True fixing_days = 3 rate_helpers = [ DepositRateHelper(quote, Period(month, Months), fixing_days, calendar, convention, end_of_month, deposit_day_counter) for quote, month in zip(quotes, tenors) ] ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts_relative = PiecewiseYieldCurve(BootstrapTrait.Discount, Interpolator.LogLinear, 2, calendar, rate_helpers, ts_day_counter, tolerance) self.assertEqual( ts_relative.reference_date, calendar.advance(settings.evaluation_date, period=Period(2, Days))) settings.evaluation_date = Date(10, 10, 2016) settlement_date = calendar.advance(settings.evaluation_date, period=Period(2, Days)) self.assertEqual(ts_relative.reference_date, settlement_date) ts_absolute = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance) self.assertEqual(ts_absolute.data, ts_relative.data) self.assertEqual(ts_absolute.dates, ts_relative.dates) self.assertEqual(ts_absolute.times, ts_relative.times)
def make_term_structure(rates, dt_obs): """ rates is a dictionary-like structure with labels as keys and rates (decimal) as values. TODO: Make it more generic """ settlement_date = pydate_to_qldate(dt_obs) rate_helpers = [] for label in rates.keys(): r = rates[label] h = make_rate_helper(label, r, settlement_date) rate_helpers.append(h) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) return ts
def test_bump_yieldcurve(self): settings = Settings() settings.evaluation_date = Date(6, 10, 2016) # Market information calendar = TARGET() quotes = [ SimpleQuote(0.0096), SimpleQuote(0.0145), SimpleQuote(0.0194) ] tenors = [3, 6, 12] deposit_day_counter = Actual365Fixed() convention = ModifiedFollowing end_of_month = True fixing_days = 3 rate_helpers = [ DepositRateHelper(quote, Period(month, Months), fixing_days, calendar, convention, end_of_month, deposit_day_counter) for quote, month in zip(quotes, tenors) ] ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve(BootstrapTrait.Discount, Interpolator.LogLinear, 2, calendar, rate_helpers, ts_day_counter, tolerance) old_discount = ts.discount(ts.max_date) # parallel shift of 1 bps for rh in rate_helpers: rh.quote += 1e-4 self.assertEqual([q.value for q in quotes], [rh.quote for rh in rate_helpers]) new_discount = ts.discount(ts.max_date) self.assertTrue(new_discount < old_discount)
def test_discount_curve(self): settings = Settings() settings.evaluation_date = Date(6, 10, 2016) # Market information calendar = TARGET() quotes = [ SimpleQuote(0.0096), SimpleQuote(0.0145), SimpleQuote(0.0194) ] tenors = [3, 6, 12] deposit_day_counter = Actual365Fixed() convention = ModifiedFollowing end_of_month = True fixing_days = 3 rate_helpers = [ DepositRateHelper(quote, Period(month, Months), fixing_days, calendar, convention, end_of_month, deposit_day_counter) for quote, month in zip(quotes, tenors) ] ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve(BootstrapTrait.ForwardRate, Interpolator.BackwardFlat, 2, calendar, rate_helpers, ts_day_counter, tolerance) dates = [rh.latest_date for rh in rate_helpers] dfs = [ts.discount(d) for d in dates] dates.insert(0, ts.reference_date) dfs.insert(0, 1) ts_discount = DiscountCurve(dates, dfs, ts_day_counter, calendar) self.assertTrue(ts.discount(0.75), ts_discount.discount(0.75))
def test_yield(self): rates_data = [('Libor1M', .01), ('Libor3M', .015), ('Libor6M', .017), ('Swap1Y', .02), ('Swap2Y', .03), ('Swap3Y', .04), ('Swap5Y', .05), ('Swap7Y', .06), ('Swap10Y', .07), ('Swap20Y', .08)] settlement_date = pydate_to_qldate('01-Dec-2013') rate_helpers = [] for label, rate in rates_data: h = make_rate_helper(label, rate, settlement_date) rate_helpers.append(h) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) zc = zero_rate(ts, (200, 300), settlement_date) # not a real test - just verify execution self.assertAlmostEqual(zc[1][0], 0.0189, 2)
def test_bond_schedule_today(self): '''Test date calculations and role of settings when evaluation date set to current date. ''' todays_date = today() settings = Settings() settings.evaluation_date = todays_date calendar = TARGET() effective_date = Date(10, Jul, 2006) termination_date = calendar.advance(effective_date, 10, Years, convention=Unadjusted) settlement_days = 3 face_amount = 100.0 coupon_rate = 0.05 redemption = 100.0 fixed_bond_schedule = Schedule(effective_date, termination_date, Period(Annual), calendar, ModifiedFollowing, ModifiedFollowing, Backward) issue_date = effective_date bond = FixedRateBond(settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(ISMA), Following, redemption, issue_date) self.assertEquals(calendar.advance(todays_date, 3, Days), bond.settlement_date())
def test_extrapolation(self): rate_helpers = build_helpers() settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) print('dt Obs: %s\ndt Eval: %s\ndt Settle: %s' % (dtObs, eval_date, settlement_date)) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve[BootstrapTrait.Discount, LogLinear].from_reference_date( settlement_date, rate_helpers, ts_day_counter, accuracy=tolerance) # max_date raises an exception without extrapolation... self.assertFalse(ts.extrapolation) with self.assertRaises(RuntimeError) as ctx: ts.discount(ts.max_date + 1) self.assertTrue( str(ctx.exception) in ( "time (30.011) is past max curve time (30.0082)", "1st iteration: failed at 2nd alive instrument"))
fixedLegFrequency = Annual fixedLegTenor = Period(1,Years) fixedLegAdjustment = Unadjusted fixedLegDayCounter = Thirty360() floatingLegFrequency = Semiannual floatingLegTenor = Period(6,Months) floatingLegAdjustment = ModifiedFollowing swapHelpers = [ SwapRateHelper.from_tenor(swaps[(n,unit)], Period(n,unit), calendar, fixedLegFrequency, fixedLegAdjustment, fixedLegDayCounter, Euribor6M()) for n, unit in swaps.keys() ] ### Curve building ts_daycounter = ActualActual(ISDA) # term-structure construction helpers = depositHelpers + swapHelpers depoSwapCurve = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, Interpolator.LogLinear, settlementDate, helpers, ts_daycounter ) helpers = depositHelpers[:2] + futuresHelpers + swapHelpers[1:] depoFuturesSwapCurve = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, Interpolator.LogLinear,settlementDate, helpers, ts_daycounter ) helpers = depositHelpers[:3] + fraHelpers + swapHelpers depoFraSwapCurve = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, Interpolator.LogLinear, settlementDate, helpers, ts_daycounter
def test_zero_curve(self): try: settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) print('dt Obs: %s\ndt Eval: %s\ndt Settle: %s' % (dtObs, eval_date, settlement_date)) depositData = [[1, Months, 'Libor1M', 5.32], [3, Months, 'Libor3M', 5.35], [6, Months, 'Libor6M', 5.35]] swapData = [[1, Years, 'Swap1Y', 5.31], [2, Years, 'Swap2Y', 5.06], [3, Years, 'Swap3Y', 5.00], [4, Years, 'Swap4Y', 5.01], [5, Years, 'Swap5Y', 5.04], [7, Years, 'Swap7Y', 5.12], [10, Years, 'Swap10Y', 5.22], [30, Years, 'Swap30Y', 5.44]] rate_helpers = [] end_of_month = True for m, period, label, rate in depositData: tenor = Period(m, Months) helper = DepositRateHelper(SimpleQuote(rate / 100.0), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) liborIndex = Libor('USD Libor', Period(3, Months), settlement_days, USDCurrency(), calendar, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, label, rate in swapData: helper = SwapRateHelper.from_tenor(SimpleQuote(rate / 100.0), Period(m, Years), calendar, Semiannual, ModifiedFollowing, Thirty360(), liborIndex, spread, fwdStart) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) # max_date raises an exception... dtMax = ts.max_date print('max date: %s' % dtMax) except RuntimeError as e: print('Exception (expected):\n%s' % e) self.assertTrue(True) except Exception: self.assertFalse()
# <codecell> def flat_rate(forward, daycounter): return FlatForward( forward=SimpleQuote(forward), settlement_days=0, calendar=NullCalendar(), daycounter=daycounter ) settings = Settings.instance() settlement_date = today() settings.evaluation_date = settlement_date daycounter = ActualActual() calendar = NullCalendar() interest_rate = .1 dividend_yield = .04 risk_free_ts = flat_rate(interest_rate, daycounter) dividend_ts = flat_rate(dividend_yield, daycounter) s0 = SimpleQuote(100.0) # Heston model v0 = 0.05 kappa = 5.0 theta = 0.05
def test_deposit_swap(self): settings = Settings() # Market information calendar = TARGET() todays_date = Date(1, Mar, 2012) # must be a business day eval_date = calendar.adjust(todays_date) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date); depositData = [[ 1, Months, 4.581 ], [ 2, Months, 4.573 ], [ 3, Months, 4.557 ], [ 6, Months, 4.496 ], [ 9, Months, 4.490 ]] swapData = [[ 1, Years, 4.54 ], [ 5, Years, 4.99 ], [ 10, Years, 5.47 ], [ 20, Years, 5.89 ], [ 30, Years, 5.96 ]] rate_helpers = [] end_of_month = True for m, period, rate in depositData: tenor = Period(m, Months) helper = DepositRateHelper(SimpleQuote(rate/100), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) liborIndex = Libor( 'USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360() ) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, rate in swapData: helper = SwapRateHelper.from_tenor( SimpleQuote(rate/100), Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart ) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance ) self.assertEqual(settlement_date, ts.reference_date) # this is not a real test ... self.assertAlmostEqual(0.9103, ts.discount(calendar.advance(todays_date, 2, Years)),3) self.assertAlmostEqual(0.7836, ts.discount(calendar.advance(todays_date, 5, Years)),3) self.assertAlmostEqual(0.5827, ts.discount(calendar.advance(todays_date, 10, Years)),3) self.assertAlmostEqual(0.4223, ts.discount(calendar.advance(todays_date, 15, Years)),3)
def test_display(self): settings = Settings() # Date setup calendar = TARGET() # Settlement date settlement_date = calendar.adjust(Date(28, January, 2011)) # Evaluation date fixing_days = 1 settlement_days = 1 todays_date = calendar.advance( settlement_date, -fixing_days, Days ) settings.evaluation_date = todays_date # Bound attributes face_amount = 100.0 redemption = 100.0 issue_date = Date(27, January, 2011) maturity_date = Date(31, August, 2020) coupon_rate = 0.03625 bond_yield = 0.034921 flat_discounting_term_structure = YieldTermStructure() flat_term_structure = FlatForward( reference_date = settlement_date, forward = bond_yield, daycounter = Actual365Fixed(), #actual_actual.ActualActual(actual_actual.Bond), compounding = Compounded, frequency = Semiannual) # have a look at the FixedRateBondHelper to simplify this # construction flat_discounting_term_structure.link_to(flat_term_structure) #Rate fixed_bond_schedule = Schedule( issue_date, maturity_date, Period(Semiannual), UnitedStates(market=GOVERNMENTBOND), Unadjusted, Unadjusted, Backward, False); bond = FixedRateBond( settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(Bond), Unadjusted, redemption, issue_date ) d=bf.startDate(bond) zspd=bf.zSpread(bond, 100.0, flat_term_structure, Actual365Fixed(), Compounded, Semiannual, settlement_date, 1e-6, 100, 0.5) #Also need a test case for a PiecewiseTermStructure... depositData = [[ 1, Months, 4.581 ], [ 2, Months, 4.573 ], [ 3, Months, 4.557 ], [ 6, Months, 4.496 ], [ 9, Months, 4.490 ]] swapData = [[ 1, Years, 4.54 ], [ 5, Years, 4.99 ], [ 10, Years, 5.47 ], [ 20, Years, 5.89 ], [ 30, Years, 5.96 ]] rate_helpers = [] end_of_month = True for m, period, rate in depositData: tenor = Period(m, Months) helper = DepositRateHelper(SimpleQuote(rate/100), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) liborIndex = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360(), YieldTermStructure(relinkable=False)) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, rate in swapData: helper = SwapRateHelper.from_tenor( SimpleQuote(rate/100), Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart ) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance) pyc_zspd=bf.zSpread(bond, 102.0, ts, ActualActual(ISDA), Compounded, Semiannual, Date(1, April, 2015), 1e-6, 100, 0.5) pyc_zspd_disco=bf.zSpread(bond, 95.0, ts, ActualActual(ISDA), Compounded, Semiannual, settlement_date, 1e-6, 100, 0.5) yld = bf.yld(bond, 102.0, ActualActual(ISDA), Compounded, Semiannual, settlement_date, 1e-6, 100, 0.5) dur = bf.duration(bond, yld, ActualActual(ISDA), Compounded, Semiannual, 2, settlement_date) yld_disco = bf.yld(bond, 95.0, ActualActual(ISDA), Compounded, Semiannual, settlement_date, 1e-6, 100, 0.5) dur_disco = bf.duration(bond, yld_disco, ActualActual(ISDA), Compounded, Semiannual, 2, settlement_date) self.assertEqual(round(zspd, 6), 0.001281) self.assertEqual(round(pyc_zspd, 4), -0.0264) self.assertEqual(round(pyc_zspd_disco, 4), -0.0114) self.assertEqual(round(yld, 4), 0.0338) self.assertEqual(round(yld_disco, 4), 0.0426) self.assertEqual(round(dur, 4), 8.0655) self.assertEqual(round(dur_disco, 4), 7.9702)
def test_bates_det_jump(self): # this looks like a bug in QL: # Bates Det Jump model does not have sigma as parameter, yet # changing sigma changes the result! settlement_date = today() self.settings.evaluation_date = settlement_date daycounter = ActualActual() exercise_date = settlement_date + 6 * Months payoff = PlainVanillaPayoff(Put, 1290) exercise = EuropeanExercise(exercise_date) option = VanillaOption(payoff, exercise) risk_free_ts = flat_rate(0.02, daycounter) dividend_ts = flat_rate(0.04, daycounter) spot = 1290 ival = {'delta': 3.6828677022272715e-06, 'kappa': 19.02581428347027, 'kappaLambda': 1.1209758060939223, 'lambda': 0.06524550732595163, 'nu': -1.8968106563601956, 'rho': -0.7480898462264719, 'sigma': 1.0206363887835108, 'theta': 0.01965384459461113, 'thetaLambda': 0.028915397380738218, 'v0': 0.06566800935242285} process = BatesProcess( risk_free_ts, dividend_ts, SimpleQuote(spot), ival['v0'], ival['kappa'], ival['theta'], ival['sigma'], ival['rho'], ival['lambda'], ival['nu'], ival['delta']) model = BatesDetJumpModel(process, ival['kappaLambda'], ival['thetaLambda']) engine = BatesDetJumpEngine(model, 64) option.set_pricing_engine(engine) calc_1 = option.net_present_value ival['sigma'] = 1.e-6 process = BatesProcess( risk_free_ts, dividend_ts, SimpleQuote(spot), ival['v0'], ival['kappa'], ival['theta'], ival['sigma'], ival['rho'], ival['lambda'], ival['nu'], ival['delta']) model = BatesDetJumpModel(process, ival['kappaLambda'], ival['thetaLambda']) engine = BatesDetJumpEngine(model, 64) option.set_pricing_engine(engine) calc_2 = option.net_present_value if(abs(calc_1-calc_2) > 1.e-5): print('calc 1 %f calc 2 %f' % (calc_1, calc_2)) self.assertNotEqual(calc_1, calc_2)
def get_term_structure(df_libor, dtObs): settings = Settings() # libor as fixed in London, but cash-flows are determined according to # US calendar, hence the need to combine both holidays lists calendar = JointCalendar(UnitedStates(), UnitedKingdom()) # must be a business day eval_date = calendar.adjust(dateToDate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) depositData = [[1, Months, 'Libor1M'], [3, Months, 'Libor3M'], [6, Months, 'Libor6M']] swapData = [[1, Years, 'Swap1Y'], [2, Years, 'Swap2Y'], [3, Years, 'Swap3Y'], [4, Years, 'Swap4Y'], [5, Years, 'Swap5Y'], [7, Years, 'Swap7Y'], [10, Years, 'Swap10Y'], [30, Years, 'Swap30Y']] rate_helpers = [] end_of_month = True for m, period, label in depositData: tenor = Period(m, Months) rate = df_libor.get_value(dtObs, label) helper = DepositRateHelper(float(rate / 100), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) endOfMonth = True liborIndex = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, label in swapData: rate = df_libor.get_value(dtObs, label) helper = SwapRateHelper.from_tenor(rate / 100., Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = term_structure_factory('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) return ts
def test_bucketanalysis_bond(self): settings = Settings() calendar = TARGET() settlement_date = calendar.adjust(Date(28, January, 2011)) simple_quotes = [] fixing_days = 1 settlement_days = 1 todays_date = calendar.advance(settlement_date, -fixing_days, Days) settings.evaluation_date = todays_date face_amount = 100.0 redemption = 100.0 issue_date = Date(27, January, 2011) maturity_date = Date(1, January, 2021) coupon_rate = 0.055 bond_yield = 0.034921 flat_discounting_term_structure = YieldTermStructure() flat_term_structure = FlatForward(reference_date=settlement_date, forward=bond_yield, daycounter=Actual365Fixed(), compounding=Compounded, frequency=Semiannual) flat_discounting_term_structure.link_to(flat_term_structure) fixed_bond_schedule = Schedule.from_rule( issue_date, maturity_date, Period(Semiannual), UnitedStates(market=GovernmentBond), Unadjusted, Unadjusted, Backward, False) bond = FixedRateBond(settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(Bond), Unadjusted, redemption, issue_date) zspd = bf.zSpread(bond, 100.0, flat_term_structure, Actual365Fixed(), Compounded, Semiannual, settlement_date, 1e-6, 100, 0.5) depositData = [[1, Months, 4.581], [2, Months, 4.573], [3, Months, 4.557], [6, Months, 4.496], [9, Months, 4.490]] swapData = [[1, Years, 4.54], [5, Years, 4.99], [10, Years, 5.47], [20, Years, 5.89], [30, Years, 5.96]] rate_helpers = [] end_of_month = True for m, period, rate in depositData: tenor = Period(m, Months) sq_rate = SimpleQuote(rate / 100) helper = DepositRateHelper(sq_rate, tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) simple_quotes.append(sq_rate) rate_helpers.append(helper) liborIndex = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, rate in swapData: sq_rate = SimpleQuote(rate / 100) helper = SwapRateHelper.from_tenor(sq_rate, Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart) simple_quotes.append(sq_rate) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance) discounting_term_structure = YieldTermStructure() discounting_term_structure.link_to(ts) pricing_engine = DiscountingBondEngine(discounting_term_structure) bond.set_pricing_engine(pricing_engine) self.assertAlmostEqual(bond.npv, 100.83702940160767) ba = bucket_analysis([simple_quotes], [bond], [1], 0.0001, 1) self.assertTrue(2, ba) self.assertTrue(type(tuple), ba) self.assertEqual(len(simple_quotes), len(ba[0][0])) self.assertEqual(0, ba[0][0][8])
def get_term_structure(df_libor, dtObs): settings = Settings() # Market information calendar = TARGET() # must be a business day eval_date = calendar.adjust(dateToDate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date); depositData =[[1, Months, 'Libor1M'], [3, Months, 'Libor3M'], [6, Months, 'Libor6M']] swapData = [[ 1, Years, 'Swap1Y'], [ 2, Years, 'Swap2Y'], [ 3, Years, 'Swap3Y'], [ 4, Years, 'Swap4Y'], [ 5, Years, 'Swap5Y'], [ 7, Years, 'Swap7Y'], [ 10, Years,'Swap10Y'], [ 30, Years,'Swap30Y']] rate_helpers = [] end_of_month = True for m, period, label in depositData: tenor = Period(m, Months) rate = df_libor.get_value(dtObs, label) helper = DepositRateHelper(float(rate/100), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) endOfMonth = True liborIndex = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, ModifiedFollowing, endOfMonth, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, label in swapData: rate = df_libor.get_value(dtObs, label) helper = SwapRateHelper(SimpleQuote(rate/100), Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = term_structure_factory('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) return ts
def setUp(self): self.calendar = UnitedKingdom() today = Date(25, 11, 2009) evaluation_date = self.calendar.adjust(today) Settings().evaluation_date = evaluation_date day_counter = ActualActual() rpi_schedule = Schedule.from_rule(Date(20, 7, 2007), Date(20, 11, 2009), Period(1, Months), self.calendar, ModifiedFollowing) self.cpi_ts = ZeroInflationTermStructure() self.yts = FlatForward(evaluation_date, 0.05, day_counter) self.ii = UKRPI(False, self.cpi_ts) fix_data = [ 206.1, 207.3, 208.0, 208.9, 209.7, 210.9, 209.8, 211.4, 212.1, 214.0, 215.1, 216.8, 216.5, 217.2, 218.4, 217.7, 216, 212.9, 210.1, 211.4, 211.3, 211.5, 212.8, 213.4, 213.4, 213.4, 214.4 ] for date, data in zip(rpi_schedule, fix_data): self.ii.add_fixing(date, data) dates = [ Date(25, 11, 2010), Date(25, 11, 2011), Date(26, 11, 2012), Date(25, 11, 2013), Date(25, 11, 2014), Date(25, 11, 2015), Date(25, 11, 2016), Date(25, 11, 2017), Date(25, 11, 2018), Date(25, 11, 2019), Date(25, 11, 2021), Date(25, 11, 2024), Date(26, 11, 2029), Date(27, 11, 2034), Date(25, 11, 2039), Date(25, 11, 2049), Date(25, 11, 2059) ] rates = [ 3.0495, 2.93, 2.9795, 3.029, 3.1425, 3.211, 3.2675, 3.3625, 3.405, 3.48, 3.576, 3.649, 3.751, 3.77225, 3.77, 3.734, 3.714 ] observation_lag = Period('2M') self.helpers = [ZeroCouponInflationSwapHelper( SimpleQuote(r / 100), observation_lag, maturity, self.calendar, ModifiedFollowing, day_counter, self.ii, self.yts) \ for maturity, r in zip(dates, rates)] base_zero_rate = rates[0] / 100 self.cpi_ts.link_to( PiecewiseZeroInflationCurve(Interpolator.Linear, evaluation_date, self.calendar, day_counter, observation_lag, self.ii.frequency, self.ii.interpolated, base_zero_rate, self.helpers))