CreditCurveScenario.java

  1. package org.drip.state.boot;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2013 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2012 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2011 Lakshmi Krishnamurthy
  16.  *
  17.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  18.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  19.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  20.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  21.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  22.  *      and computational support.
  23.  *  
  24.  *      https://lakshmidrip.github.io/DROP/
  25.  *  
  26.  *  DROP is composed of three modules:
  27.  *  
  28.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  29.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  30.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  31.  *
  32.  *  DROP Product Core implements libraries for the following:
  33.  *  - Fixed Income Analytics
  34.  *  - Loan Analytics
  35.  *  - Transaction Cost Analytics
  36.  *
  37.  *  DROP Portfolio Core implements libraries for the following:
  38.  *  - Asset Allocation Analytics
  39.  *  - Asset Liability Management Analytics
  40.  *  - Capital Estimation Analytics
  41.  *  - Exposure Analytics
  42.  *  - Margin Analytics
  43.  *  - XVA Analytics
  44.  *
  45.  *  DROP Computational Core implements libraries for the following:
  46.  *  - Algorithm Support
  47.  *  - Computation Support
  48.  *  - Function Analysis
  49.  *  - Model Validation
  50.  *  - Numerical Analysis
  51.  *  - Numerical Optimizer
  52.  *  - Spline Builder
  53.  *  - Statistical Learning
  54.  *
  55.  *  Documentation for DROP is Spread Over:
  56.  *
  57.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  58.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  59.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  60.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  61.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  62.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  63.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  64.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  65.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  66.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  67.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  68.  *
  69.  *  Licensed under the Apache License, Version 2.0 (the "License");
  70.  *      you may not use this file except in compliance with the License.
  71.  *  
  72.  *  You may obtain a copy of the License at
  73.  *      http://www.apache.org/licenses/LICENSE-2.0
  74.  *  
  75.  *  Unless required by applicable law or agreed to in writing, software
  76.  *      distributed under the License is distributed on an "AS IS" BASIS,
  77.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  78.  *  
  79.  *  See the License for the specific language governing permissions and
  80.  *      limitations under the License.
  81.  */

  82. /**
  83.  * <i>CreditCurveScenario</i> uses the hazard rate calibration instruments along with the component
  84.  * calibrator to produce scenario hazard rate curves. CreditCurveScenario typically first constructs the
  85.  * actual curve calibrator instance to localize the intelligence around curve construction. It then uses this
  86.  * curve calibrator instance to build individual curves or the sequence of node bumped scenario curves. The
  87.  * curves in the set may be an array, or tenor-keyed.
  88.  *
  89.  *  <br><br>
  90.  *  <ul>
  91.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  92.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  93.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/README.md">Latent State Inference and Creation Utilities</a></li>
  94.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/boot/README.md">Bootable Discount, Credit, Volatility States</a></li>
  95.  *  </ul>
  96.  * <br><br>
  97.  *
  98.  * @author Lakshmi Krishnamurthy
  99.  */

  100. public class CreditCurveScenario {
  101.     static class TranslatedQuoteMeasure {
  102.         java.lang.String _strMeasure = "";
  103.         double _dblQuote = java.lang.Double.NaN;

  104.         TranslatedQuoteMeasure (
  105.             final java.lang.String strMeasure,
  106.             final double dblQuote)
  107.         {
  108.             _dblQuote = dblQuote;
  109.             _strMeasure = strMeasure;
  110.         }
  111.     }

  112.     private static final TranslatedQuoteMeasure TranslateQuoteMeasure (
  113.         final org.drip.product.definition.CalibratableComponent comp,
  114.         final org.drip.param.valuation.ValuationParams valParams,
  115.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  116.         final org.drip.state.discount.MergedDiscountForwardCurve dc,
  117.         final org.drip.state.credit.CreditCurve cc,
  118.         final java.lang.String strMeasure,
  119.         final double dblQuote)
  120.     {
  121.         if (!(comp instanceof org.drip.product.definition.CreditDefaultSwap) ||
  122.             (!"FlatSpread".equalsIgnoreCase (strMeasure) && !"QuotedSpread".equalsIgnoreCase (strMeasure)))
  123.             return new TranslatedQuoteMeasure (strMeasure, dblQuote);

  124.         org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> mapQSMeasures =
  125.             ((org.drip.product.definition.CreditDefaultSwap) comp).valueFromQuotedSpread (valParams,
  126.                 pricerParams, org.drip.param.creator.MarketParamsBuilder.Credit (dc, cc), null,
  127.                     0.01, dblQuote);

  128.         return null == mapQSMeasures ? null : new TranslatedQuoteMeasure ("Upfront", mapQSMeasures.get
  129.             ("Upfront"));
  130.     }

  131.     /**
  132.      * Calibrate a Credit Curve
  133.      *
  134.      * @param strName Credit Curve name
  135.      * @param valParams ValuationParams
  136.      * @param aCalibInst Array of Calibration Instruments
  137.      * @param adblCalibQuote Array of component quotes
  138.      * @param astrCalibMeasure Array of the calibration measures
  139.      * @param dblRecovery Component recovery
  140.      * @param bFlat Flat Calibration (True), or real bootstrapping (false)
  141.      * @param dc Base Discount Curve
  142.      * @param gc Govvie Curve
  143.      * @param lsfc Latent State Fixings Container
  144.      * @param vcp Valuation Customization Parameters
  145.      * @param cp The Calibration Parameters
  146.      *
  147.      * @return CreditCurve Instance
  148.      */

  149.     public static final org.drip.state.credit.CreditCurve Standard (
  150.         final java.lang.String strName,
  151.         final org.drip.param.valuation.ValuationParams valParams,
  152.         final org.drip.product.definition.CalibratableComponent[] aCalibInst,
  153.         final double[] adblCalibQuote,
  154.         final java.lang.String[] astrCalibMeasure,
  155.         final double dblRecovery,
  156.         final boolean bFlat,
  157.         final org.drip.state.discount.MergedDiscountForwardCurve dc,
  158.         final org.drip.state.govvie.GovvieCurve gc,
  159.         final org.drip.param.market.LatentStateFixingsContainer lsfc,
  160.         final org.drip.param.valuation.ValuationCustomizationParams vcp,
  161.         final org.drip.param.definition.CalibrationParams cp)
  162.     {
  163.         if (null == valParams || null == aCalibInst || null == adblCalibQuote || null == astrCalibMeasure ||
  164.             null == dc)
  165.             return null;

  166.         int iNumComp = aCalibInst.length;
  167.         int aiDate[] = new int[iNumComp];
  168.         double adblHazardRate[] = new double[iNumComp];

  169.         if (0 == iNumComp || adblCalibQuote.length != iNumComp || astrCalibMeasure.length != iNumComp)
  170.             return null;

  171.         for (int i = 0; i < iNumComp; ++i) {
  172.             if (null == aCalibInst[i]) return null;

  173.             adblHazardRate[i] = java.lang.Double.NaN;

  174.             aiDate[i] = aCalibInst[i].maturityDate().julian();
  175.         }

  176.         org.drip.state.credit.ExplicitBootCreditCurve ebcc =
  177.             org.drip.state.creator.ScenarioCreditCurveBuilder.Hazard (new org.drip.analytics.date.JulianDate
  178.                 (valParams.valueDate()), strName, dc.currency(), aiDate, adblHazardRate, dblRecovery);

  179.         org.drip.param.pricer.CreditPricerParams pricerParams = new org.drip.param.pricer.CreditPricerParams
  180.             (7, null, false, org.drip.param.pricer.CreditPricerParams.PERIOD_DISCRETIZATION_DAY_STEP);

  181.         for (int i = 0; i < iNumComp; ++i) {
  182.             TranslatedQuoteMeasure tqm = TranslateQuoteMeasure (aCalibInst[i], valParams, pricerParams, dc,
  183.                 ebcc, astrCalibMeasure[i], adblCalibQuote[i]);

  184.             if (null == tqm) return null;

  185.             if (!org.drip.state.nonlinear.NonlinearCurveBuilder.CreditCurve (valParams, aCalibInst[i],
  186.                 tqm._dblQuote, tqm._strMeasure, bFlat, i, ebcc, dc, gc, pricerParams, lsfc, vcp, cp))
  187.             {
  188.                 return null;
  189.             }
  190.         }

  191.         ebcc.setInstrCalibInputs (valParams, bFlat, dc, gc, pricerParams, aCalibInst, adblCalibQuote,
  192.             astrCalibMeasure, lsfc, vcp);

  193.         return ebcc;
  194.     }

  195.     /**
  196.      * Create an array of tenor bumped credit curves
  197.      *
  198.      * @param strName Credit Curve Name
  199.      * @param valParams ValuationParams
  200.      * @param aCalibInst Array of Calibration Instruments
  201.      * @param adblCalibQuote Array of component quotes
  202.      * @param astrCalibMeasure Array of the calibration measures
  203.      * @param dblRecovery Component recovery
  204.      * @param bFlat Flat Calibration (True), or real bootstrapping (false)
  205.      * @param dblBump Amount of bump applied to the tenor
  206.      * @param dc Base Discount Curve
  207.      * @param gc Govvie Curve
  208.      * @param lsfc Latent State Fixings Container
  209.      * @param vcp Valuation Customization Parameters
  210.      *
  211.      * @return Array of CreditCurves
  212.      */

  213.     public static final org.drip.state.credit.CreditCurve[] Tenor (
  214.         final java.lang.String strName,
  215.         final org.drip.param.valuation.ValuationParams valParams,
  216.         final org.drip.product.definition.CalibratableComponent[] aCalibInst,
  217.         final double[] adblCalibQuote,
  218.         final java.lang.String[] astrCalibMeasure,
  219.         final double dblRecovery,
  220.         final boolean bFlat,
  221.         final double dblBump,
  222.         final org.drip.state.discount.MergedDiscountForwardCurve dc,
  223.         final org.drip.state.govvie.GovvieCurve gc,
  224.         final org.drip.param.market.LatentStateFixingsContainer lsfc,
  225.         final org.drip.param.valuation.ValuationCustomizationParams vcp)
  226.     {
  227.         if (null == valParams || null == aCalibInst || null == adblCalibQuote || null == astrCalibMeasure ||
  228.             null == dc)
  229.             return null;

  230.         int iNumComp = aCalibInst.length;
  231.         org.drip.state.credit.CreditCurve[] aCreditCurve = new org.drip.state.credit.CreditCurve[iNumComp];

  232.         if (0 == iNumComp || adblCalibQuote.length != iNumComp || astrCalibMeasure.length != iNumComp)
  233.             return null;

  234.         for (int i = 0; i < iNumComp; ++i) {
  235.             double[] adblTenorQuote = new double [iNumComp];

  236.             for (int j = 0; j < iNumComp; ++j)
  237.                 adblTenorQuote[j] += (j == i ? dblBump : 0.);

  238.             if (null == (aCreditCurve[i] = Standard (strName, valParams, aCalibInst, adblTenorQuote,
  239.                 astrCalibMeasure, dblRecovery, bFlat, dc, gc, lsfc, vcp, null)))
  240.                 return null;
  241.         }

  242.         return aCreditCurve;
  243.     }

  244.     /**
  245.      * Create an tenor named map of tenor bumped credit curves
  246.      *
  247.      * @param strName Credit Curve name
  248.      * @param valParams ValuationParams
  249.      * @param aCalibInst Array of Calibration Instruments
  250.      * @param adblCalibQuote Array of component quotes
  251.      * @param astrCalibMeasure Array of the calibration measures
  252.      * @param dblRecovery Component recovery
  253.      * @param bFlat Flat Calibration (True), or real bootstrapping (false)
  254.      * @param dblBump Amount of bump applied to the tenor
  255.      * @param dc Base Discount Curve
  256.      * @param gc Govvie Curve
  257.      * @param lsfc Latent State Fixings Container
  258.      * @param vcp Valuation Customization Parameters
  259.      *
  260.      * @return Tenor named map of tenor bumped credit curves
  261.      */

  262.     public static final org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.credit.CreditCurve>
  263.         TenorMap (
  264.             final java.lang.String strName,
  265.             final org.drip.param.valuation.ValuationParams valParams,
  266.             final org.drip.product.definition.CalibratableComponent[] aCalibInst,
  267.             final double[] adblCalibQuote,
  268.             final java.lang.String[] astrCalibMeasure,
  269.             final double dblRecovery,
  270.             final boolean bFlat,
  271.             final double dblBump,
  272.             final org.drip.state.discount.MergedDiscountForwardCurve dc,
  273.             final org.drip.state.govvie.GovvieCurve gc,
  274.             final org.drip.param.market.LatentStateFixingsContainer lsfc,
  275.             final org.drip.param.valuation.ValuationCustomizationParams vcp)
  276.     {
  277.         if (null == valParams || null == aCalibInst || null == adblCalibQuote || null == astrCalibMeasure ||
  278.             null == dc)
  279.             return null;

  280.         int iNumComp = aCalibInst.length;

  281.         if (0 == iNumComp || adblCalibQuote.length != iNumComp || astrCalibMeasure.length != iNumComp)
  282.             return null;

  283.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.credit.CreditCurve>
  284.             mapTenorCreditCurve = new
  285.                 org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.credit.CreditCurve>();

  286.         for (int i = 0; i < iNumComp; ++i) {
  287.             org.drip.state.credit.CreditCurve cc = null;
  288.             double[] adblTenorQuote = new double[iNumComp];

  289.             for (int j = 0; j < iNumComp; ++j)
  290.                 adblTenorQuote[j] = adblCalibQuote[j] + (j == i ? dblBump : 0.);

  291.             if (null == (cc = Standard (strName, valParams, aCalibInst, adblTenorQuote, astrCalibMeasure,
  292.                 dblRecovery, bFlat, dc, gc, lsfc, vcp, null)))
  293.                 return null;

  294.             mapTenorCreditCurve.put (org.drip.analytics.date.DateUtil.YYYYMMDD
  295.                 (aCalibInst[i].maturityDate().julian()), cc);
  296.         }

  297.         return mapTenorCreditCurve;
  298.     }
  299. }