Пример #1
0
        public void testLinearInterpolationMultipleSpreads()
        {
            // Testing linear interpolation with more than two spreaded dates...

            CommonVars vars = new CommonVars();

            List <Handle <Quote> > spreads = new List <Handle <Quote> >();
            SimpleQuote            spread1 = new SimpleQuote(0.02);
            SimpleQuote            spread2 = new SimpleQuote(0.02);
            SimpleQuote            spread3 = new SimpleQuote(0.035);
            SimpleQuote            spread4 = new SimpleQuote(0.04);

            spreads.Add(new Handle <Quote>(spread1));
            spreads.Add(new Handle <Quote>(spread2));
            spreads.Add(new Handle <Quote>(spread3));
            spreads.Add(new Handle <Quote>(spread4));

            List <Date> spreadDates = new List <Date>();

            spreadDates.Add(vars.calendar.advance(vars.today, 90, TimeUnit.Days));
            spreadDates.Add(vars.calendar.advance(vars.today, 150, TimeUnit.Days));
            spreadDates.Add(vars.calendar.advance(vars.today, 30, TimeUnit.Months));
            spreadDates.Add(vars.calendar.advance(vars.today, 40, TimeUnit.Months));

            Date interpolationDate = vars.calendar.advance(vars.today, 120, TimeUnit.Days);

            ZeroYieldStructure spreadedTermStructure =
                new PiecewiseZeroSpreadedTermStructure(
                    new Handle <YieldTermStructure>(vars.termStructure),
                    spreads, spreadDates);

            double t = vars.dayCount.yearFraction(vars.today, interpolationDate);
            double interpolatedZeroRate = spreadedTermStructure.zeroRate(t, vars.compounding).value();

            double tolerance    = 1e-9;
            double expectedRate = vars.termStructure.zeroRate(t, vars.compounding).value() +
                                  spread1.value();

            if (Math.Abs(interpolatedZeroRate - expectedRate) > tolerance)
            {
                QAssert.Fail(
                    "unable to reproduce interpolated rate\n"

                    + "    calculated: " + interpolatedZeroRate + "\n"
                    + "    expected: " + expectedRate);
            }
        }
Пример #2
0
        public void testFlatInterpolationRight()
        {
            // Testing flat interpolation after the last spreaded date...

            CommonVars vars = new CommonVars();

            List <Handle <Quote> > spreads = new List <Handle <Quote> >();
            SimpleQuote            spread1 = new SimpleQuote(0.02);
            SimpleQuote            spread2 = new SimpleQuote(0.03);

            spreads.Add(new Handle <Quote>(spread1));
            spreads.Add(new Handle <Quote>(spread2));

            List <Date> spreadDates = new List <Date>();

            spreadDates.Add(vars.calendar.advance(vars.today, 8, TimeUnit.Months));
            spreadDates.Add(vars.calendar.advance(vars.today, 15, TimeUnit.Months));

            Date interpolationDate = vars.calendar.advance(vars.today, 20, TimeUnit.Months);

            ZeroYieldStructure spreadedTermStructure =
                new PiecewiseZeroSpreadedTermStructure(
                    new Handle <YieldTermStructure>(vars.termStructure),
                    spreads, spreadDates);

            spreadedTermStructure.enableExtrapolation();

            double t = vars.dayCount.yearFraction(vars.today, interpolationDate);
            double interpolatedZeroRate = spreadedTermStructure.zeroRate(t, vars.compounding).value();

            double tolerance    = 1e-9;
            double expectedRate = vars.termStructure.zeroRate(t, vars.compounding).value() + spread2.value();

            if (Math.Abs(interpolatedZeroRate - expectedRate) > tolerance)
            {
                QAssert.Fail("unable to reproduce interpolated rate\n"
                             + "    calculated: " + interpolatedZeroRate + "\n"
                             + "    expected: " + expectedRate);
            }
        }
        public void testDefaultInterpolation()
        {
            // Testing default interpolation between two dates...

            CommonVars vars = new CommonVars();

            List <Handle <Quote> > spreads = new List <Handle <Quote> >();
            SimpleQuote            spread1 = new SimpleQuote(0.02);
            SimpleQuote            spread2 = new SimpleQuote(0.02);

            spreads.Add(new Handle <Quote>(spread1));
            spreads.Add(new Handle <Quote>(spread2));

            List <Date> spreadDates = new List <Date>();

            spreadDates.Add(vars.calendar.advance(vars.today, 75, TimeUnit.Days));
            spreadDates.Add(vars.calendar.advance(vars.today, 160, TimeUnit.Days));

            Date interpolationDate = vars.calendar.advance(vars.today, 100, TimeUnit.Days);

            ZeroYieldStructure spreadedTermStructure =
                new PiecewiseZeroSpreadedTermStructure(
                    new Handle <YieldTermStructure>(vars.termStructure),
                    spreads, spreadDates);

            double t = vars.dayCount.yearFraction(vars.today, interpolationDate);
            double interpolatedZeroRate = spreadedTermStructure.zeroRate(t, vars.compounding).value();

            double tolerance    = 1e-9;
            double expectedRate = vars.termStructure.zeroRate(t, vars.compounding).value() +
                                  spread1.value();

            if (Math.Abs(interpolatedZeroRate - expectedRate) > tolerance)
            {
                Assert.Fail(
                    "unable to reproduce interpolated rate\n"
                    + "    calculated: " + interpolatedZeroRate + "\n"
                    + "    expected: " + expectedRate);
            }
        }
        public void testDefaultInterpolation()
        {
            // Testing default interpolation between two dates...

             CommonVars vars = new CommonVars();

             List<Handle<Quote>> spreads = new List<Handle<Quote>>();
             SimpleQuote spread1 = new SimpleQuote(0.02);
             SimpleQuote spread2 = new SimpleQuote(0.02);
             spreads.Add(new Handle<Quote>(spread1));
             spreads.Add(new Handle<Quote>(spread2));

             List<Date> spreadDates = new List<Date>();
             spreadDates.Add(vars.calendar.advance(vars.today, 75, TimeUnit.Days));
             spreadDates.Add(vars.calendar.advance(vars.today, 160, TimeUnit.Days));

             Date interpolationDate = vars.calendar.advance(vars.today, 100, TimeUnit.Days);

             ZeroYieldStructure spreadedTermStructure =
             new PiecewiseZeroSpreadedTermStructure(
                                    new Handle<YieldTermStructure>(vars.termStructure),
                                    spreads, spreadDates);

             double t = vars.dayCount.yearFraction(vars.today, interpolationDate);
             double interpolatedZeroRate = spreadedTermStructure.zeroRate(t, vars.compounding).value();

             double tolerance = 1e-9;
             double expectedRate = vars.termStructure.zeroRate(t, vars.compounding).value() +
                             spread1.value();

             if (Math.Abs(interpolatedZeroRate - expectedRate) > tolerance)
            Assert.Fail(
                "unable to reproduce interpolated rate\n"
                + "    calculated: " + interpolatedZeroRate + "\n"
                + "    expected: " + expectedRate);
        }