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); }
public static double CVA(DateTime originDate, DateTime[] EPEDates, double[] EPEExposures, HazzardCurve hazzardCurve, IIrCurve discountCurve, double LGD) { if (EPEDates.Length != EPEExposures.Length) { throw new Exception("Number of EPE dates and EPE values must be equal"); } var lastDate = originDate; var cva = 0.0; for (var i = 0; i < EPEDates.Length; i++) { if (EPEDates[i] < originDate) { continue; } var pDefault = hazzardCurve.GetDefaultProbability(lastDate, EPEDates[i]); var df = discountCurve.GetDf(originDate, EPEDates[i]); cva += EPEExposures[i] * pDefault * df * LGD; lastDate = EPEDates[i]; } return(-cva); }
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); }
//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); }