コード例 #1
0
        public void testCashedValues()
        {
            Date     startDate = new Date(01, 03, 2007);
            Period   period    = new Period(360, TimeUnit.Months);
            Calendar calendar  = new TARGET();
            Date     endDate   = calendar.advance(startDate, period, BusinessDayConvention.Unadjusted);

            Schedule schedule = new Schedule(startDate, endDate, new Period(1, TimeUnit.Months), calendar,
                                             BusinessDayConvention.Unadjusted,
                                             BusinessDayConvention.Unadjusted,
                                             DateGeneration.Rule.Backward, false);

            // PSA 100%
            PSACurve psa100 = new PSACurve(startDate);

            double[] listCPR = { 0.2000, 0.4000, 0.6000, 0.8000, 1.0000, 1.2000, 1.4000, 1.6000, 1.8000, 2.0000, 2.2000, 2.4000, 2.6000, 2.8000,
                                 3.0000, 3.2000, 3.4000, 3.6000, 3.8000, 4.0000, 4.2000, 4.4000, 4.6000, 4.8000, 5.0000, 5.2000, 5.4000, 5.6000,
                                 5.8000, 6.0000 };

            for (int i = 0; i < schedule.Count; i++)
            {
                if (i <= 29)
                {
                    QAssert.AreEqual(listCPR[i], psa100.getCPR(schedule[i]) * 100, 0.001);
                }
                else
                {
                    QAssert.AreEqual(6.0000, psa100.getCPR(schedule[i]) * 100);
                }
            }
        }
コード例 #2
0
        public void testEventSetForWholeYears()
        {
            // Testing that catastrophe events are split correctly for periods of whole years

            EventSet      catRisk    = new EventSet(sampleEvents, eventsStart, eventsEnd);
            CatSimulation simulation = catRisk.newSimulation(new Date(1, Month.January, 2015), new Date(31, Month.December, 2015));

            QAssert.Require(simulation);

            List <KeyValuePair <Date, double> > path = new List <KeyValuePair <Date, double> >();

            QAssert.Require(simulation.nextPath(path));
            QAssert.AreEqual(0, path.Count);

            QAssert.Require(simulation.nextPath(path));
            QAssert.AreEqual(1, path.Count);
            QAssert.AreEqual(new Date(1, Month.February, 2015), path[0].Key);
            QAssert.AreEqual(100, path[0].Value);

            QAssert.Require(simulation.nextPath(path));
            QAssert.AreEqual(1, path.Count);
            QAssert.AreEqual(new Date(1, Month.July, 2015), path[0].Key);
            QAssert.AreEqual(150, path[0].Value);

            QAssert.Require(simulation.nextPath(path));
            QAssert.AreEqual(1, path.Count);
            QAssert.AreEqual(new Date(5, Month.January, 2015), path[0].Key);
            QAssert.AreEqual(50, path[0].Value);

            QAssert.Require(!simulation.nextPath(path));
        }
コード例 #3
0
ファイル: T_Vector.cs プロジェクト: OpenDerivatives/QLCore
        public void testCloneICloneable()
        {
            Vector vector = new Vector(Data);
            Vector clone  = (Vector)((QLCore.ICloneable)vector).Clone();

            QAssert.AreNotSame(vector, clone);
            QAssert.AreEqual(vector.Count, clone.Count);
            QAssert.CollectionAreEqual(vector, clone);
            vector[0] = 100;
            QAssert.CollectionAreNotEqual(vector, clone);
        }
コード例 #4
0
ファイル: T_Vector.cs プロジェクト: OpenDerivatives/QLCore
        public void testClone()
        {
            Vector vector = new Vector(Data);
            Vector clone  = vector.Clone();

            QAssert.AreNotSame(vector, clone);
            QAssert.AreEqual(vector.Count, clone.Count);
            QAssert.CollectionAreEqual(vector, clone);
            vector[0] = 100;
            QAssert.CollectionAreNotEqual(vector, clone);
        }
