CreditCurveScenarioContainer.java

  1. package org.drip.param.market;

  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>CreditCurveScenarioContainer</i> contains the place holder for the bump parameters and the curves for
  84.  * the different credit curve scenarios. Contains the spread and the recovery bumps, and the credit curve
  85.  * scenario generator object that wraps the calibration instruments. It also contains the base credit curve,
  86.  * spread bumped up/down credit curves, recovery bumped up/down credit curves, and the tenor mapped up/down
  87.  * credit curves.
  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/param/README.md">Product Cash Flow, Valuation, Market, Pricing, and Quoting Parameters</a></li>
  94.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/param/market/README.md">Curves Surfaces Quotes Fixings Container</a></li>
  95.  *  </ul>
  96.  *
  97.  * @author Lakshmi Krishnamurthy
  98.  */

  99. public class CreditCurveScenarioContainer {

  100.     /**
  101.      * CC Scenario Base
  102.      */

  103.     public static final int CC_BASE = 0;

  104.     /**
  105.      * CC Scenario Parallel Up
  106.      */

  107.     public static final int CC_FLAT_UP = 1;

  108.     /**
  109.      * CC Scenario Parallel Down
  110.      */

  111.     public static final int CC_FLAT_DN = 2;

  112.     /**
  113.      * CC Scenario Tenor Up
  114.      */

  115.     public static final int CC_TENOR_UP = 4;

  116.     /**
  117.      * CC Scenario Tenor Down
  118.      */

  119.     public static final int CC_TENOR_DN = 8;

  120.     /**
  121.      * CC Scenario Recovery Parallel Up
  122.      */

  123.     public static final int CC_RR_FLAT_UP = 16;

  124.     /**
  125.      * CC Scenario Recovery Parallel Down
  126.      */

  127.     public static final int CC_RR_FLAT_DN = 32;

  128.     private double _dblCouponBump = java.lang.Double.NaN;
  129.     private double _dblRecoveryBump = java.lang.Double.NaN;
  130.     private org.drip.state.credit.CreditCurve _ccBase = null;
  131.     private org.drip.state.credit.CreditCurve _ccBumpUp = null;
  132.     private org.drip.state.credit.CreditCurve _ccBumpDn = null;
  133.     private org.drip.state.credit.CreditCurve _ccRecoveryUp = null;
  134.     private org.drip.state.credit.CreditCurve _ccRecoveryDn = null;
  135.     private org.drip.product.definition.CalibratableComponent[] _aCalibInst = null;
  136.     private org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.credit.CreditCurve>
  137.         _mapCCCustom = null;
  138.     private org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.credit.CreditCurve>
  139.         _mapCCTenorBumpUp = null;
  140.     private org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.credit.CreditCurve>
  141.         _mapCCTenorBumpDn = null;

  142.     /**
  143.      * Construct CreditCurveScenarioContainer from the array of calibration instruments, the coupon bump
  144.      *  parameter, and the recovery bump parameter
  145.      *
  146.      * @param aCalibInst Array of calibration instruments
  147.      * @param dblCouponBump Coupon Bump
  148.      * @param dblRecoveryBump Recovery Bump
  149.      *
  150.      * @throws java.lang.Exception Thrown if inputs are invalid
  151.      */

  152.     public CreditCurveScenarioContainer (
  153.         final org.drip.product.definition.CalibratableComponent[] aCalibInst,
  154.         final double dblCouponBump,
  155.         final double dblRecoveryBump)
  156.         throws java.lang.Exception
  157.     {
  158.         if (null == (_aCalibInst = aCalibInst) || 0 == _aCalibInst.length ||
  159.             !org.drip.numerical.common.NumberUtil.IsValid (_dblCouponBump = dblCouponBump) ||
  160.                 !org.drip.numerical.common.NumberUtil.IsValid (_dblRecoveryBump = dblRecoveryBump))
  161.             throw new java.lang.Exception ("CreditCurveScenarioContainer ctr => Invalid Inputs!");
  162.     }

  163.     /**
  164.      * Cook and save the credit curves corresponding to the scenario specified
  165.      *
  166.      * @param strName Credit Curve Name
  167.      * @param valParams ValuationParams
  168.      * @param dc Base Discount Curve
  169.      * @param gc Govvie Curve
  170.      * @param astrCalibMeasure Matched array of Calibration measures
  171.      * @param adblQuote Matched array of Quotes
  172.      * @param dblRecovery Curve Recovery
  173.      * @param lsfc Latent State Fixings Container
  174.      * @param vcp Valuation Customization Parameters
  175.      * @param bFlat Whether the calibration is to a flat curve
  176.      * @param iScenario One of the values in the CC_ enum listed above.
  177.      *
  178.      * @return Success (true), failure (false)
  179.      */

  180.     public boolean cookScenarioCC (
  181.         final java.lang.String strName,
  182.         final org.drip.param.valuation.ValuationParams valParams,
  183.         final org.drip.state.discount.MergedDiscountForwardCurve dc,
  184.         final org.drip.state.govvie.GovvieCurve gc,
  185.         final java.lang.String[] astrCalibMeasure,
  186.         final double[] adblQuote,
  187.         final double dblRecovery,
  188.         final org.drip.param.market.LatentStateFixingsContainer lsfc,
  189.         final org.drip.param.valuation.ValuationCustomizationParams vcp,
  190.         final boolean bFlat,
  191.         final int iScenario)
  192.     {
  193.         if (null == (_ccBase = org.drip.state.boot.CreditCurveScenario.Standard (strName, valParams,
  194.             _aCalibInst, adblQuote, astrCalibMeasure, dblRecovery, bFlat, dc, gc, lsfc, vcp, null)))
  195.             return false;

  196.         if (0 != (org.drip.param.market.CreditCurveScenarioContainer.CC_FLAT_UP & iScenario)) {
  197.             if (null == (_ccBumpUp = org.drip.state.boot.CreditCurveScenario.Standard (strName, valParams,
  198.                 _aCalibInst, org.drip.analytics.support.Helper.BumpQuotes (adblQuote, _dblCouponBump, false),
  199.                     astrCalibMeasure, dblRecovery, bFlat, dc, gc, lsfc, vcp, null)))
  200.                 return false;
  201.         }

  202.         if (0 != (org.drip.param.market.CreditCurveScenarioContainer.CC_FLAT_DN & iScenario)) {
  203.             if (null == (_ccBumpDn = org.drip.state.boot.CreditCurveScenario.Standard (strName, valParams,
  204.                 _aCalibInst, org.drip.analytics.support.Helper.BumpQuotes (adblQuote, -_dblCouponBump,
  205.                     false), astrCalibMeasure, dblRecovery, bFlat, dc, gc, lsfc, vcp, null)))
  206.                 return false;
  207.         }

  208.         if (0 != (org.drip.param.market.CreditCurveScenarioContainer.CC_TENOR_UP & iScenario)) {
  209.             if (null == (_mapCCTenorBumpUp = org.drip.state.boot.CreditCurveScenario.TenorMap (strName,
  210.                 valParams, _aCalibInst, adblQuote, astrCalibMeasure, dblRecovery, bFlat, _dblCouponBump, dc,
  211.                     gc, lsfc, vcp)))
  212.                 return false;
  213.         }

  214.         if (0 != (org.drip.param.market.CreditCurveScenarioContainer.CC_TENOR_DN & iScenario)) {
  215.             if (null == (_mapCCTenorBumpDn = org.drip.state.boot.CreditCurveScenario.TenorMap (strName,
  216.                 valParams, _aCalibInst, adblQuote, astrCalibMeasure, dblRecovery, bFlat, -_dblCouponBump, dc,
  217.                     gc, lsfc, vcp)))
  218.                 return false;
  219.         }

  220.         if (0 != (org.drip.param.market.CreditCurveScenarioContainer.CC_RR_FLAT_UP & iScenario)) {
  221.             if (null == (_ccRecoveryUp = org.drip.state.boot.CreditCurveScenario.Standard (strName,
  222.                 valParams, _aCalibInst, adblQuote, astrCalibMeasure, dblRecovery + _dblRecoveryBump, bFlat,
  223.                     dc, gc, lsfc, vcp, null)))
  224.                 return false;
  225.         }

  226.         if (0 != (org.drip.param.market.CreditCurveScenarioContainer.CC_RR_FLAT_DN & iScenario)) {
  227.             if (null == (_ccRecoveryDn = org.drip.state.boot.CreditCurveScenario.Standard (strName,
  228.                 valParams, _aCalibInst, adblQuote, astrCalibMeasure, dblRecovery - _dblRecoveryBump, bFlat,
  229.                     dc, gc, lsfc, vcp, null)))
  230.                 return false;
  231.         }

  232.         return true;
  233.     }

  234.     /**
  235.      * Cook the credit curve according to the desired tweak parameters
  236.      *
  237.      * @param strName Scenario Credit Curve Name
  238.      * @param strCustomName Scenario Name
  239.      * @param valParams Valuation Parameters
  240.      * @param dc Discount Curve
  241.      * @param gc Govvie Curve
  242.      * @param astrCalibMeasure Array of calibration measures
  243.      * @param adblQuote Double array of input quotes
  244.      * @param dblRecovery Recovery Rate
  245.      * @param lsfc Latent State Fixings Container
  246.      * @param vcp Valuation Customization Parameters
  247.      * @param bFlat Whether the calibration is flat
  248.      * @param rvtpDC Node Tweak Parameters for the Base Discount Curve
  249.      * @param rvtpTSY Node Tweak Parameters for the TSY Discount Curve
  250.      * @param rvtpCC Node Tweak Parameters for the Credit Curve
  251.      *
  252.      * @return True - Credit Curve successfully created
  253.      */

  254.     public boolean cookCustomCC (
  255.         final java.lang.String strName,
  256.         final java.lang.String strCustomName,
  257.         final org.drip.param.valuation.ValuationParams valParams,
  258.         final org.drip.state.discount.MergedDiscountForwardCurve dc,
  259.         final org.drip.state.govvie.GovvieCurve gc,
  260.         final java.lang.String[] astrCalibMeasure,
  261.         final double[] adblQuote,
  262.         final double dblRecovery,
  263.         final org.drip.param.market.LatentStateFixingsContainer lsfc,
  264.         final org.drip.param.valuation.ValuationCustomizationParams vcp,
  265.         final boolean bFlat,
  266.         final org.drip.param.definition.ManifestMeasureTweak rvtpDC,
  267.         final org.drip.param.definition.ManifestMeasureTweak rvtpTSY,
  268.         final org.drip.param.definition.ManifestMeasureTweak rvtpCC)
  269.     {
  270.         if (null == dc) return false;

  271.         org.drip.state.discount.MergedDiscountForwardCurve dcAdj = (org.drip.state.discount.MergedDiscountForwardCurve)
  272.             dc.customTweakManifestMeasure ("Rate", rvtpDC);

  273.         org.drip.state.govvie.GovvieCurve gcAdj = (org.drip.state.govvie.GovvieCurve)
  274.             gc.customTweakManifestMeasure ("Rate", rvtpTSY);

  275.         org.drip.state.credit.CreditCurve ccBaseCustom = org.drip.state.boot.CreditCurveScenario.Standard
  276.             (strName, valParams, _aCalibInst, adblQuote, astrCalibMeasure, dblRecovery, bFlat, null == dcAdj
  277.                 ? dc : dcAdj, null == gcAdj ? gc : gcAdj, lsfc, vcp, null);

  278.         if (null == ccBaseCustom) return false;

  279.         if (null == _mapCCCustom)
  280.             _mapCCCustom = new
  281.                 org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.credit.CreditCurve>();

  282.         org.drip.state.credit.CreditCurve ccCustom = (org.drip.state.credit.CreditCurve)
  283.             ccBaseCustom.customTweakManifestMeasure ("Rate", rvtpCC);

  284.         if (null == ccCustom)
  285.             _mapCCCustom.put (strCustomName, ccBaseCustom);
  286.         else
  287.             _mapCCCustom.put (strCustomName, ccCustom);

  288.         return true;
  289.     }

  290.     /**
  291.      * Return the base credit curve
  292.      *
  293.      * @return The base credit curve
  294.      */

  295.     public org.drip.state.credit.CreditCurve base()
  296.     {
  297.         return _ccBase;
  298.     }

  299.     /**
  300.      * Return the bump up credit curve
  301.      *
  302.      * @return The Bumped up credit curve
  303.      */

  304.     public org.drip.state.credit.CreditCurve bumpUp()
  305.     {
  306.         return _ccBumpUp;
  307.     }

  308.     /**
  309.      * Return the bump Down credit curve
  310.      *
  311.      * @return The Bumped Down credit curve
  312.      */

  313.     public org.drip.state.credit.CreditCurve bumpDown()
  314.     {
  315.         return _ccBumpDn;
  316.     }

  317.     /**
  318.      * Return the recovery bump up credit curve
  319.      *
  320.      * @return The Recovery Bumped up credit curve
  321.      */

  322.     public org.drip.state.credit.CreditCurve bumpRecoveryUp()
  323.     {
  324.         return _ccRecoveryUp;
  325.     }

  326.     /**
  327.      * Return the recovery bump Down credit curve
  328.      *
  329.      * @return The Recovery Bumped Down credit curve
  330.      */

  331.     public org.drip.state.credit.CreditCurve bumpRecoveryDown()
  332.     {
  333.         return _ccRecoveryDn;
  334.     }

  335.     /**
  336.      * Return the tenor bump up credit curve map
  337.      *
  338.      * @return The Tenor Bumped up credit curve Map
  339.      */

  340.     public org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.credit.CreditCurve> tenorBumpUp()
  341.     {
  342.         return _mapCCTenorBumpUp;
  343.     }

  344.     /**
  345.      * Return the tenor bump Down credit curve map
  346.      *
  347.      * @return The Tenor Bumped Down credit curve Map
  348.      */

  349.     public org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.credit.CreditCurve>
  350.         tenorBumpDown()
  351.     {
  352.         return _mapCCTenorBumpDn;
  353.     }

  354.     /**
  355.      * Return the Custom credit curve map
  356.      *
  357.      * @return The Custom credit curve Map
  358.      */

  359.     public org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.credit.CreditCurve> custom()
  360.     {
  361.         return _mapCCCustom;
  362.     }
  363. }