public double getRT(DateTime t1) { OMLib.Conventions.DayCount.Actual365 dc = new OMLib.Conventions.DayCount.Actual365(); double t_ = (double)dc.DayCount(latestReference_, t1) / 365; return(getRT_(t_)); }
public double discount(DateTime enddate) { OMLib.Conventions.DayCount.Actual365 dc = new OMLib.Conventions.DayCount.Actual365(); double t1 = (double)dc.DayCount(latestReference_, enddate) / 365; return(Math.Exp(-getRT_(t1))); }
public double SurvivalProb(DateTime enddate) { OMLib.Conventions.DayCount.Actual365 dc = new OMLib.Conventions.DayCount.Actual365(); double t1 = (double)dc.DayCount(latestReference_.AddDays(1), enddate) / 365; return(Math.Exp(-getRT_(t1))); }
public void addhazardrate(DateTime jumpdate, double hazardrate) { jumps_.Add(hazardrate); jumpDates_.Add(jumpdate); nJumps_ += 1; OMLib.Conventions.DayCount.Actual365 dc = new OMLib.Conventions.DayCount.Actual365(); double x = (double)dc.DayCount(latestReference_, jumpDates_[nJumps_ - 1]) / 365; t.Add(x); rt.Add(jumps_[nJumps_ - 1] * t[nJumps_ - 1]); }
//public double ProtectionLeg_Approx(List<CashFlow> cf, double notional, PiecewiseconstantHazardRate HazardTermStructure, // YieldTermStructure yt, DateTime tradedate, DateTime settlementDate, double recoveryrate) //{ // if (cf.Count() == 0) // { // return 0.0; // } // double totalNPV = 0.0; // for (int i = 0; i < cf.Count; ++i) // { // if (i == 0) // { // totalNPV += (1 - recoveryrate)*notional * cf[i].DiscountFactor * (1- cf[i].Survivalprobability); // } // else // { // totalNPV += notional * cf[i].DiscountFactor * (cf[i - 1].Survivalprobability // - cf[i].Survivalprobability) * (1 - recoveryrate); // } // } // return totalNPV/yt.discount(settlementDate); //} public double calculateSinglePeriodAccrualOnDefault(List <CashFlow> cf, double coupon, DateTime tradedate, YieldTermStructure yieldCurve, PiecewiseconstantHazardRate creditCurve, List <DateTime> Jumps, DateTime lastpayment) { double Acc = 0; DateTime effectiveStart = tradedate.AddDays(1); for (int i = 0; i < cf.Count; ++i) { //Accured on default DateTime t_0 = (i > 0) ? cf[i - 1].CashFlowDate.AddDays(-1) : tradedate; DateTime T = cf[i].CashFlowDate; if (i == cf.Count - 1) { T = cf[i].CashFlowDate; } else { T = cf[i].CashFlowDate.AddDays(-1); } List <DateTime> knots = new List <DateTime>(); knots.Add(t_0); for (int j = 0; j < Jumps.Count; j++) { if ((DateTime.Compare(Jumps[j], T) < 0) && (DateTime.Compare(t_0, Jumps[j]) < 0)) { knots.Add(Jumps[j]); } } knots.Add(T); DateTime t = knots[0]; double ht0 = creditCurve.getRT(t); double rt0 = yieldCurve.getRT(t); double b0 = Math.Exp(-rt0 - ht0); // this is the risky discount factor OMLib.Conventions.DayCount.Actual365 dc = new OMLib.Conventions.DayCount.Actual365(); double t0; if (i == 0) { t0 = (double)dc.DayCount(lastpayment.AddDays(1), knots[0]) / 365; } else { t0 = (double)dc.DayCount(cf[i].CashFlowDate.AddDays(1), knots[0]) / 365; } double pv = 0.0; int nItems = knots.Count; for (int j = 1; j < nItems; ++j) { t = knots[j]; double ht1 = creditCurve.getRT(t); double rt1 = yieldCurve.getRT(t); double b1 = Math.Exp(-rt1 - ht1); double dt = (double)dc.DayCount(knots[j - 1], knots[j]) / 365; double dht = ht1 - ht0; double drt = rt1 - rt0; double dhrt = dht + drt + 1e-50; double tPV; double t1; if (i == 0) { t1 = (double)dc.DayCount(lastpayment.AddDays(1), knots[j]) / 365; } else { t1 = (double)dc.DayCount(cf[i].CashFlowDate.AddDays(1), knots[j]) / 365; } if (Math.Abs(dhrt) < 1e-5) { tPV = dht * b0 * (t0 * (Math.Exp(-dhrt) - 1) / (-dhrt) + dt * ((-dhrt - 1) * (Math.Exp(-dhrt) - 1) - dhrt) / (dhrt * dhrt)); } else { tPV = dht * dt / dhrt * ((b0 - b1) / dhrt - b1); } t0 = t1; pv += tPV; ht0 = ht1; rt0 = rt1; b0 = b1; } Acc += pv; } return(Acc); }
public void hazardratecurve(YieldTermStructure yt, OMLib.Conventions.DayCount.Actual365 dayCounter, List <double> QuotedSpot, List <double> QuotedSpread, double FixRate, double Spread_traded, double Upfront, DateTime maturity, DateTime firstpaymentdate, DateTime evalDate, DateTime formerpaymentdate, string frequency, double recovery) { List <DateTime> dates = new List <DateTime>(); List <DateTime> discountDates = new List <DateTime>(); int accrued = dayCounter.DayCount(formerpaymentdate, tradedate); if (QuotedSpread == null) { if (Spread_traded > 0) { // Infer the constant hazard rate from the quoted spread /*When the Spread is input: Consider a CDS with exactly the same specification as * the Standard CDS except that its Coupon Rate is equal to the Spread. Assume that * the Upfront of this CDS is zero. Solve for the Constant Hazard Rate that gives this * CDS a MTM equal to minus its Accrued Premium (based on a Coupon Rate equal * to Spread) discounted riskless to T */ impliedhazardrate ih = new impliedhazardrate(); List <double> spread = new List <double> { Spread_traded }; List <DateTime> mat = new List <DateTime> { maturity }; PiecewiseconstantHazardRate flatrate = ih.impliedHazardRate(-this.Notional * Spread_traded * accrued / 360, this.Notional, this.tradedate, spread, mat, yt, dayCounter, frequency, firstpaymentdate, this.formerpaymentdate, this.Jump_Nodes, recovery); this.piecewiseHazardRate = flatrate; } else if (Upfront > 0) { // Infer the constant hazard rate from the quoted upfront /*When the Upfront is input: Solve for the Constant Hazard Rate that gives the * Standard CDS a MTM equal to Notional * Upfront – AccruedPremium discounted riskless to T*/ impliedhazardrate ih = new impliedhazardrate(); List <double> spread = new List <double> { Spread_traded }; List <DateTime> mat = new List <DateTime> { maturity }; PiecewiseconstantHazardRate flatrate = ih.impliedHazardRate(this.Notional * Upfront - this.accruedamt, this.Notional, this.tradedate, spread, mat, yt, dayCounter, frequency, firstpaymentdate, this.formerpaymentdate, this.Jump_Nodes, recovery); this.piecewiseHazardRate = flatrate; } } else // Construct the credit curve with the predefined term structure { dates.Add(firstpaymentdate.AddMonths(6)); dates.Add(firstpaymentdate.AddYears(1)); dates.Add(firstpaymentdate.AddYears(3)); dates.Add(firstpaymentdate.AddYears(5)); dates.Add(firstpaymentdate.AddYears(7)); dates.Add(firstpaymentdate.AddYears(10)); for (int i = 0; i < QuotedSpread.Count(); i++) { QuotedSpread[i] = QuotedSpread[i] / 10000; } impliedhazardrate implied = new impliedhazardrate(); PiecewiseconstantHazardRate hazardcurve = implied.impliedHazardRate(0, this.Notional, this.tradedate, QuotedSpread, dates, yt, dayCounter, frequency, this.firstpaymentdate, this.formerpaymentdate, this.Jump_Nodes, recovery); this.piecewiseHazardRate = hazardcurve; List <double> survival = new List <double>(); for (int i = 0; i < dates.Count; i++) { survival.Add(piecewiseHazardRate.SurvivalProb(dates[i].AddDays(1))); } } }