コード例 #5
0
ファイル: T_Vector.cs プロジェクト: OpenDerivatives/QLCore
        public void testHashCode()
        {
            Vector vector = new Vector(Data);

            QAssert.AreEqual(vector.GetHashCode(), vector.GetHashCode());
            QAssert.AreEqual(vector.GetHashCode(),
                             new Vector(new List <double>()
            {
                1, 2, 3, 4, 5
            }).GetHashCode());
            QAssert.AreNotEqual(vector.GetHashCode(), new Vector(new List <double>()
            {
                1
            }).GetHashCode());
        }
コード例 #6
0
        public void testFunctionValueEqualsCostFunctionAtCurrentValue()
        {
            var testCostFunction = new TestCostFunction();
            var problem          = new Problem(testCostFunction, new NoConstraint(), new Vector(new List <double> {
                3, 7.4
            }));
            var endCriteria = new EndCriteria(maxIterations: 1000, maxStationaryStateIterations: 10, rootEpsilon: 0, functionEpsilon: 1e-10, gradientNormEpsilon: null);
            var method      = new BFGS();

            var endType = method.minimize(problem, endCriteria);

            QAssert.AreEqual(EndCriteria.Type.StationaryFunctionValue, endType);

            QAssert.AreEqual(problem.functionValue(), testCostFunction.value(problem.currentValue()));
        }
コード例 #7
0
        public void testEventSetForNoEvents()
        {
            // Testing that catastrophe events are split correctly when there are no simulated events

            List <KeyValuePair <Date, double> > emptyEvents = new List <KeyValuePair <Date, double> >();
            EventSet      catRisk    = new EventSet(emptyEvents, eventsStart, eventsEnd);
            CatSimulation simulation = catRisk.newSimulation(new Date(2, Month.January, 2015), new Date(5, Month.January, 2016));

            QAssert.Require(simulation);

            List <KeyValuePair <Date, double> > path = new List <KeyValuePair <Date, double> >();

            QAssert.Require(simulation.nextPath(path));
            QAssert.AreEqual(0, path.Count);

            QAssert.Require(simulation.nextPath(path));
            QAssert.AreEqual(0, path.Count);

            QAssert.Require(!simulation.nextPath(path));
        }
コード例 #8
0
        public void testInterpolatedZeroCurveWithRefDateAndTenorDates()
        {
            CommonVars vars = new CommonVars();

            // Create the interpolated curve
            var refDate = new Date(1, 10, 2015);
            var dates   = new List <Date>()
            {
                new Date(30, 12, 2015),
                new Date(30, 3, 2016),
                new Date(30, 9, 2016),
                new Date(29, 9, 2017),
                new Date(28, 9, 2018),
                new Date(30, 9, 2019),
                new Date(30, 9, 2020),
                new Date(30, 9, 2021),
                new Date(30, 9, 2022),
                new Date(29, 9, 2023),
                new Date(30, 9, 2024),
                new Date(30, 9, 2025),
                new Date(30, 9, 2030),
                new Date(28, 9, 2035),
                new Date(29, 9, 2045),
            };

            var yields = new List <double>()
            {
                -0.002558362,
                -0.002478462,
                -0.00248845,
                -0.002498437,
                -0.00196903,
                -0.001219628,
                -0.000209989,
                0.000940221,
                0.00220121,
                0.003493045,
                0.004785712,
                0.00602906,
                0.010909594,
                0.013132837,
                0.01403893
            };

            var curve = new InterpolatedZeroCurve <Linear>(dates,
                                                           yields,
                                                           new ActualActual(ActualActual.Convention.ISMA),
                                                           new Linear(),
                                                           Compounding.Continuous,
                                                           Frequency.Annual, refDate);

            Dictionary <Date, double> tenors2 = new Dictionary <Date, double>
            {
                { new Date(30, 12, 2015), -0.002558362 },
                { new Date(30, 3, 2016), -0.002478462 },
                { new Date(30, 9, 2016), -0.00248845 },
                { new Date(29, 9, 2017), -0.002498437 },
                { new Date(28, 9, 2018), -0.00196903 },
                { new Date(30, 9, 2019), -0.001219628 },
                { new Date(30, 9, 2020), -0.000209989 },
                { new Date(30, 9, 2021), 0.000940221 },
                { new Date(30, 9, 2022), 0.00220121 },
                { new Date(29, 9, 2023), 0.003493045 },
                { new Date(30, 9, 2024), 0.004785712 },
                { new Date(30, 9, 2025), 0.00602906 },
                { new Date(30, 9, 2030), 0.010909594 },
                { new Date(28, 9, 2035), 0.013132837 },
                { new Date(29, 9, 2045), 0.01403893 }
            };

            // Make sure the points come back as expected
            var tenors = new[] { 0.25, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 15.0, 20.0, 30.0 };

            for (int i = 0; i < tenors.Length; i++)
            {
                var test = curve.interpolation_.value(tenors[i], true);
                QAssert.AreEqual(yields[i], test);
            }
            QAssert.AreNotEqual(yields[0], curve.interpolation_.value(0.0, true));
        }
