CDSPayerReceiver.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.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.pricer.option.BlackScholesAlgorithm;
  10. import org.drip.product.creator.*;
  11. import org.drip.product.definition.*;
  12. import org.drip.product.option.CDSEuropeanOption;
  13. import org.drip.product.params.LastTradingDateSetting;
  14. import org.drip.product.rates.*;
  15. import org.drip.service.env.EnvManager;
  16. import org.drip.state.creator.*;
  17. import org.drip.state.credit.CreditCurve;
  18. import org.drip.state.discount.MergedDiscountForwardCurve;
  19. import org.drip.state.identifier.*;

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

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

  95. /**
  96.  * <i>CDSPayerReceiver</i> contains the Demonstration of Valuing a Payer/Receiver CDS European Option Sample.
  97.  *  
  98.  * <br><br>
  99.  *  <ul>
  100.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  101.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  102.  *      <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>
  103.  *      <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>
  104.  *  </ul>
  105.  * <br><br>
  106.  *
  107.  * @author Lakshmi Krishnamurthy
  108.  */

  109. public class CDSPayerReceiver {

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

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

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

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

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

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

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

  162.         return aCalibComp;
  163.     }

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

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

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

  184.         return aIRS;
  185.     }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  314.             /*
  315.              * Calibration Quote
  316.              */

  317.             adblQuotes[i] = 100.;

  318.             /*
  319.              * Calibration Measure
  320.              */

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

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

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

  336.         /*
  337.          * Valuation Parameters
  338.          */

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

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

  343.         CreditPricerParams pricerParams = CreditPricerParams.Standard();

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

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

  365.         return cc;
  366.     }

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

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

  377.         JulianDate dtSpot = DateUtil.Today();

  378.         String strCurrency = "USD";
  379.         String strCreditCurve = "DB";
  380.         String strCDSForwardStartTenor = "3M";
  381.         String strCDSMaturityTenor = "5Y";
  382.         double dblCDSCoupon = 0.1;
  383.         String strManifestMeasure = "FairPremium";
  384.         double dblFairPremiumVolatility = 0.4;

  385.         MergedDiscountForwardCurve dcFunding = MakeDC (
  386.             dtSpot,
  387.             strCurrency
  388.         );

  389.         CreditCurve cc = MakeCC (
  390.             dtSpot,
  391.             strCreditCurve,
  392.             dcFunding
  393.         );

  394.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Credit (
  395.             dcFunding,
  396.             cc
  397.         );

  398.         CreditDefaultSwap cdsForward = CDSBuilder.CreateSNAC (
  399.             dtSpot.addTenor (strCDSForwardStartTenor),
  400.             strCDSMaturityTenor,
  401.             dblCDSCoupon,
  402.             strCreditCurve
  403.         );

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

  405.         CreditPricerParams pricerParams = CreditPricerParams.Standard();

  406.         csqc.setCustomVolatility (
  407.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  408.                 dtSpot.julian(),
  409.                 VolatilityLabel.Standard (CustomLabel.Standard (cdsForward.name() + "_" + strManifestMeasure)),
  410.                 strCurrency,
  411.                 dblFairPremiumVolatility
  412.             )
  413.         );

  414.         Map<String, Double> mapCDSForwardOutput = cdsForward.value (
  415.             valParams,
  416.             pricerParams,
  417.             csqc,
  418.             null
  419.         );

  420.         double dblStrike = 1.01 * mapCDSForwardOutput.get (strManifestMeasure);

  421.         CDSEuropeanOption cdsOptionReceiver = new CDSEuropeanOption (
  422.             cdsForward.name() + "::RECEIVER_OPT",
  423.             cdsForward,
  424.             strManifestMeasure,
  425.             true,
  426.             dblStrike,
  427.             new LastTradingDateSetting (
  428.                 LastTradingDateSetting.MID_CURVE_OPTION_QUARTERLY,
  429.                 "",
  430.                 Integer.MIN_VALUE
  431.             ),
  432.             new BlackScholesAlgorithm(),
  433.             null
  434.         );

  435.         Map<String, Double> mapReceiverOptionOutput = cdsOptionReceiver.value (
  436.             valParams,
  437.             null,
  438.             csqc,
  439.             null
  440.         );

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

  442.         for (Map.Entry<String, Double> me : mapReceiverOptionOutput.entrySet())
  443.             System.out.println ("\t" + me.getKey() + " => " + me.getValue());

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

  445.         CDSEuropeanOption cdsOptionPayer = new CDSEuropeanOption (
  446.             cdsForward.name() + "::PAYER_OPT",
  447.             cdsForward,
  448.             strManifestMeasure,
  449.             false,
  450.             dblStrike,
  451.             new LastTradingDateSetting (
  452.                 LastTradingDateSetting.MID_CURVE_OPTION_QUARTERLY,
  453.                 "",
  454.                 Integer.MIN_VALUE
  455.             ),
  456.             new BlackScholesAlgorithm(),
  457.             null
  458.         );

  459.         Map<String, Double> mapPayerOptionOutput = cdsOptionPayer.value (
  460.             valParams,
  461.             null,
  462.             csqc,
  463.             null
  464.         );

  465.         for (Map.Entry<String, Double> me : mapPayerOptionOutput.entrySet())
  466.             System.out.println ("\t" + me.getKey() + " => " + me.getValue());

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

  468.         EnvManager.TerminateEnv();
  469.     }
  470. }