Exemplo n.º 1
0
        public void testSingleInstrumentBootstrap()
        {
            //Testing single-instrument curve bootstrap...

            Calendar calendar = new TARGET();

            Date today = Settings.Instance.evaluationDate();

            int settlementDays = 0;

            double quote = 0.005;
            Period tenor = new Period(2, TimeUnit.Years);

            Frequency             frequency  = Frequency.Quarterly;
            BusinessDayConvention convention = BusinessDayConvention.Following;

            DateGeneration.Rule rule       = DateGeneration.Rule.TwentiethIMM;
            DayCounter          dayCounter = new Thirty360();
            double recoveryRate            = 0.4;

            RelinkableHandle <YieldTermStructure> discountCurve = new RelinkableHandle <YieldTermStructure>();

            discountCurve.linkTo(new FlatForward(today, 0.06, new Actual360()));

            List <CdsHelper> helpers = new List <CdsHelper>();

            helpers.Add(
                new SpreadCdsHelper(quote, tenor,
                                    settlementDays, calendar,
                                    frequency, convention, rule,
                                    dayCounter, recoveryRate,
                                    discountCurve));

            PiecewiseDefaultCurve <HazardRate, BackwardFlat> defaultCurve = new PiecewiseDefaultCurve <HazardRate, BackwardFlat>(today, helpers,
                                                                                                                                 dayCounter);

            defaultCurve.recalculate();
        }
