public override void calculate() { double basisPoint = 1.0e-4; Date exerciseDate = arguments_.exercise.date(0); // the part of the swap preceding exerciseDate should be truncated // to avoid taking into account unwanted cashflows // for the moment we add a check avoiding this situation VanillaSwap swap = arguments_.swap; double strike = swap.fixedRate; List <CashFlow> fixedLeg = swap.fixedLeg(); FixedRateCoupon firstCoupon = fixedLeg[0] as FixedRateCoupon; Utils.QL_REQUIRE(firstCoupon != null, () => "wrong coupon type"); Utils.QL_REQUIRE(firstCoupon.accrualStartDate() >= exerciseDate, () => "swap start (" + firstCoupon.accrualStartDate() + ") before exercise date (" + exerciseDate + ") not supported in Black swaption engine"); // using the forecasting curve swap.setPricingEngine(new DiscountingSwapEngine(swap.iborIndex().forwardingTermStructure())); double atmForward = swap.fairRate(); // Volatilities are quoted for zero-spreaded swaps. // Therefore, any spread on the floating leg must be removed // with a corresponding correction on the fixed leg. if (swap.spread.IsNotEqual(0.0)) { double correction = swap.spread * Math.Abs(swap.floatingLegBPS() / swap.fixedLegBPS()); strike -= correction; atmForward -= correction; results_.additionalResults["spreadCorrection"] = correction; } else { results_.additionalResults["spreadCorrection"] = 0.0; } results_.additionalResults["strike"] = strike; results_.additionalResults["atmForward"] = atmForward; // using the discounting curve swap.setPricingEngine(new DiscountingSwapEngine(discountCurve_, false)); double annuity = 0; switch (arguments_.settlementType) { case Settlement.Type.Physical: { annuity = Math.Abs(swap.fixedLegBPS()) / basisPoint; break; } case Settlement.Type.Cash: { DayCounter dayCount = firstCoupon.dayCounter(); // we assume that the cash settlement date is equal // to the swap start date Date discountDate = model_ == CashAnnuityModel.DiscountCurve ? firstCoupon.accrualStartDate() : discountCurve_.link.referenceDate(); double fixedLegCashBPS = CashFlows.bps(fixedLeg, new InterestRate(atmForward, dayCount, Compounding.Compounded, Frequency.Annual), false, discountDate); annuity = Math.Abs(fixedLegCashBPS / basisPoint) * discountCurve_.link.discount(discountDate); break; } default: Utils.QL_FAIL("unknown settlement type"); break; } results_.additionalResults["annuity"] = annuity; double swapLength = vol_.link.swapLength(swap.floatingSchedule().dates().First(), swap.floatingSchedule().dates().Last()); results_.additionalResults["swapLength"] = swapLength; double variance = vol_.link.blackVariance(exerciseDate, swapLength, strike); double displacement = displacement_ == null ? vol_.link.shift(exerciseDate, swapLength) : Convert.ToDouble(displacement_); double stdDev = Math.Sqrt(variance); results_.additionalResults["stdDev"] = stdDev; Option.Type w = (arguments_.type == VanillaSwap.Type.Payer) ? Option.Type.Call : Option.Type.Put; results_.value = new Spec().value(w, strike, atmForward, stdDev, annuity, displacement); double exerciseTime = vol_.link.timeFromReference(exerciseDate); results_.additionalResults["vega"] = new Spec().vega(strike, atmForward, stdDev, exerciseTime, annuity, displacement); }
public override void calculate() { Utils.QL_REQUIRE(numericalFix_ == NumericalFix.None || numericalFix_ == NumericalFix.Taylor, () => "numerical fix must be None or Taylor"); Utils.QL_REQUIRE(accrualBias_ == AccrualBias.HalfDayBias || accrualBias_ == AccrualBias.NoBias, () => "accrual bias must be HalfDayBias or NoBias"); Utils.QL_REQUIRE(forwardsInCouponPeriod_ == ForwardsInCouponPeriod.Flat || forwardsInCouponPeriod_ == ForwardsInCouponPeriod.Piecewise, () => "forwards in coupon period must be Flat or Piecewise"); // it would be possible to handle the cases which are excluded below, // but the ISDA engine is not explicitly specified to handle them, // so we just forbid them too Actual365Fixed dc = new Actual365Fixed(); Actual360 dc1 = new Actual360(); Actual360 dc2 = new Actual360(true); Date evalDate = Settings.evaluationDate(); // check if given curves are ISDA compatible // (the interpolation is checked below) Utils.QL_REQUIRE(!discountCurve_.empty(), () => "no discount term structure set"); Utils.QL_REQUIRE(!probability_.empty(), () => "no probability term structure set"); Utils.QL_REQUIRE(discountCurve_.link.dayCounter() == dc, () => "yield term structure day counter (" + discountCurve_.link.dayCounter() + ") should be Act/365(Fixed)"); Utils.QL_REQUIRE(probability_.link.dayCounter() == dc, () => "probability term structure day counter (" + probability_.link.dayCounter() + ") should be " + "Act/365(Fixed)"); Utils.QL_REQUIRE(discountCurve_.link.referenceDate() == evalDate, () => "yield term structure reference date (" + discountCurve_.link.referenceDate() + " should be evaluation date (" + evalDate + ")"); Utils.QL_REQUIRE(probability_.link.referenceDate() == evalDate, () => "probability term structure reference date (" + probability_.link.referenceDate() + " should be evaluation date (" + evalDate + ")"); Utils.QL_REQUIRE(arguments_.settlesAccrual, () => "ISDA engine not compatible with non accrual paying CDS"); Utils.QL_REQUIRE(arguments_.paysAtDefaultTime, () => "ISDA engine not compatible with end period payment"); Utils.QL_REQUIRE((arguments_.claim as FaceValueClaim) != null, () => "ISDA engine not compatible with non face value claim"); Date maturity = arguments_.maturity; Date effectiveProtectionStart = Date.Max(arguments_.protectionStart, evalDate + 1); // collect nodes from both curves and sort them List <Date> yDates = new List <Date>(), cDates = new List <Date>(); var castY1 = discountCurve_.link as PiecewiseYieldCurve <Discount, LogLinear>; var castY2 = discountCurve_.link as InterpolatedForwardCurve <BackwardFlat>; var castY3 = discountCurve_.link as InterpolatedForwardCurve <ForwardFlat>; var castY4 = discountCurve_.link as FlatForward; if (castY1 != null) { if (castY1.dates() != null) { yDates = castY1.dates(); } } else if (castY2 != null) { yDates = castY2.dates(); } else if (castY3 != null) { yDates = castY3.dates(); } else if (castY4 != null) { } else { Utils.QL_FAIL("Yield curve must be flat forward interpolated"); } var castC1 = probability_.link as InterpolatedSurvivalProbabilityCurve <LogLinear>; var castC2 = probability_.link as InterpolatedHazardRateCurve <BackwardFlat>; var castC3 = probability_.link as FlatHazardRate; if (castC1 != null) { cDates = castC1.dates(); } else if (castC2 != null) { cDates = castC2.dates(); } else if (castC3 != null) { } else { Utils.QL_FAIL("Credit curve must be flat forward interpolated"); } // Todo check List <Date> nodes = yDates.Union(cDates).ToList(); if (nodes.empty()) { nodes.Add(maturity); } double nFix = (numericalFix_ == NumericalFix.None ? 1E-50 : 0.0); // protection leg pricing (npv is always negative at this stage) double protectionNpv = 0.0; Date d0 = effectiveProtectionStart - 1; double P0 = discountCurve_.link.discount(d0); double Q0 = probability_.link.survivalProbability(d0); Date d1; int result = nodes.FindIndex(item => item > effectiveProtectionStart); for (int it = result; it < nodes.Count; ++it) { if (nodes[it] > maturity) { d1 = maturity; it = nodes.Count - 1; //early exit } else { d1 = nodes[it]; } double P1 = discountCurve_.link.discount(d1); double Q1 = probability_.link.survivalProbability(d1); double fhat = Math.Log(P0) - Math.Log(P1); double hhat = Math.Log(Q0) - Math.Log(Q1); double fhphh = fhat + hhat; if (fhphh < 1E-4 && numericalFix_ == NumericalFix.Taylor) { double fhphhq = fhphh * fhphh; protectionNpv += P0 * Q0 * hhat * (1.0 - 0.5 * fhphh + 1.0 / 6.0 * fhphhq - 1.0 / 24.0 * fhphhq * fhphh + 1.0 / 120 * fhphhq * fhphhq); } else { protectionNpv += hhat / (fhphh + nFix) * (P0 * Q0 - P1 * Q1); } d0 = d1; P0 = P1; Q0 = Q1; } protectionNpv *= arguments_.claim.amount(null, arguments_.notional.Value, recoveryRate_); results_.defaultLegNPV = protectionNpv; // premium leg pricing (npv is always positive at this stage) double premiumNpv = 0.0, defaultAccrualNpv = 0.0; for (int i = 0; i < arguments_.leg.Count; ++i) { FixedRateCoupon coupon = arguments_.leg[i] as FixedRateCoupon; Utils.QL_REQUIRE(coupon.dayCounter() == dc || coupon.dayCounter() == dc1 || coupon.dayCounter() == dc2, () => "ISDA engine requires a coupon day counter Act/365Fixed " + "or Act/360 (" + coupon.dayCounter() + ")"); // premium coupons if (!arguments_.leg[i].hasOccurred(evalDate, includeSettlementDateFlows_)) { double x1 = coupon.amount(); double x2 = discountCurve_.link.discount(coupon.date()); double x3 = probability_.link.survivalProbability(coupon.date() - 1); premiumNpv += coupon.amount() * discountCurve_.link.discount(coupon.date()) * probability_.link.survivalProbability(coupon.date() - 1); } // default accruals if (!new simple_event(coupon.accrualEndDate()) .hasOccurred(effectiveProtectionStart, false)) { Date start = Date.Max(coupon.accrualStartDate(), effectiveProtectionStart) - 1; Date end = coupon.date() - 1; double tstart = discountCurve_.link.timeFromReference(coupon.accrualStartDate() - 1) - (accrualBias_ == AccrualBias.HalfDayBias ? 1.0 / 730.0 : 0.0); List <Date> localNodes = new List <Date>(); localNodes.Add(start); //add intermediary nodes, if any if (forwardsInCouponPeriod_ == ForwardsInCouponPeriod.Piecewise) { foreach (Date node in nodes) { if (node > start && node < end) { localNodes.Add(node); } } //std::vector<Date>::const_iterator it0 = std::upper_bound(nodes.begin(), nodes.end(), start); //std::vector<Date>::const_iterator it1 = std::lower_bound(nodes.begin(), nodes.end(), end); //localNodes.insert(localNodes.end(), it0, it1); } localNodes.Add(end); double defaultAccrThisNode = 0.0; Date firstnode = localNodes.First(); double t0 = discountCurve_.link.timeFromReference(firstnode); P0 = discountCurve_.link.discount(firstnode); Q0 = probability_.link.survivalProbability(firstnode); foreach (Date node in localNodes.Skip(1)) //for (++node; node != localNodes.Last(); ++node) { double t1 = discountCurve_.link.timeFromReference(node); double P1 = discountCurve_.link.discount(node); double Q1 = probability_.link.survivalProbability(node); double fhat = Math.Log(P0) - Math.Log(P1); double hhat = Math.Log(Q0) - Math.Log(Q1); double fhphh = fhat + hhat; if (fhphh < 1E-4 && numericalFix_ == NumericalFix.Taylor) { // see above, terms up to (f+h)^3 seem more than enough, // what exactly is implemented in the standard isda C // code ? double fhphhq = fhphh * fhphh; defaultAccrThisNode += hhat * P0 * Q0 * ((t0 - tstart) * (1.0 - 0.5 * fhphh + 1.0 / 6.0 * fhphhq - 1.0 / 24.0 * fhphhq * fhphh) + (t1 - t0) * (0.5 - 1.0 / 3.0 * fhphh + 1.0 / 8.0 * fhphhq - 1.0 / 30.0 * fhphhq * fhphh)); } else { defaultAccrThisNode += (hhat / (fhphh + nFix)) * ((t1 - t0) * ((P0 * Q0 - P1 * Q1) / (fhphh + nFix) - P1 * Q1) + (t0 - tstart) * (P0 * Q0 - P1 * Q1)); } t0 = t1; P0 = P1; Q0 = Q1; } defaultAccrualNpv += defaultAccrThisNode * arguments_.notional.Value * coupon.rate() * 365.0 / 360.0; } } results_.couponLegNPV = premiumNpv + defaultAccrualNpv; // upfront flow npv double upfPVO1 = 0.0; results_.upfrontNPV = 0.0; if (!arguments_.upfrontPayment.hasOccurred( evalDate, includeSettlementDateFlows_)) { upfPVO1 = discountCurve_.link.discount(arguments_.upfrontPayment.date()); if (arguments_.upfrontPayment.amount().IsNotEqual(0.0)) { results_.upfrontNPV = upfPVO1 * arguments_.upfrontPayment.amount(); } } results_.accrualRebateNPV = 0.0; if (arguments_.accrualRebate != null && arguments_.accrualRebate.amount().IsNotEqual(0.0) && !arguments_.accrualRebate.hasOccurred(evalDate, includeSettlementDateFlows_)) { results_.accrualRebateNPV = discountCurve_.link.discount(arguments_.accrualRebate.date()) * arguments_.accrualRebate.amount(); } double upfrontSign = 1; if (arguments_.side == Protection.Side.Seller) { results_.defaultLegNPV *= -1.0; results_.accrualRebateNPV *= -1.0; } else { results_.couponLegNPV *= -1.0; results_.upfrontNPV *= -1.0; } results_.value = results_.defaultLegNPV + results_.couponLegNPV + results_.upfrontNPV + results_.accrualRebateNPV; results_.errorEstimate = null; if (results_.couponLegNPV.IsNotEqual(0.0)) { results_.fairSpread = -results_.defaultLegNPV * arguments_.spread / (results_.couponLegNPV + results_.accrualRebateNPV); } else { results_.fairSpread = null; } double upfrontSensitivity = upfPVO1 * arguments_.notional.Value; if (upfrontSensitivity.IsNotEqual(0.0)) { results_.fairUpfront = -upfrontSign * (results_.defaultLegNPV + results_.couponLegNPV + results_.accrualRebateNPV) / upfrontSensitivity; } else { results_.fairUpfront = null; } double basisPoint = 1.0e-4; if (arguments_.spread.IsNotEqual(0.0)) { results_.couponLegBPS = results_.couponLegNPV * basisPoint / arguments_.spread; } else { results_.couponLegBPS = null; } if (arguments_.upfront != null && arguments_.upfront.IsNotEqual(0.0)) { results_.upfrontBPS = results_.upfrontNPV * basisPoint / (arguments_.upfront); } else { results_.upfrontBPS = null; } }
public override void calculate() { Utils.QL_REQUIRE(integrationStep_ != null, () => "null period set"); Utils.QL_REQUIRE(!discountCurve_.empty(), () => "no discount term structure set"); Utils.QL_REQUIRE(!probability_.empty(), () => "no probability term structure set"); Date today = Settings.evaluationDate(); Date settlementDate = discountCurve_.link.referenceDate(); // Upfront Flow NPV. Either we are on-the-run (no flow) // or we are forward start double upfPVO1 = 0.0; if (!arguments_.upfrontPayment.hasOccurred(settlementDate, includeSettlementDateFlows_)) { // date determining the probability survival so we have to pay // the upfront (did not knock out) Date effectiveUpfrontDate = arguments_.protectionStart > probability_.link.referenceDate() ? arguments_.protectionStart : probability_.link.referenceDate(); upfPVO1 = probability_.link.survivalProbability(effectiveUpfrontDate) * discountCurve_.link.discount(arguments_.upfrontPayment.date()); } results_.upfrontNPV = upfPVO1 * arguments_.upfrontPayment.amount(); results_.couponLegNPV = 0.0; results_.defaultLegNPV = 0.0; for (int i = 0; i < arguments_.leg.Count; ++i) { if (arguments_.leg[i].hasOccurred(settlementDate, includeSettlementDateFlows_)) { continue; } FixedRateCoupon coupon = arguments_.leg[i] as FixedRateCoupon; // In order to avoid a few switches, we calculate the NPV // of both legs as a positive quantity. We'll give them // the right sign at the end. Date paymentDate = coupon.date(), startDate = (i == 0 ? arguments_.protectionStart : coupon.accrualStartDate()), endDate = coupon.accrualEndDate(); Date effectiveStartDate = (startDate <= today && today <= endDate) ? today : startDate; double couponAmount = coupon.amount(); double S = probability_.link.survivalProbability(paymentDate); // On one side, we add the fixed rate payments in case of // survival. results_.couponLegNPV += S * couponAmount * discountCurve_.link.discount(paymentDate); // On the other side, we add the payment (and possibly the // accrual) in case of default. Period step = integrationStep_; Date d0 = effectiveStartDate; Date d1 = Date.Min(d0 + step, endDate); double P0 = probability_.link.defaultProbability(d0); double endDiscount = discountCurve_.link.discount(paymentDate); do { double B = arguments_.paysAtDefaultTime ? discountCurve_.link.discount(d1) : endDiscount; double P1 = probability_.link.defaultProbability(d1); double dP = P1 - P0; // accrual... if (arguments_.settlesAccrual) { if (arguments_.paysAtDefaultTime) { results_.couponLegNPV += coupon.accruedAmount(d1) * B * dP; } else { results_.couponLegNPV += couponAmount * B * dP; } } // ...and claim. double claim = arguments_.claim.amount(d1, arguments_.notional.Value, recoveryRate_); results_.defaultLegNPV += claim * B * dP; // setup for next time around the loop P0 = P1; d0 = d1; d1 = Date.Min(d0 + step, endDate); } while (d0 < endDate); } double upfrontSign = 1.0; switch (arguments_.side) { case Protection.Side.Seller: results_.defaultLegNPV *= -1.0; break; case Protection.Side.Buyer: results_.couponLegNPV *= -1.0; results_.upfrontNPV *= -1.0; upfrontSign = -1.0; break; default: Utils.QL_FAIL("unknown protection side"); break; } results_.value = results_.defaultLegNPV + results_.couponLegNPV + results_.upfrontNPV; results_.errorEstimate = null; if (results_.couponLegNPV != 0.0) { results_.fairSpread = -results_.defaultLegNPV * arguments_.spread / results_.couponLegNPV; } else { results_.fairSpread = null; } double upfrontSensitivity = upfPVO1 * arguments_.notional.Value; if (upfrontSensitivity != 0.0) { results_.fairUpfront = -upfrontSign * (results_.defaultLegNPV + results_.couponLegNPV) / upfrontSensitivity; } else { results_.fairUpfront = null; } if (arguments_.spread != 0.0) { results_.couponLegBPS = results_.couponLegNPV * basisPoint / arguments_.spread; } else { results_.couponLegBPS = null; } if (arguments_.upfront.HasValue && arguments_.upfront.Value != 0.0) { results_.upfrontBPS = results_.upfrontNPV * basisPoint / (arguments_.upfront.Value); } else { results_.upfrontBPS = null; } }
public override void calculate() { Utils.QL_REQUIRE(!discountCurve_.empty(), () => "no discount term structure set"); Utils.QL_REQUIRE(!probability_.empty(), () => "no probability term structure set"); Date today = Settings.evaluationDate(); Date settlementDate = discountCurve_.link.referenceDate(); // Upfront Flow NPV. Either we are on-the-run (no flow) // or we are forward start double upfPVO1 = 0.0; if (!arguments_.upfrontPayment.hasOccurred(settlementDate, includeSettlementDateFlows_)) { // date determining the probability survival so we have to pay // the upfront (did not knock out) Date effectiveUpfrontDate = arguments_.protectionStart > probability_.link.referenceDate() ? arguments_.protectionStart : probability_.link.referenceDate(); upfPVO1 = probability_.link.survivalProbability(effectiveUpfrontDate) * discountCurve_.link.discount(arguments_.upfrontPayment.date()); } results_.upfrontNPV = upfPVO1 * arguments_.upfrontPayment.amount(); results_.couponLegNPV = 0.0; results_.defaultLegNPV = 0.0; for (int i = 0; i < arguments_.leg.Count; ++i) { if (arguments_.leg[i].hasOccurred(settlementDate, includeSettlementDateFlows_)) { continue; } FixedRateCoupon coupon = arguments_.leg[i] as FixedRateCoupon; // In order to avoid a few switches, we calculate the NPV // of both legs as a positive quantity. We'll give them // the right sign at the end. Date paymentDate = coupon.date(), startDate = coupon.accrualStartDate(), endDate = coupon.accrualEndDate(); // this is the only point where it might not coincide if (i == 0) { startDate = arguments_.protectionStart; } Date effectiveStartDate = (startDate <= today && today <= endDate) ? today : startDate; Date defaultDate = // mid-point effectiveStartDate + (endDate - effectiveStartDate) / 2; double S = probability_.link.survivalProbability(paymentDate); double P = probability_.link.defaultProbability(effectiveStartDate, endDate); // on one side, we add the fixed rate payments in case of // survival... results_.couponLegNPV += S * coupon.amount() * discountCurve_.link.discount(paymentDate); // ...possibly including accrual in case of default. if (arguments_.settlesAccrual) { if (arguments_.paysAtDefaultTime) { results_.couponLegNPV += P * coupon.accruedAmount(defaultDate) * discountCurve_.link.discount(defaultDate); } else { // pays at the end results_.couponLegNPV += P * coupon.amount() * discountCurve_.link.discount(paymentDate); } } // on the other side, we add the payment in case of default. double claim = arguments_.claim.amount(defaultDate, arguments_.notional.Value, recoveryRate_); if (arguments_.paysAtDefaultTime) { results_.defaultLegNPV += P * claim * discountCurve_.link.discount(defaultDate); } else { results_.defaultLegNPV += P * claim * discountCurve_.link.discount(paymentDate); } } double upfrontSign = 1.0; switch (arguments_.side) { case Protection.Side.Seller: results_.defaultLegNPV *= -1.0; break; case Protection.Side.Buyer: results_.couponLegNPV *= -1.0; results_.upfrontNPV *= -1.0; upfrontSign = -1.0; break; default: Utils.QL_FAIL("unknown protection side"); break; } results_.value = results_.defaultLegNPV + results_.couponLegNPV + results_.upfrontNPV; results_.errorEstimate = null; if (results_.couponLegNPV != 0.0) { results_.fairSpread = -results_.defaultLegNPV * arguments_.spread / results_.couponLegNPV; } else { results_.fairSpread = null; } double upfrontSensitivity = upfPVO1 * arguments_.notional.Value; if (upfrontSensitivity != 0.0) { results_.fairUpfront = -upfrontSign * (results_.defaultLegNPV + results_.couponLegNPV) / upfrontSensitivity; } else { results_.fairUpfront = null; } if (arguments_.spread != 0.0) { results_.couponLegBPS = results_.couponLegNPV * basisPoint / arguments_.spread.Value; } else { results_.couponLegBPS = null; } if (arguments_.upfront.HasValue && arguments_.upfront.Value != 0.0) { results_.upfrontBPS = results_.upfrontNPV * basisPoint / (arguments_.upfront.Value); } else { results_.upfrontBPS = null; } }