SurvivalRecoveryStateShifted.java

  1. package org.drip.template.statebump;

  2. import java.util.Map;

  3. import org.drip.analytics.date.*;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.param.market.CurveSurfaceQuoteContainer;
  6. import org.drip.param.valuation.ValuationParams;
  7. import org.drip.product.definition.Component;
  8. import org.drip.service.env.EnvManager;
  9. import org.drip.service.template.*;
  10. import org.drip.state.credit.CreditCurve;
  11. import org.drip.state.discount.MergedDiscountForwardCurve;

  12. /*
  13.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  14.  */

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

  88. /**
  89.  * <i>SurvivalRecoveryStateShifted</i> demonstrates the Generation of the Tenor Bumped Credit Curves.
  90.  *
  91.  *  <br><br>
  92.  *  <ul>
  93.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  94.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  95.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/template/README.md">Pricing/Risk Templates for Fixed Income Component Products</a></li>
  96.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/template/statebump/README.md">Shifted Latent State Construction Template</a></li>
  97.  *  </ul>
  98.  * <br><br>
  99.  *
  100.  * @author Lakshmi Krishnamurthy
  101.  */

  102. public class SurvivalRecoveryStateShifted {

  103.     private static final MergedDiscountForwardCurve OvernightCurve (
  104.         final JulianDate dtSpot,
  105.         final String strCurrency)
  106.         throws Exception
  107.     {
  108.         String[] astrDepositMaturityTenor = new String[] {
  109.             "1D",
  110.             // "2D",
  111.             "3D"
  112.         };

  113.         double[] adblDepositQuote = new double[] {
  114.             0.0004,     // 1D
  115.             // 0.0004,      // 2D
  116.             0.0004      // 3D
  117.         };

  118.         String[] astrShortEndOISMaturityTenor = new String[] {
  119.             "1W",
  120.             "2W",
  121.             "3W",
  122.             "1M"
  123.         };

  124.         double[] adblShortEndOISQuote = new double[] {
  125.             0.00070,    //   1W
  126.             0.00069,    //   2W
  127.             0.00078,    //   3W
  128.             0.00074     //   1M
  129.         };

  130.         String[] astrOISFuturesEffectiveTenor = new String[] {
  131.             "1M",
  132.             "2M",
  133.             "3M",
  134.             "4M",
  135.             "5M"
  136.         };

  137.         String[] astrOISFuturesMaturityTenor = new String[] {
  138.             "1M",
  139.             "1M",
  140.             "1M",
  141.             "1M",
  142.             "1M"
  143.         };

  144.         double[] adblOISFuturesQuote = new double[] {
  145.              0.00046,    //   1M x 1M
  146.              0.00016,    //   2M x 1M
  147.             -0.00007,    //   3M x 1M
  148.             -0.00013,    //   4M x 1M
  149.             -0.00014     //   5M x 1M
  150.         };

  151.         String[] astrLongEndOISMaturityTenor = new String[] {
  152.             "15M",
  153.             "18M",
  154.             "21M",
  155.             "02Y",
  156.             "03Y",
  157.             "04Y",
  158.             "05Y",
  159.             "06Y",
  160.             "07Y",
  161.             "08Y",
  162.             "09Y",
  163.             "10Y",
  164.             "11Y",
  165.             "12Y",
  166.             "15Y",
  167.             "20Y",
  168.             "25Y",
  169.             "30Y"
  170.         };

  171.         double[] adblLongEndOISQuote = new double[] {
  172.             0.00002,    //  15M
  173.             0.00008,    //  18M
  174.             0.00021,    //  21M
  175.             0.00036,    //   2Y
  176.             0.00127,    //   3Y
  177.             0.00274,    //   4Y
  178.             0.00456,    //   5Y
  179.             0.00647,    //   6Y
  180.             0.00827,    //   7Y
  181.             0.00996,    //   8Y
  182.             0.01147,    //   9Y
  183.             0.01280,    //  10Y
  184.             0.01404,    //  11Y
  185.             0.01516,    //  12Y
  186.             0.01764,    //  15Y
  187.             0.01939,    //  20Y
  188.             0.02003,    //  25Y
  189.             0.02038     //  30Y
  190.         };

  191.         return LatentMarketStateBuilder.SmoothOvernightCurve (
  192.             dtSpot,
  193.             strCurrency,
  194.             astrDepositMaturityTenor,
  195.             adblDepositQuote,
  196.             "Rate",
  197.             astrShortEndOISMaturityTenor,
  198.             adblShortEndOISQuote,
  199.             "SwapRate",
  200.             astrOISFuturesEffectiveTenor,
  201.             astrOISFuturesMaturityTenor,
  202.             adblOISFuturesQuote,
  203.             "SwapRate",
  204.             astrLongEndOISMaturityTenor,
  205.             adblLongEndOISQuote,
  206.             "SwapRate"
  207.         );
  208.     }

  209.     public static final void main (
  210.         final String[] astrArgs)
  211.         throws Exception
  212.     {
  213.         /*
  214.          * Initialize the Credit Analytics Library
  215.          */

  216.         EnvManager.InitEnv ("");

  217.         String strCurrency = "EUR";
  218.         double dblBump = 1.0;
  219.         boolean bIsBumpProportional = false;

  220.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  221.             2017,
  222.             DateUtil.DECEMBER,
  223.             21
  224.         );

  225.         MergedDiscountForwardCurve dcOvernight = OvernightCurve (
  226.             dtSpot,
  227.             strCurrency
  228.         );

  229.         String[] astrCDSMaturityTenor = new String[] {
  230.             "06M",
  231.             "01Y",
  232.             "02Y",
  233.             "03Y",
  234.             "04Y",
  235.             "05Y",
  236.             "07Y",
  237.             "10Y"
  238.         };

  239.         double[] adblCDSParSpread = new double[] {
  240.              60.,   //  6M
  241.              68.,   //  1Y
  242.              88.,   //  2Y
  243.             102.,   //  3Y
  244.             121.,   //  4Y
  245.             138.,   //  5Y
  246.             168.,   //  7Y
  247.             188.    // 10Y
  248.         };

  249.         Map<String, CreditCurve> mapBumpedCreditCurve = LatentMarketStateBuilder.BumpedCreditCurve (
  250.             dtSpot,
  251.             "QTX",
  252.             astrCDSMaturityTenor,
  253.             adblCDSParSpread,
  254.             adblCDSParSpread,
  255.             "FairPremium",
  256.             dcOvernight,
  257.             dblBump,
  258.             bIsBumpProportional
  259.         );

  260.         Component[] aCDS = OTCInstrumentBuilder.CDS (
  261.             dtSpot,
  262.             astrCDSMaturityTenor,
  263.             adblCDSParSpread,
  264.             strCurrency,
  265.             "QTX"
  266.         );

  267.         System.out.println ("\n\t|----------------------------------------------------------------------------||");

  268.         ValuationParams valParams = ValuationParams.Spot (dtSpot.julian());

  269.         for (Map.Entry<String, CreditCurve> meCredit : mapBumpedCreditCurve.entrySet()) {
  270.             String strKey = meCredit.getKey();

  271.             if (!strKey.startsWith ("cds")) continue;

  272.             CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  273.             csqc.setFundingState (dcOvernight);

  274.             csqc.setCreditState (meCredit.getValue());

  275.             System.out.print ("\t|  [" + meCredit.getKey() + "] => ");

  276.             for (Component comp : aCDS)
  277.                 System.out.print (FormatUtil.FormatDouble (comp.measureValue (
  278.                     valParams,
  279.                     null,
  280.                     csqc,
  281.                     null,
  282.                     "FairPremium"
  283.                 ), 1, 1, 1.) + " |");

  284.             System.out.print ("|\n");
  285.         }

  286.         System.out.println ("\t|----------------------------------------------------------------------------||");

  287.         System.out.println ("\n\t|--------------------------------------------||");

  288.         CurveSurfaceQuoteContainer csqcBase = new CurveSurfaceQuoteContainer();

  289.         csqcBase.setFundingState (dcOvernight);

  290.         csqcBase.setCreditState (mapBumpedCreditCurve.get ("Base"));

  291.         CurveSurfaceQuoteContainer csqcBump = new CurveSurfaceQuoteContainer();

  292.         csqcBump.setFundingState (dcOvernight);

  293.         csqcBump.setCreditState (mapBumpedCreditCurve.get ("Bump"));

  294.         for (Component comp : aCDS)
  295.             System.out.println (
  296.                 "\t| FAIR PREMIUM => " +
  297.                 comp.maturityDate() + " |" +
  298.                 FormatUtil.FormatDouble (comp.measureValue (
  299.                     valParams,
  300.                     null,
  301.                     csqcBase,
  302.                     null,
  303.                     "FairPremium"
  304.                 ), 3, 1, 1.) + " |" +
  305.                 FormatUtil.FormatDouble (comp.measureValue (
  306.                     valParams,
  307.                     null,
  308.                     csqcBump,
  309.                     null,
  310.                     "FairPremium"
  311.                 ), 3, 1, 1.) + " ||"
  312.             );

  313.         System.out.println ("\t|--------------------------------------------||");
  314.     }
  315. }