Example #1
0
        public void testQuoteChanging()
        {
            // Testing quote update...

            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, 100, TimeUnit.Days));
            spreadDates.Add(vars.calendar.advance(vars.today, 150, TimeUnit.Days));

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

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

            double t = vars.dayCount.yearFraction(vars.settlementDate, interpolationDate);
            double interpolatedZeroRate = spreadedTermStructure.zeroRate(t, vars.compounding).value();
            double tolerance            = 1e-9;
            double expectedRate         = vars.termStructure.zeroRate(t, vars.compounding).value() +
                                          0.03;

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

            spread2.setValue(0.025);

            interpolatedZeroRate = spreadedTermStructure.zeroRate(t, vars.compounding).value();
            expectedRate         = vars.termStructure.zeroRate(t, vars.compounding).value() +
                                   0.025;

            if (Math.Abs(interpolatedZeroRate - expectedRate) > tolerance)
            {
                QAssert.Fail(
                    "unable to reproduce interpolated rate\n"
                    + "    calculated: " + interpolatedZeroRate + "\n"
                    + "    expected: " + expectedRate);
            }
        }
Example #2
0
        public void testSetInterpolationFactory()
        {
            // Testing factory constructor with additional parameters...

            CommonVars vars = new CommonVars();

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

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

            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));
            spreadDates.Add(vars.calendar.advance(vars.today, 25, TimeUnit.Months));

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

            ZeroYieldStructure spreadedTermStructure;

            Frequency freq = Frequency.NoFrequency;

            Cubic factory = new Cubic(CubicInterpolation.DerivativeApprox.Spline,
                                      false,
                                      CubicInterpolation.BoundaryCondition.SecondDerivative, 0,
                                      CubicInterpolation.BoundaryCondition.SecondDerivative, 0);

            spreadedTermStructure =
                new InterpolatedPiecewiseZeroSpreadedTermStructure <Cubic>(
                    new Handle <YieldTermStructure>(vars.termStructure),
                    spreads, spreadDates, vars.compounding,
                    freq, vars.dayCount, factory);

            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() +
                                  0.026065770863;

            if (Math.Abs(interpolatedZeroRate - expectedRate) > tolerance)
            {
                QAssert.Fail(
                    "unable to reproduce interpolated rate\n"
                    + "    calculated: " + interpolatedZeroRate + "\n"
                    + "    expected: " + expectedRate);
            }
        }
Example #3
0
        public void testLinearInterpolation()
        {
            // Testing linear 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.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, 100, TimeUnit.Days));
            spreadDates.Add(vars.calendar.advance(vars.today, 150, TimeUnit.Days));

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

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

            Date d0 = vars.calendar.advance(vars.today, 100, TimeUnit.Days);
            Date d1 = vars.calendar.advance(vars.today, 150, TimeUnit.Days);
            Date d2 = vars.calendar.advance(vars.today, 120, TimeUnit.Days);

            double m            = (0.03 - 0.02) / vars.dayCount.yearFraction(d0, d1);
            double expectedRate = m * vars.dayCount.yearFraction(d0, d2) + 0.054;

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

            double tolerance = 1e-9;

            if (Math.Abs(interpolatedZeroRate - expectedRate) > tolerance)
            {
                QAssert.Fail(
                    "unable to reproduce interpolated rate\n"
                    + "    calculated: " + interpolatedZeroRate + "\n"
                    + "    expected: " + expectedRate);
            }
        }
        public void testForwardFlatInterpolation()
        {
            // Testing forward flat 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.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, 75, TimeUnit.Days));
            spreadDates.Add(vars.calendar.advance(vars.today, 260, TimeUnit.Days));

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

            ZeroYieldStructure spreadedTermStructure =
                new InterpolatedPiecewiseZeroSpreadedTermStructure <ForwardFlat>(
                    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 testBackwardFlatInterpolation()
        {
            // Testing backward flat 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.03);
             SimpleQuote spread3 = new SimpleQuote(0.04);
             spreads.Add(new Handle<Quote>(spread1));
             spreads.Add(new Handle<Quote>(spread2));
             spreads.Add(new Handle<Quote>(spread3));

             List<Date> spreadDates = new List<Date>();
             spreadDates.Add(vars.calendar.advance(vars.today, 100, TimeUnit.Days));
             spreadDates.Add(vars.calendar.advance(vars.today, 200, TimeUnit.Days));
             spreadDates.Add(vars.calendar.advance(vars.today, 300, TimeUnit.Days));

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

             ZeroYieldStructure spreadedTermStructure =
             new InterpolatedPiecewiseZeroSpreadedTermStructure<BackwardFlat>(
                             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() +
                             spread2.value();

             if (Math.Abs(interpolatedZeroRate - expectedRate) > tolerance)
            Assert.Fail(
                "unable to reproduce interpolated rate\n"
                + "    calculated: " + interpolatedZeroRate + "\n"
                + "    expected: " + expectedRate);
        }
        public void testSetInterpolationFactory()
        {
            // Testing factory constructor with additional parameters...

             CommonVars vars = new CommonVars();

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

             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));
             spreadDates.Add(vars.calendar.advance(vars.today, 25, TimeUnit.Months));

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

             ZeroYieldStructure spreadedTermStructure;

             Frequency freq = Frequency.NoFrequency;

             Cubic factory = new Cubic(CubicInterpolation.DerivativeApprox.Spline,
                                   false,
                                   CubicInterpolation.BoundaryCondition.SecondDerivative, 0,
                                   CubicInterpolation.BoundaryCondition.SecondDerivative, 0);

             spreadedTermStructure =
             new InterpolatedPiecewiseZeroSpreadedTermStructure<Cubic>(
                                    new Handle<YieldTermStructure>(vars.termStructure),
                                    spreads, spreadDates, vars.compounding,
                                    freq, vars.dayCount, factory);

             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() +
                             0.026065770863;

             if (Math.Abs(interpolatedZeroRate - expectedRate) > tolerance)
            Assert.Fail(
                "unable to reproduce interpolated rate\n"
                + "    calculated: " + interpolatedZeroRate + "\n"
                + "    expected: " + expectedRate);
        }
        public void testLinearInterpolation()
        {
            // Testing linear 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.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, 100, TimeUnit.Days));
             spreadDates.Add(vars.calendar.advance(vars.today, 150, TimeUnit.Days));

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

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

             Date d0 = vars.calendar.advance(vars.today, 100, TimeUnit.Days);
             Date d1 = vars.calendar.advance(vars.today, 150, TimeUnit.Days);
             Date d2 = vars.calendar.advance(vars.today, 120, TimeUnit.Days);

             double m = (0.03 - 0.02) / vars.dayCount.yearFraction(d0, d1);
             double expectedRate = m * vars.dayCount.yearFraction(d0, d2) + 0.054;

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

             double tolerance = 1e-9;

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