FixFloatAPI.java

  1. package org.drip.service.product;

  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>FixFloatAPI</i> contains the Functionality associated with the Horizon Analysis of the Fix Float Swap.
  79.  *
  80.  * <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  84.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/README.md">Environment, Product/Definition Containers, and Scenario/State Manipulation APIs</a></li>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/product/README.md">Product Horizon PnL Attribution Decomposition</a></li>
  86.  *  </ul>
  87.  * <br><br>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class FixFloatAPI {

  92.     /**
  93.      * Compute the Horizon Change Attribution Details for the Specified Fix-Float Swap
  94.      *
  95.      * @param dcFirst First Day Discount Curve
  96.      * @param dcSecond Second Date Discount Curve
  97.      * @param mapRollDownDiscountCurve Map of the Roll Down Discount Curve
  98.      * @param strMaturityTenor Fix Float Swap Maturity Tenor
  99.      *
  100.      * @return The Horizon Change Attribution Instance
  101.      */

  102.     public static final org.drip.historical.attribution.PositionChangeComponents HorizonChangeAttribution (
  103.         final org.drip.state.discount.MergedDiscountForwardCurve dcFirst,
  104.         final org.drip.state.discount.MergedDiscountForwardCurve dcSecond,
  105.         final
  106.             org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.state.discount.MergedDiscountForwardCurve>
  107.             mapRollDownDiscountCurve,
  108.         final java.lang.String strMaturityTenor)
  109.     {
  110.         if (null == mapRollDownDiscountCurve || 0 == mapRollDownDiscountCurve.size()) return null;

  111.         org.drip.market.otc.FixedFloatSwapConvention ffsc =
  112.             org.drip.market.otc.IBORFixedFloatContainer.ConventionFromJurisdiction (dcFirst.currency(),
  113.                 "ALL", strMaturityTenor, "MAIN");

  114.         if (null == ffsc) return null;

  115.         int iSettleLag = ffsc.spotLag();

  116.         org.drip.analytics.date.JulianDate dtFirst = dcFirst.epoch();

  117.         org.drip.product.rates.FixFloatComponent ffc = ffsc.createFixFloatComponent (dtFirst,
  118.             strMaturityTenor, 0., 0., 1.);

  119.         if (null == ffc) return null;

  120.         org.drip.param.market.CurveSurfaceQuoteContainer csqcFirst = new
  121.             org.drip.param.market.CurveSurfaceQuoteContainer();

  122.         if (!csqcFirst.setFundingState (dcFirst)) return null;

  123.         org.drip.param.market.CurveSurfaceQuoteContainer csqcSecond = new
  124.             org.drip.param.market.CurveSurfaceQuoteContainer();

  125.         if (!csqcSecond.setFundingState (dcSecond)) return null;

  126.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.param.market.CurveSurfaceQuoteContainer>
  127.             mapCSQCRollDown = new
  128.                 org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.param.market.CurveSurfaceQuoteContainer>();

  129.         for (java.lang.String strRollDownTenor : mapRollDownDiscountCurve.keySet()) {
  130.             org.drip.param.market.CurveSurfaceQuoteContainer csqcRollDown = new
  131.                 org.drip.param.market.CurveSurfaceQuoteContainer();

  132.             org.drip.state.discount.MergedDiscountForwardCurve dcRollDown = mapRollDownDiscountCurve.get
  133.                 (strRollDownTenor);

  134.             if (null == dcRollDown || !csqcRollDown.setFundingState (dcRollDown)) return null;

  135.             mapCSQCRollDown.put (strRollDownTenor, csqcRollDown);
  136.         }

  137.         try {
  138.             double dblSwapRate = ffc.measureValue (org.drip.param.valuation.ValuationParams.Spot
  139.                 (dtFirst.addBusDays (iSettleLag, ffc.payCurrency()).julian()), null, csqcFirst, null,
  140.                     "SwapRate");

  141.             return org.drip.historical.engine.HorizonChangeExplainExecutor.GenerateAttribution (new
  142.                 org.drip.historical.engine.FixFloatExplainProcessor (ffsc.createFixFloatComponent (dtFirst,
  143.                     strMaturityTenor, dblSwapRate, 0., 1.), iSettleLag, "SwapRate", dblSwapRate, dtFirst,
  144.                         dcSecond.epoch(), csqcFirst, csqcSecond, mapCSQCRollDown));
  145.         } catch (java.lang.Exception e) {
  146.             e.printStackTrace();
  147.         }

  148.         return null;
  149.     }

  150.     /**
  151.      * Generate the Funding Curve Horizon Metrics
  152.      *
  153.      * @param dtFirst The First Date
  154.      * @param dtSecond The Second Date
  155.      * @param astrFundingDepositInstrumentTenor Array of Funding Curve Deposit Instrument Maturity Tenors
  156.      * @param adblFirstFundingDepositInstrument Array of First Date Funding Curve Deposit Instrument Quotes
  157.      * @param adblSecondFundingDepositInstrument Array of Second Date Funding Curve Deposit Instrument Quotes
  158.      * @param astrFundingFixFloatTenor Array of Funding Curve Fix Float Instrument Maturity Tenors
  159.      * @param adblFirstFundingFixFloat Array of First Date Funding Curve Fix Float Swap Rates
  160.      * @param adblSecondFundingFixFloat Array of Second Date Funding Curve Fix Float Swap Rates
  161.      * @param strCurrency Funding Currency
  162.      * @param strMaturityTenor Maturity Tenor
  163.      * @param astrRollDownHorizon Array of the Roll Down Horizon Tenors
  164.      * @param iLatentStateType Latent State Type
  165.      *
  166.      * @return The Funding Curve Horizon Metrics
  167.      */

  168.     public static final org.drip.historical.attribution.PositionChangeComponents HorizonChangeAttribution (
  169.         final org.drip.analytics.date.JulianDate dtFirst,
  170.         final org.drip.analytics.date.JulianDate dtSecond,
  171.         final java.lang.String[] astrFundingDepositInstrumentTenor,
  172.         final double[] adblFirstFundingDepositInstrument,
  173.         final double[] adblSecondFundingDepositInstrument,
  174.         final java.lang.String[] astrFundingFixFloatTenor,
  175.         final double[] adblFirstFundingFixFloat,
  176.         final double[] adblSecondFundingFixFloat,
  177.         final java.lang.String strCurrency,
  178.         final java.lang.String strMaturityTenor,
  179.         final java.lang.String[] astrRollDownHorizon,
  180.         final int iLatentStateType)
  181.     {
  182.         if (null == dtFirst || null == dtSecond || dtFirst.julian() >= dtSecond.julian()) return null;

  183.         int iNumFundingDepositInstrument = null == astrFundingDepositInstrumentTenor ? 0 :
  184.             astrFundingDepositInstrumentTenor.length;
  185.         int iNumFirstFundingDepositInstrument = null == adblFirstFundingDepositInstrument ? 0 :
  186.             adblFirstFundingDepositInstrument.length;
  187.         int iNumSecondFundingDepositInstrument = null == adblSecondFundingDepositInstrument ? 0 :
  188.             adblSecondFundingDepositInstrument.length;
  189.         int iNumFundingFixFloat = null == astrFundingFixFloatTenor ? 0 : astrFundingFixFloatTenor.length;
  190.         int iNumFirstFundingFixFloat = null == adblFirstFundingFixFloat ? 0 :
  191.             adblFirstFundingFixFloat.length;
  192.         int iNumSecondFundingFixFloat = null == adblSecondFundingFixFloat ? 0 :
  193.             adblSecondFundingFixFloat.length;
  194.         int iNumRollDownHorizon = null == astrRollDownHorizon ? 0 : astrRollDownHorizon .length;

  195.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.state.discount.MergedDiscountForwardCurve>
  196.             mapRollDownDiscountCurve = 0 == iNumRollDownHorizon ? null : new
  197.                 org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.state.discount.MergedDiscountForwardCurve>();

  198.         if (0 == iNumFundingDepositInstrument || iNumFundingDepositInstrument !=
  199.             iNumFirstFundingDepositInstrument || iNumFundingDepositInstrument !=
  200.                 iNumSecondFundingDepositInstrument || 0 == iNumFundingFixFloat || iNumFundingFixFloat !=
  201.                     iNumFirstFundingFixFloat || iNumFundingFixFloat != iNumSecondFundingFixFloat)
  202.             return null;

  203.         org.drip.state.discount.MergedDiscountForwardCurve dcFirst =
  204.             org.drip.service.template.LatentMarketStateBuilder.FundingCurve (dtFirst, strCurrency,
  205.                 astrFundingDepositInstrumentTenor, adblFirstFundingDepositInstrument, "ForwardRate", null,
  206.                     "ForwardRate", astrFundingFixFloatTenor, adblFirstFundingFixFloat, "SwapRate",
  207.                         iLatentStateType);

  208.         org.drip.state.discount.MergedDiscountForwardCurve dcSecond =
  209.             org.drip.service.template.LatentMarketStateBuilder.FundingCurve (dtSecond, strCurrency,
  210.                 astrFundingDepositInstrumentTenor, adblSecondFundingDepositInstrument, "ForwardRate", null,
  211.                     "ForwardRate", astrFundingFixFloatTenor, adblSecondFundingFixFloat, "SwapRate",
  212.                         iLatentStateType);

  213.         org.drip.state.discount.MergedDiscountForwardCurve dcRollDown =
  214.             org.drip.service.template.LatentMarketStateBuilder.FundingCurve (dtSecond, strCurrency,
  215.                 astrFundingDepositInstrumentTenor, adblFirstFundingDepositInstrument, "ForwardRate", null,
  216.                     "ForwardRate", astrFundingFixFloatTenor, adblFirstFundingFixFloat, "SwapRate",
  217.                         iLatentStateType);

  218.         if (null == dcRollDown) return null;

  219.         mapRollDownDiscountCurve.put ("Native", dcRollDown);

  220.         for (int j = 0; j < iNumRollDownHorizon; ++j) {
  221.             org.drip.state.discount.MergedDiscountForwardCurve dcHorizonRollDown =
  222.                 org.drip.service.template.LatentMarketStateBuilder.FundingCurve (dtFirst.addTenor
  223.                     (astrRollDownHorizon[j]), strCurrency, astrFundingDepositInstrumentTenor,
  224.                         adblFirstFundingDepositInstrument, "ForwardRate", null, "ForwardRate",
  225.                             astrFundingFixFloatTenor, adblFirstFundingFixFloat, "SwapRate",
  226.                                 iLatentStateType);

  227.             if (null == dcHorizonRollDown) return null;

  228.             mapRollDownDiscountCurve.put (astrRollDownHorizon[j], dcHorizonRollDown);
  229.         }

  230.         return HorizonChangeAttribution (dcFirst, dcSecond, mapRollDownDiscountCurve, strMaturityTenor);
  231.     }

  232.     /**
  233.      * Generate the Funding Curve Horizon Metrics
  234.      *
  235.      * @param adtSpot Array of Spot
  236.      * @param iHorizonGap The Horizon Gap
  237.      * @param astrFundingDepositInstrumentTenor Array of Funding Curve Deposit Instrument Maturity Tenors
  238.      * @param aadblFundingDepositInstrumentQuote Array of Funding Curve Deposit Instrument Forward Rates
  239.      * @param astrFundingFixFloatTenor Array of Funding Curve Fix Float Instrument Maturity Tenors
  240.      * @param aadblFundingFixFloatQuote Array of Funding Curve Fix Float Instrument Swap Rates
  241.      * @param strCurrency Funding Currency
  242.      * @param strMaturityTenor Maturity Tenor
  243.      * @param astrRollDownHorizon Array of the Roll Down Horizon Tenors
  244.      * @param iLatentStateType Latent State Type
  245.      *
  246.      * @return The Funding Curve Horizon Metrics
  247.      */

  248.     public static final java.util.List<org.drip.historical.attribution.PositionChangeComponents>
  249.         HorizonChangeAttribution (
  250.             final org.drip.analytics.date.JulianDate[] adtSpot,
  251.             final int iHorizonGap,
  252.             final java.lang.String[] astrFundingDepositInstrumentTenor,
  253.             final double[][] aadblFundingDepositInstrumentQuote,
  254.             final java.lang.String[] astrFundingFixFloatTenor,
  255.             final double[][] aadblFundingFixFloatQuote,
  256.             final java.lang.String strCurrency,
  257.             final java.lang.String strMaturityTenor,
  258.             final java.lang.String[] astrRollDownHorizon,
  259.             final int iLatentStateType)
  260.     {
  261.         if (null == adtSpot || 0 >= iHorizonGap || null == aadblFundingDepositInstrumentQuote || null ==
  262.             aadblFundingFixFloatQuote)
  263.             return null;

  264.         int iNumClose = adtSpot.length;
  265.         int iNumRollDownTenor = null == astrRollDownHorizon ? 0 : astrRollDownHorizon.length;

  266.         if (0 == iNumClose || iNumClose != aadblFundingDepositInstrumentQuote.length || iNumClose !=
  267.             aadblFundingFixFloatQuote.length || 0 == iNumRollDownTenor)
  268.             return null;

  269.         java.util.List<org.drip.historical.attribution.PositionChangeComponents> lsPCC = new
  270.             java.util.ArrayList<org.drip.historical.attribution.PositionChangeComponents>();

  271.         for (int i = iHorizonGap; i < iNumClose; ++i) {
  272.             org.drip.historical.attribution.PositionChangeComponents pcc = HorizonChangeAttribution
  273.                 (adtSpot[i - iHorizonGap], adtSpot[i], astrFundingDepositInstrumentTenor,
  274.                     aadblFundingDepositInstrumentQuote[i - iHorizonGap],
  275.                         aadblFundingDepositInstrumentQuote[i], astrFundingFixFloatTenor,
  276.                             aadblFundingFixFloatQuote[i - iHorizonGap], aadblFundingFixFloatQuote[i],
  277.                                 strCurrency, strMaturityTenor, astrRollDownHorizon, iLatentStateType);

  278.             if (null != pcc) lsPCC.add (pcc);
  279.         }

  280.         return lsPCC;
  281.     }
  282. }