CDXNAIGS215YAttribution.java

  1. package org.drip.sample.creditindexpnl;

  2. import java.util.List;

  3. import org.drip.analytics.date.JulianDate;
  4. import org.drip.feed.loader.*;
  5. import org.drip.historical.attribution.*;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.service.env.EnvManager;
  8. import org.drip.service.product.CreditIndexAPI;

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

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

  84. /**
  85.  * <i>CDXNAIGS215YAttribution</i> contains the Functionality associated with the Attribution of the CDX NA IG
  86.  * 5Y S21 Index.
  87.  *  
  88.  * <br><br>
  89.  *  <ul>
  90.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  91.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  92.  *      <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>
  93.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/creditindexpnl/README.md">CDX NA IG PnL Attribution</a></li>
  94.  *  </ul>
  95.  * <br><br>
  96.  *
  97.  * @author Lakshmi Krishnamurthy
  98.  */

  99. public class CDXNAIGS215YAttribution {

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

  105.         int iSeries = 21;
  106.         int iHorizonGap = 1;
  107.         String strClosesLocation = "C:\\DROP\\Daemons\\Transforms\\CreditCDXMarks\\CDXNAIGS" + iSeries + "5YReconstitutor.csv";
  108.         String[] astrFundingFixingMaturityTenor = new String[] {
  109.             "1Y",
  110.             "2Y",
  111.             "3Y",
  112.             "4Y",
  113.             "5Y",
  114.             "6Y",
  115.             "7Y",
  116.             "8Y",
  117.             "9Y",
  118.             "10Y",
  119.             "11Y",
  120.             "12Y",
  121.             "15Y",
  122.             "20Y",
  123.             "25Y",
  124.             "30Y",
  125.             "40Y",
  126.             "50Y"
  127.         };

  128.         CSVGrid csvGrid = CSVParser.StringGrid (
  129.             strClosesLocation,
  130.             true
  131.         );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  153.         int iNumClose = adtClose.length;
  154.         JulianDate[] adtSpot = new JulianDate[iNumClose];
  155.         double[][] aadblFundingFixingQuote = new double[iNumClose][18];

  156.         for (int i = 0; i < iNumClose; ++i) {
  157.             adtSpot[i] = adtClose[i];
  158.             aadblFundingFixingQuote[i][0] = adblFundingFixingQuote1Y[i];
  159.             aadblFundingFixingQuote[i][1] = adblFundingFixingQuote2Y[i];
  160.             aadblFundingFixingQuote[i][2] = adblFundingFixingQuote3Y[i];
  161.             aadblFundingFixingQuote[i][3] = adblFundingFixingQuote4Y[i];
  162.             aadblFundingFixingQuote[i][4] = adblFundingFixingQuote5Y[i];
  163.             aadblFundingFixingQuote[i][5] = adblFundingFixingQuote6Y[i];
  164.             aadblFundingFixingQuote[i][6] = adblFundingFixingQuote7Y[i];
  165.             aadblFundingFixingQuote[i][7] = adblFundingFixingQuote8Y[i];
  166.             aadblFundingFixingQuote[i][8] = adblFundingFixingQuote9Y[i];
  167.             aadblFundingFixingQuote[i][9] = adblFundingFixingQuote10Y[i];
  168.             aadblFundingFixingQuote[i][10] = adblFundingFixingQuote11Y[i];
  169.             aadblFundingFixingQuote[i][11] = adblFundingFixingQuote12Y[i];
  170.             aadblFundingFixingQuote[i][12] = adblFundingFixingQuote15Y[i];
  171.             aadblFundingFixingQuote[i][13] = adblFundingFixingQuote20Y[i];
  172.             aadblFundingFixingQuote[i][14] = adblFundingFixingQuote25Y[i];
  173.             aadblFundingFixingQuote[i][15] = adblFundingFixingQuote30Y[i];
  174.             aadblFundingFixingQuote[i][16] = adblFundingFixingQuote40Y[i];
  175.             aadblFundingFixingQuote[i][17] = adblFundingFixingQuote50Y[i];
  176.             adblCreditIndexQuotedSpread[i] *= 10000.;
  177.         }

  178.         List<PositionChangeComponents> lsPCC = CreditIndexAPI.HorizonChangeAttribution (
  179.             adtSpot,
  180.             1,
  181.             astrFundingFixingMaturityTenor,
  182.             aadblFundingFixingQuote,
  183.             astrFullCreditIndexName,
  184.             adblCreditIndexQuotedSpread
  185.         );

  186.         System.out.println ("FirstDate,SecondDate,CreditLabel,Horizon,TotalPnL,MarketShiftPnL,RollDownPnL,AccrualPnL,ExplainedPnL,UnexplainedPnL,FixedCoupon,FirstFairPremium,SecondFairPremium,RollDownFairPremium,CleanFixedDV01");

  187.         for (PositionChangeComponents pcc : lsPCC) {
  188.             if (null == pcc) continue;

  189.             CDSMarketSnap cdsmsFirst = (CDSMarketSnap) pcc.pmsFirst();

  190.             CDSMarketSnap cdsmsSecond = (CDSMarketSnap) pcc.pmsSecond();

  191.             System.out.println (
  192.                 pcc.firstDate() + ", " +
  193.                 pcc.secondDate() + ", " +
  194.                 cdsmsFirst.creditLabel() + ", " +
  195.                 iHorizonGap + "," +
  196.                 FormatUtil.FormatDouble (pcc.grossChange(), 2, 4, 10000.) + ", " +
  197.                 FormatUtil.FormatDouble (pcc.marketRealizationChange(), 2, 4, 10000.) + ", " +
  198.                 FormatUtil.FormatDouble (pcc.marketRollDownChange(), 1, 4, 10000.) + ", " +
  199.                 FormatUtil.FormatDouble (pcc.accrualChange(), 1, 4, 10000.) + ", " +
  200.                 FormatUtil.FormatDouble (pcc.explainedChange(), 2, 4, 10000.) + ", " +
  201.                 FormatUtil.FormatDouble (pcc.unexplainedChange(), 1, 4, 10000.) + ", " +
  202.                 FormatUtil.FormatDouble (cdsmsFirst.fixedCoupon(), 1, 2, 10000.) + ", " +
  203.                 FormatUtil.FormatDouble (cdsmsFirst.currentFairPremium(), 1, 4, 10000.) + ", " +
  204.                 FormatUtil.FormatDouble (cdsmsSecond.currentFairPremium(), 1, 4, 10000.) + ", " +
  205.                 FormatUtil.FormatDouble (cdsmsFirst.rollDownFairPremium(), 1, 4, 10000.) + ", " +
  206.                 FormatUtil.FormatDouble (cdsmsFirst.cleanDV01(), 1, 4, 1.)
  207.             );
  208.         }

  209.         EnvManager.TerminateEnv();
  210.     }
  211. }