CDXNAIGS205YMetrics.java

  1. package org.drip.sample.credithistorical;

  2. import java.util.*;

  3. import org.drip.analytics.date.JulianDate;
  4. import org.drip.feed.loader.*;
  5. import org.drip.historical.state.CreditCurveMetrics;
  6. import org.drip.service.env.EnvManager;
  7. import org.drip.service.state.CreditCurveAPI;

  8. /*
  9.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  10.  */

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

  83. /**
  84.  * <i>CDXNAIGS205YMetrics</i> generates the Historical Credit Survival/Recovery Metrics for the Index
  85.  * Contract CDX NA IG S20 5Y.
  86.  *  
  87.  * <br><br>
  88.  *  <ul>
  89.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  90.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  91.  *      <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>
  92.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/credithistorical/README.md">CDX NA IG Historical Metrics</a></li>
  93.  *  </ul>
  94.  * <br><br>
  95.  *
  96.  * @author Lakshmi Krishnamurthy
  97.  */

  98. public class CDXNAIGS205YMetrics {

  99.     public static final void main (
  100.         final String[] astrArgs)
  101.         throws Exception
  102.     {
  103.         EnvManager.InitEnv ("");

  104.         int iSeries = 20;
  105.         String strClosesLocation = "C:\\DROP\\Daemons\\Transforms\\CreditCDXMarks\\CDXNAIGS" + iSeries + "5YReconstitutor.csv";
  106.         String[] astrForTenor = new String[] {
  107.             "1Y",
  108.             "2Y",
  109.             "3Y",
  110.             "4Y",
  111.             "5Y",
  112.             "6Y",
  113.             "7Y",
  114.             "8Y",
  115.             "9Y",
  116.             "10Y",
  117.             "11Y",
  118.             "12Y",
  119.             "15Y",
  120.             "20Y",
  121.             "25Y",
  122.         };
  123.         String[] astrFundingFixingMaturityTenor = new String[] {
  124.             "1Y",
  125.             "2Y",
  126.             "3Y",
  127.             "4Y",
  128.             "5Y",
  129.             "6Y",
  130.             "7Y",
  131.             "8Y",
  132.             "9Y",
  133.             "10Y",
  134.             "11Y",
  135.             "12Y",
  136.             "15Y",
  137.             "20Y",
  138.             "25Y",
  139.             "30Y",
  140.             "40Y",
  141.             "50Y"
  142.         };

  143.         CSVGrid csvGrid = CSVParser.StringGrid (
  144.             strClosesLocation,
  145.             true
  146.         );

  147.         JulianDate[] adtClose = csvGrid.dateArrayAtColumn (0);

  148.         double[] adblFundingFixingQuote1Y = csvGrid.doubleArrayAtColumn (1);

  149.         double[] adblFundingFixingQuote2Y = csvGrid.doubleArrayAtColumn (2);

  150.         double[] adblFundingFixingQuote3Y = csvGrid.doubleArrayAtColumn (3);

  151.         double[] adblFundingFixingQuote4Y = csvGrid.doubleArrayAtColumn (4);

  152.         double[] adblFundingFixingQuote5Y = csvGrid.doubleArrayAtColumn (5);

  153.         double[] adblFundingFixingQuote6Y = csvGrid.doubleArrayAtColumn (6);

  154.         double[] adblFundingFixingQuote7Y = csvGrid.doubleArrayAtColumn (7);

  155.         double[] adblFundingFixingQuote8Y = csvGrid.doubleArrayAtColumn (8);

  156.         double[] adblFundingFixingQuote9Y = csvGrid.doubleArrayAtColumn (9);

  157.         double[] adblFundingFixingQuote10Y = csvGrid.doubleArrayAtColumn (10);

  158.         double[] adblFundingFixingQuote11Y = csvGrid.doubleArrayAtColumn (11);

  159.         double[] adblFundingFixingQuote12Y = csvGrid.doubleArrayAtColumn (12);

  160.         double[] adblFundingFixingQuote15Y = csvGrid.doubleArrayAtColumn (13);

  161.         double[] adblFundingFixingQuote20Y = csvGrid.doubleArrayAtColumn (14);

  162.         double[] adblFundingFixingQuote25Y = csvGrid.doubleArrayAtColumn (15);

  163.         double[] adblFundingFixingQuote30Y = csvGrid.doubleArrayAtColumn (16);

  164.         double[] adblFundingFixingQuote40Y = csvGrid.doubleArrayAtColumn (17);

  165.         double[] adblFundingFixingQuote50Y = csvGrid.doubleArrayAtColumn (18);

  166.         String[] astrFullCreditIndexName = csvGrid.stringArrayAtColumn (19);

  167.         double[] adblCreditIndexQuotedSpread = csvGrid.doubleArrayAtColumn (20);

  168.         int iNumClose = adtClose.length;
  169.         JulianDate[] adtSpot = new JulianDate[iNumClose];
  170.         double[][] aadblFundingFixingQuote = new double[iNumClose][18];

  171.         for (int i = 0; i < iNumClose; ++i) {
  172.             adtSpot[i] = adtClose[i];
  173.             aadblFundingFixingQuote[i][0] = adblFundingFixingQuote1Y[i];
  174.             aadblFundingFixingQuote[i][1] = adblFundingFixingQuote2Y[i];
  175.             aadblFundingFixingQuote[i][2] = adblFundingFixingQuote3Y[i];
  176.             aadblFundingFixingQuote[i][3] = adblFundingFixingQuote4Y[i];
  177.             aadblFundingFixingQuote[i][4] = adblFundingFixingQuote5Y[i];
  178.             aadblFundingFixingQuote[i][5] = adblFundingFixingQuote6Y[i];
  179.             aadblFundingFixingQuote[i][6] = adblFundingFixingQuote7Y[i];
  180.             aadblFundingFixingQuote[i][7] = adblFundingFixingQuote8Y[i];
  181.             aadblFundingFixingQuote[i][8] = adblFundingFixingQuote9Y[i];
  182.             aadblFundingFixingQuote[i][9] = adblFundingFixingQuote10Y[i];
  183.             aadblFundingFixingQuote[i][10] = adblFundingFixingQuote11Y[i];
  184.             aadblFundingFixingQuote[i][11] = adblFundingFixingQuote12Y[i];
  185.             aadblFundingFixingQuote[i][12] = adblFundingFixingQuote15Y[i];
  186.             aadblFundingFixingQuote[i][13] = adblFundingFixingQuote20Y[i];
  187.             aadblFundingFixingQuote[i][14] = adblFundingFixingQuote25Y[i];
  188.             aadblFundingFixingQuote[i][15] = adblFundingFixingQuote30Y[i];
  189.             aadblFundingFixingQuote[i][16] = adblFundingFixingQuote40Y[i];
  190.             aadblFundingFixingQuote[i][17] = adblFundingFixingQuote50Y[i];
  191.             adblCreditIndexQuotedSpread[i] *= 10000.;
  192.         }

  193.         int i = 0;
  194.         String strDump = "Date,QuotedSpread";

  195.         for (String strForTenor : astrForTenor)
  196.             strDump += ",SURVIVAL::" + strForTenor + ",RECOVERY::" + strForTenor;

  197.         System.out.println (strDump);

  198.         TreeMap<JulianDate, CreditCurveMetrics> mapCCM = CreditCurveAPI.HorizonMetrics (
  199.             adtSpot,
  200.             astrFundingFixingMaturityTenor,
  201.             aadblFundingFixingQuote,
  202.             astrFullCreditIndexName,
  203.             adblCreditIndexQuotedSpread,
  204.             astrForTenor
  205.         );

  206.         Set<JulianDate> setSpotDate = mapCCM.keySet();

  207.         for (JulianDate dtSpot : setSpotDate) {
  208.             strDump = dtSpot.toString() + "," + adblCreditIndexQuotedSpread[i++];

  209.             CreditCurveMetrics ccmSpot = mapCCM.get (dtSpot);

  210.             for (String strForTenor : astrForTenor) {
  211.                 JulianDate dtFor = dtSpot.addTenor (strForTenor);

  212.                 strDump += "," + ccmSpot.survivalProbability (dtFor) + "," + ccmSpot.recoveryRate (dtFor);
  213.             }

  214.             System.out.println (strDump);
  215.         }

  216.         EnvManager.TerminateEnv();
  217.     }
  218. }