CreditDefaultSwapIndex.java

  1. package org.drip.sample.securitysuite;

  2. import java.util.Map;

  3. import org.drip.analytics.cashflow.CompositePeriod;
  4. import org.drip.analytics.date.*;
  5. import org.drip.analytics.support.CaseInsensitiveTreeMap;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.param.creator.MarketParamsBuilder;
  8. import org.drip.param.market.CurveSurfaceQuoteContainer;
  9. import org.drip.param.pricer.CreditPricerParams;
  10. import org.drip.param.valuation.ValuationParams;
  11. import org.drip.product.creator.CDSBuilder;
  12. import org.drip.product.definition.CreditDefaultSwap;
  13. import org.drip.service.env.EnvManager;
  14. import org.drip.service.template.LatentMarketStateBuilder;
  15. import org.drip.state.credit.CreditCurve;
  16. import org.drip.state.discount.MergedDiscountForwardCurve;

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

  20. /*!
  21.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  22.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  23.  *
  24.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  25.  *      libraries targeting analysts and developers
  26.  *      https://lakshmidrip.github.io/DRIP/
  27.  *  
  28.  *  DRIP is composed of four main libraries:
  29.  *  
  30.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  31.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  32.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  33.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  34.  *
  35.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  36.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  37.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  38.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  39.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  40.  *
  41.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  42.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  43.  *
  44.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  45.  *
  46.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  47.  *
  48.  *  Licensed under the Apache License, Version 2.0 (the "License");
  49.  *      you may not use this file except in compliance with the License.
  50.  *  
  51.  *  You may obtain a copy of the License at
  52.  *      http://www.apache.org/licenses/LICENSE-2.0
  53.  *  
  54.  *  Unless required by applicable law or agreed to in writing, software
  55.  *      distributed under the License is distributed on an "AS IS" BASIS,
  56.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  57.  *  
  58.  *  See the License for the specific language governing permissions and
  59.  *      limitations under the License.
  60.  */

  61. /**
  62.  * CreditDefaultSwapIndex demonstrates the Analytics Calculation/Reconciliation for a CDX.
  63.  *
  64.  * @author Lakshmi Krishnamurthy
  65.  */

  66. public class CreditDefaultSwapIndex {

  67.     private static final MergedDiscountForwardCurve FundingCurve (
  68.         final JulianDate dtSpot,
  69.         final String strCurrency,
  70.         final double dblBump)
  71.         throws Exception
  72.     {
  73.         String[] astrDepositMaturityTenor = new String[] {
  74.             "2D"
  75.         };

  76.         double[] adblDepositQuote = new double[] {
  77.             0.013161 + dblBump // 2D
  78.         };

  79.         double[] adblFuturesQuote = new double[] {
  80.             0.013225 + dblBump, // 98.6775
  81.             0.014250 + dblBump, // 98.5750
  82.             0.014750 + dblBump, // 98.5250
  83.             0.015250 + dblBump, // 98.4750
  84.             0.015750 + dblBump,  // 98.4250
  85.             0.016500 + dblBump   // 98.3500
  86.         };

  87.         String[] astrFixFloatMaturityTenor = new String[] {
  88.             "02Y",
  89.             "03Y",
  90.             "04Y",
  91.             "05Y",
  92.             "06Y",
  93.             "07Y",
  94.             "08Y",
  95.             "09Y",
  96.             "10Y",
  97.             "11Y",
  98.             "12Y",
  99.             "15Y",
  100.             "20Y",
  101.             "25Y",
  102.             "30Y",
  103.             "40Y",
  104.             "50Y"
  105.         };

  106.         double[] adblFixFloatQuote = new double[] {
  107.             0.015540 + dblBump, //  2Y
  108.             0.016423 + dblBump, //  3Y
  109.             0.017209 + dblBump, //  4Y
  110.             0.017980 + dblBump, //  5Y
  111.             0.018743 + dblBump, //  6Y
  112.             0.019455 + dblBump, //  7Y
  113.             0.020080 + dblBump, //  8Y
  114.             0.020651 + dblBump, //  9Y
  115.             0.021195 + dblBump, // 10Y
  116.             0.021651 + dblBump, // 11Y
  117.             0.022065 + dblBump, // 12Y
  118.             0.022952 + dblBump, // 15Y
  119.             0.023825 + dblBump, // 20Y
  120.             0.024175 + dblBump, // 25Y
  121.             0.024347 + dblBump, // 30Y
  122.             0.024225 + dblBump, // 40Y
  123.             0.023968 + dblBump  // 50Y
  124.         };

  125.         return LatentMarketStateBuilder.SmoothFundingCurve (
  126.             dtSpot,
  127.             strCurrency,
  128.             astrDepositMaturityTenor,
  129.             adblDepositQuote,
  130.             "ForwardRate",
  131.             adblFuturesQuote,
  132.             "ForwardRate",
  133.             astrFixFloatMaturityTenor,
  134.             adblFixFloatQuote,
  135.             "SwapRate"
  136.         );
  137.     }

  138.     private static final CreditCurve CreditCurve (
  139.         final JulianDate dtSpot,
  140.         final String strCreditCurve,
  141.         final MergedDiscountForwardCurve mdfc,
  142.         final double dblBump)
  143.         throws Exception
  144.     {
  145.         return LatentMarketStateBuilder.CreditCurve (
  146.             dtSpot,
  147.             strCreditCurve,
  148.             new String[] {
  149.                  "6M",
  150.                  "1Y",
  151.                  "2Y",
  152.                  "3Y",
  153.                  "4Y",
  154.                  "5Y",
  155.                  "7Y",
  156.                 "10Y",
  157.                 "20Y",
  158.                 "30Y",
  159.             },
  160.             new double[] {
  161.                 392.509,    //  6M
  162.                 320.707,    //  1Y
  163.                 393.624,    //  2Y
  164.                 472.869,    //  3Y
  165.                 570.360,    //  4Y
  166.                 663.920,    //  5Y
  167.                 779.463,    //  7Y
  168.                 957.555,    // 10Y
  169.                 908.712,    // 20Y
  170.                 900.297,    // 30Y
  171.             },
  172.             new double[] {
  173.                 392.509,    //  6M
  174.                 320.707,    //  1Y
  175.                 393.624,    //  2Y
  176.                 472.869,    //  3Y
  177.                 570.360,    //  4Y
  178.                 663.920,    //  5Y
  179.                 779.463,    //  7Y
  180.                 957.555,    // 10Y
  181.                 908.712,    // 20Y
  182.                 900.297,    // 30Y
  183.             },
  184.             "FairPremium",
  185.             mdfc
  186.         );
  187.     }

  188.     public static final void main (
  189.         final String[] astrArgs)
  190.         throws Exception
  191.     {
  192.         EnvManager.InitEnv ("");

  193.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  194.             2017,
  195.             DateUtil.SEPTEMBER,
  196.             17
  197.         );

  198.         JulianDate dtIssue = DateUtil.CreateFromYMD (
  199.             2017,
  200.             DateUtil.JUNE,
  201.             20
  202.         );

  203.         String strCDXTenor = "5Y";
  204.         String strCurrency = "USD";
  205.         String strCDXName = "CDXNAHY";
  206.         double dblCDXFixedCoupon = 0.05;

  207.         CreditDefaultSwap cdx = CDSBuilder.CreateSNAC (
  208.             dtIssue,
  209.             strCDXTenor,
  210.             dblCDXFixedCoupon,
  211.             strCDXName
  212.         );

  213.         MergedDiscountForwardCurve mdfc = FundingCurve (
  214.             dtSpot,
  215.             strCurrency,
  216.             0.
  217.         );

  218.         CreditCurve cc = CreditCurve (
  219.             dtSpot,
  220.             strCDXName,
  221.             mdfc,
  222.             0.
  223.         );

  224.         CurveSurfaceQuoteContainer csqc = MarketParamsBuilder.Credit (
  225.             mdfc,
  226.             cc
  227.         );

  228.         ValuationParams valParams = new ValuationParams (
  229.             dtSpot,
  230.             dtSpot,
  231.             strCurrency
  232.         );

  233.         CreditPricerParams pricerParams = CreditPricerParams.Standard();

  234.         CaseInsensitiveTreeMap<Double> mapOutput = cdx.value (
  235.             valParams,
  236.             pricerParams,
  237.             csqc,
  238.             null
  239.         );

  240.         System.out.println ("");

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

  242.         for (Map.Entry<String, Double> me : mapOutput.entrySet())
  243.             System.out.println ("\t | " + me.getKey() + " => " + me.getValue());

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

  245.         System.out.println ("");

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

  247.         for (CompositePeriod p : cdx.couponPeriods())
  248.             System.out.println (
  249.                 "\t | " +
  250.                 DateUtil.YYYYMMDD (p.startDate()) + " | " +
  251.                 DateUtil.YYYYMMDD (p.endDate()) + " | " +
  252.                 DateUtil.YYYYMMDD (p.payDate()) + " | " +
  253.                 FormatUtil.FormatDouble (p.couponDCF(), 1, 3, 1.) + " | " +
  254.                 FormatUtil.FormatDouble (p.couponDCF(), 1, 2, 0.01 * 1.) + " | " +
  255.                 FormatUtil.FormatDouble (mdfc.df (p.payDate()), 1, 4, 1.) + " | " +
  256.                 FormatUtil.FormatDouble (cc.survival (p.payDate()), 1, 4, 1.) + " ||"
  257.             );

  258.         System.out.println ("\t |---------------------------------------------------------------------------||");
  259.     }
  260. }