CDSValuationMetrics.java

  1. package org.drip.sample.credit;

  2. import java.util.Map;

  3. import org.drip.analytics.date.*;
  4. import org.drip.market.otc.*;
  5. import org.drip.param.creator.MarketParamsBuilder;
  6. import org.drip.param.market.CurveSurfaceQuoteContainer;
  7. import org.drip.param.pricer.CreditPricerParams;
  8. import org.drip.param.valuation.ValuationParams;
  9. import org.drip.product.creator.*;
  10. import org.drip.product.definition.*;
  11. import org.drip.product.rates.FixFloatComponent;
  12. import org.drip.service.env.EnvManager;
  13. import org.drip.state.creator.*;
  14. import org.drip.state.credit.CreditCurve;
  15. import org.drip.state.discount.MergedDiscountForwardCurve;
  16. import org.drip.state.identifier.ForwardLabel;

  17. /*
  18.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  19.  */

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

  92. /**
  93.  * <i>CDSValuationMetrics</i> contains the Demonstration of Valuing a Payer/Receiver CDS European Option
  94.  * Sample.
  95.  *  
  96.  * <br><br>
  97.  *  <ul>
  98.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  99.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  100.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  101.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/cashflow/README.md">Single Name Portfolio CDS Analytics</a></li>
  102.  *  </ul>
  103.  * <br><br>
  104.  *
  105.  * @author Lakshmi Krishnamurthy
  106.  */

  107. public class CDSValuationMetrics {

  108.     private static final FixFloatComponent OTCFixFloat (
  109.         final JulianDate dtSpot,
  110.         final String strCurrency,
  111.         final String strMaturityTenor,
  112.         final double dblCoupon)
  113.     {
  114.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  115.             strCurrency,
  116.             "ALL",
  117.             strMaturityTenor,
  118.             "MAIN"
  119.         );

  120.         return ffConv.createFixFloatComponent (
  121.             dtSpot,
  122.             strMaturityTenor,
  123.             dblCoupon,
  124.             0.,
  125.             1.
  126.         );
  127.     }

  128.     /*
  129.      * Construct the Array of Deposit Instruments from the given set of parameters
  130.      *
  131.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  132.      */

  133.     private static final CalibratableComponent[] DepositInstrumentsFromMaturityDays (
  134.         final JulianDate dtEffective,
  135.         final int[] aiDay,
  136.         final int iNumFutures,
  137.         final String strCurrency)
  138.         throws Exception
  139.     {
  140.         CalibratableComponent[] aCalibComp = new CalibratableComponent[aiDay.length + iNumFutures];

  141.         for (int i = 0; i < aiDay.length; ++i)
  142.             aCalibComp[i] = SingleStreamComponentBuilder.Deposit (
  143.                 dtEffective,
  144.                 dtEffective.addBusDays (
  145.                     aiDay[i],
  146.                     strCurrency
  147.                 ),
  148.                 ForwardLabel.Create (
  149.                     strCurrency,
  150.                     "3M"
  151.                 )
  152.             );

  153.         CalibratableComponent[] aEDF = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  154.             dtEffective,
  155.             iNumFutures,
  156.             strCurrency
  157.         );

  158.         for (int i = aiDay.length; i < aiDay.length + iNumFutures; ++i)
  159.             aCalibComp[i] = aEDF[i - aiDay.length];

  160.         return aCalibComp;
  161.     }

  162.     /*
  163.      * Construct the Array of Swap Instruments from the given set of parameters
  164.      *
  165.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  166.      */

  167.     private static final CalibratableComponent[] SwapInstrumentsFromMaturityTenor (
  168.         final JulianDate dtSpot,
  169.         final String strCurrency,
  170.         final String[] astrMaturityTenor,
  171.         final double[] adblCoupon)
  172.         throws Exception
  173.     {
  174.         FixFloatComponent[] aIRS = new FixFloatComponent[astrMaturityTenor.length];

  175.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  176.             aIRS[i] = OTCFixFloat (
  177.                 dtSpot,
  178.                 strCurrency,
  179.                 astrMaturityTenor[i],
  180.                 adblCoupon[i]
  181.             );

  182.         return aIRS;
  183.     }

  184.     /*
  185.      * Construct the discount curve using the following steps:
  186.      *  - Construct the array of cash instruments and their quotes.
  187.      *  - Construct the array of swap instruments and their quotes.
  188.      *  - Construct a shape preserving and smoothing KLK Hyperbolic Spline from the cash/swap instruments.
  189.      *
  190.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  191.      */

  192.     private static final MergedDiscountForwardCurve MakeDC (
  193.         final JulianDate dtSpot,
  194.         final String strCurrency)
  195.         throws Exception
  196.     {
  197.         /*
  198.          * Construct the array of Deposit instruments and their quotes.
  199.          */

  200.         CalibratableComponent[] aDepositComp = DepositInstrumentsFromMaturityDays (
  201.             dtSpot,
  202.             new int[] {
  203.                 1, 2, 3, 7, 14, 21, 30, 60
  204.             },
  205.             0,
  206.             strCurrency
  207.         );

  208.         double[] adblDepositQuote = new double[] {
  209.             0.01200, 0.01200, 0.01200, 0.01450, 0.01550, 0.01600, 0.01660, 0.01850
  210.         };

  211.         String[] astrDepositManifestMeasure = new String[] {
  212.             "ForwardRate",
  213.             "ForwardRate",
  214.             "ForwardRate",
  215.             "ForwardRate",
  216.             "ForwardRate",
  217.             "ForwardRate",
  218.             "ForwardRate",
  219.             "ForwardRate"
  220.         };

  221.         /*
  222.          * Construct the array of Swap instruments and their quotes.
  223.          */

  224.         double[] adblSwapQuote = new double[] {
  225.             0.02604,    //  4Y
  226.             0.02808,    //  5Y
  227.             0.02983,    //  6Y
  228.             0.03136,    //  7Y
  229.             0.03268,    //  8Y
  230.             0.03383,    //  9Y
  231.             0.03488,    // 10Y
  232.             0.03583,    // 11Y
  233.             0.03668,    // 12Y
  234.             0.03833,    // 15Y
  235.             0.03854,    // 20Y
  236.             0.03672,    // 25Y
  237.             0.03510,    // 30Y
  238.             0.03266,    // 40Y
  239.             0.03145     // 50Y
  240.         };

  241.         String[] astrSwapManifestMeasure = new String[] {
  242.             "SwapRate",    //  4Y
  243.             "SwapRate",    //  5Y
  244.             "SwapRate",    //  6Y
  245.             "SwapRate",    //  7Y
  246.             "SwapRate",    //  8Y
  247.             "SwapRate",    //  9Y
  248.             "SwapRate",    // 10Y
  249.             "SwapRate",    // 11Y
  250.             "SwapRate",    // 12Y
  251.             "SwapRate",    // 15Y
  252.             "SwapRate",    // 20Y
  253.             "SwapRate",    // 25Y
  254.             "SwapRate",    // 30Y
  255.             "SwapRate",    // 40Y
  256.             "SwapRate"     // 50Y
  257.         };

  258.         CalibratableComponent[] aSwapComp = SwapInstrumentsFromMaturityTenor (
  259.             dtSpot,
  260.             strCurrency,
  261.             new java.lang.String[] {
  262.                 "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y", "40Y", "50Y"
  263.             },
  264.             adblSwapQuote
  265.         );

  266.         /*
  267.          * Construct a shape preserving and smoothing KLK Hyperbolic Spline from the cash/swap instruments.
  268.          */

  269.         return ScenarioDiscountCurveBuilder.CubicKLKHyperbolicDFRateShapePreserver (
  270.             "KLK_HYPERBOLIC_SHAPE_TEMPLATE",
  271.             new ValuationParams (
  272.                 dtSpot,
  273.                 dtSpot,
  274.                 "USD"
  275.             ),
  276.             aDepositComp,
  277.             adblDepositQuote,
  278.             astrDepositManifestMeasure,
  279.             aSwapComp,
  280.             adblSwapQuote,
  281.             astrSwapManifestMeasure,
  282.             false
  283.         );
  284.     }

  285.     /*
  286.      * Sample API demonstrating the creation of the Credit Curve from the CDS instruments
  287.      *
  288.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  289.      */

  290.     private static CreditCurve MakeCC (
  291.         final JulianDate dtSpot,
  292.         final String strCreditCurve,
  293.         final MergedDiscountForwardCurve dcFunding)
  294.         throws Exception
  295.     {
  296.         /*
  297.          * Populate the instruments, the calibration measures, and the calibration quotes
  298.          */

  299.         double[] adblQuotes = new double[5];
  300.         String[] astrCalibMeasure = new String[5];
  301.         CreditDefaultSwap[] aCDS = new CreditDefaultSwap[5];

  302.         for (int i = 0; i < 5; ++i) {

  303.             /*
  304.              * The Calibration CDS
  305.              */

  306.             aCDS[i] = CDSBuilder.CreateSNAC (
  307.                 dtSpot,
  308.                 (i + 1) + "Y",
  309.                 0.01,
  310.                 strCreditCurve
  311.             );

  312.             /*
  313.              * Calibration Quote
  314.              */

  315.             adblQuotes[i] = 100.;

  316.             /*
  317.              * Calibration Measure
  318.              */

  319.             astrCalibMeasure[i] = "FairPremium";
  320.         }

  321.         /*
  322.          * Create the Credit Curve from the give CDS instruments
  323.          */

  324.         CreditCurve cc = ScenarioCreditCurveBuilder.Custom (
  325.             strCreditCurve,
  326.             dtSpot,
  327.             aCDS,
  328.             dcFunding,
  329.             adblQuotes,
  330.             astrCalibMeasure,
  331.             0.4,
  332.             false
  333.         );

  334.         /*
  335.          * Valuation Parameters
  336.          */

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

  338.         /*
  339.          * Standard Credit Pricer Parameters (check javadoc for details)
  340.          */

  341.         CreditPricerParams pricerParams = CreditPricerParams.Standard();

  342.         /*
  343.          * Re-calculate the input calibration measures for the input CDSes
  344.          */

  345.         for (int i = 0; i < aCDS.length; ++i)
  346.             System.out.println (
  347.                 "\t" + astrCalibMeasure[i] + "[" + i + "] = " +
  348.                 aCDS[i].measureValue (
  349.                     valParams, pricerParams, MarketParamsBuilder.Create (
  350.                         dcFunding,
  351.                         null,
  352.                         null,
  353.                         cc,
  354.                         null,
  355.                         null,
  356.                         null,
  357.                         null
  358.                     ),
  359.                     null,
  360.                     astrCalibMeasure[i]
  361.                 )
  362.             );

  363.         return cc;
  364.     }

  365.     /*
  366.      * Sample API demonstrating the display of the CDS coupon and loss cash flow
  367.      *
  368.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  369.      */

  370.     public static final void main (
  371.         final String[] astrArgs)
  372.         throws Exception
  373.     {
  374.         EnvManager.InitEnv ("");

  375.         JulianDate dtSpot = DateUtil.Today();

  376.         String strCurrency = "USD";
  377.         String strCreditCurve = "DB";
  378.         String strCDSForwardStartTenor = "3M";
  379.         String strCDSMaturityTenor = "5Y";
  380.         double dblCDSCoupon = 0.1;

  381.         MergedDiscountForwardCurve dcFunding = MakeDC (
  382.             dtSpot,
  383.             strCurrency
  384.         );

  385.         CreditCurve cc = MakeCC (
  386.             dtSpot,
  387.             strCreditCurve,
  388.             dcFunding
  389.         );

  390.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Credit (
  391.             dcFunding,
  392.             cc
  393.         );

  394.         CreditDefaultSwap cdsForward = CDSBuilder.CreateSNAC (
  395.             dtSpot.addTenor (strCDSForwardStartTenor),
  396.             strCDSMaturityTenor,
  397.             dblCDSCoupon,
  398.             strCreditCurve
  399.         );

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

  401.         CreditPricerParams pricerParams = CreditPricerParams.Standard();

  402.         Map<String, Double> mapCDSForward = cdsForward.value (
  403.             valParams,
  404.             pricerParams,
  405.             csqc,
  406.             null
  407.         );

  408.         for (Map.Entry<String, Double> me : mapCDSForward.entrySet())
  409.             System.out.println (me.getKey() + " => " + me.getValue());

  410.         EnvManager.TerminateEnv();
  411.     }
  412. }