コード例 #9
0
        public void testCatBondWithDoomOnceInTenYearsProportional()
        {
            // Testing floating-rate cat bond in a doom once in 10 years scenario with proportional notional reduction

            CommonVars vars = new CommonVars();

            Date today = new Date(22, Month.November, 2004);

            Settings.setEvaluationDate(today);

            int settlementDays = 1;

            Handle <YieldTermStructure> riskFreeRate  = new Handle <YieldTermStructure>(Utilities.flatRate(today, 0.025, new Actual360()));
            Handle <YieldTermStructure> discountCurve = new Handle <YieldTermStructure>(Utilities.flatRate(today, 0.03, new Actual360()));

            IborIndex index      = new USDLibor(new Period(6, TimeUnit.Months), riskFreeRate);
            int       fixingDays = 1;

            double tolerance = 1.0e-6;

            IborCouponPricer pricer = new BlackIborCouponPricer(new Handle <OptionletVolatilityStructure>());

            Schedule sch =
                new Schedule(new Date(30, Month.November, 2004),
                             new Date(30, Month.November, 2008),
                             new Period(Frequency.Semiannual),
                             new UnitedStates(UnitedStates.Market.GovernmentBond),
                             BusinessDayConvention.ModifiedFollowing, BusinessDayConvention.ModifiedFollowing,
                             DateGeneration.Rule.Backward, false);

            List <KeyValuePair <Date, double> > events = new List <KeyValuePair <Date, double> >();

            events.Add(new KeyValuePair <Date, double>(new Date(30, Month.November, 2008), 1000));
            CatRisk doomCatRisk = new EventSet(events, new Date(30, Month.November, 2004), new Date(30, Month.November, 2044));

            CatRisk noCatRisk = new EventSet(new List <KeyValuePair <Date, double> > (),
                                             new Date(1, Month.Jan, 2000), new Date(31, Month.Dec, 2010));

            EventPaymentOffset paymentOffset = new NoOffset();
            NotionalRisk       notionalRisk  = new ProportionalNotionalRisk(paymentOffset, 500, 1500);

            FloatingCatBond catBond =
                new FloatingCatBond(settlementDays, vars.faceAmount, sch,
                                    index, new ActualActual(ActualActual.Convention.ISMA),
                                    notionalRisk,
                                    BusinessDayConvention.ModifiedFollowing, fixingDays,
                                    new List <double>(), new List <double>(),
                                    new List <double?>(), new List <double?>(),
                                    false,
                                    100.0, new Date(30, Month.November, 2004));

            IPricingEngine catBondEngine = new MonteCarloCatBondEngine(doomCatRisk, discountCurve);

            catBond.setPricingEngine(catBondEngine);
            Utils.setCouponPricer(catBond.cashflows(), pricer);

            double price                 = catBond.cleanPrice();
            double yield                 = catBond.yield(new ActualActual(ActualActual.Convention.ISMA), Compounding.Simple, Frequency.Annual);
            double lossProbability       = catBond.lossProbability();
            double exhaustionProbability = catBond.exhaustionProbability();
            double expectedLoss          = catBond.expectedLoss();

            QAssert.AreEqual(0.1, lossProbability, tolerance);
            QAssert.AreEqual(0.0, exhaustionProbability, tolerance);
            QAssert.AreEqual(0.05, expectedLoss, tolerance);

            IPricingEngine catBondEngineRF = new MonteCarloCatBondEngine(noCatRisk, discountCurve);

            catBond.setPricingEngine(catBondEngineRF);

            double riskFreePrice           = catBond.cleanPrice();
            double riskFreeYield           = catBond.yield(new ActualActual(ActualActual.Convention.ISMA), Compounding.Simple, Frequency.Annual);
            double riskFreeLossProbability = catBond.lossProbability();
            double riskFreeExpectedLoss    = catBond.expectedLoss();

            QAssert.AreEqual(0.0, riskFreeLossProbability, tolerance);
            QAssert.IsTrue(Math.Abs(riskFreeExpectedLoss) < tolerance);

            QAssert.AreEqual(riskFreePrice * 0.95, price, tolerance);
            QAssert.IsTrue(riskFreeYield < yield);
        }
