internal static global::System.Runtime.InteropServices.HandleRef getCPtr(UKRPI obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; }
// setup public CommonVars() { backup = new SavedSettings(); nominalUK = new RelinkableHandle <YieldTermStructure>(); cpiUK = new RelinkableHandle <ZeroInflationTermStructure>(); hcpi = new RelinkableHandle <ZeroInflationTermStructure>(); zciisD = new List <Date>(); zciisR = new List <double>(); hii = new RelinkableHandle <ZeroInflationIndex>(); nominals = new InitializedList <double>(1, 1000000); // option variables frequency = Frequency.Annual; // usual setup volatility = 0.01; length = 7; calendar = new UnitedKingdom(); convention = BusinessDayConvention.ModifiedFollowing; Date today = new Date(1, Month.June, 2010); evaluationDate = calendar.adjust(today); Settings.setEvaluationDate(evaluationDate); settlementDays = 0; fixingDays = 0; settlement = calendar.advance(today, settlementDays, TimeUnit.Days); startDate = settlement; dcZCIIS = new ActualActual(); dcNominal = new ActualActual(); // uk rpi index // fixing data Date from = new Date(1, Month.July, 2007); Date to = new Date(1, Month.June, 2010); Schedule rpiSchedule = new MakeSchedule().from(from).to(to) .withTenor(new Period(1, TimeUnit.Months)) .withCalendar(new UnitedKingdom()) .withConvention(BusinessDayConvention.ModifiedFollowing).value(); double[] fixData = { 206.1, 207.3, 208.0, 208.9, 209.7, 210.9, 209.8, 211.4, 212.1, 214.0, 215.1, 216.8, // 2008 216.5, 217.2, 218.4, 217.7, 216.0, 212.9, 210.1, 211.4, 211.3, 211.5, 212.8, 213.4, // 2009 213.4, 214.4, 215.3, 216.0, 216.6, 218.0, 217.9, 219.2, 220.7, 222.8, -999, -999, // 2010 -999 }; // link from cpi index to cpi TS bool interp = false;// this MUST be false because the observation lag is only 2 months // for ZCIIS; but not for contract if the contract uses a bigger lag. ii = new UKRPI(interp, hcpi); for (int i = 0; i < rpiSchedule.Count; i++) { ii.addFixing(rpiSchedule[i], fixData[i], true);// force overwrite in case multiple use } Datum[] nominalData = { new Datum(new Date(2, Month.June, 2010), 0.499997), new Datum(new Date(3, Month.June, 2010), 0.524992), new Datum(new Date(8, Month.June, 2010), 0.524974), new Datum(new Date(15, Month.June, 2010), 0.549942), new Datum(new Date(22, Month.June, 2010), 0.549913), new Datum(new Date(1, Month.July, 2010), 0.574864), new Datum(new Date(2, Month.August, 2010), 0.624668), new Datum(new Date(1, Month.September, 2010), 0.724338), new Datum(new Date(16, Month.September, 2010), 0.769461), new Datum(new Date(1, Month.December, 2010), 0.997501), //{ Date( 16, December, 2010), 0.838164 ), new Datum(new Date(17, Month.March, 2011), 0.916996), new Datum(new Date(16, Month.June, 2011), 0.984339), new Datum(new Date(22, Month.September, 2011), 1.06085), new Datum(new Date(22, Month.December, 2011), 1.141788), new Datum(new Date(1, Month.June, 2012), 1.504426), new Datum(new Date(3, Month.June, 2013), 1.92064), new Datum(new Date(2, Month.June, 2014), 2.290824), new Datum(new Date(1, Month.June, 2015), 2.614394), new Datum(new Date(1, Month.June, 2016), 2.887445), new Datum(new Date(1, Month.June, 2017), 3.122128), new Datum(new Date(1, Month.June, 2018), 3.322511), new Datum(new Date(3, Month.June, 2019), 3.483997), new Datum(new Date(1, Month.June, 2020), 3.616896), new Datum(new Date(1, Month.June, 2022), 3.8281), new Datum(new Date(2, Month.June, 2025), 4.0341), new Datum(new Date(3, Month.June, 2030), 4.070854), new Datum(new Date(1, Month.June, 2035), 4.023202), new Datum(new Date(1, Month.June, 2040), 3.954748), new Datum(new Date(1, Month.June, 2050), 3.870953), new Datum(new Date(1, Month.June, 2060), 3.85298), new Datum(new Date(2, Month.June, 2070), 3.757542), new Datum(new Date(3, Month.June, 2080), 3.651379) }; int nominalDataLength = 33 - 1; List <Date> nomD = new List <Date>(); List <double> nomR = new List <double>(); for (int i = 0; i < nominalDataLength; i++) { nomD.Add(nominalData[i].date); nomR.Add(nominalData[i].rate / 100.0); } YieldTermStructure nominalTS = new InterpolatedZeroCurve <Linear>(nomD, nomR, dcNominal); nominalUK.linkTo(nominalTS); // now build the zero inflation curve observationLag = new Period(2, TimeUnit.Months); contractObservationLag = new Period(3, TimeUnit.Months); contractObservationInterpolation = InterpolationType.Flat; Datum[] zciisData = { new Datum(new Date(1, Month.June, 2011), 3.087), new Datum(new Date(1, Month.June, 2012), 3.12), new Datum(new Date(1, Month.June, 2013), 3.059), new Datum(new Date(1, Month.June, 2014), 3.11), new Datum(new Date(1, Month.June, 2015), 3.15), new Datum(new Date(1, Month.June, 2016), 3.207), new Datum(new Date(1, Month.June, 2017), 3.253), new Datum(new Date(1, Month.June, 2018), 3.288), new Datum(new Date(1, Month.June, 2019), 3.314), new Datum(new Date(1, Month.June, 2020), 3.401), new Datum(new Date(1, Month.June, 2022), 3.458), new Datum(new Date(1, Month.June, 2025), 3.52), new Datum(new Date(1, Month.June, 2030), 3.655), new Datum(new Date(1, Month.June, 2035), 3.668), new Datum(new Date(1, Month.June, 2040), 3.695), new Datum(new Date(1, Month.June, 2050), 3.634), new Datum(new Date(1, Month.June, 2060), 3.629), }; zciisDataLength = 17; for (int i = 0; i < zciisDataLength; i++) { zciisD.Add(zciisData[i].date); zciisR.Add(zciisData[i].rate); } // now build the helpers ... List <BootstrapHelper <ZeroInflationTermStructure> > helpers = makeHelpers(zciisData, zciisDataLength, ii, observationLag, calendar, convention, dcZCIIS); // we can use historical or first ZCIIS for this // we know historical is WAY off market-implied, so use market implied flat. baseZeroRate = zciisData[0].rate / 100.0; PiecewiseZeroInflationCurve <Linear> pCPIts = new PiecewiseZeroInflationCurve <Linear>( evaluationDate, calendar, dcZCIIS, observationLag, ii.frequency(), ii.interpolated(), baseZeroRate, new Handle <YieldTermStructure>(nominalTS), helpers); pCPIts.recalculate(); cpiUK.linkTo(pCPIts); hii.linkTo(ii); // make sure that the index has the latest zero inflation term structure hcpi.linkTo(pCPIts); // cpi CF price surf data Period[] cfMat = { new Period(3, TimeUnit.Years), new Period(5, TimeUnit.Years), new Period(7, TimeUnit.Years), new Period(10, TimeUnit.Years), new Period(15, TimeUnit.Years), new Period(20, TimeUnit.Years), new Period(30, TimeUnit.Years) }; double[] cStrike = { 3, 4, 5, 6 }; double[] fStrike = { -1, 0, 1, 2 }; int ncStrikes = 4, nfStrikes = 4, ncfMaturities = 7; double[][] cPrice = { new double[4] { 227.6, 100.27, 38.8, 14.94 }, new double[4] { 345.32, 127.9, 40.59, 14.11 }, new double[4] { 477.95, 170.19, 50.62, 16.88 }, new double[4] { 757.81, 303.95, 107.62, 43.61 }, new double[4] { 1140.73, 481.89, 168.4, 63.65 }, new double[4] { 1537.6, 607.72, 172.27, 54.87 }, new double[4] { 2211.67, 839.24, 184.75, 45.03 } }; double[][] fPrice = { new double[4] { 15.62, 28.38, 53.61, 104.6 }, new double[4] { 21.45, 36.73, 66.66, 129.6 }, new double[4] { 24.45, 42.08, 77.04, 152.24 }, new double[4] { 39.25, 63.52, 109.2, 203.44 }, new double[4] { 36.82, 63.62, 116.97, 232.73 }, new double[4] { 39.7, 67.47, 121.79, 238.56 }, new double[4] { 41.48, 73.9, 139.75, 286.75 } }; // now load the data into vector and Matrix classes cStrikesUK = new List <double>(); fStrikesUK = new List <double>(); cfMaturitiesUK = new List <Period>(); for (int i = 0; i < ncStrikes; i++) { cStrikesUK.Add(cStrike[i]); } for (int i = 0; i < nfStrikes; i++) { fStrikesUK.Add(fStrike[i]); } for (int i = 0; i < ncfMaturities; i++) { cfMaturitiesUK.Add(cfMat[i]); } cPriceUK = new Matrix(ncStrikes, ncfMaturities); fPriceUK = new Matrix(nfStrikes, ncfMaturities); for (int i = 0; i < ncStrikes; i++) { for (int j = 0; j < ncfMaturities; j++) { (cPriceUK)[i, j] = cPrice[j][i] / 10000.0; } } for (int i = 0; i < nfStrikes; i++) { for (int j = 0; j < ncfMaturities; j++) { (fPriceUK)[i, j] = fPrice[j][i] / 10000.0; } } }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(UKRPI obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
public void testZeroIndex() { // Testing zero inflation indices... EUHICP euhicp = new EUHICP(true); if (euhicp.name() != "EU HICP" || euhicp.frequency() != Frequency.Monthly || euhicp.revised() || !euhicp.interpolated() || euhicp.availabilityLag() != new Period(1, TimeUnit.Months)) { Assert.Fail("wrong EU HICP data (" + euhicp.name() + ", " + euhicp.frequency() + ", " + euhicp.revised() + ", " + euhicp.interpolated() + ", " + euhicp.availabilityLag() + ")"); } UKRPI ukrpi = new UKRPI(false); if (ukrpi.name() != "UK RPI" || ukrpi.frequency() != Frequency.Monthly || ukrpi.revised() || ukrpi.interpolated() || ukrpi.availabilityLag() != new Period(1, TimeUnit.Months)) { Assert.Fail("wrong UK RPI data (" + ukrpi.name() + ", " + ukrpi.frequency() + ", " + ukrpi.revised() + ", " + ukrpi.interpolated() + ", " + ukrpi.availabilityLag() + ")"); } // Retrieval test. //---------------- // make sure of the evaluation date Date evaluationDate = new Date(13, Month.August, 2007); evaluationDate = new UnitedKingdom().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, 206.1, -999.0 }; bool interp = false; UKRPI iir = new UKRPI(interp); for (int i = 0; i < rpiSchedule.Count - 1; i++) { iir.addFixing(rpiSchedule[i], fixData[i]); } Date todayMinusLag = evaluationDate - iir.availabilityLag(); KeyValuePair <Date, Date> lim1 = Utils.inflationPeriod(todayMinusLag, iir.frequency()); todayMinusLag = lim1.Key; double eps = 1.0e-8; // -1 because last value not yet available, // (no TS so can't forecast). for (int i = 0; i < rpiSchedule.Count - 1; i++) { KeyValuePair <Date, Date> lim = Utils.inflationPeriod(rpiSchedule[i], iir.frequency()); for (Date d = lim.Key; d <= lim.Value; d++) { if (d < Utils.inflationPeriod(todayMinusLag, iir.frequency()).Key) { if (Math.Abs(iir.fixing(d) - fixData[i]) > eps) { Assert.Fail("Fixings not constant within a period: " + iir.fixing(d) + ", should be " + fixData[i]); } } } } }
// setup public CommonVars() { backup = new SavedSettings(); cleaner = new IndexHistoryCleaner(); nominalUK = new RelinkableHandle <YieldTermStructure>(); cpiUK = new RelinkableHandle <ZeroInflationTermStructure>(); hcpi = new RelinkableHandle <ZeroInflationTermStructure>(); zciisD = new List <Date>(); zciisR = new List <double>(); hii = new RelinkableHandle <ZeroInflationIndex>(); nominals = new InitializedList <double>(1, 1000000); // option variables frequency = Frequency.Annual; // usual setup volatility = 0.01; length = 7; calendar = new UnitedKingdom(); convention = BusinessDayConvention.ModifiedFollowing; Date today = new Date(25, Month.November, 2009); evaluationDate = calendar.adjust(today); Settings.setEvaluationDate(evaluationDate); settlementDays = 0; fixingDays = 0; settlement = calendar.advance(today, settlementDays, TimeUnit.Days); startDate = settlement; dcZCIIS = new ActualActual(); dcNominal = new ActualActual(); // uk rpi index // fixing data Date from = new Date(20, Month.July, 2007); Date to = new Date(20, Month.November, 2009); Schedule rpiSchedule = new MakeSchedule().from(from).to(to) .withTenor(new Period(1, TimeUnit.Months)) .withCalendar(new UnitedKingdom()) .withConvention(BusinessDayConvention.ModifiedFollowing).value(); double[] fixData = { 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, -999.0, -999.0 }; // link from cpi index to cpi TS bool interp = false;// this MUST be false because the observation lag is only 2 months // for ZCIIS; but not for contract if the contract uses a bigger lag. ii = new UKRPI(interp, hcpi); for (int i = 0; i < rpiSchedule.Count; i++) { ii.addFixing(rpiSchedule[i], fixData[i], true);// force overwrite in case multiple use } Datum[] nominalData = { new Datum(new Date(26, Month.November, 2009), 0.475), new Datum(new Date(2, Month.December, 2009), 0.47498), new Datum(new Date(29, Month.December, 2009), 0.49988), new Datum(new Date(25, Month.February, 2010), 0.59955), new Datum(new Date(18, Month.March, 2010), 0.65361), new Datum(new Date(25, Month.May, 2010), 0.82830), new Datum(new Date(17, Month.June, 2010), 0.7), new Datum(new Date(16, Month.September, 2010), 0.78960), new Datum(new Date(16, Month.December, 2010), 0.93762), new Datum(new Date(17, Month.March, 2011), 1.12037), new Datum(new Date(22, Month.September, 2011), 1.52011), new Datum(new Date(25, Month.November, 2011), 1.78399), new Datum(new Date(26, Month.November, 2012), 2.41170), new Datum(new Date(25, Month.November, 2013), 2.83935), new Datum(new Date(25, Month.November, 2014), 3.12888), new Datum(new Date(25, Month.November, 2015), 3.34298), new Datum(new Date(25, Month.November, 2016), 3.50632), new Datum(new Date(27, Month.November, 2017), 3.63666), new Datum(new Date(26, Month.November, 2018), 3.74723), new Datum(new Date(25, Month.November, 2019), 3.83988), new Datum(new Date(25, Month.November, 2021), 4.00508), new Datum(new Date(25, Month.November, 2024), 4.16042), new Datum(new Date(26, Month.November, 2029), 4.15577), new Datum(new Date(27, Month.November, 2034), 4.04933), new Datum(new Date(25, Month.November, 2039), 3.95217), new Datum(new Date(25, Month.November, 2049), 3.80932), new Datum(new Date(25, Month.November, 2059), 3.80849), new Datum(new Date(25, Month.November, 2069), 3.72677), new Datum(new Date(27, Month.November, 2079), 3.63082) }; int nominalDataLength = 30 - 1; List <Date> nomD = new List <Date>(); List <double> nomR = new List <double>(); for (int i = 0; i < nominalDataLength; i++) { nomD.Add(nominalData[i].date); nomR.Add(nominalData[i].rate / 100.0); } YieldTermStructure nominal = new InterpolatedZeroCurve <Linear>(nomD, nomR, dcNominal); nominalUK.linkTo(nominal); // now build the zero inflation curve observationLag = new Period(2, TimeUnit.Months); contractObservationLag = new Period(3, TimeUnit.Months); contractObservationInterpolation = InterpolationType.Flat; Datum[] zciisData = { new Datum(new Date(25, Month.November, 2010), 3.0495), new Datum(new Date(25, Month.November, 2011), 2.93), new Datum(new Date(26, Month.November, 2012), 2.9795), new Datum(new Date(25, Month.November, 2013), 3.029), new Datum(new Date(25, Month.November, 2014), 3.1425), new Datum(new Date(25, Month.November, 2015), 3.211), new Datum(new Date(25, Month.November, 2016), 3.2675), new Datum(new Date(25, Month.November, 2017), 3.3625), new Datum(new Date(25, Month.November, 2018), 3.405), new Datum(new Date(25, Month.November, 2019), 3.48), new Datum(new Date(25, Month.November, 2021), 3.576), new Datum(new Date(25, Month.November, 2024), 3.649), new Datum(new Date(26, Month.November, 2029), 3.751), new Datum(new Date(27, Month.November, 2034), 3.77225), new Datum(new Date(25, Month.November, 2039), 3.77), new Datum(new Date(25, Month.November, 2049), 3.734), new Datum(new Date(25, Month.November, 2059), 3.714) }; zciisDataLength = 17; for (int i = 0; i < zciisDataLength; i++) { zciisD.Add(zciisData[i].date); zciisR.Add(zciisData[i].rate); } // now build the helpers ... List <BootstrapHelper <ZeroInflationTermStructure> > helpers = makeHelpers(zciisData, zciisDataLength, ii, observationLag, calendar, convention, dcZCIIS); // we can use historical or first ZCIIS for this // we know historical is WAY off market-implied, so use market implied flat. double baseZeroRate = zciisData[0].rate / 100.0; PiecewiseZeroInflationCurve <Linear> pCPIts = new PiecewiseZeroInflationCurve <Linear>( evaluationDate, calendar, dcZCIIS, observationLag, ii.frequency(), ii.interpolated(), baseZeroRate, new Handle <YieldTermStructure>(nominalUK), helpers); pCPIts.recalculate(); cpiUK.linkTo(pCPIts); // make sure that the index has the latest zero inflation term structure hcpi.linkTo(pCPIts); }
public void testZeroTermStructure() { // Testing zero inflation term structure... SavedSettings backup = new SavedSettings(); // try the Zero 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, 206.1, -999.0 }; RelinkableHandle <ZeroInflationTermStructure> hz = new RelinkableHandle <ZeroInflationTermStructure>(); bool interp = false; UKRPI iiUKRPI = new UKRPI(interp, hz); for (int i = 0; i < rpiSchedule.Count; i++) { iiUKRPI.addFixing(rpiSchedule[i], fixData[i]); } ZeroInflationIndex ii = iiUKRPI as ZeroInflationIndex; YieldTermStructure nominalTS = nominalTermStructure(); // now build the zero inflation curve Datum[] zcData = { new Datum(new Date(13, Month.August, 2008), 2.93), new Datum(new Date(13, Month.August, 2009), 2.95), new Datum(new Date(13, Month.August, 2010), 2.965), new Datum(new Date(15, Month.August, 2011), 2.98), new Datum(new Date(13, Month.August, 2012), 3.0), new Datum(new Date(13, Month.August, 2014), 3.06), new Datum(new Date(13, Month.August, 2017), 3.175), new Datum(new Date(13, Month.August, 2019), 3.243), new Datum(new Date(15, Month.August, 2022), 3.293), new Datum(new Date(14, Month.August, 2027), 3.338), new Datum(new Date(13, Month.August, 2032), 3.348), new Datum(new Date(15, Month.August, 2037), 3.348), new Datum(new Date(13, Month.August, 2047), 3.308), new Datum(new Date(13, Month.August, 2057), 3.228) }; Period observationLag = new Period(2, TimeUnit.Months); DayCounter dc = new Thirty360(); Frequency frequency = Frequency.Monthly; List <BootstrapHelper <ZeroInflationTermStructure> > helpers = makeHelpers(zcData, zcData.Length, ii, observationLag, calendar, bdc, dc); double baseZeroRate = zcData[0].rate / 100.0; PiecewiseZeroInflationCurve <Linear> pZITS = new PiecewiseZeroInflationCurve <Linear>( evaluationDate, calendar, dc, observationLag, frequency, ii.interpolated(), baseZeroRate, new Handle <YieldTermStructure>(nominalTS), helpers); pZITS.recalculate(); // first check that the zero rates on the curve match the data // and that the helpers give the correct impled rates const double eps = 0.00000001; bool forceLinearInterpolation = false; for (int i = 0; i < zcData.Length; i++) { Assert.IsTrue(Math.Abs(zcData[i].rate / 100.0 - pZITS.zeroRate(zcData[i].date, observationLag, forceLinearInterpolation)) < eps, "ZITS zeroRate != instrument " + pZITS.zeroRate(zcData[i].date, observationLag, forceLinearInterpolation) + " vs " + zcData[i].rate / 100.0 + " interpolation: " + ii.interpolated() + " forceLinearInterpolation " + forceLinearInterpolation); Assert.IsTrue(Math.Abs(helpers[i].impliedQuote() - zcData[i].rate / 100.0) < eps, "ZITS implied quote != instrument " + helpers[i].impliedQuote() + " vs " + zcData[i].rate / 100.0); } // now test the forecasting capability of the index. hz.linkTo(pZITS); from = hz.link.baseDate(); to = hz.link.maxDate() - new Period(1, TimeUnit.Months); // a bit of margin for adjustments Schedule testIndex = new MakeSchedule().from(from).to(to) .withTenor(new Period(1, TimeUnit.Months)) .withCalendar(new UnitedKingdom()) .withConvention(BusinessDayConvention.ModifiedFollowing).value(); // we are testing UKRPI which is not interpolated Date bd = hz.link.baseDate(); double bf = ii.fixing(bd); for (int i = 0; i < testIndex.Count; i++) { Date d = testIndex[i]; double z = hz.link.zeroRate(d, new Period(0, TimeUnit.Days)); double t = hz.link.dayCounter().yearFraction(bd, d); if (!ii.interpolated()) // because fixing constant over period { t = hz.link.dayCounter().yearFraction(bd, Utils.inflationPeriod(d, ii.frequency()).Key); } double calc = bf * Math.Pow(1 + z, t); if (t <= 0) { calc = ii.fixing(d, false); // still historical } if (Math.Abs(calc - ii.fixing(d, true)) / 10000.0 > eps) { Assert.Fail("ZC index does not forecast correctly for date " + d + " from base date " + bd + " with fixing " + bf + ", correct: " + calc + ", fix: " + ii.fixing(d, true) + ", t " + t); } } //=========================================================================================== // Test zero-inflation-indexed (i.e. cpi ratio) cashflow // just ordinary indexed cashflow with a zero inflation index Date baseDate = new Date(1, Month.January, 2006); Date fixDate = new Date(1, Month.August, 2014); Date payDate = new UnitedKingdom().adjust(fixDate + new Period(3, TimeUnit.Months), BusinessDayConvention.ModifiedFollowing); Index ind = ii as Index; Utils.QL_REQUIRE(ind != null, () => "dynamic_pointer_cast to Index from InflationIndex failed"); double notional = 1000000.0;//1m IndexedCashFlow iicf = new IndexedCashFlow(notional, ind, baseDate, fixDate, payDate); double correctIndexed = ii.fixing(iicf.fixingDate()) / ii.fixing(iicf.baseDate()); double calculatedIndexed = iicf.amount() / iicf.notional(); Assert.IsTrue(Math.Abs(correctIndexed - calculatedIndexed) < eps, "IndexedCashFlow indexing wrong: " + calculatedIndexed + " vs correct = " + correctIndexed); //=========================================================================================== // Test zero coupon swap // first make one ... ZeroInflationIndex zii = ii as ZeroInflationIndex; Utils.QL_REQUIRE(zii != null, () => "dynamic_pointer_cast to ZeroInflationIndex from UKRPI failed"); ZeroCouponInflationSwap nzcis = new ZeroCouponInflationSwap(ZeroCouponInflationSwap.Type.Payer, 1000000.0, evaluationDate, zcData[6].date, // end date = maturity calendar, bdc, dc, zcData[6].rate / 100.0, // fixed rate zii, observationLag); // N.B. no coupon pricer because it is not a coupon, effect of inflation curve via // inflation curve attached to the inflation index. Handle <YieldTermStructure> hTS = new Handle <YieldTermStructure>(nominalTS); IPricingEngine sppe = new DiscountingSwapEngine(hTS); nzcis.setPricingEngine(sppe); // ... and price it, should be zero Assert.IsTrue(Math.Abs(nzcis.NPV()) < 0.00001, "ZCIS does not reprice to zero " + nzcis.NPV() + evaluationDate + " to " + zcData[6].date + " becoming " + nzcis.maturityDate() + " rate " + zcData[6].rate + " fixed leg " + nzcis.legNPV(0) + " indexed-predicted inflated leg " + nzcis.legNPV(1) + " discount " + nominalTS.discount(nzcis.maturityDate())); //=========================================================================================== // Test multiplicative seasonality in price // //Seasonality factors NOT normalized //and UKRPI is not interpolated Date trueBaseDate = Utils.inflationPeriod(hz.link.baseDate(), ii.frequency()).Value; Date seasonallityBaseDate = new Date(31, Month.January, trueBaseDate.year()); List <double> seasonalityFactors = new List <double>(12); seasonalityFactors.Add(1.003245); seasonalityFactors.Add(1.000000); seasonalityFactors.Add(0.999715); seasonalityFactors.Add(1.000495); seasonalityFactors.Add(1.000929); seasonalityFactors.Add(0.998687); seasonalityFactors.Add(0.995949); seasonalityFactors.Add(0.994682); seasonalityFactors.Add(0.995949); seasonalityFactors.Add(1.000519); seasonalityFactors.Add(1.003705); seasonalityFactors.Add(1.004186); //Creating two different seasonality objects // MultiplicativePriceSeasonality seasonality_1 = new MultiplicativePriceSeasonality(); InitializedList <double> seasonalityFactors_1 = new InitializedList <double>(12, 1.0); seasonality_1.set(seasonallityBaseDate, Frequency.Monthly, seasonalityFactors_1); MultiplicativePriceSeasonality seasonality_real = new MultiplicativePriceSeasonality(seasonallityBaseDate, Frequency.Monthly, seasonalityFactors); //Testing seasonality correction when seasonality factors are = 1 // double[] fixing = { ii.fixing(new Date(14, Month.January, 2013), true), ii.fixing(new Date(14, Month.February, 2013), true), ii.fixing(new Date(14, Month.March, 2013), true), ii.fixing(new Date(14, Month.April, 2013), true), ii.fixing(new Date(14, Month.May, 2013), true), ii.fixing(new Date(14, Month.June, 2013), true), ii.fixing(new Date(14, Month.July, 2013), true), ii.fixing(new Date(14, Month.August, 2013), true), ii.fixing(new Date(14, Month.September, 2013), true), ii.fixing(new Date(14, Month.October, 2013), true), ii.fixing(new Date(14, Month.November, 2013), true), ii.fixing(new Date(14, Month.December, 2013), true) }; hz.link.setSeasonality(seasonality_1); Utils.QL_REQUIRE(hz.link.hasSeasonality(), () => "[44] incorrectly believes NO seasonality correction"); double[] seasonalityFixing_1 = { ii.fixing(new Date(14, Month.January, 2013), true), ii.fixing(new Date(14, Month.February, 2013), true), ii.fixing(new Date(14, Month.March, 2013), true), ii.fixing(new Date(14, Month.April, 2013), true), ii.fixing(new Date(14, Month.May, 2013), true), ii.fixing(new Date(14, Month.June, 2013), true), ii.fixing(new Date(14, Month.July, 2013), true), ii.fixing(new Date(14, Month.August, 2013), true), ii.fixing(new Date(14, Month.September, 2013), true), ii.fixing(new Date(14, Month.October, 2013), true), ii.fixing(new Date(14, Month.November, 2013), true), ii.fixing(new Date(14, Month.December, 2013), true) }; for (int i = 0; i < 12; i++) { if (Math.Abs(fixing[i] - seasonalityFixing_1[i]) > eps) { Assert.Fail("Seasonality doesn't work correctly when seasonality factors are set = 1"); } } //Testing seasonality correction when seasonality factors are different from 1 // //0.998687 is the seasonality factor corresponding to June (the base CPI curve month) // double[] expectedFixing = { ii.fixing(new Date(14, Month.January, 2013), true) * 1.003245 / 0.998687, ii.fixing(new Date(14, Month.February, 2013), true) * 1.000000 / 0.998687, ii.fixing(new Date(14, Month.March, 2013), true) * 0.999715 / 0.998687, ii.fixing(new Date(14, Month.April, 2013), true) * 1.000495 / 0.998687, ii.fixing(new Date(14, Month.May, 2013), true) * 1.000929 / 0.998687, ii.fixing(new Date(14, Month.June, 2013), true) * 0.998687 / 0.998687, ii.fixing(new Date(14, Month.July, 2013), true) * 0.995949 / 0.998687, ii.fixing(new Date(14, Month.August, 2013), true) * 0.994682 / 0.998687, ii.fixing(new Date(14, Month.September, 2013), true) * 0.995949 / 0.998687, ii.fixing(new Date(14, Month.October, 2013), true) * 1.000519 / 0.998687, ii.fixing(new Date(14, Month.November, 2013), true) * 1.003705 / 0.998687, ii.fixing(new Date(14, Month.December, 2013), true) * 1.004186 / 0.998687 }; hz.link.setSeasonality(seasonality_real); double[] seasonalityFixing_real = { ii.fixing(new Date(14, Month.January, 2013), true), ii.fixing(new Date(14, Month.February, 2013), true), ii.fixing(new Date(14, Month.March, 2013), true), ii.fixing(new Date(14, Month.April, 2013), true), ii.fixing(new Date(14, Month.May, 2013), true), ii.fixing(new Date(14, Month.June, 2013), true), ii.fixing(new Date(14, Month.July, 2013), true), ii.fixing(new Date(14, Month.August, 2013), true), ii.fixing(new Date(14, Month.September, 2013), true), ii.fixing(new Date(14, Month.October, 2013), true), ii.fixing(new Date(14, Month.November, 2013), true), ii.fixing(new Date(14, Month.December, 2013), true) }; for (int i = 0; i < 12; i++) { if (Math.Abs(expectedFixing[i] - seasonalityFixing_real[i]) > 0.01) { Assert.Fail("Seasonality doesn't work correctly when considering seasonality factors != 1 " + expectedFixing[i] + " vs " + seasonalityFixing_real[i]); } } //Testing Unset function // Utils.QL_REQUIRE(hz.link.hasSeasonality(), () => "[4] incorrectly believes NO seasonality correction"); hz.link.setSeasonality(); Utils.QL_REQUIRE(!hz.link.hasSeasonality(), () => "[5] incorrectly believes HAS seasonality correction"); double[] seasonalityFixing_unset = { ii.fixing(new Date(14, Month.January, 2013), true), ii.fixing(new Date(14, Month.February, 2013), true), ii.fixing(new Date(14, Month.March, 2013), true), ii.fixing(new Date(14, Month.April, 2013), true), ii.fixing(new Date(14, Month.May, 2013), true), ii.fixing(new Date(14, Month.June, 2013), true), ii.fixing(new Date(14, Month.July, 2013), true), ii.fixing(new Date(14, Month.August, 2013), true), ii.fixing(new Date(14, Month.September, 2013), true), ii.fixing(new Date(14, Month.October, 2013), true), ii.fixing(new Date(14, Month.November, 2013), true), ii.fixing(new Date(14, Month.December, 2013), true) }; for (int i = 0; i < 12; i++) { if (Math.Abs(seasonalityFixing_unset[i] - seasonalityFixing_1[i]) > eps) { Assert.Fail("UnsetSeasonality doesn't work correctly " + seasonalityFixing_unset[i] + " vs " + seasonalityFixing_1[i]); } } //============================================================================== // now do an INTERPOLATED index, i.e. repeat everything on a fake version of // UKRPI (to save making another term structure) bool interpYES = true; UKRPI iiUKRPIyes = new UKRPI(interpYES, hz); for (int i = 0; i < fixData.Length; i++) { iiUKRPIyes.addFixing(rpiSchedule[i], fixData[i]); } ZeroInflationIndex iiyes = iiUKRPIyes as ZeroInflationIndex; // now build the zero inflation curve // same data, bigger lag or it will be a self-contradiction Period observationLagyes = new Period(3, TimeUnit.Months); List <BootstrapHelper <ZeroInflationTermStructure> > helpersyes = makeHelpers(zcData, zcData.Length, iiyes, observationLagyes, calendar, bdc, dc); PiecewiseZeroInflationCurve <Linear> pZITSyes = new PiecewiseZeroInflationCurve <Linear>( evaluationDate, calendar, dc, observationLagyes, frequency, iiyes.interpolated(), baseZeroRate, new Handle <YieldTermStructure>(nominalTS), helpersyes); pZITSyes.recalculate(); // first check that the zero rates on the curve match the data // and that the helpers give the correct impled rates forceLinearInterpolation = false; // still for (int i = 0; i < zcData.Length; i++) { Assert.IsTrue(Math.Abs(zcData[i].rate / 100.0 - pZITSyes.zeroRate(zcData[i].date, observationLagyes, forceLinearInterpolation)) < eps, "ZITS INTERPOLATED zeroRate != instrument " + pZITSyes.zeroRate(zcData[i].date, observationLagyes, forceLinearInterpolation) + " date " + zcData[i].date + " observationLagyes " + observationLagyes + " vs " + zcData[i].rate / 100.0 + " interpolation: " + iiyes.interpolated() + " forceLinearInterpolation " + forceLinearInterpolation); Assert.IsTrue(Math.Abs(helpersyes[i].impliedQuote() - zcData[i].rate / 100.0) < eps, "ZITS INTERPOLATED implied quote != instrument " + helpersyes[i].impliedQuote() + " vs " + zcData[i].rate / 100.0); } //====================================================================================== // now test the forecasting capability of the index. hz.linkTo(pZITSyes); from = hz.link.baseDate() + new Period(1, TimeUnit.Months); // to avoid historical linear bit for rest of base month to = hz.link.maxDate() - new Period(1, TimeUnit.Months); // a bit of margin for adjustments testIndex = new MakeSchedule().from(from).to(to) .withTenor(new Period(1, TimeUnit.Months)) .withCalendar(new UnitedKingdom()) .withConvention(BusinessDayConvention.ModifiedFollowing).value(); // we are testing UKRPI which is FAKE interpolated for testing here bd = hz.link.baseDate(); bf = iiyes.fixing(bd); for (int i = 0; i < testIndex.Count; i++) { Date d = testIndex[i]; double z = hz.link.zeroRate(d, new Period(0, TimeUnit.Days)); double t = hz.link.dayCounter().yearFraction(bd, d); double calc = bf * Math.Pow(1 + z, t); if (t <= 0) { calc = iiyes.fixing(d); // still historical } if (Math.Abs(calc - iiyes.fixing(d)) > eps) { Assert.Fail("ZC INTERPOLATED index does not forecast correctly for date " + d + " from base date " + bd + " with fixing " + bf + ", correct: " + calc + ", fix: " + iiyes.fixing(d) + ", t " + t + ", zero " + z); } } //=========================================================================================== // Test zero coupon swap ZeroInflationIndex ziiyes = iiyes as ZeroInflationIndex; Utils.QL_REQUIRE(ziiyes != null, () => "dynamic_pointer_cast to ZeroInflationIndex from UKRPI-I failed"); ZeroCouponInflationSwap nzcisyes = new ZeroCouponInflationSwap(ZeroCouponInflationSwap.Type.Payer, 1000000.0, evaluationDate, zcData[6].date, // end date = maturity calendar, bdc, dc, zcData[6].rate / 100.0, // fixed rate ziiyes, observationLagyes); // N.B. no coupon pricer because it is not a coupon, effect of inflation curve via // inflation curve attached to the inflation index. nzcisyes.setPricingEngine(sppe); // ... and price it, should be zero Assert.IsTrue(Math.Abs(nzcisyes.NPV()) < 0.00001, "ZCIS-I does not reprice to zero " + nzcisyes.NPV() + evaluationDate + " to " + zcData[6].date + " becoming " + nzcisyes.maturityDate() + " rate " + zcData[6].rate + " fixed leg " + nzcisyes.legNPV(0) + " indexed-predicted inflated leg " + nzcisyes.legNPV(1) + " discount " + nominalTS.discount(nzcisyes.maturityDate()) ); // remove circular refernce hz.linkTo(new ZeroInflationTermStructure()); }