FundingFixFloatMarksReconstitutor.java

  1. package org.drip.feed.transformer;

  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>FundingFixFloatMarksReconstitutor</i> transforms the Funding Instrument Manifest Measures (e.g.,
  79.  * Forward Rate for Deposits, Forward Rate for Futures, and Swap Rates for Fix/Float Swap) Feed Inputs into
  80.  * Formats appropriate for Funding Curve Construction and Measure Generation.
  81.  *
  82.  *  <br><br>
  83.  *  <ul>
  84.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  85.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  86.  *      <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>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/feed/transformer/README.md">Market Data Reconstitutive Feed Transformer</a></li>
  88.  *  </ul>
  89.  *
  90.  * @author Lakshmi Krishnamurthy
  91.  */

  92. public class FundingFixFloatMarksReconstitutor {

  93.     /**
  94.      * The Standard Funding Input Calibration Manifest Measure Scaler
  95.      */

  96.     public static final double s_dblScaler = 0.01;

  97.     /**
  98.      * The Standard Deposit Maturity Tenors
  99.      */

  100.     public static final java.lang.String[] s_astrDepositTenor = new java.lang.String[] {"1M", "2M", "3M",
  101.         "4M", "5M", "6M"};

  102.     /**
  103.      * The Standard Fix Float Maturity Tenors
  104.      */

  105.     public static final java.lang.String[] s_astrFixFloatTenor = new java.lang.String[] {"1Y", "2Y", "3Y",
  106.         "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y", "40Y", "50Y"};

  107.     /**
  108.      * Dump the Regularized Marks of the ISTQ Map
  109.      *
  110.      * @param strCurrency Currency
  111.      * @param mapISTQ The ISTQ Map
  112.      * @param iLatentStateType SHAPE PRESERVING/SMOOTH
  113.      *
  114.      * @return TRUE - The Regularized ISTQ Map Successfully Dumped
  115.      */

  116.     public static final boolean RegularizeMarks (
  117.         final java.lang.String strCurrency,
  118.         final java.util.Map<org.drip.analytics.date.JulianDate, org.drip.feed.loader.InstrumentSetTenorQuote>
  119.             mapISTQ,
  120.         final int iLatentStateType)
  121.     {
  122.         if (null == mapISTQ || 0 == mapISTQ.size()) return false;

  123.         java.lang.String strHeader = "Date,";

  124.         for (java.lang.String strMaturityTenor : s_astrDepositTenor)
  125.             strHeader += "DEPOSITPROC:" + strMaturityTenor + ",";

  126.         for (java.lang.String strMaturityTenor : s_astrFixFloatTenor)
  127.             strHeader += "FIXFLOATPROC:" + strMaturityTenor + ",";

  128.         for (java.lang.String strMaturityTenor : s_astrDepositTenor)
  129.             strHeader += "<<DEPOSITRAW:" + strMaturityTenor + ">>,";

  130.         for (java.lang.String strMaturityTenor : s_astrFixFloatTenor)
  131.             strHeader += "<<FIXFLOATRAW:" + strMaturityTenor + ">>,";

  132.         System.out.println (strHeader);

  133.         for (java.util.Map.Entry<org.drip.analytics.date.JulianDate,
  134.             org.drip.feed.loader.InstrumentSetTenorQuote> meISTQ : mapISTQ.entrySet()) {
  135.             if (null == meISTQ) continue;

  136.             org.drip.analytics.date.JulianDate dtSpot = meISTQ.getKey();

  137.             org.drip.feed.loader.InstrumentSetTenorQuote istq = meISTQ.getValue();

  138.             if (null == dtSpot || null == istq) continue;

  139.             java.lang.String strDump = dtSpot.toString() + ",";

  140.             double[] adblDepositQuote = istq.instrumentQuote ("DEPOSIT");

  141.             java.lang.String[] astrDepositMaturityTenor = istq.instrumentTenor ("DEPOSIT");

  142.             double[] adblFixFloatQuote = istq.instrumentQuote ("FIXFLOAT");

  143.             java.lang.String[] astrFixFloatMaturityTenor = istq.instrumentTenor ("FIXFLOAT");

  144.             int iNumDepositQuote = null == adblDepositQuote ? 0 : adblDepositQuote.length;
  145.             int iNumFixFloatQuote = null == adblFixFloatQuote ? 0 : adblFixFloatQuote.length;
  146.             int iNumDepositTenor = null == astrDepositMaturityTenor ? 0 : astrDepositMaturityTenor.length;
  147.             int iNumFixFloatTenor = null == astrFixFloatMaturityTenor ? 0 : astrFixFloatMaturityTenor.length;

  148.             if (0 == iNumFixFloatQuote || iNumDepositQuote != iNumDepositTenor || iNumFixFloatQuote !=
  149.                 iNumFixFloatTenor)
  150.                 continue;

  151.             org.drip.state.discount.MergedDiscountForwardCurve dcFunding =
  152.                 org.drip.service.template.LatentMarketStateBuilder.FundingCurve (dtSpot, strCurrency,
  153.                     astrDepositMaturityTenor, adblDepositQuote, "ForwardRate", null, "ForwardRate",
  154.                         astrFixFloatMaturityTenor, adblFixFloatQuote, "SwapRate", iLatentStateType);

  155.             org.drip.param.market.CurveSurfaceQuoteContainer csqc = new
  156.                 org.drip.param.market.CurveSurfaceQuoteContainer();

  157.             if (!csqc.setFundingState (dcFunding)) continue;

  158.             org.drip.param.valuation.ValuationParams valParams =
  159.                 org.drip.param.valuation.ValuationParams.Spot (dtSpot.julian());

  160.             org.drip.product.rates.SingleStreamComponent[] aSSCDeposit =
  161.                 org.drip.service.template.OTCInstrumentBuilder.FundingDeposit (dtSpot, strCurrency,
  162.                     s_astrDepositTenor);

  163.             for (org.drip.product.rates.SingleStreamComponent sscDeposit : aSSCDeposit) {
  164.                 try {
  165.                     strDump += org.drip.numerical.common.FormatUtil.FormatDouble (sscDeposit.measureValue
  166.                         (valParams, null, csqc, null, "ForwardRate"), 1, 6, 1.) + ",";
  167.                 } catch (java.lang.Exception e) {
  168.                     e.printStackTrace();

  169.                     continue;
  170.                 }
  171.             }

  172.             org.drip.product.rates.FixFloatComponent[] aFixFloat =
  173.                 org.drip.service.template.OTCInstrumentBuilder.FixFloatStandard (dtSpot, strCurrency, "ALL",
  174.                     s_astrFixFloatTenor, "MAIN", 0.);

  175.             for (org.drip.product.rates.FixFloatComponent ffc : aFixFloat) {
  176.                 try {
  177.                     strDump += org.drip.numerical.common.FormatUtil.FormatDouble (ffc.measureValue (valParams,
  178.                         null, csqc, null, "SwapRate"), 1, 6, 1.) + ",";
  179.                 } catch (java.lang.Exception e) {
  180.                     e.printStackTrace();

  181.                     continue;
  182.                 }
  183.             }

  184.             for (int i = 0; i < iNumFixFloatQuote; ++i)
  185.                 strDump += org.drip.numerical.common.FormatUtil.FormatDouble (adblFixFloatQuote[i], 1, 6, 1.) +
  186.                     ",";

  187.             System.out.println (strDump);
  188.         }

  189.         return true;
  190.     }

  191.     /**
  192.      * Re-constitute the Horizon Quote Marks Using a Shape Preserving Re-constructor
  193.      *
  194.      * @param strCurrency Currency
  195.      * @param strMarksLocation The Location of the CSV Marks File
  196.      *
  197.      * @return The Transformed Horizon Quote Marks
  198.      */

  199.     public static final boolean ShapePreservingRegularization (
  200.         final java.lang.String strCurrency,
  201.         final java.lang.String strMarksLocation)
  202.     {
  203.         org.drip.feed.loader.CSVGrid csvGrid = org.drip.feed.loader.CSVParser.StringGrid (strMarksLocation,
  204.             false);

  205.         return null == csvGrid ? false : RegularizeMarks (strCurrency, csvGrid.groupedOrderedDouble
  206.             (s_dblScaler), org.drip.service.template.LatentMarketStateBuilder.SHAPE_PRESERVING);
  207.     }

  208.     /**
  209.      * Re-constitute the Horizon Quote Marks Using a Smooth Re-constructor
  210.      *
  211.      * @param strCurrency Currency
  212.      * @param strMarksLocation The Location of the CSV Marks File
  213.      *
  214.      * @return The Transformed Horizon Quote Marks
  215.      */

  216.     public static final boolean SmoothRegularization (
  217.         final java.lang.String strCurrency,
  218.         final java.lang.String strMarksLocation)
  219.     {
  220.         org.drip.feed.loader.CSVGrid csvGrid = org.drip.feed.loader.CSVParser.StringGrid (strMarksLocation,
  221.             false);

  222.         return null == csvGrid ? false : RegularizeMarks (strCurrency, csvGrid.groupedOrderedDouble
  223.             (s_dblScaler), org.drip.service.template.LatentMarketStateBuilder.SMOOTH);
  224.     }
  225. }