def test(self): from ppf.date_time import date from ppf.date_time import months from ppf.date_time import Feb, Apr, Jul, Oct, Jan from numpy import zeros expiries = [ date(2006, Feb, 11), date(2006, Apr, 11), date(2006, Jul, 11), date(2006, Oct, 11), date(2007, Jan, 11), date(2008, Jan, 11), date(2009, Jan, 11), date(2010, Jan, 11), date(2011, Jan, 11), date(2012, Jan, 11), date(2013, Jan, 11) ] tenors = [months(12), months(24), months(36)] vols = zeros((len(expiries), len(tenors))) # expiry, tenor surface 1y 2y 3y vols[0, 0], vols[0, 1], vols[0, 2] = 200.00, 76.25, 64.00 # 1m vols[1, 0], vols[1, 1], vols[1, 2] = 98.50, 84.75, 69.00 # 3m vols[2, 0], vols[2, 1], vols[2, 2] = 98.00, 81.75, 68.00 # 6m vols[3, 0], vols[3, 1], vols[3, 2] = 101.25, 82.25, 69.25 # 9m vols[4, 0], vols[4, 1], vols[4, 2] = 106.00, 82.00, 69.25 # 1y vols[5, 0], vols[5, 1], vols[5, 2] = 78.75, 73.25, 61.25 # 2y vols[6, 0], vols[6, 1], vols[6, 2] = 66.25, 59.00, 50.00 # 3y vols[7, 0], vols[7, 1], vols[7, 2] = 55.25, 47.75, 41.75 # 4y vols[8, 0], vols[8, 1], vols[8, 2] = 44.75, 40.25, 35.50 # 5y vols[9, 0], vols[9, 1], vols[9, 2] = 32.00, 30.50, 28.25 # 6y vols[10, 0], vols[10, 1], vols[10, 2] = 26.50, 24.25, 24.25 # 7y base = date(2006, Jan, 11) sig = ppf.market.surface( [int(t - base) / 365.0 for t in expiries], [m.number_of_months().as_number() for m in tenors], vols) tol = 1.0e-8 for i in range(len(expiries)): expiry = expiries[i] t = int(expiry - base) / 365.0 for j in range(len(tenors)): tenor = tenors[j] T = tenor.number_of_months().as_number() assert math.fabs(sig(t, T) - vols[i, j]) <= tol
def test(self): from ppf.date_time import date from ppf.date_time import months from ppf.date_time import Feb, Apr, Jul, Oct, Jan from numpy import zeros expiries = [ date(2006, Feb, 11) , date(2006, Apr, 11) , date(2006, Jul, 11) , date(2006, Oct, 11) , date(2007, Jan, 11) , date(2008, Jan, 11) , date(2009, Jan, 11) , date(2010, Jan, 11) , date(2011, Jan, 11) , date(2012, Jan, 11) , date(2013, Jan, 11) ] tenors = [ months(12), months(24), months(36) ] vols = zeros((len(expiries), len(tenors))) # expiry, tenor surface 1y 2y 3y vols[ 0, 0], vols[ 0, 1], vols[ 0, 2] = 200.00, 76.25, 64.00 # 1m vols[ 1, 0], vols[ 1, 1], vols[ 1, 2] = 98.50, 84.75, 69.00 # 3m vols[ 2, 0], vols[ 2, 1], vols[ 2, 2] = 98.00, 81.75, 68.00 # 6m vols[ 3, 0], vols[ 3, 1], vols[ 3, 2] = 101.25, 82.25, 69.25 # 9m vols[ 4, 0], vols[ 4, 1], vols[ 4, 2] = 106.00, 82.00, 69.25 # 1y vols[ 5, 0], vols[ 5, 1], vols[ 5, 2] = 78.75, 73.25, 61.25 # 2y vols[ 6, 0], vols[ 6, 1], vols[ 6, 2] = 66.25, 59.00, 50.00 # 3y vols[ 7, 0], vols[ 7, 1], vols[ 7, 2] = 55.25, 47.75, 41.75 # 4y vols[ 8, 0], vols[ 8, 1], vols[ 8, 2] = 44.75, 40.25, 35.50 # 5y vols[ 9, 0], vols[ 9, 1], vols[ 9, 2] = 32.00, 30.50, 28.25 # 6y vols[10, 0], vols[10, 1], vols[10, 2] = 26.50, 24.25, 24.25 # 7y base = date(2006, Jan, 11); sig = ppf.market.surface( [int(t - base)/365.0 for t in expiries] , [m.number_of_months().as_number() for m in tenors] , vols) tol=1.0e-8 for i in range(len(expiries)): expiry = expiries[i] t = int(expiry - base)/365.0 for j in range(len(tenors)): tenor = tenors[j] T = tenor.number_of_months().as_number() assert math.fabs(sig(t, T) - vols[i, j]) <= tol
def test_libor(self): from ppf.date_time \ import date, shift, modified_following, basis_act_360, months pd = date(2008, 01, 01) env = ppf.market.environment(pd) times = numpy.linspace(0, 2, 5) factors = numpy.array([math.exp(-0.05*t) for t in times]) env.add_curve("zc.disc.eur" , ppf.market.curve(times, factors, ppf.math.interpolation.loglinear)) expiries, tenors = [0.1, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 5.0], [0, 90] env.add_surface("ve.term.eur.hw" , ppf.market.surface(expiries, tenors, numpy.zeros((8, 2)))) env.add_constant("cv.mr.eur.hw", 0.0) rd = date(2008, 07, 01) libor_obs = \ ppf.core.libor_rate( \ None #attributes , 0 #flow-id , 0 #reset-id , rd #reset-date , "eur"#reset-currency , rd #projection-start-date , shift(rd + months(6), modified_following)#projection-end-date , basis_act_360#projection-basis , ppf.core.fixing(False))# fixing (and no spread) r = ppf.model.hull_white.requestor() s = ppf.model.hull_white.lattice.state("eur", 11, 3.5) sx = s.fill(0.25, r, env) f = ppf.model.hull_white.fill(2.0) libortT = f.libor(0.25, libor_obs, env, r, sx) exp = \ [0.0499418283138 ,0.0499418283138 ,0.0499418283138 ,0.0499418283138 ,0.0499418283138 ,0.0499418283138 ,0.0499418283138 ,0.0499418283138 ,0.0499418283138 ,0.0499418283138 ,0.0499418283138 ] _assert_seq_close(exp, libortT)
def test_discounted_libor_rollback(self): from ppf.date_time \ import date, shift, modified_following, basis_act_360, months pd = date(2008, 01, 01) env = ppf.market.environment(pd) times = numpy.linspace(0, 6, 10) factors = numpy.array([math.exp(-0.05*t) for t in times]) env.add_curve("zc.disc.eur" , ppf.market.curve(times, factors, ppf.math.interpolation.loglinear)) expiries, tenors = [0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 5.0, 6.0], [0, 90] values = numpy.zeros((9, 2)) values.fill(0.001) env.add_surface("ve.term.eur.hw" , ppf.market.surface(expiries, tenors, values)) env.add_constant("cv.mr.eur.hw", 0.01) r = ppf.model.hull_white.requestor() s = ppf.model.hull_white.lattice.state("eur", 41, 4.5) f = ppf.model.hull_white.fill(5.0) rd = date(2011, 01, 01) libor_obs = \ ppf.core.libor_rate( \ None #attributes , 0 #flow-id , 0 #reset-id , rd #reset-date , "eur"#reset-currency , rd #projection-start-date , shift(rd + months(6), modified_following)#projection-end-date , basis_act_360#projection-basis , ppf.core.fixing(False))# fixing (and no spread) t = env.relative_date(libor_obs.proj_start_date())/365.0 T = env.relative_date(libor_obs.proj_end_date())/365.0 sx = s.fill(t, r, env) libort = f.libor(t, libor_obs, env, r, sx) ptT = f.numeraire_rebased_bond(t, T, "eur", env, r, sx) pv = libort*ptT*libor_obs.year_fraction() roll = ppf.model.hull_white.lattice.rollback("eur") intermediate_pv = roll.rollback(0.5*t, t, s, r, env, pv) actual = roll.rollback(0.0, 0.5*t, s, r, env, intermediate_pv).mean() expected = r.discount_factor(t, "eur", env)-r.discount_factor(T, "eur", env) _assert_seq_close([expected],[actual],1.0e-6)