/// <summary> /// Prepare for valuation. /// </summary> public override void PreValue(PriceFactorList factors) { base.PreValue(factors); SetModelParameters(fItems); PreValueDeals(fItems, factors); CallableStructuredDeal deal = (CallableStructuredDeal)Deal; // Set volatility price factors if they have been registered by model or underlying deals InterestVol.TryGet <IInterestRateVol>(factors, deal.Forecast_Rate_Cap_Volatility, fForecastCurrency, out fInterestRateVol); InterestVol.TryGet <IInterestYieldVol>(factors, deal.Forecast_Rate_Swaption_Volatility, fForecastCurrency, out fInterestYieldVol); bool needRating = Respect_Default == YesNo.Yes && !string.IsNullOrEmpty(deal.Issuer); bool needSurvival = Use_Survival_Probability == YesNo.Yes && !string.IsNullOrEmpty(deal.Issuer); if (needRating) { fCreditRating = factors.Get <CreditRating>(deal.Issuer); fRecoveryRate = factors.Get <RecoveryRate>(InterestRateUtils.GetRateId(deal.Recovery_Rate, deal.Issuer)); } if (needSurvival) { fSurvivalProb = factors.GetInterface <ISurvivalProb>(string.IsNullOrEmpty(deal.Survival_Probability) ? deal.Issuer : deal.Survival_Probability); } }
/// <summary> /// Prepare for valuation anything that is dependent upon the scenario. /// </summary> public override void PreValue(PriceFactorList factors) { base.PreValue(factors); CallableBondForward deal = (CallableBondForward)Deal; fInterestYieldVol = InterestVolBase.GetYieldVol(factors, deal.Yield_Volatility, fCurrency); fNeedsCreditRating = NeedCreditRating(); fCreditRating = NeedCreditRating() ? factors.Get <CreditRating>(deal.Issuer) : null; fRecoveryRate = NeedRecovery() ? factors.Get <RecoveryRate>(string.IsNullOrEmpty(deal.Recovery_Rate) ? deal.Issuer : deal.Recovery_Rate) : null; fSurvivalProb = NeedSurvivalProb() ? factors.GetInterface <ISurvivalProb>(string.IsNullOrEmpty(deal.Survival_Probability) ? deal.Issuer : deal.Survival_Probability) : null; }
/// <summary> /// Prepare for valuation anything that is dependent upon the scenario. /// </summary> /// <param name="factors">Price factors.</param> public override void PreValue(PriceFactorList factors) { base.PreValue(factors); BondOptionDeal deal = (BondOptionDeal)Deal; bool needRating = Respect_Default == YesNo.Yes && !string.IsNullOrEmpty(deal.Issuer); bool needSurvival = Use_Survival_Probability == YesNo.Yes && !string.IsNullOrEmpty(deal.Issuer); bool needRecovery = needRating; fCreditRating = needRating ? factors.Get <CreditRating>(deal.Issuer) : null; fRecoveryRate = needRecovery ? factors.Get <RecoveryRate>(string.IsNullOrEmpty(deal.Recovery_Rate) ? deal.Issuer : deal.Recovery_Rate) : null; fSurvivalProb = needSurvival ? factors.GetInterface <ISurvivalProb>(string.IsNullOrEmpty(deal.Survival_Probability) ? deal.Issuer : deal.Survival_Probability) : null; }
/// <summary> /// Prepare for valuation anything that is dependent upon the scenario. /// </summary> /// <param name="factors">Price factors.</param> public override void PreValue(PriceFactorList factors) { base.PreValue(factors); IInflationCashflowListDeal deal = (IInflationCashflowListDeal)Deal; fIsDefaultNever = !NeedCreditRating(); fCreditRating = NeedCreditRating() ? factors.Get <CreditRating>(deal.Issuer) : null; fRecoveryRate = NeedRecoveryRate() ? factors.Get <RecoveryRate>(GetRecoveryRateID()) : null; fSurvivalProb = NeedSurvivalProb() ? factors.GetInterface <ISurvivalProb>(GetSurvivalProbID()) : null; fInflationRate = !string.IsNullOrEmpty(deal.Index) ? factors.GetInterface <IInflationRate>(deal.Index) : null; fRepoRate = !string.IsNullOrEmpty(deal.Repo_Rate) ? factors.GetInterface <IInterestRate>(deal.Repo_Rate) : fDiscountRate; fIndexVolatility = deal is InflationOptionCashflowListDeal?factors.GetInterface <IPriceIndexVolatility>(GetPriceIndexVolatility()) : null; }
/// <summary> /// Prepare for valuation anything that is dependent upon the scenario. /// </summary> public override void PreValue(PriceFactorList factors) { base.PreValue(factors); var deal = (BondRepoBase)fDeal; if (string.IsNullOrEmpty(deal.Issuer)) { return; } fSurvivalProb = factors.GetInterface <ISurvivalProb>(string.IsNullOrEmpty(deal.Survival_Probability) ? deal.Issuer : deal.Survival_Probability); fRecoveryRate = factors.Get <RecoveryRate>(string.IsNullOrEmpty(deal.Recovery_Rate) ? deal.Issuer : deal.Recovery_Rate); }
public void OnGet() { Deaths = diagnosisService.Deaths(); Recovered = diagnosisService.Recovered(); Diagnoses = diagnosisService.GetDiagnosesWithCorona(); var newDiagnosis = Diagnoses .Where(x => (x.Death == true && x.Recovered == true) || (x.Death == true && x.Recovered == false) || (x.Death == false && x.Recovered == true)) .GroupBy(d => d.Recovered); foreach (var item in newDiagnosis) { RecoveryRate.Add(new StatisticsCore { Recovered = item.Key == true ? "Recovered " + String.Format("{0:0.00}", ((double)item.Count() / (Deaths.Count() + Recovered.Count())) * 100) + "%" : "Deaths " + String.Format("{0:0.00}", ((double)item.Count() / (Deaths.Count() + Recovered.Count())) * 100) + "%", TotalPatients = item.GroupBy(x => x.Patient.Name).Count() }); } }
/// <summary> /// Prepare for valuation anything that is dependent upon the scenario. /// </summary> public override void PreValue(PriceFactorList factors) { base.PreValue(factors); CFListBaseDeal <TCashflowList> deal = (CFListBaseDeal <TCashflowList>)fDeal; if (string.IsNullOrEmpty(fDeal.GetIssuer())) { return; } if (UseSurvivalProbability()) { fSurvivalProb = factors.GetInterface <ISurvivalProb>(string.IsNullOrEmpty(deal.GetSurvivalProbability()) ? deal.GetIssuer() : deal.GetSurvivalProbability()); } if (RespectDefault()) { fRecoveryRate = factors.Get <RecoveryRate>(InterestRateUtils.GetRateId(deal.GetRecoveryRate(), deal.GetIssuer())); fCreditRating = factors.Get <CreditRating>(deal.GetIssuer()); } }
/// <summary> /// This method Adjusts the bond price vector by taking the default into account give the default time vector. /// </summary> public static void AdjustForDefault(double baseDate, double valueDate, Vector bondPrice, TDate expiryDate, bool respectDefault, Vector underlyingIsAlive, Vector historicalRecovery, Vector defaultTime, IInterestRate discountRate, RecoveryRate recoveryRate) { // Adjust the bond price for default: check scenario by scenario for defaults, overwriting bondPrice as necessary if (!respectDefault) { return; } double currentTime = CalcUtils.DaysToYears(valueDate - baseDate); double tExpiry = CalcUtils.DaysToYears(expiryDate - baseDate); BondOptionValuation.AdjustBondValueForDefault(1.0, tExpiry, bondPrice, underlyingIsAlive, historicalRecovery, defaultTime, currentTime, discountRate, recoveryRate); }
/// <summary> /// Modify the pv and cash taking the date of default into account. /// </summary> public override void GetDefaultValue(double baseDate, double valueDate, Vector defaultDate, RecoveryRate recoveryRate, Vector pv, Vector cash) { CFFixedInterestListDeal deal = (CFFixedInterestListDeal)fDeal; double principal = GetPrincipal(deal.Cashflows, valueDate); double settlementDate = deal.Settlement_Date; double t = CalcUtils.DaysToYears(valueDate - baseDate); using (var cache = Vector.CacheLike(pv)) { // Approximation: recover only principal, neglecting accrued interest Vector recovery = cache.Get(fBuySellSign * principal * recoveryRate.Get(t)); if (valueDate <= settlementDate) { // Set the pv to (recovery - settlementAmount) * df when defaultDate <= valueDate <= settlementDate. // Set cash to (recovery - settlementAmount) when defaultDate <= valueDate = settlementDate (cash is always zero before settlementDate). double tSettle = CalcUtils.DaysToYears(settlementDate - baseDate); double settlementAmount = fBuySellSign * fSettlementAmount; Vector hasDefaulted = cache.Get(defaultDate <= valueDate); pv.AssignConditional(hasDefaulted, fRepoRate.Get(t, tSettle) * (recovery - settlementAmount), pv); if (cash != null && valueDate == settlementDate) { cash.AssignConditional(hasDefaulted, pv, cash); } } else { // after settlement date recovery.MultiplyBy(defaultDate >= valueDate); // set to zero if already defaulted Vector notDefaulted = cache.Get(defaultDate > valueDate); pv.AssignConditional(notDefaulted, pv, recovery); if (cash != null) { cash.AssignConditional(notDefaulted, cash, recovery); } } } }
/// <summary> /// This method check scenario by scenario for defaults, overwriting bondValue as necessary /// </summary> public static void AdjustBondValueForDefault(double notional, double expiryTime, Vector bondValue, Vector bondIsAlive, Vector historicalRecovery, Vector defaultTime, double currentTime, IInterestRate discountRate, RecoveryRate recoveryRate) { using (var cache = Vector.CacheLike(bondValue)) { Vector recoveryValue = cache.Get(); Vector expiryDf = cache.Get(); discountRate.GetValue(expiryDf, currentTime, expiryTime); recoveryRate.GetValue(recoveryValue, currentTime); // If this is the period when the bond defaults fill in the historical recovery amount. historicalRecovery.AssignConditional((defaultTime <= currentTime).And(bondIsAlive), recoveryValue * notional, historicalRecovery); bondValue.AssignConditional((defaultTime <= currentTime), expiryDf * historicalRecovery, bondValue); bondIsAlive.AssignConditional((defaultTime <= currentTime), 0.0, bondIsAlive); } }
/// <summary> /// Returns the recovery rate to be used for realized default. /// </summary> protected override void RealizedRecoveryRate(Vector vout, RecoveryRate RR, double t) { RR.GetValue(vout, t); }
/// <summary> /// Returns the recovery rate to be used for realized default. /// </summary> protected abstract void RealizedRecoveryRate(Vector vout, RecoveryRate RR, double t);
/// <summary> /// Vector valuation function. /// </summary> public override void Value(ValuationResults valuationResults, PriceFactorList factors, BaseTimeGrid baseTimes) { PreValue(factors); TimeGridIterator tgi = new TimeGridIterator(fT); PVProfiles result = valuationResults.Profile; CashAccumulators accumulator = valuationResults.Cash; DealCreditLinkedNoteBase deal = (DealCreditLinkedNoteBase)fDeal; ISurvivalProb SP = GetSurvivalProbability(factors); RecoveryRate RR = GetRecoveryRate(factors); CreditRating CR = GetCreditRating(factors); double tEffective = CalcUtils.DaysToYears(deal.Effective_Date - factors.BaseDate); double scale = (deal.Buy_Sell == BuySell.Buy) ? +deal.Notional_Amount : -deal.Notional_Amount; double purchasePrice = Percentage.PercentagePoint * deal.Price; double couponRate = (deal.Coupon_Type == InterestRateType.Fixed) ? Percentage.PercentagePoint * deal.Coupon_Rate : 0.0; double couponSpread = (deal.Coupon_Type == InterestRateType.Fixed) ? 0.0 : BasisPoint.BasisPointValue * deal.Coupon_Spread; double indexTenor = (deal.Index_Tenor > 0.0) ? deal.Index_Tenor : deal.Coupon_Interval; using (var cache = Vector.Cache(factors.NumScenarios)) { bool[] hasDefaulted = (Respect_Default == YesNo.Yes) ? new bool[factors.NumScenarios] : null; Vector defaultTime = (Respect_Default == YesNo.Yes) ? cache.Get() : null; Vector historicalRecovery = (Respect_Default == YesNo.Yes) ? cache.GetClear() : null; if (hasDefaulted != null && CR != null) { DefaultTime(defaultTime, CR); } Vector npv = cache.Get(); Vector cash = cache.Get(); Vector pStart = cache.Get(); Vector pEnd = cache.Get(); Vector amount = cache.Get(); Vector recovery = cache.Get(); Vector dfLast = cache.Get(); Vector df = cache.Get(); cash.Clear(); var defaultedBeforeTheBaseDate = Respect_Default == YesNo.Yes && CreditRating.DefaultedBeforeBaseDate(CR, factors.BaseDate); while (tgi.Next()) { if (defaultedBeforeTheBaseDate) { npv.Clear(); result.AppendVector(tgi.Date, npv); break; } if (!deal.Principal_Guaranteed && Respect_Default == YesNo.Yes) { RealizedRecoveryRate(recovery, RR, tgi.T); } // Assume defaults are rare and start by valuing under all scenarios without realized defaults // Value of principal repayment SurvivalProbability(pEnd, factors, SP, tgi.T, fT.fLast); fDiscountRate.GetValue(dfLast, tgi.T, fT.fLast); if (deal.Principal_Guaranteed) { npv.Assign(dfLast); } else { npv.Assign(dfLast * pEnd); } if (accumulator != null && tgi.T == fT.fLast) { cash.Assign(npv); } // Value of coupons for (int i = PayDates.Count - 1; i >= 0 && tgi.Date <= PayDates[i]; --i) { double tPay = CalcUtils.DaysToYears(PayDates[i] - factors.BaseDate); double tReset = CalcUtils.DaysToYears(ResetDates[i] - factors.BaseDate); double tPrevious = Math.Max(tgi.T, tReset); SurvivalProbability(pStart, factors, SP, tgi.T, tPrevious); if (deal.Coupon_Type == InterestRateType.Floating) { // Forecast a coupon, add the spread InterestRateUtils.LiborRate(amount, fForecastRate, tgi.T, tReset, tReset, tReset + indexTenor, deal.Index_Day_Count); amount.Add(couponSpread); amount.MultiplyBy(Accruals[i]); } else { // Fixed coupon amount.Assign(couponRate * Accruals[i]); } // The value of the coupon if no default npv.Add(amount * fDiscountRate.Get(tgi.T, tPay) * pEnd); if (accumulator != null && tgi.T == tPay) { cash.Assign(amount); } // The recovery value on default - assume guaranteed principal paid at end, recovery paid immediately if (!deal.Principal_Guaranteed) { npv.Add(fDiscountRate.Get(tgi.T, 0.5 * (tPay + tPrevious)) * (pStart - pEnd) * PricingRecoveryRate(SP)); } pEnd.DestructiveAssign(pStart); } // Now check for realized default scenario by scenario, overwriting NPV and cash as appropriate if (Respect_Default == YesNo.Yes && defaultTime != null) { if (tgi.T < tEffective) { fDiscountRate.GetValue(df, tgi.T, tEffective); } for (int i = 0; i < npv.Count; ++i) { if (defaultTime[i] > tgi.T) { continue; } if (deal.Principal_Guaranteed) { npv[i] = dfLast[i]; // full principal paid at maturity } else { if (!hasDefaulted[i]) { historicalRecovery[i] = recovery[i]; // record the historical recovery rate } if (tgi.T < tEffective) { npv[i] = df[i] * historicalRecovery[i]; // The discounted recovery value of the principal will be paid out on the effective date } else if (tgi.T == tEffective || !hasDefaulted[i]) { npv[i] = historicalRecovery[i]; // The full recovery amount is paid out } else { npv[i] = 0.0; // default is in the past but we are after effective date; settlement has already occurred. } } hasDefaulted[i] = true; } } // Value of purchase price if (tgi.T < tEffective) { npv.Add(-purchasePrice * fDiscountRate.Get(tgi.T, tEffective)); } else if (tgi.T == tEffective) { npv.Add(-purchasePrice); if (accumulator != null) { cash.Add(-purchasePrice); } } result.AppendVector(tgi.Date, scale * npv * fFxRate.Get(tgi.T)); if (accumulator != null) { accumulator.Accumulate(fFxRate, tgi.Date, scale * cash); } } // After maturity result.Complete(fT); } }
/// <summary> /// Modify the pv and cash taking the date of default into account. /// </summary> public override void GetDefaultValue(double baseDate, double valueDate, Vector defaultDate, RecoveryRate recoveryRate, Vector pv, Vector cash) { var deal = (CFFloatingInterestListDeal)Deal; double principal = GetPrincipal(fCashflows, valueDate); double settlementDate = deal.Settlement_Date; double t = CalcUtils.DaysToYears(valueDate - baseDate); using (var cache = Vector.CacheLike(pv)) { // Approximation: recover only principal, neglecting accrued interest Vector recovery = cache.Get(fBuySellSign * principal * recoveryRate.Get(t)); if (valueDate <= settlementDate) { // Set the pv to (recovery - settlementAmount) * df when defaultDate <= valueDate <= settlementDate. // Set cash to (recovery - settlementAmount) when defaultDate <= valueDate = settlementDate (cash is always zero before settlementDate). double tSettle = CalcUtils.DaysToYears(settlementDate - baseDate); Vector settlementAmount = cache.GetClear(); if (deal.Settlement_Amount_Is_Clean == YesNo.Yes) { fCashflows.CalculateAccrual(settlementAmount, baseDate, settlementDate, false, deal.AccrualHolidayCalendar(), fForecastRate); } settlementAmount.Add(deal.Settlement_Amount); Vector hasDefaulted = cache.Get(defaultDate <= valueDate); pv.AssignConditional(hasDefaulted, fRepoRate.Get(t, tSettle) * (recovery - fBuySellSign * settlementAmount), pv); if (cash != null && valueDate == settlementDate) { cash.AssignConditional(hasDefaulted, pv, cash); } } else { // after settlement date recovery.MultiplyBy(defaultDate >= valueDate); // set to zero if already defaulted Vector notDefaulted = cache.Get(defaultDate > valueDate); pv.AssignConditional(notDefaulted, pv, recovery); if (cash != null) { cash.AssignConditional(notDefaulted, cash, recovery); } } } }