OvernightIndexMarksReconstitutor.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>OvernightIndexMarksReconstitutor</i> transforms the Overnight Instrument Manifest Measures (e.g.,
  79.  * Deposits and OIS) Feed Inputs into Formats appropriate for Overnight Curve Construction and Measure
  80.  * 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 OvernightIndexMarksReconstitutor {

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

  96.     public static final double s_dblScaler = 0.01;

  97.     /**
  98.      * The Standard Overnight Swap Maturity Tenors
  99.      */

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

  102.     /**
  103.      * Dump the Regularized Marks of the ISTQ Map
  104.      *
  105.      * @param strCurrency Currency
  106.      * @param mapISTQ The ISTQ Map
  107.      * @param iLatentStateType SHAPE PRESERVING/SMOOTH
  108.      *
  109.      * @return TRUE - The Regularized ISTQ Map Successfully Dumped
  110.      */

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

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

  119.         for (java.lang.String strMaturityTenor : s_astrMaturityTenor)
  120.             strHeader += "OISPROC:" + strMaturityTenor + ",";

  121.         for (java.lang.String strMaturityTenor : s_astrMaturityTenor)
  122.             strHeader += "<<OISRAW:" + strMaturityTenor + ">>,";

  123.         System.out.println (strHeader);

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

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

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

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

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

  131.             double[] adblOISQuote = istq.instrumentQuote ("OIS");

  132.             java.lang.String[] astrOISMaturityTenor = istq.instrumentTenor ("OIS");

  133.             int iNumRegularizedTenor = s_astrMaturityTenor.length;
  134.             double[] adblRegularizedOISQuote = new double[iNumRegularizedTenor];
  135.             int iNumOISQuote = null == adblOISQuote ? 0 : adblOISQuote.length;
  136.             int iNumOISTenor = null == astrOISMaturityTenor ? 0 : astrOISMaturityTenor.length;

  137.             if (0 == iNumOISQuote || iNumOISQuote != iNumOISTenor) continue;

  138.             org.drip.state.discount.MergedDiscountForwardCurve dcOvernight =
  139.                 org.drip.service.template.LatentMarketStateBuilder.OvernightCurve (dtSpot, strCurrency, null,
  140.                     null, null, null, null, null, null, null, null, null, astrOISMaturityTenor, adblOISQuote,
  141.                         "SwapRate", iLatentStateType);

  142.             org.drip.param.market.CurveSurfaceQuoteContainer csqc = new
  143.                 org.drip.param.market.CurveSurfaceQuoteContainer();

  144.             if (!csqc.setFundingState (dcOvernight)) continue;

  145.             org.drip.param.valuation.ValuationParams valParams =
  146.                 org.drip.param.valuation.ValuationParams.Spot (dtSpot.julian());

  147.             for (int i = 0; i < iNumRegularizedTenor; ++i)
  148.                 adblRegularizedOISQuote[i] = 0.;

  149.             org.drip.product.rates.FixFloatComponent[] aOIS =
  150.                 org.drip.service.template.OTCInstrumentBuilder.OISFixFloat (dtSpot, strCurrency,
  151.                     s_astrMaturityTenor, adblRegularizedOISQuote, false);

  152.             for (org.drip.product.rates.FixFloatComponent ffc : aOIS) {
  153.                 try {
  154.                     strDump += org.drip.numerical.common.FormatUtil.FormatDouble (ffc.measureValue (valParams,
  155.                         null, csqc, null, "SwapRate"), 1, 6, 1.) + ",";
  156.                 } catch (java.lang.Exception e) {
  157.                     e.printStackTrace();

  158.                     continue;
  159.                 }
  160.             }

  161.             for (int i = 0; i < iNumOISQuote; ++i)
  162.                 strDump += org.drip.numerical.common.FormatUtil.FormatDouble (adblOISQuote[i], 1, 6, 1.) + ",";

  163.             System.out.println (strDump);
  164.         }

  165.         return true;
  166.     }

  167.     /**
  168.      * Re-constitute the Horizon Quote Marks Using a Shape Preserving Re-constructor
  169.      *
  170.      * @param strCurrency Currency
  171.      * @param strMarksLocation The Location of the CSV Marks File
  172.      *
  173.      * @return The Transformed Horizon Quote Marks
  174.      */

  175.     public static final boolean ShapePreservingRegularization (
  176.         final java.lang.String strCurrency,
  177.         final java.lang.String strMarksLocation)
  178.     {
  179.         org.drip.feed.loader.CSVGrid csvGrid = org.drip.feed.loader.CSVParser.StringGrid (strMarksLocation,
  180.             false);

  181.         return null == csvGrid ? false : RegularizeMarks (strCurrency, csvGrid.groupedOrderedDouble
  182.             (s_dblScaler), org.drip.service.template.LatentMarketStateBuilder.SHAPE_PRESERVING);
  183.     }

  184.     /**
  185.      * Re-constitute the Horizon Quote Marks Using a Smooth Re-constructor
  186.      *
  187.      * @param strCurrency Currency
  188.      * @param strMarksLocation The Location of the CSV Marks File
  189.      *
  190.      * @return The Transformed Horizon Quote Marks
  191.      */

  192.     public static final boolean SmoothRegularization (
  193.         final java.lang.String strCurrency,
  194.         final java.lang.String strMarksLocation)
  195.     {
  196.         org.drip.feed.loader.CSVGrid csvGrid = org.drip.feed.loader.CSVParser.StringGrid (strMarksLocation,
  197.             false);

  198.         return null == csvGrid ? false : RegularizeMarks (strCurrency, csvGrid.groupedOrderedDouble
  199.             (s_dblScaler), org.drip.service.template.LatentMarketStateBuilder.SMOOTH);
  200.     }
  201. }