FixFloatPnLAttributor.java

  1. package org.drip.feed.metric;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

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

  77. /**
  78.  * <i>FixFloatPnLAttributor</i> generates the Date Valuation and Position Change PnL Explain Attributions for
  79.  * the Standard OTC Fix Float Swap.
  80.  *
  81.  *  <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/feed/README.md">Load, Transform, and compute Target Metrics across Feeds</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/feed/metric/README.md">Feed Horizon - PnL Explain/Attribution</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class FixFloatPnLAttributor {

  92.     /**
  93.      * Generate the Explain Components for the specified Fix Float Product
  94.      *
  95.      * @param strCurrency The Fix-Float Swap Currency
  96.      * @param strMaturityTenor The Fix-Float Swap Maturity Tenor
  97.      * @param iHorizonGap The Valuation Horizon Gap
  98.      * @param strFeedTranformLocation The Closing Funding Curve Quotes Location
  99.      * @param astrFundingDepositTenor The Funding Curve Deposit Instrument Maturity Tenors
  100.      * @param aiFundingDepositColumn The Funding Curve Deposit Instrument Quote Columns
  101.      * @param astrFundingFixFloatTenor The Funding Curve Fix Float Swap Instrument Maturity Tenors
  102.      * @param aiFundingFixFloatColumn The Funding Curve Fix Float Swap Instrument Quote Columns
  103.      * @param astrRollDownHorizonTenor Array of the Roll Down Horizon Tenors
  104.      *
  105.      * @return List of the Position Change Components
  106.      */

  107.     public static final java.util.List<org.drip.historical.attribution.PositionChangeComponents>
  108.         TenorHorizonExplainComponents (
  109.             final java.lang.String strCurrency,
  110.             final java.lang.String strMaturityTenor,
  111.             final int iHorizonGap,
  112.             final java.lang.String strFeedTranformLocation,
  113.             final java.lang.String[] astrFundingDepositTenor,
  114.             final int[] aiFundingDepositColumn,
  115.             final java.lang.String[] astrFundingFixFloatTenor,
  116.             final int[] aiFundingFixFloatColumn,
  117.             final java.lang.String[] astrRollDownHorizonTenor)
  118.     {
  119.         if (null == astrFundingDepositTenor || null == aiFundingDepositColumn || null ==
  120.             astrFundingFixFloatTenor || null == aiFundingFixFloatColumn)
  121.             return null;

  122.         int iNumFundingDeposit = astrFundingDepositTenor.length;
  123.         int iNumFundingFixFloat = astrFundingFixFloatTenor.length;
  124.         double[][] aadblFundingDepositClose = new double[iNumFundingDeposit][];
  125.         double[][] aadblFundingFixFloatClose = new double[iNumFundingFixFloat][];

  126.         if (0 == iNumFundingDeposit || iNumFundingDeposit != aiFundingDepositColumn.length || 0 ==
  127.             iNumFundingFixFloat || iNumFundingFixFloat != aiFundingFixFloatColumn.length)
  128.             return null;

  129.         org.drip.feed.loader.CSVGrid csvGrid = org.drip.feed.loader.CSVParser.StringGrid
  130.             (strFeedTranformLocation, true);

  131.         if (null == csvGrid) return null;

  132.         org.drip.analytics.date.JulianDate[] adtClose = csvGrid.dateArrayAtColumn (0);

  133.         int iNumClose = adtClose.length;
  134.         double[][] aadblFundingDepositQuote = new double[iNumClose][iNumFundingDeposit];
  135.         double[][] aadblFundingFixFloatQuote = new double[iNumClose][iNumFundingFixFloat];
  136.         org.drip.analytics.date.JulianDate[] adtSpot = new org.drip.analytics.date.JulianDate[iNumClose];

  137.         for (int i = 0; i < iNumFundingDeposit; ++i)
  138.             aadblFundingDepositClose[i] = csvGrid.doubleArrayAtColumn (aiFundingDepositColumn[i]);

  139.         for (int i = 0; i < iNumFundingFixFloat; ++i)
  140.             aadblFundingFixFloatClose[i] = csvGrid.doubleArrayAtColumn (aiFundingFixFloatColumn[i]);

  141.         for (int i = 0; i < iNumClose; ++i) {
  142.             adtSpot[i] = adtClose[i];

  143.             for (int j = 0; j < iNumFundingDeposit; ++j)
  144.                 aadblFundingDepositQuote[i][j] = aadblFundingDepositClose[j][i];

  145.             for (int j = 0; j < iNumFundingFixFloat; ++j)
  146.                 aadblFundingFixFloatQuote[i][j] = aadblFundingFixFloatClose[j][i];
  147.         }

  148.         return org.drip.service.product.FixFloatAPI.HorizonChangeAttribution (adtSpot, iHorizonGap,
  149.             astrFundingDepositTenor, aadblFundingDepositQuote, astrFundingFixFloatTenor,
  150.                 aadblFundingFixFloatQuote, strCurrency, strMaturityTenor, astrRollDownHorizonTenor,
  151.                     org.drip.service.template.LatentMarketStateBuilder.SHAPE_PRESERVING);
  152.     }

  153.     /**
  154.      * Generate the Tenor Horizon Explain Components
  155.      *
  156.      * @param strCurrency The Fix-Float Swap Currency
  157.      * @param astrMaturityTenor Array of Fix-Float Swap Maturity Tenors
  158.      * @param aiHorizonGap Array of the Valuation Horizon Gaps
  159.      * @param strFeedTranformLocation The Closing Funding Curve Quotes Location
  160.      * @param astrFundingDepositTenor The Funding Curve Deposit Instrument Maturity Tenors
  161.      * @param aiFundingDepositColumn The Funding Curve Deposit Instrument Quote Columns
  162.      * @param astrFundingFixFloatTenor The Funding Curve Fix Float Swap Instrument Maturity Tenors
  163.      * @param aiFundingFixFloatColumn The Funding Curve Fix Float Swap Instrument Quote Columns
  164.      * @param astrRollDownHorizonTenor Array of the Roll Down Horizon Tenors
  165.      *
  166.      * @return List of the Position Change Components
  167.      */

  168.     public static final boolean TenorHorizonExplainComponents (
  169.         final java.lang.String strCurrency,
  170.         final java.lang.String[] astrMaturityTenor,
  171.         final int[] aiHorizonGap,
  172.         final java.lang.String strFeedTranformLocation,
  173.         final java.lang.String[] astrFundingDepositTenor,
  174.         final int[] aiFundingDepositColumn,
  175.         final java.lang.String[] astrFundingFixFloatTenor,
  176.         final int[] aiFundingFixFloatColumn,
  177.         final java.lang.String[] astrRollDownHorizonTenor)
  178.     {
  179.         boolean bFirstRun = true;

  180.         for (java.lang.String strMaturityTenor : astrMaturityTenor) {
  181.             for (int iHorizonGap : aiHorizonGap) {
  182.                 java.util.List<org.drip.historical.attribution.PositionChangeComponents> lsPCC =
  183.                     org.drip.feed.metric.FixFloatPnLAttributor.TenorHorizonExplainComponents (strCurrency,
  184.                         strMaturityTenor, iHorizonGap, strFeedTranformLocation, astrFundingDepositTenor,
  185.                             aiFundingDepositColumn, astrFundingFixFloatTenor, aiFundingFixFloatColumn,
  186.                                 astrRollDownHorizonTenor);

  187.                 if (null != lsPCC) {
  188.                     java.lang.String strHorizonTenor = 1 == iHorizonGap ? "1D" : ((iHorizonGap / 22) + "M");

  189.                     for (org.drip.historical.attribution.PositionChangeComponents pcc : lsPCC) {
  190.                         if (null != pcc) {
  191.                             if (bFirstRun) {
  192.                                 System.out.println (pcc.header() + "horizontenor,");

  193.                                 bFirstRun = false;
  194.                             }

  195.                             System.out.println (pcc.content() + strHorizonTenor + ",");
  196.                         }
  197.                     }
  198.                 }
  199.             }
  200.         }

  201.         return true;
  202.     }
  203. }