Exemplo n.º 2
0
        /* This test attempts to build a default curve from CDS spreads as of 1 Apr 2020. The spreads are real and from a
         * distressed reference entity with an inverted CDS spread curve. Using the default IterativeBootstrap with no
         * retries, the default curve building fails. Allowing retries, it expands the min survival probability bounds but
         * still fails. We set dontThrow to true in IterativeBootstrap to use a fall back curve.
         */
        public void testIterativeBootstrapRetries()
        {
            //Testing iterative bootstrap with retries...

            SavedSettings backup = new SavedSettings();

            Date asof = new Date(1, Month.Apr, 2020);

            Settings.Instance.setEvaluationDate(asof);
            Actual365Fixed tsDayCounter = new Actual365Fixed();

            // USD discount curve built out of FedFunds OIS swaps.
            List <Date> usdCurveDates = new List <Date>()
            {
                new Date(1, Month.Apr, 2020),
                new Date(2, Month.Apr, 2020),
                new Date(14, Month.Apr, 2020),
                new Date(21, Month.Apr, 2020),
                new Date(28, Month.Apr, 2020),
                new Date(6, Month.May, 2020),
                new Date(5, Month.Jun, 2020),
                new Date(7, Month.Jul, 2020),
                new Date(5, Month.Aug, 2020),
                new Date(8, Month.Sep, 2020),
                new Date(7, Month.Oct, 2020),
                new Date(5, Month.Nov, 2020),
                new Date(7, Month.Dec, 2020),
                new Date(6, Month.Jan, 2021),
                new Date(5, Month.Feb, 2021),
                new Date(5, Month.Mar, 2021),
                new Date(7, Month.Apr, 2021),
                new Date(4, Month.Apr, 2022),
                new Date(3, Month.Apr, 2023),
                new Date(3, Month.Apr, 2024),
                new Date(3, Month.Apr, 2025),
                new Date(5, Month.Apr, 2027),
                new Date(3, Month.Apr, 2030),
                new Date(3, Month.Apr, 2035),
                new Date(3, Month.Apr, 2040),
                new Date(4, Month.Apr, 2050)
            };

            List <double> usdCurveDfs = new List <double>()
            {
                1.000000000,
                0.999955835,
                0.999931070,
                0.999914629,
                0.999902799,
                0.999887990,
                0.999825782,
                0.999764392,
                0.999709076,
                0.999647785,
                0.999594638,
                0.999536198,
                0.999483093,
                0.999419291,
                0.999379417,
                0.999324981,
                0.999262356,
                0.999575101,
                0.996135441,
                0.995228348,
                0.989366687,
                0.979271200,
                0.961150726,
                0.926265361,
                0.891640651,
                0.839314063
            };

            Handle <YieldTermStructure> usdYts = new Handle <YieldTermStructure>(
                new InterpolatedDiscountCurve <LogLinear>(
                    usdCurveDates, usdCurveDfs, tsDayCounter));

            // CDS spreads
            Dictionary <Period, double> cdsSpreads = new Dictionary <Period, double>();

            cdsSpreads.Add(new Period(6, TimeUnit.Months), 2.957980250);
            cdsSpreads.Add(new Period(1, TimeUnit.Years), 3.076933100);
            cdsSpreads.Add(new Period(2, TimeUnit.Years), 2.944524520);
            cdsSpreads.Add(new Period(3, TimeUnit.Years), 2.844498960);
            cdsSpreads.Add(new Period(4, TimeUnit.Years), 2.769234420);
            cdsSpreads.Add(new Period(5, TimeUnit.Years), 2.713474100);
            double recoveryRate = 0.035;

            // Conventions
            int                   settlementDays    = 1;
            WeekendsOnly          calendar          = new WeekendsOnly();
            Frequency             frequency         = Frequency.Quarterly;
            BusinessDayConvention paymentConvention = BusinessDayConvention.Following;

            DateGeneration.Rule rule                 = DateGeneration.Rule.CDS2015;
            Actual360           dayCounter           = new Actual360();
            Actual360           lastPeriodDayCounter = new Actual360(true);

            // Create the CDS spread helpers.
            List <CdsHelper> instruments = new List <CdsHelper>();

            foreach (KeyValuePair <Period, double> it in cdsSpreads)
            {
                instruments.Add(
                    new SpreadCdsHelper(it.Value, it.Key, settlementDays, calendar,
                                        frequency, paymentConvention, rule, dayCounter, recoveryRate, usdYts, true, true, null,
                                        lastPeriodDayCounter));
            }

            // Create the default curve with the default IterativeBootstrap.
            DefaultProbabilityTermStructure dpts = new PiecewiseDefaultCurve <SurvivalProbability, LogLinear, IterativeBootstrapForCds>(asof, instruments, tsDayCounter);

            // Check that the default curve throws by requesting a default probability.
            Date testDate = new Date(21, Month.Dec, 2020);

            try
            {
                dpts.survivalProbability(testDate);
                throw new Exception();
            }
            catch {}

            // Create the default curve with an IterativeBootstrap allowing for 4 retries.
            // Use a maxFactor value of 1.0 so that we still use the previous survival probability at each pillar. In other
            // words, the survival probability cannot increase with time so best max at current pillar is the previous
            // pillar's value - there is no point increasing it on a retry.
            IterativeBootstrap <PiecewiseDefaultCurve, DefaultProbabilityTermStructure> ib =
                new IterativeBootstrap <PiecewiseDefaultCurve, DefaultProbabilityTermStructure>(null, null, null, 5, 1.0, 10.0);

            dpts = new PiecewiseDefaultCurve <SurvivalProbability, LogLinear>(asof, instruments, tsDayCounter, ib);

            // Check that the default curve still throws. It throws at the third pillar because the survival probability is
            // too low at the second pillar.
            try
            {
                dpts.survivalProbability(testDate);
                throw new Exception();
            }
            catch {}

            // Create the default curve with an IterativeBootstrap that allows for 4 retries and does not throw.
            IterativeBootstrap <PiecewiseDefaultCurve, DefaultProbabilityTermStructure> ibNoThrow =
                new IterativeBootstrap <PiecewiseDefaultCurve, DefaultProbabilityTermStructure>(null, null, null, 5, 1.0, 10.0, true, 2);

            dpts = new PiecewiseDefaultCurve <SurvivalProbability, LogLinear>(asof, instruments, tsDayCounter, ibNoThrow);

            try
            {
                dpts.survivalProbability(testDate);
            }
            catch
            {
                throw new Exception();
            }
        }