Example #1
0
        public static (double FBA, double FCA) FVA(DateTime originDate, DateTime[] ExEDates, double[] EPEExposures, double[] ENEExposures, HazzardCurve hazzardCurve, IIrCurve discountCurve, IIrCurve fundingCurve)
        {
            if (ExEDates.Length != EPEExposures.Length)
            {
                throw new Exception("Number of EPE dates and EPE values must be equal");
            }

            if (ExEDates.Length != ENEExposures.Length)
            {
                throw new Exception("Number of ENE dates and ENE values must be equal");
            }


            var lastDate = originDate;
            var fba      = 0.0;
            var fca      = 0.0;

            for (var i = 0; i < ExEDates.Length; i++)
            {
                if (ExEDates[i] < originDate)
                {
                    continue;
                }

                var pSurvival = hazzardCurve.GetSurvivalProbability(lastDate, ExEDates[i]);
                var fwdDf     = fundingCurve.GetDf(lastDate, ExEDates[i]) / discountCurve.GetDf(lastDate, ExEDates[i]);
                var df        = fundingCurve.GetDf(originDate, ExEDates[i]);
                fba += ENEExposures[i] * pSurvival / fwdDf * df;
                fca += EPEExposures[i] * pSurvival / fwdDf * df;

                lastDate = ExEDates[i];
            }
            return(fba, fca);
        }
Example #2
0
        public void HazzardCurveFact()
        {
            var hzi    = new Math.Interpolation.ConstantHazzardInterpolator(0.1);
            var origin = new DateTime(2019, 05, 28);
            var hz     = new HazzardCurve(origin, Dates.DayCountBasis.Act365F, hzi);

            Assert.Equal(1.0, hz.GetSurvivalProbability(origin, origin));
            Assert.Equal(1.0, hz.GetSurvivalProbability(origin));
            Assert.Equal(hzi.Interpolate(1.0 / 365.0), hz.GetSurvivalProbability(origin, origin.AddDays(1)));

            var dfCurve = new ConstantRateIrCurve(0.05, origin, "zzz", TestProviderHelper.CurrencyProvider.GetCurrency("USD"));

            var df = dfCurve.GetDf(origin, origin.AddDays(100));

            Assert.Equal(df, hz.RiskyDiscountFactor(origin, origin.AddDays(100), dfCurve, 0.0));
            Assert.Equal(df * (1.0 - (1.0 - hzi.Interpolate(100.0 / 365)) * 0.5), hz.RiskyDiscountFactor(origin, origin.AddDays(100), dfCurve, 0.5));
        }
Example #3
0
        //http://www.bnikolic.co.uk/cds/cdsvaluation.html
        //var contingentLeg = (1.0 - recoveryRate) *
        public double PV_PiecewiseFlat(HazzardCurve hazzardCurve, IIrCurve discountCurve, double recoveryRate, bool payAccruedOnDefault = true)
        {
            var nodeDates = FixedSchedule.Flows.Select(f => f.AccrualPeriodEnd).ToArray();
            var pv        = 0.0;

            //contingent leg
            var d = hazzardCurve.OriginDate;

            foreach (var nd in nodeDates)
            {
                var deltaT = d.CalculateYearFraction(nd, hazzardCurve.Basis);
                var s      = hazzardCurve.GetSurvivalProbability(d);
                var dd     = discountCurve.GetDf(discountCurve.BuildDate, d);
                var lambda = System.Math.Log(s / hazzardCurve.GetSurvivalProbability(nd)) / deltaT;
                var f      = System.Math.Log(dd / discountCurve.GetDf(discountCurve.BuildDate, nd)) / deltaT;
                var term1  = (lambda == 0 && f == 0) ? 1.0 : lambda / (lambda + f);
                pv += term1 * (1.0 - System.Math.Exp(-deltaT * (lambda + f))) * s * dd;
                d   = nd;
            }

            pv *= (1.0 - recoveryRate) * Notional;

            //fixed leg
            foreach (var f in FixedSchedule.Flows)
            {
                pv -= f.Notional * f.YearFraction * Spread * discountCurve.GetDf(discountCurve.BuildDate, f.SettleDate) * hazzardCurve.GetSurvivalProbability(f.SettleDate);
                if (payAccruedOnDefault)
                {
                    pv -= 0.5 * f.Notional * f.YearFraction * Spread
                          * hazzardCurve.GetDefaultProbability(f.AccrualPeriodStart, f.AccrualPeriodEnd)
                          * discountCurve.GetDf(discountCurve.BuildDate, f.SettleDate);
                }
            }

            return(pv);
        }
Example #4
0
        public double PV_SmallSteps(HazzardCurve hazzardCurve, IIrCurve discountCurve, double recoveryRate, bool payAccruedOnDefault = true)
        {
            var nodeDates = DatePeriodType.M.GenerateDateSchedule(hazzardCurve.OriginDate, FinalSensitivityDate);

            //contingent leg
            var pv = 0.0;

            for (var i = 1; i < nodeDates.Length; i++)
            {
                pv += discountCurve.GetDf(discountCurve.BuildDate, nodeDates[i])
                      * hazzardCurve.GetDefaultProbability(nodeDates[i - 1], nodeDates[i]);
            }

            pv *= (1.0 - recoveryRate) * Notional;

            //fixed leg
            foreach (var f in FixedSchedule.Flows)
            {
                pv -= f.Notional * f.YearFraction * Spread * discountCurve.GetDf(discountCurve.BuildDate, f.SettleDate) * hazzardCurve.GetSurvivalProbability(f.SettleDate);
                if (payAccruedOnDefault)
                {
                    pv -= 0.5 * f.Notional * f.YearFraction * Spread
                          * hazzardCurve.GetDefaultProbability(f.AccrualPeriodStart, f.AccrualPeriodEnd)
                          * discountCurve.GetDf(discountCurve.BuildDate, f.SettleDate);
                }
            }


            return(pv);
        }
Example #5
0
        public double PV_LinearApprox(HazzardCurve hazzardCurve, IIrCurve discountCurve, double recoveryRate, bool payAccruedOnDefault = true)
        {
            var nodeDates = DatePeriodType.M.GenerateDateSchedule(hazzardCurve.OriginDate, FinalSensitivityDate);
            var ts        = nodeDates.Select(d => discountCurve.BuildDate.CalculateYearFraction(d, DayCountBasis.ACT365F)).ToArray();

            var integrandD = new Func <DateTime, double> (d => discountCurve.GetDf(discountCurve.BuildDate, d) * -hazzardCurve.GetSurvivalProbabilitySlope(d));
            var integrandT = new Func <double, double>(t => integrandD(OriginDate.AddYearFraction(t, DayCountBasis.ACT365F)));

            //contingent leg
            var pv = (1.0 - recoveryRate) * Notional * Integration.SimpsonsRuleExtended(integrandT, 0, ts.Last(), 100);

            //fixed leg
            foreach (var f in FixedSchedule.Flows)
            {
                pv -= f.Notional * f.YearFraction * Spread * discountCurve.GetDf(discountCurve.BuildDate, f.SettleDate) * hazzardCurve.GetSurvivalProbability(f.SettleDate);
                if (payAccruedOnDefault)
                {
                    pv -= 0.5 * f.Notional * f.YearFraction * Spread
                          * hazzardCurve.GetDefaultProbability(f.AccrualPeriodStart, f.AccrualPeriodEnd)
                          * discountCurve.GetDf(discountCurve.BuildDate, f.SettleDate);
                }
            }

            return(pv);
        }