/// <summary> /// Value a caplet or floorlet under the 1 factor Hull-White model. /// </summary> public override void Value(Vector pv, Vector cash, double baseDate, double valueDate, ISACCRResult saccrResult, IIntraValuationDiagnosticsWriter intraValuationDiagnosticsWriter) { int count = fCashflows.Count(); bool forecastIsDiscount = ReferenceEquals(fForecastRate, fDiscountRate); // time of dfStart and dfEnd double tDfStart = double.NegativeInfinity; double tDfEnd = double.NegativeInfinity; using (var cache = Vector.CacheLike(pv)) { // Shared between loops Vector dfStart = cache.Get(); Vector dfEnd = cache.Get(); VectorEngine.For(0, count, LoopDirection.Backwards, i => { using (var innerCache = Vector.CacheLike(pv)) { CFFloatingInterest cashflow = fCashflows[i]; if (cashflow.Payment_Date < valueDate || cashflow.Payment_Date <= fCutoffDate) { return(LoopAction.Break); } Vector rate = innerCache.Get(); Vector dfPay = innerCache.Get(); Vector stdDev = innerCache.GetClear(); Vector amount = innerCache.GetClear(); GeneralCashflowProperties properties = fCashflows.GetCashflowProperties(i); double tPay = CalcUtils.DaysToYears(cashflow.Payment_Date - baseDate); bool haveDfPay = false; if (forecastIsDiscount && tPay == tDfStart) { dfPay.Assign(dfStart); haveDfPay = true; } using (IntraValuationDiagnosticsHelper.StartCashflow(intraValuationDiagnosticsWriter)) using (var volatilitiesAtDateStore = IntraValuationDiagnosticsHelper.CreateVolatilitiesAtDateStore(intraValuationDiagnosticsWriter, pv.Count)) { cashflow.AddPropertiesToIntraValuationDiagnostics(intraValuationDiagnosticsWriter); // Standard Libor implies single reset. var reset = cashflow.Resets.Single(); if (reset.IsKnown(baseDate)) { rate.Assign(reset.Known_Rate); } else { double tValue = CalcUtils.DaysToYears(valueDate - baseDate); double tReset = CalcUtils.DaysToYears(reset.Reset_Date - baseDate); double tStart = CalcUtils.DaysToYears(reset.Rate_Start_Date - baseDate); double tEnd = CalcUtils.DaysToYears(reset.Rate_End_Date - baseDate); // Reset is a historical or forward Libor rate. InterestRateUtils.LiborRate(rate, fForecastRate, tValue, tReset, tStart, tEnd, reset.Rate_Year_Fraction, dfStart, ref tDfStart, dfEnd, ref tDfEnd); if (tReset > tValue) { GetStandardDeviation(stdDev, tValue, tReset, tStart, tEnd); volatilitiesAtDateStore.Add(valueDate, reset.Reset_Date, stdDev); } } if (!haveDfPay && forecastIsDiscount && tPay == tDfEnd) { dfPay.Assign(dfEnd); haveDfPay = true; } // Add swaplet value amount.AddProduct(properties.Swap_Multiplier, rate); double tau = reset.Rate_Year_Fraction; rate.Assign(1.0 + rate * tau); // Add cap and floor option values. AddOptionValue(amount, OptionType.Call, rate, properties.Cap_Strike, stdDev, tau, properties.Cap_Multiplier); AddOptionValue(amount, OptionType.Put, rate, properties.Floor_Strike, stdDev, tau, properties.Floor_Multiplier); amount.Assign(fBuySellSign * (cashflow.Fixed_Amount + cashflow.Notional * (amount + cashflow.Margin) * cashflow.Accrual_Year_Fraction)); IntraValuationDiagnosticsHelper.AddImpliedVolatilities(intraValuationDiagnosticsWriter, volatilitiesAtDateStore); CFFixedList.RoundCashflow(amount, Cashflow_Rounding); CFFixedList.UpdatePvAndCash(cashflow, baseDate, valueDate, haveDfPay ? null : fDiscountRate, null, amount, dfPay, pv, cash, intraValuationDiagnosticsWriter); } } return(LoopAction.Continue); }); } }
/// <summary> /// Valuation method. /// </summary> public override void Value(ValuationResults valuationResults, PriceFactorList factors, BaseTimeGrid baseTimes) { CDOValuationParameters parameters = GetValuationParameters(factors); double scale = (fDeal.Buy_Sell == BuySell.Buy) ? +fDeal.Principal : -fDeal.Principal; double trancheSize = fDeal.Detachment - fDeal.Attachment; if (trancheSize < CalcUtils.TINY) { return; } double tUpfront = CalcUtils.DaysToYears(fDeal.Upfront_Date - factors.BaseDate); TimeGridIterator tgi = new TimeGridIterator(fT); CashAccumulators accumulator = valuationResults.Cash; PVProfiles result = valuationResults.Profile; using (IntraValuationDiagnosticsHelper.StartDeal(fIntraValuationDiagnosticsWriter, Deal)) { VectorEngine.For(tgi, () => { using (var cache = Vector.Cache(factors.NumScenarios)) { Vector npv = cache.Get(); Vector expectedWritedownPremiumNotional = cache.Get(); Vector expectedLoss = cache.Get(); Vector expectedRecovery = cache.Get(); Vector discountFactor = cache.Get(); Vector realizedIndexLoss = cache.Get(); Vector realizedIndexRecovery = cache.Get(); Vector adjustedAttachment = cache.Get(); Vector adjustedDetachment = cache.Get(); Vector trancheRemainder = cache.Get(); // Handle upfront payment if (fDeal.Upfront_Date >= tgi.Date) { npv.Assign(scale * parameters.DF.Get(tgi.T, tUpfront) * fDeal.Upfront_Amount); } else { npv.Clear(); } // reinitialise running variables expectedWritedownPremiumNotional.Clear(); expectedLoss.Clear(); expectedRecovery.Clear(); discountFactor.Assign(parameters.DF.Get(tgi.T, tgi.T)); if (accumulator != null && tgi.Date == fDeal.Upfront_Date) { accumulator.Accumulate(parameters.X, tgi.Date, fDeal.Upfront_Amount); } // Check for realized loss and recovery and adjust the attachment and detachment accordingly parameters.RealizedLoss(realizedIndexLoss, realizedIndexRecovery, tgi.T, fDeal.Payoff_Is_Digital == YesNo.Yes, fDeal.Digital_Payoff_Percentage); adjustedDetachment.Assign(VectorMath.Max(0.0, VectorMath.Min(1.0 - realizedIndexRecovery, fDeal.Detachment) - realizedIndexLoss)); adjustedAttachment.Assign(VectorMath.Max(0.0, VectorMath.Min(1.0 - realizedIndexRecovery, fDeal.Attachment) - realizedIndexLoss)); trancheRemainder.Assign((adjustedDetachment - adjustedAttachment) / trancheSize); if (adjustedDetachment.MaxElement() > CalcUtils.TINY) { // Diagnostics double sumDefaultAccrual = 0; double sumPVPremium = 0; double sumPVProtection = 0; bool needDiagnostics = tgi.T == 0.0 && fIntraValuationDiagnosticsWriter.Level > IntraValuationDiagnosticsLevel.None; using (needDiagnostics ? IntraValuationDiagnosticsHelper.StartCDO(fIntraValuationDiagnosticsWriter, tgi.Date, fDeal.Principal) : null) { // Value future coupon periods VectorEngine.For(0, PayDates.Count, i => { if (PayDates[i] < tgi.Date) { return(LoopAction.Continue); } double tPay = CalcUtils.DaysToYears(PayDates[i] - factors.BaseDate); using (var innerCache = Vector.CacheLike(npv)) { Vector oldExpectedLoss = innerCache.Get(expectedLoss); Vector oldDiscountFactor = innerCache.Get(discountFactor); Vector oldExpectedWritedownPremiumNotional = innerCache.Get(expectedWritedownPremiumNotional); Vector expectedLossAttachment = innerCache.Get(); Vector expectedLossDetachment = innerCache.Get(); Vector premiumLeg = innerCache.Get(); Vector defaultLeg = innerCache.Get(); Vector accruedInDefault = innerCache.Get(); Vector expectedRecoveryAttachment = innerCache.Get(); Vector expectedRecoveryDetachment = innerCache.Get(); Vector avgDiscountFactor = innerCache.Get(); Vector pv = innerCache.Get(); // Get the expected loss and recovery for the tranche detachment and attachment parameters.ExpectedLossAndRecovery(expectedLossDetachment, expectedRecoveryDetachment, tgi.T, tPay, adjustedDetachment, realizedIndexLoss, realizedIndexRecovery); parameters.ExpectedLossAndRecovery(expectedLossAttachment, expectedRecoveryAttachment, tgi.T, tPay, adjustedAttachment, realizedIndexLoss, realizedIndexRecovery); expectedLoss.Assign((expectedLossDetachment - expectedLossAttachment) / trancheSize); expectedRecovery.Assign((expectedRecoveryDetachment - expectedRecoveryAttachment) / trancheSize); expectedWritedownPremiumNotional.Assign(expectedLoss + expectedRecovery); // Premium leg approximation: Accrued in default pays half the accrued. Remove expected loss and recovery (top down writeoff) premiumLeg.Assign(fDeal.Spread * (trancheRemainder - expectedWritedownPremiumNotional) * Accruals[i]); accruedInDefault.Assign(fDeal.Spread * (expectedWritedownPremiumNotional - oldExpectedWritedownPremiumNotional) * 0.5 * Accruals[i]); // Default leg approximation: account for default with bullet payment at end of period defaultLeg.Assign(expectedLoss - oldExpectedLoss); // Convention: bought CDO pays the premium to the buyer discountFactor.Assign(parameters.DF.Get(tgi.T, tPay)); avgDiscountFactor.Assign(0.5 * (discountFactor + oldDiscountFactor)); pv.Assign(scale * (premiumLeg * discountFactor + (accruedInDefault - defaultLeg) * avgDiscountFactor)); npv.Add(pv); if (accumulator != null && tgi.T == tPay) { accumulator.Accumulate(parameters.X, tgi.Date, scale * (premiumLeg + accruedInDefault - defaultLeg)); } if (needDiagnostics) { using (var innerCache1 = Vector.CacheLike(npv)) { Vector expectedPremium = innerCache1.Get(scale * premiumLeg); Vector expectedDefaultAccrual = innerCache1.Get(scale * accruedInDefault); Vector expectedDefaultLoss = innerCache1.Get(scale * defaultLeg); Vector pvDefaultAccrual = innerCache1.Get(expectedDefaultAccrual * avgDiscountFactor); Vector pvPremium = innerCache1.Get(expectedPremium * discountFactor); Vector pvProctection = innerCache1.Get(-expectedDefaultLoss * avgDiscountFactor); // accumulate sums if (i >= 0) { sumDefaultAccrual += pvDefaultAccrual[0]; sumPVPremium += pvPremium[0]; sumPVProtection += pvProctection[0]; } using (IntraValuationDiagnosticsHelper.StartCashflow(fIntraValuationDiagnosticsWriter, PayDates[i])) { var remainingPool = cache.Get(1.0 - realizedIndexLoss - realizedIndexRecovery); AddIntraValuationDiagnostics(fIntraValuationDiagnosticsWriter, parameters, adjustedAttachment, adjustedDetachment, remainingPool, tgi.T, tPay); IntraValuationDiagnosticsHelper.AddDetailedCDOCashflow(fIntraValuationDiagnosticsWriter, expectedPremium, expectedRecovery, expectedDefaultAccrual, expectedDefaultLoss, discountFactor, pv); } } } } return(LoopAction.Continue); }); if (needDiagnostics) { IntraValuationDiagnosticsHelper.AddSummaryCDOAmounts(fIntraValuationDiagnosticsWriter, npv, sumDefaultAccrual, sumPVPremium, sumPVProtection); } } result.AppendVector(tgi.Date, npv * parameters.X.Get(tgi.T)); } } }); } // After maturity result.Complete(fT); }