CDSPayerReceiverAnalysis.java

  1. package org.drip.sample.creditoption;

  2. import java.util.Map;

  3. import org.drip.analytics.date.*;
  4. import org.drip.market.otc.*;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.param.creator.MarketParamsBuilder;
  7. import org.drip.param.market.CurveSurfaceQuoteContainer;
  8. import org.drip.param.pricer.CreditPricerParams;
  9. import org.drip.param.valuation.ValuationParams;
  10. import org.drip.pricer.option.BlackScholesAlgorithm;
  11. import org.drip.product.creator.*;
  12. import org.drip.product.definition.*;
  13. import org.drip.product.option.CDSEuropeanOption;
  14. import org.drip.product.params.LastTradingDateSetting;
  15. import org.drip.product.rates.FixFloatComponent;
  16. import org.drip.service.env.EnvManager;
  17. import org.drip.state.creator.*;
  18. import org.drip.state.credit.CreditCurve;
  19. import org.drip.state.discount.MergedDiscountForwardCurve;
  20. import org.drip.state.identifier.*;

  21. /*
  22.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  23.  */

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

  96. /**
  97.  * <i>CDSPayerReceiverAnalysis</i> carries out a Volatility Analysis of Payer/Receiver CDS European Option.
  98.  *  
  99.  * <br><br>
  100.  *  <ul>
  101.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  102.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  103.  *      <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>
  104.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/creditoption/README.md">CDS Single Name Index Option</a></li>
  105.  *  </ul>
  106.  * <br><br>
  107.  *
  108.  * @author Lakshmi Krishnamurthy
  109.  */

  110. public class CDSPayerReceiverAnalysis {

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

  123.         return ffConv.createFixFloatComponent (
  124.             dtSpot,
  125.             strMaturityTenor,
  126.             dblCoupon,
  127.             0.,
  128.             1.
  129.         );
  130.     }

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

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

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

  156.         CalibratableComponent[] aEDF = SingleStreamComponentBuilder.ForwardRateFuturesPack (
  157.             dtEffective,
  158.             iNumFutures,
  159.             strCurrency
  160.         );

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

  163.         return aCalibComp;
  164.     }

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

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

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

  185.         return aIRS;
  186.     }

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

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

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

  211.         double[] adblDepositQuote = new double[] {
  212.             0.01200, 0.01200, 0.01200, 0.01450, 0.01550, 0.01600, 0.01660, 0.01850
  213.         };

  214.         String[] astrDepositManifestMeasure = new String[] {
  215.             "ForwardRate",
  216.             "ForwardRate",
  217.             "ForwardRate",
  218.             "ForwardRate",
  219.             "ForwardRate",
  220.             "ForwardRate",
  221.             "ForwardRate",
  222.             "ForwardRate"
  223.         };

  224.         /*
  225.          * Construct the array of Swap instruments and their quotes.
  226.          */

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

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

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

  269.         /*
  270.          * Construct a shape preserving and smoothing KLK Hyperbolic Spline from the cash/swap instruments.
  271.          */

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

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

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

  302.         double[] adblQuotes = new double[5];
  303.         String[] astrCalibMeasure = new String[5];
  304.         CreditDefaultSwap[] aCDS = new CreditDefaultSwap[5];

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

  306.             /*
  307.              * The Calibration CDS
  308.              */

  309.             aCDS[i] = CDSBuilder.CreateSNAC (
  310.                 dtSpot,
  311.                 (i + 1) + "Y",
  312.                 0.01,
  313.                 strCreditCurve
  314.             );

  315.             /*
  316.              * Calibration Quote
  317.              */

  318.             adblQuotes[i] = 100.;

  319.             /*
  320.              * Calibration Measure
  321.              */

  322.             astrCalibMeasure[i] = "FairPremium";
  323.         }

  324.         /*
  325.          * Create the Credit Curve from the give CDS instruments
  326.          */

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

  337.         /*
  338.          * Valuation Parameters
  339.          */

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

  341.         /*
  342.          * Standard Credit Pricer Parameters (check javadoc for details)
  343.          */

  344.         CreditPricerParams pricerParams = CreditPricerParams.Standard();

  345.         /*
  346.          * Re-calculate the input calibration measures for the input CDSes
  347.          */

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

  349.         for (int i = 0; i < aCDS.length; ++i)
  350.             System.out.println (
  351.                 "\t| " + aCDS[i].name() + " =" +
  352.                 FormatUtil.FormatDouble (aCDS[i].measureValue (
  353.                     valParams, pricerParams, MarketParamsBuilder.Create (
  354.                         dcFunding,
  355.                         null,
  356.                         null,
  357.                         cc,
  358.                         null,
  359.                         null,
  360.                         null,
  361.                         null
  362.                     ),
  363.                     null,
  364.                     astrCalibMeasure[i]
  365.                 ), 1, 1, 1.) + " ||"
  366.             );

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

  368.         return cc;
  369.     }

  370.     /*
  371.      * Sample API demonstrating the display of the CDS coupon and loss cash flow
  372.      *
  373.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  374.      */

  375.     public static final void main (
  376.         final String[] astrArgs)
  377.         throws Exception
  378.     {
  379.         EnvManager.InitEnv ("");

  380.         JulianDate dtSpot = DateUtil.Today();

  381.         String strCurrency = "USD";
  382.         String strCreditCurve = "DB";
  383.         String strCDSForwardStartTenor = "3M";
  384.         String strCDSMaturityTenor = "5Y";
  385.         double dblCDSCoupon = 0.1;
  386.         String strManifestMeasure = "FairPremium";

  387.         double[] adblFairPremiumVolatility = new double[] {
  388.             0.10,
  389.             0.15,
  390.             0.20,
  391.             0.25,
  392.             0.30,
  393.             0.35,
  394.             0.40,
  395.             0.45,
  396.             0.50
  397.         };

  398.         MergedDiscountForwardCurve dcFunding = MakeDC (
  399.             dtSpot,
  400.             strCurrency
  401.         );

  402.         CreditCurve cc = MakeCC (
  403.             dtSpot,
  404.             strCreditCurve,
  405.             dcFunding
  406.         );

  407.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Credit (
  408.             dcFunding,
  409.             cc
  410.         );

  411.         CreditDefaultSwap cdsForward = CDSBuilder.CreateSNAC (
  412.             dtSpot.addTenor (strCDSForwardStartTenor),
  413.             strCDSMaturityTenor,
  414.             dblCDSCoupon,
  415.             strCreditCurve
  416.         );

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

  418.         CreditPricerParams pricerParams = CreditPricerParams.Standard();

  419.         Map<String, Double> mapCDSForwardOutput = cdsForward.value (
  420.             valParams,
  421.             pricerParams,
  422.             csqc,
  423.             null
  424.         );

  425.         double dblStrike = 1.005 * mapCDSForwardOutput.get (strManifestMeasure);

  426.         CDSEuropeanOption cdsOptionReceiver = new CDSEuropeanOption (
  427.             cdsForward.name() + "::RECEIVER_OPT",
  428.             cdsForward,
  429.             strManifestMeasure,
  430.             true,
  431.             dblStrike,
  432.             new LastTradingDateSetting (
  433.                 LastTradingDateSetting.MID_CURVE_OPTION_QUARTERLY,
  434.                 "",
  435.                 Integer.MIN_VALUE
  436.             ),
  437.             new BlackScholesAlgorithm(),
  438.             null
  439.         );

  440.         CDSEuropeanOption cdsOptionPayer = new CDSEuropeanOption (
  441.             cdsForward.name() + "::PAYER_OPT",
  442.             cdsForward,
  443.             strManifestMeasure,
  444.             false,
  445.             dblStrike,
  446.             new LastTradingDateSetting (
  447.                 LastTradingDateSetting.MID_CURVE_OPTION_QUARTERLY,
  448.                 "",
  449.                 Integer.MIN_VALUE
  450.             ),
  451.             new BlackScholesAlgorithm(),
  452.             null
  453.         );

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

  455.         System.out.println ("\t||                                                                                                                               |||");

  456.         System.out.println ("\t||                                              CDS Payer Receiver Option Analysis                                               |||");

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

  458.         System.out.println ("\t||   L -> R:                                                                                                                     |||");

  459.         System.out.println ("\t||          Forward CDS Fair Premium Log-normal Volatility                                                                       |||");

  460.         System.out.println ("\t||          Receiver ATM Fair Premium (bp)                                                                                       |||");

  461.         System.out.println ("\t||          Receiver Clean DV01                                                                                                  |||");

  462.         System.out.println ("\t||          Receiver Manifest Measure Intrinsic                                                                                  |||");

  463.         System.out.println ("\t||          Receiver Manifest Measure Intrinsic Value                                                                            |||");

  464.         System.out.println ("\t||          Receiver Moneyness Factor                                                                                            |||");

  465.         System.out.println ("\t||          Receiver Option Price                                                                                                |||");

  466.         System.out.println ("\t||          Payer ATM Fair Premium (bp)                                                                                          |||");

  467.         System.out.println ("\t||          Payer Clean DV01                                                                                                     |||");

  468.         System.out.println ("\t||          Payer Manifest Measure Intrinsic                                                                                     |||");

  469.         System.out.println ("\t||          Payer Manifest Measure Intrinsic Value                                                                               |||");

  470.         System.out.println ("\t||          Payer Moneyness Factor                                                                                               |||");

  471.         System.out.println ("\t||          Payer Option Price                                                                                                   |||");

  472.         System.out.println ("\t||                                                                                                                               |||");

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

  474.         for (double dblFairPremiumVolatility : adblFairPremiumVolatility) {
  475.             csqc.setCustomVolatility (
  476.                 ScenarioDeterministicVolatilityBuilder.FlatForward (
  477.                     dtSpot.julian(),
  478.                     VolatilityLabel.Standard (CustomLabel.Standard (cdsForward.name() + "_" + strManifestMeasure)),
  479.                     cdsForward.payCurrency(),
  480.                     dblFairPremiumVolatility
  481.                 )
  482.             );

  483.             Map<String, Double> mapReceiverOptionOutput = cdsOptionReceiver.value (
  484.                 valParams,
  485.                 null,
  486.                 csqc,
  487.                 null
  488.             );

  489.             Map<String, Double> mapPayerOptionOutput = cdsOptionPayer.value (
  490.                 valParams,
  491.                 null,
  492.                 csqc,
  493.                 null
  494.             );

  495.             System.out.println (
  496.                 "\t|| " + FormatUtil.FormatDouble (dblFairPremiumVolatility, 2, 1, 100.) + "% => " +
  497.                 FormatUtil.FormatDouble (mapReceiverOptionOutput.get ("ATMManifestMeasure"), 3, 2, 1.) + " | " +
  498.                 FormatUtil.FormatDouble (mapReceiverOptionOutput.get ("ManifestMeasurePriceTransformer"), 1, 2, 100.) + " | " +
  499.                 FormatUtil.FormatDouble (mapReceiverOptionOutput.get ("ManifestMeasureIntrinsic"), 1, 4, 100.) + " | " +
  500.                 FormatUtil.FormatDouble (mapReceiverOptionOutput.get ("ManifestMeasureIntrinsicValue"), 1, 2, 100.) + " | " +
  501.                 FormatUtil.FormatDouble (mapReceiverOptionOutput.get ("MoneynessFactor"), 1, 4, 1.) + " | " +
  502.                 FormatUtil.FormatDouble (mapReceiverOptionOutput.get ("Price"), 1, 4, 1.) + "  ||  " +
  503.                 FormatUtil.FormatDouble (mapPayerOptionOutput.get ("ATMManifestMeasure"), 3, 2, 1.) + " | " +
  504.                 FormatUtil.FormatDouble (mapPayerOptionOutput.get ("ManifestMeasurePriceTransformer"), 1, 2, 100.) + " | " +
  505.                 FormatUtil.FormatDouble (mapPayerOptionOutput.get ("ManifestMeasureIntrinsic"), 1, 4, 100.) + " | " +
  506.                 FormatUtil.FormatDouble (mapPayerOptionOutput.get ("ManifestMeasureIntrinsicValue"), 1, 2, 100.) + " | " +
  507.                 FormatUtil.FormatDouble (mapPayerOptionOutput.get ("MoneynessFactor"), 1, 4, 1.) + " | " +
  508.                 FormatUtil.FormatDouble (mapPayerOptionOutput.get ("Price"), 1, 4, 1.) + "  |||"
  509.             );
  510.         }

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

  512.         EnvManager.TerminateEnv();
  513.     }
  514. }