コード例 #10
0
        public void testCatBondInDoomScenario()
        {
            // Testing floating-rate cat bond in a doom scenario (certain default)

            CommonVars vars = new CommonVars();

            Date today = new Date(22, Month.November, 2004);

            Settings.setEvaluationDate(today);

            int settlementDays = 1;

            Handle <YieldTermStructure> riskFreeRate  = new Handle <YieldTermStructure>(Utilities.flatRate(today, 0.025, new Actual360()));
            Handle <YieldTermStructure> discountCurve = new Handle <YieldTermStructure>(Utilities.flatRate(today, 0.03, new Actual360()));

            IborIndex index      = new USDLibor(new Period(6, TimeUnit.Months), riskFreeRate);
            int       fixingDays = 1;

            double tolerance = 1.0e-6;

            IborCouponPricer pricer = new BlackIborCouponPricer(new Handle <OptionletVolatilityStructure>());

            Schedule sch = new Schedule(new Date(30, Month.November, 2004),
                                        new Date(30, Month.November, 2008),
                                        new Period(Frequency.Semiannual),
                                        new UnitedStates(UnitedStates.Market.GovernmentBond),
                                        BusinessDayConvention.ModifiedFollowing, BusinessDayConvention.ModifiedFollowing,
                                        DateGeneration.Rule.Backward, false);

            List <KeyValuePair <Date, double> > events = new List <KeyValuePair <Date, double> >();

            events.Add(new KeyValuePair <Date, double>(new Date(30, Month.November, 2004), 1000));
            CatRisk doomCatRisk = new EventSet(events,
                                               new Date(30, Month.November, 2004), new Date(30, Month.November, 2008));

            EventPaymentOffset paymentOffset = new NoOffset();
            NotionalRisk       notionalRisk  = new DigitalNotionalRisk(paymentOffset, 100);

            FloatingCatBond catBond = new FloatingCatBond(settlementDays, vars.faceAmount, sch,
                                                          index, new ActualActual(ActualActual.Convention.ISMA),
                                                          notionalRisk,
                                                          BusinessDayConvention.ModifiedFollowing, fixingDays,
                                                          new List <double>(), new List <double>(),
                                                          new List <double?>(), new List <double?>(),
                                                          false,
                                                          100.0, new Date(30, Month.November, 2004));

            IPricingEngine catBondEngine = new MonteCarloCatBondEngine(doomCatRisk, discountCurve);

            catBond.setPricingEngine(catBondEngine);
            Utils.setCouponPricer(catBond.cashflows(), pricer);

            double price = catBond.cleanPrice();

            QAssert.AreEqual(0, price);

            double lossProbability       = catBond.lossProbability();
            double exhaustionProbability = catBond.exhaustionProbability();
            double expectedLoss          = catBond.expectedLoss();

            QAssert.AreEqual(1.0, lossProbability, tolerance);
            QAssert.AreEqual(1.0, exhaustionProbability, tolerance);
            QAssert.AreEqual(1.0, expectedLoss, tolerance);
        }