public double ProtectionLegNPV_Exact(CDS cds, double notional, PiecewiseconstantHazardRate hazard, YieldTermStructure yt, DateTime tradedate, DateTime settlementDate, double recoveryrate, List <double> Jumps, List <double> creditCurveKnot) { DateTime Stepindate = tradedate.AddDays(1); OMLib.Conventions.DayCount.Actual360 dc = new OMLib.Conventions.DayCount.Actual360(); double t0 = 0; double T = cds.getProtectionEnd(); List <double> JumpNodes = new List <double>(); JumpNodes.Add(t0); for (int j = 0; j < Jumps.Count; j++) { if (Jumps[j] < T) { JumpNodes.Add(Jumps[j]); } } JumpNodes.Add(T); double ht0 = hazard.getRT_(JumpNodes[0]); double rt0 = yt.getRT_(JumpNodes[0]); double b0 = Math.Exp(-ht0 - rt0); // risky discount factor double pv = 0.0; double dPV = 0.0; for (int i = 1; i < JumpNodes.Count; ++i) { double ht1 = hazard.getRT_(JumpNodes[i]); double rt1 = yt.getRT_(JumpNodes[i]); double b1 = Math.Exp(-ht1 - rt1); double dht = ht1 - ht0; double drt = rt1 - rt0; double dhrt = dht + drt; // The formula has been modified from ISDA (but is equivalent) to avoid log(exp(x)) and explicitly // calculating the time step - it also handles the limit if (Math.Abs(dhrt) < 1e-5) { dPV = dht * b0 * (Math.Exp(-dhrt) - 1) / (-dhrt); } else { dPV = (b0 - b1) * dht / dhrt; } pv += dPV; ht0 = ht1; rt0 = rt1; b0 = b1; } return(pv * notional * (1 - recoveryrate) / yt.discount(settlementDate)); }
/** * For a future expiry date, the default adjusted forward index value is the expected (full) * value of the index plus the cash settlement of any defaults before * the expiry date, valued on the (forward) cash settlement date (usually 3 working days after * the expiry date - i.e. the expiry settlement date). * This calculation assumes an homogeneous pool that can be described by a single index curve. * * @param fwdStartingCDS A forward starting CDS to represent cash flows in the index. * The stepin date should be one day after the expiry and the cashSettlement * date (usually) 3 working days after expiry. This must contain the index recovery rate. * @param timeToExpiry the time in years between the trade date and expiry. * This should use the same DCC as the curves (ACT365F unless manually changed). * @param yieldCurve The yield curve * @param indexCoupon The coupon of the index * @param indexCurve Pseudo credit curve for the index. * @return the default adjusted forward index value */ public double defaultAdjustedForwardIndexValue( CDS fwdStartingCDS, double timeToExpiry, YieldTermStructure yieldCurve, double indexCoupon, PiecewiseconstantHazardRate indexCurve) { double defSet = expectedDefaultSettlementValue(timeToExpiry, indexCurve, fwdStartingCDS.getLGD()); return(defSet + _pricer.pv(fwdStartingCDS, yieldCurve, indexCurve, indexCoupon)); }
/** * Intrinsic (normalised) price an index from the credit curves of the individual single names. * To get the actual index value, this multiplied by the <b>initial</b> notional of the index. * * @param indexCDS analytic description of a CDS traded at a certain time * @param indexCoupon The coupon of the index (as a fraction) * @param yieldCurve The yield curve * @param intrinsicData credit curves, weights and recovery rates of the intrinsic names * @return The index value for a unit notional. */ public double indexPV( CDS indexCDS, double indexCoupon, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData) { double prot = indexProtLeg(indexCDS, yieldCurve, intrinsicData); double annuity = indexAnnuity(indexCDS, yieldCurve, intrinsicData); return(prot - indexCoupon * annuity); }
public YieldTermStructure withRate(double rate, int index) { int n = this.t.Count; List <double> t_ = this.t; List <double> rt_ = this.rt; rt_[index] = rate * t[index]; YieldTermStructure curve = new YieldTermStructure(latestReference_, jumps_, jumpDates_, t_, rt_); return(curve); }
public static PiecewiseconstantHazardRate[] buildCreditCurves(DateTime tradeDate, double[,] parSpreads, double[] recoveryRates, int[] tenors, YieldTermStructure yieldCurve) { CdsAnalyticFactory factory = new CdsAnalyticFactory(0.0); CDS[] pillarCDS = factory.makeImmCds(tradeDate, CDX_HY_TENORS); int indexSize = parSpreads.GetLength(0); PiecewiseconstantHazardRate[] creditCurves = new PiecewiseconstantHazardRate[indexSize]; //this section of code is hugely wasteful. If we do this for real (i.e. not just in a test), must improve for (int i = 0; i < indexSize; i++) { int m = parSpreads.GetLength(1); double[] spreads = new double[m]; for (int j = 0; j < m; j++) { spreads[j] = parSpreads[i, j]; } int nPillars = spreads.Length; CDS[] tempCDS = new CDS[nPillars]; double[] tempSpreads = new double[nPillars]; int count = 0; for (int j = 0; j < nPillars; j++) { if (!double.IsNaN(parSpreads[i, j])) { tempCDS[count] = pillarCDS[j].withRecoveryRate(recoveryRates[i]); tempSpreads[count] = spreads[j]; count++; } } CDS[] calCDS = null; double[] calSpreads = null; if (count == nPillars) { calCDS = tempCDS; calSpreads = tempSpreads; } else { calCDS = new CDS[count]; calSpreads = new double[count]; Array.Copy(tempCDS, 0, calCDS, 0, count); Array.Copy(tempSpreads, 0, calSpreads, 0, count); } CreditCurveCalibrator calibrator = new CreditCurveCalibrator(calCDS, yieldCurve); creditCurves[i] = calibrator.calibrate(calSpreads); } return(creditCurves); }
/** * The Points-Up-Front (PUF) of an index. This is the (clean) price of a unit notional index. * The actual clean price is this multiplied by the (current) index notional * (i.e. the initial notional times the index factor). * * @param indexCDS analytic description of a CDS traded at a certain time * @param indexCoupon The coupon of the index (as a fraction) * @param yieldCurve The yield curve * @param intrinsicData credit curves, weights and recover * @return PUF of an index */ public double indexPUF( CDS indexCDS, double indexCoupon, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData) { if (intrinsicData.getNumOfDefaults() == intrinsicData.getIndexSize()) { } return(indexPV(indexCDS, indexCoupon, yieldCurve, intrinsicData) / intrinsicData.getIndexFactor()); }
public YieldTermStructure withDiscountFactor(double discountFactor, int index) { int n = this.t.Count; List <double> t_ = this.t; List <double> rt_ = this.rt; rt_[index] = -Math.Log(discountFactor); OMLib.Conventions.DayCount.Actual365 dc = new OMLib.Conventions.DayCount.Actual365(); YieldTermStructure temp = new YieldTermStructure(latestReference_, jumps_, jumpDates_, t_, rt_); return(temp); }
/** * The (default adjusted) intrinsic forward spread of an index <b>when no defaults have yet occurred</b>. * This is defined as the ratio of expected value of the * protection leg and default settlement to the expected value of the annuity at expiry. * This calculation assumes an homogeneous pool that can be described by a single index curve. * * @param fwdStartingCDS forward starting CDS to represent cash flows in the index. * The stepin date should be one day after the expiry and the cashSettlement * date (usually) 3 working days after expiry * @param timeToExpiry the time in years between the trade date and expiry. * This should use the same DCC as the curves (ACT365F unless manually changed). * @param yieldCurve The yield curve * @param indexCurve Pseudo credit curve for the index. * @return The normalised expected default settlement value */ public double defaultAdjustedForwardSpread( CDS fwdStartingCDS, double timeToExpiry, YieldTermStructure yieldCurve, PiecewiseconstantHazardRate indexCurve) { double defSettle = expectedDefaultSettlementValue(timeToExpiry, indexCurve, fwdStartingCDS.getLGD()); double protLeg = _pricer.protectionLeg(fwdStartingCDS, yieldCurve, indexCurve); double ann = _pricer.annuity(fwdStartingCDS, yieldCurve, indexCurve); return((protLeg + defSettle) / ann); }
public double apply_(double x, YieldTermStructure curve, int curveIndex, double cachedValues, int index1, int index2, BasicFixedLeg swap, double[] paymentAmounts) { YieldTermStructure tempCurve = curve.withRate(x, curveIndex); double sum = 1.0 - cachedValues; // Floating leg at par for (int i = index1; i < index2; i++) { double t = swap.getPaymentTime(i); sum -= paymentAmounts[i] * Math.Exp(-tempCurve.getRT_(t)); } return(sum); }
public IntrinsicIndexDataBundle adjustCurves( double indexPUF, CDS indexCDS, double indexCoupon, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData) { Func <double, double> func = getHazardRateAdjFunction(indexPUF, indexCDS, indexCoupon, yieldCurve, intrinsicData); double x = ROOTFINDER.getRoot(func, 1.0); PiecewiseconstantHazardRate[] adjCC = adjustCurves(intrinsicData.getCreditCurves(), x); return(intrinsicData.withCreditCurves(adjCC)); }
private double decomposedValueOnDefault( CDS indexCDS, double indexCoupon, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData, int singleName) { double weight = intrinsicData.getWeight(singleName); double protection = intrinsicData.getLGD(singleName); double singleNamePV = _pricer.pv(indexCDS, yieldCurve, intrinsicData.getCreditCurves()[singleName], indexCoupon); return(weight * (protection - singleNamePV)); }
//******************************************************************************************************************* //* Forward values adjusted for defaults //**************************************************************************************************************** /** * For a future expiry date, the default adjusted forward index value is the expected (full) * value of the index plus the cash settlement of any defaults before * the expiry date, valued on the (forward) cash settlement date (usually 3 working days after * the expiry date - i.e. the expiry settlement date). * * @param fwdStartingCDS A forward starting CDS to represent cash flows in the index. * The stepin date should be one day after the expiry and the cashSettlement * date (usually) 3 working days after expiry. * @param timeToExpiry the time in years between the trade date and expiry. * This should use the same DCC as the curves (ACT365F unless manually changed). * @param yieldCurve The yield curve * @param indexCoupon The coupon of the index * @param intrinsicData credit curves, weights and recovery rates of the intrinsic names * initially 100 entries, and the realised recovery rates are 0.2 and 0.35, the this value is (0.8 + 0.65)/100 ) * @return the default adjusted forward index value */ public double defaultAdjustedForwardIndexValue( CDS fwdStartingCDS, double timeToExpiry, YieldTermStructure yieldCurve, double indexCoupon, IntrinsicIndexDataBundle intrinsicData) { //the expected value of the index (not including default settlement) at the expiry settlement date double indexPV1 = indexPV(fwdStartingCDS, indexCoupon, yieldCurve, intrinsicData); double d = expectedDefaultSettlementValue(timeToExpiry, intrinsicData); return(indexPV1 + d); }
/** * The intrinsic index spread. this is defined as the ratio of the intrinsic protection leg to the intrinsic annuity. * * @see #averageSpread * @param indexCDS analytic description of a CDS traded at a certain time * @param yieldCurve The yield curve * @param intrinsicData credit curves, weights and recovery rates of the intrinsic names * @return intrinsic index spread (as a fraction) */ public double intrinsicIndexSpread( CDS indexCDS, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData) { if (intrinsicData.getNumOfDefaults() == intrinsicData.getIndexSize()) { } double prot = indexProtLeg(indexCDS, yieldCurve, intrinsicData); double annuity = indexAnnuity(indexCDS, yieldCurve, intrinsicData); return(prot / annuity); }
/** * Intrinsic (normalised) price an index from the credit curves of the individual single names. * To get the actual index value, this multiplied by the <b>initial</b> notional of the index. * * @param indexCDS analytic description of a CDS traded at a certain time * @param indexCoupon The coupon of the index (as a fraction) * @param yieldCurve The yield curve * @param intrinsicData credit curves, weights and recovery rates of the intrinsic names * @param priceType Clean or dirty price * @param valuationTime The leg value is calculated for today (t=0), then rolled * forward (using the risk free yield curve) to the valuation time. * This is because cash payments occur on the cash-settlement-date, which is usually * three working days after the trade date (today) * @return The index value for a unit notional. */ public double indexPV( CDS indexCDS, double indexCoupon, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData, CdsPriceType priceType, double valuationTime) { double prot = indexProtLeg(indexCDS, yieldCurve, intrinsicData, valuationTime); double annuity = indexAnnuity(indexCDS, yieldCurve, intrinsicData, priceType, valuationTime); return(prot - indexCoupon * annuity); }
/** * The (default adjusted) intrinsic forward spread of an index. * This is defined as the ratio of expected value of the protection leg and default settlement to * the expected value of the annuity at expiry. * * @param fwdStartingCDS forward starting CDS to represent cash flows in the index. * The stepin date should be one day after the expiry and the cashSettlement * date (usually) 3 working days after expiry the time in years between the trade date and expiry. * This should use the same DCC as the curves (ACT365F unless manually changed). * @param timeToExpiry the time in years between the trade date and expiry. * This should use the same DCC as the curves (ACT365F unless manually changed). * @param yieldCurve The yield curve * @param intrinsicData credit curves, weights and recovery rates of the intrinsic names * initially 100 entries, and the realised recovery rates are 0.2 and 0.35, the this value is (0.8 + 0.65)/100 ) * @return The (default adjusted) forward spread (as a fraction) */ public double defaultAdjustedForwardSpread( CDS fwdStartingCDS, double timeToExpiry, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData) { // Note: these values are all calculated for payment on the (forward) cash settlement date // there is no point discounting to today double protLeg = indexProtLeg(fwdStartingCDS, yieldCurve, intrinsicData); double defSettle = expectedDefaultSettlementValue(timeToExpiry, intrinsicData); double ann = indexAnnuity(fwdStartingCDS, yieldCurve, intrinsicData); return((protLeg + defSettle) / ann); }
public double apply_sen(double x, YieldTermStructure curve, int curveIndex, double cachedSense, int index1, int index2, BasicFixedLeg swap, double swapRate) { YieldTermStructure tempCurve = curve.withRate(x, curveIndex); double sum = cachedSense; for (int i = index1; i < index2; i++) { double t = swap.getPaymentTime(i); // TODO have two looks ups for the same time - could have a specialist function in ISDACompliantCurve sum -= swap.getPaymentAmounts(i, swapRate) * tempCurve.getSingleNodeDiscountFactorSensitivity(t, curveIndex); } return(sum); }
private Func <double, double> getHazardRateAdjFunction( double indexPUF, CDS indexCDS, double indexCoupon, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData) { PiecewiseconstantHazardRate[] creditCurves = intrinsicData.getCreditCurves(); double clean = intrinsicData.getIndexFactor() * indexPUF; Func <double, double> function = x => _pricer.indexPV(indexCDS, indexCoupon, yieldCurve, intrinsicData.withCreditCurves(adjustCurves(creditCurves, x))) - clean; return(function); }
public void Pricing(YieldTermStructure yt, PiecewiseconstantHazardRate hazard) { //Calculate PV for both legs CashFlowCalculation engine = new CashFlowCalculation(); this.FixLeg = engine.Calculation(this.PremiumRate, this.Notional, this.Payment_Schedule, this.tradedate, yt, hazard, this.Cashsettlement, formerpaymentdate); NPV_PricingEngine engine2 = new NPV_PricingEngine(); double pv_premium = engine2.PremiumLegNPV_Exact(this.FixLeg, this.piecewiseHazardRate, this.yieldcurve, this.tradedate, this.tradedate.AddDays(3), this.Notional, this.PremiumRate, yt.jumpDates_, formerpaymentdate); double pv_protection = engine2.ProtectionLegNPV_Exact(this, this.Notional, this.piecewiseHazardRate, this.yieldcurve, this.tradedate, this.tradedate.AddDays(3), this.Recovery, yt.t, hazard.t); this.pv = pv_protection - pv_premium; this.marketvalue = this.pv + accruedamt; }
/** * For a future expiry date, the default adjusted forward index value is the expected (full) * value of the index plus the cash settlement of any defaults before * the expiry date, valued on the (forward) cash settlement date (usually 3 working days after * the expiry date - i.e. the expiry settlement date). * This calculation assumes an homogeneous pool that can be described by a single index curve. * * @param fwdStartingCDS A forward starting CDS to represent cash flows in the index. * The stepin date should be one day after the expiry and the cashSettlement * date (usually) 3 working days after expiry. This must contain the index recovery rate. * @param timeToExpiry the time in years between the trade date and expiry. * This should use the same DCC as the curves (ACT365F unless manually changed). * @param initialIndexSize The initial number of names in the index * @param yieldCurve The yield curve * @param indexCoupon The coupon of the index * @param indexCurve Pseudo credit curve for the index. * @param initialDefaultSettlement The (normalised) value of any defaults that have already * occurred (e.g. if two defaults have occurred from an index with * initially 100 entries, and the realised recovery rates are 0.2 and 0.35, the this value is (0.8 + 0.65)/100 ) * @param numDefaults The number of defaults that have already occurred * @return the default adjusted forward index value */ public double defaultAdjustedForwardIndexValue( CDS fwdStartingCDS, double timeToExpiry, int initialIndexSize, YieldTermStructure yieldCurve, double indexCoupon, PiecewiseconstantHazardRate indexCurve, double initialDefaultSettlement, int numDefaults) { double f = (initialIndexSize - numDefaults) / ((double)initialIndexSize); double defSet = expectedDefaultSettlementValue(initialIndexSize, timeToExpiry, indexCurve, fwdStartingCDS.getLGD(), initialDefaultSettlement, numDefaults); return(defSet + f * _pricer.pv(fwdStartingCDS, yieldCurve, indexCurve, indexCoupon)); }
/** * The (default adjusted) intrinsic forward spread of an index. * This is defined as the ratio of expected value of the protection leg and default settlement to * the expected value of the annuity at expiry. This calculation assumes an homogeneous pool that * can be described by a single index curve. * * @param fwdStartingCDS forward starting CDS to represent cash flows in the index. * The stepin date should be one day after the expiry and the cashSettlement * date (usually) 3 working days after expiry * @param timeToExpiry the time in years between the trade date and expiry. * This should use the same DCC as the curves (ACT365F unless manually changed). * @param initialIndexSize The initial number of names in the index * @param yieldCurve The yield curve * @param indexCurve Pseudo credit curve for the index. * @param initialDefaultSettlement The (normalised) value of any defaults that have * already occurred (e.g. if two defaults have occurred from an index with * initially 100 entries, and the realised recovery rates are 0.2 and 0.35, the this value is (0.8 + 0.65)/100 ) * @param numDefaults The number of defaults that have already occurred * @return The normalised expected default settlement value */ public double defaultAdjustedForwardSpread( CDS fwdStartingCDS, double timeToExpiry, int initialIndexSize, YieldTermStructure yieldCurve, PiecewiseconstantHazardRate indexCurve, double initialDefaultSettlement, int numDefaults) { double f = (initialIndexSize - numDefaults) / ((double)initialIndexSize); double defSettle = expectedDefaultSettlementValue(initialIndexSize, timeToExpiry, indexCurve, fwdStartingCDS.getLGD(), initialDefaultSettlement, numDefaults); double protLeg = f * _pricer.protectionLeg(fwdStartingCDS, yieldCurve, indexCurve); double ann = f * _pricer.annuity(fwdStartingCDS, yieldCurve, indexCurve); return((protLeg + defSettle) / ann); }
public void Build_yield_curve(DateTime TRADE_DATE) { var reader = new StreamReader(File.OpenRead(@"C:\Users\LeonDing\Source\Workspaces\OTC\CDSPro\Interest Rates.csv")); var title = reader.ReadLine(); List <double> interest_rates = new List <double>(); while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(','); interest_rates.Add(Convert.ToDouble(values[3])); } InterestRateCurve IRC = new InterestRateCurve(); YieldTermStructure yt = new YieldTermStructure(TRADE_DATE); this.YIELD_CURVE = IRC.calculation2(TRADE_DATE, interest_rates); }
/** * The change in the intrinsic value of a CDS index when zero rate at node points of the yield curve is bumped by 1bps. * If the index is priced as a single name CDS, use {@link InterestRateSensitivityCalculator}. * * @param indexCDS The CDS index * @param indexCoupon The index coupon * @param yieldCurve The yield curve * @param intrinsicData Credit curves, weights and recovery rates of the intrinsic names * @return bucketed IR01 */ public double[] bucketedIR01( CDS indexCDS, double indexCoupon, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData) { double basePV = indexPV(indexCDS, indexCoupon, yieldCurve, intrinsicData, CdsPriceType.DIRTY); int n = yieldCurve.t.Count; double[] res = new double[n]; for (int i = 0; i < n; ++i) { YieldTermStructure bumpedYieldCurve = yieldCurve.withRate(yieldCurve.getZeroRateAtIndex(i) + ONE_BPS, i); double bumpedPV = indexPV(indexCDS, indexCoupon, bumpedYieldCurve, intrinsicData, CdsPriceType.DIRTY); res[i] = bumpedPV - basePV; } return(res); }
/** * The change in the intrinsic value of a CDS index when the yield curve is bumped by 1bps. * If the index is priced as a single name CDS, use {@link InterestRateSensitivityCalculator}. * * @param indexCDS The CDS index * @param indexCoupon The index coupon * @param yieldCurve The yield curve * @param intrinsicData Credit curves, weights and recovery rates of the intrinsic names * @return parallel IR01 */ public double parallelIR01( CDS indexCDS, double indexCoupon, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData) { double pv = indexPV(indexCDS, indexCoupon, yieldCurve, intrinsicData, CdsPriceType.DIRTY); int nKnots = yieldCurve.t.Count; double[] rates = yieldCurve.getKnotZeroRates().ToArray(); for (int i = 0; i < nKnots; ++i) { rates[i] += ONE_BPS; } YieldTermStructure yieldCurveUp = yieldCurve.withRates(rates.ToList()); double pvUp = indexPV(indexCDS, indexCoupon, yieldCurveUp, intrinsicData, CdsPriceType.DIRTY); return(pvUp - pv); }
public double PremiumLegNPV_Exact(CDS cds, PiecewiseconstantHazardRate hazard, YieldTermStructure yt, DateTime tradedate, DateTime settlementDate, double notional, double coupon, List <double> Jumps, DateTime lastpayment) { double ita = (double)365 / 360; double totalNPV = 0.0; CdsCoupon[] cf = cds.getCoupons(); for (int i = 0; i < cf.Length; ++i) { totalNPV += cf[i].getYearFrac() * notional * Math.Exp(-hazard.getRT_(cf[i].getEffEnd())) * Math.Exp(-yt.getRT_(cf[i].getEffEnd())); } double accrualpaidondefault = calculateSinglePeriodAccrualOnDefault(cf, coupon, tradedate, yt, hazard, lastpayment); totalNPV += ita * coupon * accrualpaidondefault * notional / yt.discount(tradedate.AddDays(3)); OMLib.Conventions.DayCount.Actual360 dc = new OMLib.Conventions.DayCount.Actual360(); Calendar calendar = new UnitedStates(); return(totalNPV / Math.Exp(-yt.getRT_(cds.getCashSettleTime()))); }
/** * The normalised intrinsic value of the protection leg of a CDS portfolio (index). * The actual value of the leg is this multiplied by the <b>initial</b> notional of the index. * * @param indexCDS representation of the index cashflows (seen from today). * @param yieldCurve The current yield curves * @param intrinsicData credit curves, weights and recovery rates of the intrinsic names * @param valuationTime Valuation time. The leg value is calculated for today (t=0), * then rolled forward (using the risk free yield curve) to the valuation time. * This is because cash payments occur on the cash-settlement-date, which is usually * three working days after the trade date (today) * @return The normalised intrinsic value of the protection leg. */ public double indexProtLeg( CDS indexCDS, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData, double valuationTime) { CDS cds = indexCDS.withRecoveryRate(0.0); int n = intrinsicData.getIndexSize(); double protLeg = 0; for (int i = 0; i < n; i++) { if (!intrinsicData.isDefaulted(i)) { protLeg += intrinsicData.getWeight(i) * intrinsicData.getLGD(i) * _pricer.protectionLeg(cds, yieldCurve, intrinsicData.getCreditCurve(i), 0); } } protLeg /= Math.Exp(-yieldCurve.getRT_(valuationTime)); return(protLeg); }
/** * The intrinsic annuity of a CDS portfolio (index) for a unit (initial) notional. * The value of the premium leg is this multiplied by the <b> initial</b> notional of the index * and the index coupon (as a fraction). * * @param indexCDS representation of the index cashflows (seen from today). * @param yieldCurve The current yield curves * @param intrinsicData credit curves, weights and recovery rates of the intrinsic names * @param priceType Clean or dirty * @param valuationTime Valuation time. The leg value is calculated for today (t=0), * then rolled forward (using the risk free yield curve) to the valuation time. * This is because cash payments occur on the cash-settlement-date, which is usually * three working days after the trade date (today) * @return The intrinsic annuity of a CDS portfolio (index) */ public double indexAnnuity( CDS indexCDS, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData, CdsPriceType priceType, double valuationTime) { int n = intrinsicData.getIndexSize(); double a = 0; for (int i = 0; i < n; i++) { if (!intrinsicData.isDefaulted(i)) { a += intrinsicData.getWeight(i) * _pricer.annuity(indexCDS, yieldCurve, intrinsicData.getCreditCurve(i), priceType, 0); } } a /= Math.Exp(-yieldCurve.getRT_(valuationTime)); return(a); }
public void curve_output(YieldTermStructure yt, PiecewiseconstantHazardRate ct) { double it = ct.SurvivalProb(new DateTime(2021, 06, 20)); List <double> yield = new List <double>(); for (int j = 1; j < 120; j++) { DateTime t = this.evalDate.AddMonths(j); double t_ = (double)j / 12; yield.Add(-Math.Log(yt.discount(t)) / t_); } this.yield_series = yield; List <double> survival = new List <double>(); for (int j = 1; j < 120; j++) { DateTime t = this.tradedate.AddMonths(j); survival.Add(ct.SurvivalProb(t)); } this.survival_prob = survival; }
/** * Values on per-name default * @param indexCDS The CDS index * @param indexCoupon The index coupon * @param yieldCurve The yield curve * @param intrinsicData Credit curves, weights and recovery rates of the intrinsic names * @return The jump to default */ public double[] jumpToDefault( CDS indexCDS, double indexCoupon, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData) { int indexSize = intrinsicData.getIndexSize(); double[] res = new double[indexSize]; for (int i = 0; i < indexSize; ++i) { if (intrinsicData.isDefaulted(i)) { res[i] = 0.0; } else { res[i] = decomposedValueOnDefault(indexCDS, indexCoupon, yieldCurve, intrinsicData, i); } } return(res); }
public double PremiumLegNPV_Exact(List <CashFlow> cf, PiecewiseconstantHazardRate hazard, YieldTermStructure yt, DateTime tradedate, DateTime settlementDate, double notional, double coupon, List <DateTime> Jumps, DateTime lastpayment) { if (cf.Count() == 0) { return(0.0); } double ita = (double)365 / 360; double totalNPV = 0.0; for (int i = 0; i < cf.Count; ++i) { totalNPV += cf[i].Amount * cf[i].DiscountFactor * cf[i].Survivalprobability; } double accrualpaidondefault = calculateSinglePeriodAccrualOnDefault(cf, coupon, tradedate, yt, hazard, Jumps, lastpayment); totalNPV += ita * coupon * accrualpaidondefault * notional / yt.discount(tradedate.AddDays(3)); OMLib.Conventions.DayCount.Actual360 dc = new OMLib.Conventions.DayCount.Actual360(); Calendar calendar = new UnitedStates(); return(totalNPV / yt.discount(settlementDate)); }
/** * Sensitivity of the intrinsic value of a CDS index to intrinsic CDS recovery rates. * * @param indexCDS The CDS index * @param indexCoupon The index coupon * @param yieldCurve The yield curve * @param intrinsicData Credit curves, weights and recovery rates of the intrinsic names * @return The sensitivity */ public double[] recovery01( CDS indexCDS, double indexCoupon, YieldTermStructure yieldCurve, IntrinsicIndexDataBundle intrinsicData) { CDS zeroRR = indexCDS.withRecoveryRate(0.0); int indexSize = intrinsicData.getIndexSize(); double[] res = new double[indexSize]; for (int i = 0; i < indexSize; ++i) { if (intrinsicData.isDefaulted(i)) { res[i] = 0.0; } else { res[i] = -_pricer.protectionLeg(zeroRR, yieldCurve, intrinsicData.getCreditCurve(i)) * intrinsicData.getWeight(i); } } return(res); }