public void testInstrumentEquality() { // Testing inflation capped/floored coupon against inflation capfloor instrument... CommonVars vars = new CommonVars(); int[] lengths = { 1, 2, 3, 5, 7, 10, 15, 20 }; // vol is low ... double[] strikes = { 0.01, 0.025, 0.029, 0.03, 0.031, 0.035, 0.07 }; // yoy inflation vol is generally very low double[] vols = { 0.001, 0.005, 0.010, 0.015, 0.020 }; // this is model independent // capped coupon = fwd - cap, and fwd = swap(0) // floored coupon = fwd + floor for (int whichPricer = 0; whichPricer < 3; whichPricer++) { for (int i = 0; i < lengths.Length; i++) { for (int j = 0; j < strikes.Length; j++) { for (int k = 0; k < vols.Length; k++) { List <CashFlow> leg = vars.makeYoYLeg(vars.evaluationDate, lengths[i]); Instrument cap = vars.makeYoYCapFloor(CapFloorType.Cap, leg, strikes[j], vols[k], whichPricer); Instrument floor = vars.makeYoYCapFloor(CapFloorType.Floor, leg, strikes[j], vols[k], whichPricer); Date from = vars.nominalTS.link.referenceDate(); Date to = from + new Period(lengths[i], TimeUnit.Years); Schedule yoySchedule = new MakeSchedule().from(from).to(to) .withTenor(new Period(1, TimeUnit.Years)) .withCalendar(new UnitedKingdom()) .withConvention(BusinessDayConvention.Unadjusted) .backwards().value(); YearOnYearInflationSwap swap = new YearOnYearInflationSwap(YearOnYearInflationSwap.Type.Payer, 1000000.0, yoySchedule, //fixed schedule, but same as yoy 0.0, //strikes[j], vars.dc, yoySchedule, vars.iir, vars.observationLag, 0.0, //spread on index vars.dc, new UnitedKingdom()); Handle <YieldTermStructure> hTS = new Handle <YieldTermStructure>(vars.nominalTS); IPricingEngine sppe = new DiscountingSwapEngine(hTS); swap.setPricingEngine(sppe); List <CashFlow> leg2 = vars.makeYoYCapFlooredLeg(whichPricer, from, lengths[i], new InitializedList <double>(lengths[i], strikes[j]), //cap new List <double>(), //floor vols[k], 1.0, // gearing 0.0); // spread List <CashFlow> leg3 = vars.makeYoYCapFlooredLeg(whichPricer, from, lengths[i], new List <double>(), // cap new InitializedList <double>(lengths[i], strikes[j]), //floor vols[k], 1.0, // gearing 0.0); // spread // N.B. nominals are 10e6 double capped = CashFlows.npv(leg2, vars.nominalTS, false); if (Math.Abs(capped - (swap.NPV() - cap.NPV())) > 1.0e-6) { QAssert.Fail( "capped coupon != swap(0) - cap:\n" + " length: " + lengths[i] + " years\n" + " volatility: " + vols[k] + "\n" + " strike: " + strikes[j] + "\n" + " cap value: " + cap.NPV() + "\n" + " swap value: " + swap.NPV() + "\n" + " capped coupon " + capped); } // N.B. nominals are 10e6 double floored = CashFlows.npv(leg3, vars.nominalTS, false); if (Math.Abs(floored - (swap.NPV() + floor.NPV())) > 1.0e-6) { QAssert.Fail( "floored coupon != swap(0) + floor :\n" + " length: " + lengths[i] + " years\n" + " volatility: " + vols[k] + "\n" + " strike: " + strikes[j] + "\n" + " floor value: " + floor.NPV() + "\n" + " swap value: " + swap.NPV() + "\n" + " floored coupon " + floored); } } } } } // remove circular refernce vars.hy.linkTo(null); }
public void testParity() { // Testing yoy inflation cap/floor parity... CommonVars vars = new CommonVars(); int[] lengths = { 1, 2, 3, 5, 7, 10, 15, 20 }; // vol is low ... double[] strikes = { 0.0, 0.025, 0.029, 0.03, 0.031, 0.035, 0.07 }; // yoy inflation vol is generally very low double[] vols = { 0.001, 0.005, 0.010, 0.015, 0.020 }; // cap-floor-swap parity is model-independent for (int whichPricer = 0; whichPricer < 3; whichPricer++) { for (int i=0; i<lengths.Length; i++) { for (int j=0; j<strikes.Length; j++) { for (int k=0; k<vols.Length; k++) { List<CashFlow> leg = vars.makeYoYLeg(vars.evaluationDate,lengths[i]); Instrument cap = vars.makeYoYCapFloor(CapFloorType.Cap, leg, strikes[j], vols[k], whichPricer); Instrument floor = vars.makeYoYCapFloor(CapFloorType.Floor, leg, strikes[j], vols[k], whichPricer); Date from = vars.nominalTS.link.referenceDate(); Date to = from+new Period(lengths[i],TimeUnit.Years); Schedule yoySchedule = new MakeSchedule().from(from).to(to) .withTenor(new Period(1,TimeUnit.Years)) .withConvention(BusinessDayConvention.Unadjusted) .withCalendar(new UnitedKingdom()).backwards().value(); YearOnYearInflationSwap swap = new YearOnYearInflationSwap (YearOnYearInflationSwap.Type.Payer, 1000000.0, yoySchedule,//fixed schedule, but same as yoy strikes[j], vars.dc, yoySchedule, vars.iir, vars.observationLag, 0.0, //spread on index vars.dc, new UnitedKingdom()); Handle<YieldTermStructure> hTS = new Handle<YieldTermStructure>(vars.nominalTS); IPricingEngine sppe = new DiscountingSwapEngine(hTS); swap.setPricingEngine(sppe); // N.B. nominals are 10e6 if (Math.Abs((cap.NPV()-floor.NPV()) - swap.NPV()) > 1.0e-6) { Assert.Fail( "put/call parity violated:\n" + " length: " + lengths[i] + " years\n" + " volatility: " + vols[k] + "\n" + " strike: " + strikes[j] + "\n" + " cap value: " + cap.NPV() + "\n" + " floor value: " + floor.NPV() + "\n" + " swap value: " + swap.NPV()); } } } } } // remove circular refernce vars.hy.linkTo(new YoYInflationTermStructure()); }
public void testYYTermStructure() { // Testing year-on-year inflation term structure... SavedSettings backup = new SavedSettings(); //IndexHistoryCleaner cleaner; // try the YY UK Calendar calendar = new UnitedKingdom(); BusinessDayConvention bdc = BusinessDayConvention.ModifiedFollowing; Date evaluationDate = new Date(13, Month.August, 2007); evaluationDate = calendar.adjust(evaluationDate); Settings.setEvaluationDate(evaluationDate); // fixing data Date from = new Date(1, Month.January, 2005); Date to = new Date(13, Month.August, 2007); Schedule rpiSchedule = new MakeSchedule().from(from).to(to) .withTenor(new Period(1,TimeUnit.Months)) .withCalendar(new UnitedKingdom()) .withConvention(BusinessDayConvention.ModifiedFollowing).value(); double[] fixData = { 189.9, 189.9, 189.6, 190.5, 191.6, 192.0, 192.2, 192.2, 192.6, 193.1, 193.3, 193.6, 194.1, 193.4, 194.2, 195.0, 196.5, 197.7, 198.5, 198.5, 199.2, 200.1, 200.4, 201.1, 202.7, 201.6, 203.1, 204.4, 205.4, 206.2, 207.3 }; RelinkableHandle<YoYInflationTermStructure> hy = new RelinkableHandle<YoYInflationTermStructure>(); bool interp = false; YYUKRPIr iir = new YYUKRPIr(interp, hy); for (int i=0; i<fixData.Length; i++) { iir.addFixing(rpiSchedule[i], fixData[i]); } YieldTermStructure nominalTS = nominalTermStructure(); // now build the YoY inflation curve Datum[] yyData = { new Datum( new Date(13, Month.August, 2008), 2.95 ), new Datum( new Date(13, Month.August, 2009), 2.95 ), new Datum( new Date(13, Month.August, 2010), 2.93 ), new Datum( new Date(15, Month.August, 2011), 2.955 ), new Datum( new Date(13, Month.August, 2012), 2.945 ), new Datum( new Date(13, Month.August, 2013), 2.985 ), new Datum( new Date(13, Month.August, 2014), 3.01 ), new Datum( new Date(13, Month.August, 2015), 3.035 ), new Datum( new Date(13, Month.August, 2016), 3.055 ), // note that new Datum( new Date(13, Month.August, 2017), 3.075 ), // some dates will be on new Datum( new Date(13, Month.August, 2019), 3.105 ), // holidays but the payment new Datum( new Date(15, Month.August, 2022), 3.135 ), // calendar will roll them new Datum( new Date(13, Month.August, 2027), 3.155 ), new Datum( new Date(13, Month.August, 2032), 3.145 ), new Datum( new Date(13, Month.August, 2037), 3.145 ) }; Period observationLag = new Period(2,TimeUnit.Months); DayCounter dc = new Thirty360(); // now build the helpers ... List<BootstrapHelper<YoYInflationTermStructure>> helpers = makeHelpers (yyData, yyData.Length, iir,observationLag, calendar, bdc, dc); double baseYYRate = yyData[0].rate/100.0; PiecewiseYoYInflationCurve<Linear> pYYTS = new PiecewiseYoYInflationCurve<Linear>( evaluationDate, calendar, dc, observationLag, iir.frequency(),iir.interpolated(), baseYYRate, new Handle<YieldTermStructure>(nominalTS), helpers); pYYTS.recalculate(); // validation // yoy swaps should reprice to zero // yy rates should not equal yySwap rates double eps = 0.000001; // usual swap engine Handle<YieldTermStructure> hTS = new Handle<YieldTermStructure>(nominalTS); IPricingEngine sppe = new DiscountingSwapEngine(hTS); // make sure that the index has the latest yoy term structure hy.linkTo(pYYTS); for (int j = 1; j < yyData.Length; j++) { from = nominalTS.referenceDate(); to = yyData[j].date; Schedule yoySchedule = new MakeSchedule().from(from).to(to) .withConvention(BusinessDayConvention.Unadjusted) // fixed leg gets calendar from .withCalendar(calendar) // schedule .withTenor(new Period(1,TimeUnit.Years)).value(); // .back YearOnYearInflationSwap yyS2 = new YearOnYearInflationSwap( YearOnYearInflationSwap.Type.Payer, 1000000.0, yoySchedule,//fixed schedule, but same as yoy yyData[j].rate/100.0, dc, yoySchedule, iir, observationLag, 0.0, //spread on index dc, new UnitedKingdom()); yyS2.setPricingEngine(sppe); Assert.IsTrue(Math.Abs(yyS2.NPV())<eps,"fresh yoy swap NPV!=0 from TS " +"swap quote for pt " + j + ", is " + yyData[j].rate/100.0 +" vs YoY rate "+ pYYTS.yoyRate(yyData[j].date-observationLag) +" at quote date "+(yyData[j].date-observationLag) +", NPV of a fresh yoy swap is " + yyS2.NPV() +"\n fair rate " + yyS2.fairRate() +" payment "+yyS2.paymentConvention()); } int jj=3; for (int k = 0; k < 14; k++) { from = nominalTS.referenceDate() - new Period(k,TimeUnit.Months); to = yyData[jj].date - new Period(k,TimeUnit.Months); Schedule yoySchedule = new MakeSchedule().from(from).to(to) .withConvention(BusinessDayConvention.Unadjusted) // fixed leg gets calendar from .withCalendar(calendar) // schedule .withTenor(new Period(1,TimeUnit.Years)) .value(); //backwards() YearOnYearInflationSwap yyS3 = new YearOnYearInflationSwap( YearOnYearInflationSwap.Type.Payer, 1000000.0, yoySchedule,//fixed schedule, but same as yoy yyData[jj].rate/100.0, dc, yoySchedule, iir, observationLag, 0.0, //spread on index dc, new UnitedKingdom()); yyS3.setPricingEngine(sppe); Assert.IsTrue(Math.Abs(yyS3.NPV())< 20000.0, "unexpected size of aged YoY swap, aged " + k +" months: YY aged NPV = " + yyS3.NPV() +", legs "+ yyS3.legNPV(0) + " and " + yyS3.legNPV(1) ); } // remove circular refernce hy.linkTo( new YoYInflationTermStructure()); }
public void testParity() { // Testing yoy inflation cap/floor parity... CommonVars vars = new CommonVars(); int[] lengths = { 1, 2, 3, 5, 7, 10, 15, 20 }; // vol is low ... double[] strikes = { 0.0, 0.025, 0.029, 0.03, 0.031, 0.035, 0.07 }; // yoy inflation vol is generally very low double[] vols = { 0.001, 0.005, 0.010, 0.015, 0.020 }; // cap-floor-swap parity is model-independent for (int whichPricer = 0; whichPricer < 3; whichPricer++) { for (int i = 0; i < lengths.Length; i++) { for (int j = 0; j < strikes.Length; j++) { for (int k = 0; k < vols.Length; k++) { List <CashFlow> leg = vars.makeYoYLeg(vars.evaluationDate, lengths[i]); Instrument cap = vars.makeYoYCapFloor(CapFloorType.Cap, leg, strikes[j], vols[k], whichPricer); Instrument floor = vars.makeYoYCapFloor(CapFloorType.Floor, leg, strikes[j], vols[k], whichPricer); Date from = vars.nominalTS.link.referenceDate(); Date to = from + new Period(lengths[i], TimeUnit.Years); Schedule yoySchedule = new MakeSchedule().from(from).to(to) .withTenor(new Period(1, TimeUnit.Years)) .withConvention(BusinessDayConvention.Unadjusted) .withCalendar(new UnitedKingdom()).backwards().value(); YearOnYearInflationSwap swap = new YearOnYearInflationSwap (YearOnYearInflationSwap.Type.Payer, 1000000.0, yoySchedule, //fixed schedule, but same as yoy strikes[j], vars.dc, yoySchedule, vars.iir, vars.observationLag, 0.0, //spread on index vars.dc, new UnitedKingdom()); Handle <YieldTermStructure> hTS = new Handle <YieldTermStructure>(vars.nominalTS); IPricingEngine sppe = new DiscountingSwapEngine(hTS); swap.setPricingEngine(sppe); // N.B. nominals are 10e6 if (Math.Abs((cap.NPV() - floor.NPV()) - swap.NPV()) > 1.0e-6) { Assert.Fail( "put/call parity violated:\n" + " length: " + lengths[i] + " years\n" + " volatility: " + vols[k] + "\n" + " strike: " + strikes[j] + "\n" + " cap value: " + cap.NPV() + "\n" + " floor value: " + floor.NPV() + "\n" + " swap value: " + swap.NPV()); } } } } } // remove circular refernce vars.hy.linkTo(new YoYInflationTermStructure()); }
public void testYYTermStructure() { // Testing year-on-year inflation term structure... SavedSettings backup = new SavedSettings(); //IndexHistoryCleaner cleaner; // try the YY UK Calendar calendar = new UnitedKingdom(); BusinessDayConvention bdc = BusinessDayConvention.ModifiedFollowing; Date evaluationDate = new Date(13, Month.August, 2007); evaluationDate = calendar.adjust(evaluationDate); Settings.setEvaluationDate(evaluationDate); // fixing data Date from = new Date(1, Month.January, 2005); Date to = new Date(13, Month.August, 2007); Schedule rpiSchedule = new MakeSchedule().from(from).to(to) .withTenor(new Period(1, TimeUnit.Months)) .withCalendar(new UnitedKingdom()) .withConvention(BusinessDayConvention.ModifiedFollowing).value(); double[] fixData = { 189.9, 189.9, 189.6, 190.5, 191.6, 192.0, 192.2, 192.2, 192.6, 193.1, 193.3, 193.6, 194.1, 193.4, 194.2, 195.0, 196.5, 197.7, 198.5, 198.5, 199.2, 200.1, 200.4, 201.1, 202.7, 201.6, 203.1, 204.4, 205.4, 206.2, 207.3 }; RelinkableHandle <YoYInflationTermStructure> hy = new RelinkableHandle <YoYInflationTermStructure>(); bool interp = false; YYUKRPIr iir = new YYUKRPIr(interp, hy); for (int i = 0; i < fixData.Length; i++) { iir.addFixing(rpiSchedule[i], fixData[i]); } YieldTermStructure nominalTS = nominalTermStructure(); // now build the YoY inflation curve Datum[] yyData = { new Datum(new Date(13, Month.August, 2008), 2.95), new Datum(new Date(13, Month.August, 2009), 2.95), new Datum(new Date(13, Month.August, 2010), 2.93), new Datum(new Date(15, Month.August, 2011), 2.955), new Datum(new Date(13, Month.August, 2012), 2.945), new Datum(new Date(13, Month.August, 2013), 2.985), new Datum(new Date(13, Month.August, 2014), 3.01), new Datum(new Date(13, Month.August, 2015), 3.035), new Datum(new Date(13, Month.August, 2016), 3.055), // note that new Datum(new Date(13, Month.August, 2017), 3.075), // some dates will be on new Datum(new Date(13, Month.August, 2019), 3.105), // holidays but the payment new Datum(new Date(15, Month.August, 2022), 3.135), // calendar will roll them new Datum(new Date(13, Month.August, 2027), 3.155), new Datum(new Date(13, Month.August, 2032), 3.145), new Datum(new Date(13, Month.August, 2037), 3.145) }; Period observationLag = new Period(2, TimeUnit.Months); DayCounter dc = new Thirty360(); // now build the helpers ... List <BootstrapHelper <YoYInflationTermStructure> > helpers = makeHelpers(yyData, yyData.Length, iir, observationLag, calendar, bdc, dc); double baseYYRate = yyData[0].rate / 100.0; PiecewiseYoYInflationCurve <Linear> pYYTS = new PiecewiseYoYInflationCurve <Linear>( evaluationDate, calendar, dc, observationLag, iir.frequency(), iir.interpolated(), baseYYRate, new Handle <YieldTermStructure>(nominalTS), helpers); pYYTS.recalculate(); // validation // yoy swaps should reprice to zero // yy rates should not equal yySwap rates double eps = 0.000001; // usual swap engine Handle <YieldTermStructure> hTS = new Handle <YieldTermStructure>(nominalTS); IPricingEngine sppe = new DiscountingSwapEngine(hTS); // make sure that the index has the latest yoy term structure hy.linkTo(pYYTS); for (int j = 1; j < yyData.Length; j++) { from = nominalTS.referenceDate(); to = yyData[j].date; Schedule yoySchedule = new MakeSchedule().from(from).to(to) .withConvention(BusinessDayConvention.Unadjusted) // fixed leg gets calendar from .withCalendar(calendar) // schedule .withTenor(new Period(1, TimeUnit.Years)).value(); // .back YearOnYearInflationSwap yyS2 = new YearOnYearInflationSwap( YearOnYearInflationSwap.Type.Payer, 1000000.0, yoySchedule, //fixed schedule, but same as yoy yyData[j].rate / 100.0, dc, yoySchedule, iir, observationLag, 0.0, //spread on index dc, new UnitedKingdom()); yyS2.setPricingEngine(sppe); Assert.IsTrue(Math.Abs(yyS2.NPV()) < eps, "fresh yoy swap NPV!=0 from TS " + "swap quote for pt " + j + ", is " + yyData[j].rate / 100.0 + " vs YoY rate " + pYYTS.yoyRate(yyData[j].date - observationLag) + " at quote date " + (yyData[j].date - observationLag) + ", NPV of a fresh yoy swap is " + yyS2.NPV() + "\n fair rate " + yyS2.fairRate() + " payment " + yyS2.paymentConvention()); } int jj = 3; for (int k = 0; k < 14; k++) { from = nominalTS.referenceDate() - new Period(k, TimeUnit.Months); to = yyData[jj].date - new Period(k, TimeUnit.Months); Schedule yoySchedule = new MakeSchedule().from(from).to(to) .withConvention(BusinessDayConvention.Unadjusted) // fixed leg gets calendar from .withCalendar(calendar) // schedule .withTenor(new Period(1, TimeUnit.Years)) .value(); //backwards() YearOnYearInflationSwap yyS3 = new YearOnYearInflationSwap( YearOnYearInflationSwap.Type.Payer, 1000000.0, yoySchedule, //fixed schedule, but same as yoy yyData[jj].rate / 100.0, dc, yoySchedule, iir, observationLag, 0.0, //spread on index dc, new UnitedKingdom()); yyS3.setPricingEngine(sppe); Assert.IsTrue(Math.Abs(yyS3.NPV()) < 20000.0, "unexpected size of aged YoY swap, aged " + k + " months: YY aged NPV = " + yyS3.NPV() + ", legs " + yyS3.legNPV(0) + " and " + yyS3.legNPV(1) ); } // remove circular refernce hy.linkTo(new YoYInflationTermStructure()); }