CDSO.java

  1. package org.drip.sample.bloomberg;

  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.pricer.CreditPricerParams;
  7. import org.drip.param.valuation.ValuationParams;
  8. import org.drip.pricer.option.BlackScholesAlgorithm;
  9. import org.drip.product.creator.CDSBuilder;
  10. import org.drip.product.definition.CreditDefaultSwap;
  11. import org.drip.product.option.CDSEuropeanOption;
  12. import org.drip.service.env.EnvManager;
  13. import org.drip.service.template.LatentMarketStateBuilder;
  14. import org.drip.state.creator.ScenarioDeterministicVolatilityBuilder;
  15. import org.drip.state.credit.CreditCurve;
  16. import org.drip.state.discount.MergedDiscountForwardCurve;
  17. import org.drip.state.identifier.*;

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

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

  93. /**
  94.  * <i>CDSO</i> contains the sample demonstrating the replication of Bloomberg's CDSO functionality.
  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/bloomberg/README.md">Bloomberg CDSO CDSW SWPM YAS</a></li>
  102.  *  </ul>
  103.  * <br><br>
  104.  *
  105.  * @author Lakshmi Krishnamurthy
  106.  */

  107. public class CDSO {

  108.     private static final MergedDiscountForwardCurve MarketFundingCurve (
  109.         final JulianDate dtSpot,
  110.         final String strCurrency,
  111.         final double dblBump)
  112.     {
  113.         return LatentMarketStateBuilder.SmoothFundingCurve (
  114.             dtSpot,
  115.             strCurrency,
  116.             new String[] {
  117.                 "02D",
  118.                 "07D",
  119.                 "14D",
  120.                 "30D",
  121.                 "60D"
  122.             },
  123.             new double[] {
  124.                 0.0017 + dblBump,   //  2D
  125.                 0.0017 + dblBump,   //  7D
  126.                 0.0018 + dblBump,   // 14D
  127.                 0.0020 + dblBump,   // 30D
  128.                 0.0023 + dblBump    // 60D
  129.             },
  130.             "ForwardRate",
  131.             new double[] {
  132.                 0.0027 + dblBump,
  133.                 0.0032 + dblBump,
  134.                 0.0041 + dblBump,
  135.                 0.0054 + dblBump,
  136.                 0.0077 + dblBump,
  137.                 0.0104 + dblBump,
  138.                 0.0134 + dblBump,
  139.                 0.0160 + dblBump
  140.             },
  141.             "ForwardRate",
  142.             new String[] {
  143.                 "04Y",
  144.                 "05Y",
  145.                 "06Y",
  146.                 "07Y",
  147.                 "08Y",
  148.                 "09Y",
  149.                 "10Y",
  150.                 "11Y",
  151.                 "12Y",
  152.                 "15Y",
  153.                 "20Y",
  154.                 "25Y",
  155.                 "30Y",
  156.                 "40Y",
  157.                 "50Y"
  158.             },
  159.             new double[] {
  160.                 0.0166 + dblBump,   //  4Y
  161.                 0.0206 + dblBump,   //  5Y
  162.                 0.0241 + dblBump,   //  6Y
  163.                 0.0269 + dblBump,   //  7Y
  164.                 0.0292 + dblBump,   //  8Y
  165.                 0.0311 + dblBump,   //  9Y
  166.                 0.0326 + dblBump,   // 10Y
  167.                 0.0340 + dblBump,   // 11Y
  168.                 0.0351 + dblBump,   // 12Y
  169.                 0.0375 + dblBump,   // 15Y
  170.                 0.0393 + dblBump,   // 20Y
  171.                 0.0402 + dblBump,   // 25Y
  172.                 0.0407 + dblBump,   // 30Y
  173.                 0.0409 + dblBump,   // 40Y
  174.                 0.0409 + dblBump    // 50Y
  175.             },
  176.             "SwapRate"
  177.         );
  178.     }

  179.     private static final CreditCurve MarketCreditCurve (
  180.         final JulianDate dtSpot,
  181.         final String strCreditCurve,
  182.         final String strManifestMeasure,
  183.         final MergedDiscountForwardCurve dcFunding,
  184.         final double dblBump,
  185.         final boolean bDisplay)
  186.         throws Exception
  187.     {
  188.         String[] astrCDSMaturityTenor = new String[] {
  189.             "06M",
  190.             "01Y",
  191.             "02Y",
  192.             "03Y",
  193.             "04Y",
  194.             "05Y",
  195.             "07Y",
  196.             "10Y"
  197.         };

  198.         double[] adblCDSParSpread = new double[] {
  199.             100. + dblBump, //  6M
  200.             100. + dblBump, //  1Y
  201.             100. + dblBump, //  2Y
  202.             100. + dblBump, //  3Y
  203.             100. + dblBump, //  4Y
  204.             100. + dblBump, //  5Y
  205.             100. + dblBump, //  7Y
  206.             100. + dblBump  // 10Y
  207.         };

  208.         CreditCurve cc = LatentMarketStateBuilder.CreditCurve (
  209.             dtSpot,
  210.             strCreditCurve,
  211.             astrCDSMaturityTenor,
  212.             adblCDSParSpread,
  213.             adblCDSParSpread,
  214.             strManifestMeasure,
  215.             dcFunding
  216.         );

  217.         if (!bDisplay) return cc;

  218.         CreditPricerParams pricerParams = CreditPricerParams.Standard();

  219.         CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  220.         csqc.setFundingState (dcFunding);

  221.         csqc.setCreditState (cc);

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

  223.         System.out.println ("\t| CREDIT SPREAD ||");

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

  225.         for (int i = 0; i < adblCDSParSpread.length; ++i)
  226.             System.out.println (
  227.                 "\t| " + astrCDSMaturityTenor[i] + " |" +
  228.                 FormatUtil.FormatDouble (
  229.                     CDSBuilder.CreateSNAC (
  230.                         dtSpot,
  231.                         astrCDSMaturityTenor[i],
  232.                         0.1,
  233.                         strCreditCurve
  234.                     ).measureValue (
  235.                         ValuationParams.Spot (dtSpot.julian()),
  236.                         pricerParams,
  237.                         csqc,
  238.                         null,
  239.                         strManifestMeasure
  240.                     ),
  241.                 3, 3, 1.) + " ||"
  242.             );

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

  244.         return cc;
  245.     }

  246.     public static final void main (
  247.         final String[] astrArgs)
  248.         throws Exception
  249.     {
  250.         EnvManager.InitEnv (
  251.             "",
  252.             true
  253.         );

  254.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  255.             2016,
  256.             DateUtil.FEBRUARY,
  257.             2
  258.         );

  259.         JulianDate dtCashPay = DateUtil.CreateFromYMD (
  260.             2016,
  261.             DateUtil.FEBRUARY,
  262.             5
  263.         );

  264.         String strCurrency = "USD";
  265.         String strCreditCurve = "DB";
  266.         String strCDSForwardTenor = "5Y";
  267.         double dblCDSForwardCoupon = 0.1;
  268.         double dblFairPremiumVolatility = 0.3;
  269.         String strManifestMeasure = "FairPremium";
  270.         double dblNotional = 10000000.;
  271.         double dblCreditBump = 10.;
  272.         double dblFundingBump = .0001;

  273.         MergedDiscountForwardCurve dcFunding = MarketFundingCurve (
  274.             dtSpot,
  275.             strCurrency,
  276.             0.
  277.         );

  278.         CreditCurve cc = MarketCreditCurve (
  279.             dtSpot,
  280.             strCreditCurve,
  281.             strManifestMeasure,
  282.             dcFunding,
  283.             0.,
  284.             true
  285.         );

  286.         CreditPricerParams pricerParams = CreditPricerParams.Standard();

  287.         CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  288.         csqc.setFundingState (dcFunding);

  289.         csqc.setCreditState (cc);

  290.         JulianDate dtEffective = DateUtil.CreateFromYMD (
  291.             2016,
  292.             DateUtil.MARCH,
  293.             20
  294.         );

  295.         CreditDefaultSwap cdsForward = CDSBuilder.CreateSNAC (
  296.             dtEffective,
  297.             strCDSForwardTenor,
  298.             dblCDSForwardCoupon,
  299.             strCreditCurve
  300.         );

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

  302.         System.out.println ("\t|        UNDERLYING CDS FORWARD       |");

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

  304.         System.out.println ("\t|  Effective Date    : " + cdsForward.effectiveDate());

  305.         System.out.println ("\t|  Maturity Date     : " + cdsForward.maturityDate());

  306.         System.out.println ("\t|  Notional          : " + FormatUtil.FormatDouble (dblNotional, 1, 0, 1.));

  307.         System.out.println ("\t|  Payment Frequency : " + cdsForward.freq());

  308.         System.out.println ("\t|  Currency          : " + cdsForward.payCurrency());

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

  310.         csqc.setCustomVolatility (
  311.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  312.                 dtSpot.julian(),
  313.                 VolatilityLabel.Standard (CustomLabel.Standard (cdsForward.name() + "_" + strManifestMeasure)),
  314.                 strCurrency,
  315.                 dblFairPremiumVolatility
  316.             )
  317.         );

  318.         ValuationParams valParams = new ValuationParams (
  319.             dtSpot,
  320.             dtCashPay,
  321.             strCurrency
  322.         );

  323.         Map<String, Double> mapCDSForwardOutput = cdsForward.value (
  324.             valParams,
  325.             pricerParams,
  326.             csqc,
  327.             null
  328.         );

  329.         double dblStrike = mapCDSForwardOutput.get (strManifestMeasure);

  330.         CDSEuropeanOption cdsOptionPayer = new CDSEuropeanOption (
  331.             cdsForward.name() + "::PAYER_OPT",
  332.             cdsForward,
  333.             strManifestMeasure,
  334.             false,
  335.             dblStrike,
  336.             null,
  337.             new BlackScholesAlgorithm(),
  338.             null
  339.         );

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

  341.         System.out.println ("\t|          OPTION PARAMETERS          |");

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

  343.         System.out.println ("\t| Payer Swaption?  " + true);

  344.         System.out.println ("\t| Exercise Type  : " + "European");

  345.         System.out.println ("\t| Knock Out?       " + true);

  346.         System.out.println ("\t| Strike         : " + FormatUtil.FormatDouble (dblStrike, 1, 0, 1.));

  347.         System.out.println ("\t| Start Date     : " + dtSpot);

  348.         System.out.println ("\t| Cash Pay Date  : " + dtCashPay);

  349.         System.out.println ("\t| Exercise Date  : " + cdsOptionPayer.exerciseDate());

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

  351.         Map<String, Double> mapPayerOptionOutput = cdsOptionPayer.value (
  352.             valParams,
  353.             null,
  354.             csqc,
  355.             null
  356.         );

  357.         double dblOptionPriceBase = mapPayerOptionOutput.get ("Price") / cdsForward.initialNotional();

  358.         CreditCurve ccBumpUp = MarketCreditCurve (
  359.             dtSpot,
  360.             strCreditCurve,
  361.             strManifestMeasure,
  362.             dcFunding,
  363.             dblCreditBump,
  364.             false
  365.         );

  366.         CreditCurve ccBumpDown = MarketCreditCurve (
  367.             dtSpot,
  368.             strCreditCurve,
  369.             strManifestMeasure,
  370.             dcFunding,
  371.             -dblCreditBump,
  372.             false
  373.         );

  374.         csqc.setCreditState (ccBumpUp);

  375.         Map<String, Double> mapPayerOptionOutputCreditBumpUp = cdsOptionPayer.value (
  376.             valParams,
  377.             null,
  378.             csqc,
  379.             null
  380.         );

  381.         double dblOptionPriceCreditBumpUp = mapPayerOptionOutputCreditBumpUp.get ("Price") / cdsForward.initialNotional();

  382.         csqc.setCreditState (ccBumpDown);

  383.         Map<String, Double> mapPayerOptionOutputCreditBumpDown = cdsOptionPayer.value (
  384.             valParams,
  385.             null,
  386.             csqc,
  387.             null
  388.         );

  389.         double dblOptionPriceCreditBumpDown = mapPayerOptionOutputCreditBumpDown.get ("Price") / cdsForward.initialNotional();

  390.         MergedDiscountForwardCurve dcFundingBumpUp = MarketFundingCurve (
  391.             dtSpot,
  392.             strCurrency,
  393.             dblFundingBump
  394.         );

  395.         csqc.setFundingState (dcFundingBumpUp);

  396.         csqc.setCreditState (cc);

  397.         Map<String, Double> mapPayerOptionOutputFundingBumpUp = cdsOptionPayer.value (
  398.             valParams,
  399.             null,
  400.             csqc,
  401.             null
  402.         );

  403.         double dblOptionPriceFundingBumpUp = mapPayerOptionOutputFundingBumpUp.get ("Price") / cdsForward.initialNotional();

  404.         MergedDiscountForwardCurve dcFundingBumpDown = MarketFundingCurve (
  405.             dtSpot,
  406.             strCurrency,
  407.             -dblFundingBump
  408.         );

  409.         csqc.setFundingState (dcFundingBumpDown);

  410.         Map<String, Double> mapPayerOptionOutputFundingBumpDown = cdsOptionPayer.value (
  411.             valParams,
  412.             null,
  413.             csqc,
  414.             null
  415.         );

  416.         double dblOptionPriceFundingBumpDown = mapPayerOptionOutputFundingBumpDown.get ("Price") / cdsForward.initialNotional();

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

  418.         System.out.println ("\t|           OPTION INPUTS AND PRICING           |");

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

  420.         System.out.println ("\t| Valuation Date          : " + dtSpot);

  421.         System.out.println ("\t| Fair Premium Volatility : " + FormatUtil.FormatDouble (dblFairPremiumVolatility, 1, 3, 100.) + "%");

  422.         System.out.println ("\t| Option Premium          : " + FormatUtil.FormatDouble (dblOptionPriceBase, 1, 5, 100.) + "%");

  423.         System.out.println ("\t| Option MTM              : " + FormatUtil.FormatDouble (dblOptionPriceBase * dblNotional, 1, 2, 1.));

  424.         System.out.println ("\t| Credit Spread Delta 01  : " + FormatUtil.FormatDouble (0.5 * (dblOptionPriceCreditBumpUp - dblOptionPriceCreditBumpDown) * dblNotional / dblCreditBump, 1, 2, 1.));

  425.         System.out.println ("\t| Credit Spread Gamma 01  : " + FormatUtil.FormatDouble (0.5 * (dblOptionPriceCreditBumpUp + dblOptionPriceCreditBumpDown - 2. * dblOptionPriceBase) * dblNotional / (dblCreditBump * dblCreditBump), 1, 2, 1.));

  426.         System.out.println ("\t| Funding Spread Delta 01 : " + FormatUtil.FormatDouble (0.5 * (dblOptionPriceFundingBumpUp - dblOptionPriceFundingBumpDown) * dblNotional / (10000. * dblFundingBump), 1, 2, 1.));

  427.         System.out.println ("\t| Funding Spread Gamma 01 : " + FormatUtil.FormatDouble (0.5 * (dblOptionPriceFundingBumpUp + dblOptionPriceFundingBumpDown - 2. * dblOptionPriceBase) * dblNotional / (10000. * 10000. * dblFundingBump * dblFundingBump), 1, 2, 1.));

  428.         System.out.println ("\t| ATM Forward             : " + FormatUtil.FormatDouble (mapPayerOptionOutput.get ("ATMManifestMeasure"), 1, 0, 1.));

  429.         System.out.println ("\t| FPG Delta               : " + FormatUtil.FormatDouble (mapPayerOptionOutput.get ("FPGDelta"), 1, 4, 1.));

  430.         System.out.println ("\t| FPG Gamma               : " + FormatUtil.FormatDouble (mapPayerOptionOutput.get ("FPGGamma"), 1, 4, 1.));

  431.         System.out.println ("\t| FPG Vega                : " + FormatUtil.FormatDouble (mapPayerOptionOutput.get ("FPGVega"), 1, 2, 1.));

  432.         System.out.println ("\t| FPG Theta               : " + FormatUtil.FormatDouble (mapPayerOptionOutput.get ("FPGTheta"), 1, 2, 1.));

  433.         System.out.println ("\t| Vega (1%)               : " + FormatUtil.FormatDouble (mapPayerOptionOutput.get ("Vega") / cdsForward.initialNotional() * dblNotional * 0.01, 1, 2, 1.));

  434.         System.out.println ("\t| Theta                   : " + FormatUtil.FormatDouble (mapPayerOptionOutput.get ("Theta") / cdsForward.initialNotional() * dblNotional / 365.25, 1, 2, 1.));

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

  436.         EnvManager.TerminateEnv();
  437.     }
  438. }