GovvieTreasuryMarksReconstitutor.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>GovvieTreasuryMarksReconstitutor</i> transforms the Treasury Marks (e.g., Yield) Feed Inputs into
  79.  * Formats appropriate for Govvie Curve Construction and Measure Generation.
  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/transformer/README.md">Market Data Reconstitutive Feed Transformer</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class GovvieTreasuryMarksReconstitutor {

  92.     /**
  93.      * The Standard Treasury Input Calibration Manifest Measure
  94.      */

  95.     public static final java.lang.String s_strCalibrationMeasure = "Yield";

  96.     /**
  97.      * The Standard Treasury Input Calibration Manifest Measure Scaler
  98.      */

  99.     public static final double s_dblScaler = 0.01;

  100.     /**
  101.      * The Standard Treasury Market Yield Re-constitution Benchmark Tenors
  102.      */

  103.     public static final java.lang.String[] s_astrOutputBenchmarkTenor = new java.lang.String[] {
  104.         "2Y", "3Y", "4Y", "5Y", "7Y", "10Y", "20Y", "30Y"
  105.     };

  106.     /**
  107.      * Re-constitute the Horizon Benchmark Marks
  108.      *
  109.      * @param strTreasuryType The Treasury Type
  110.      * @param mapClosingMarks Map of the Dates Closing Marks
  111.      * @param strManifestMeasure The Govvie Curve Calibration Manifest Measure
  112.      * @param astrOutputBenchmarkTenor Tenors of the Output Benchmark Desired
  113.      *
  114.      * @return The Transformed Horizon Benchmark Yield
  115.      */

  116.     public static final boolean RegularizeBenchmarkMarks (
  117.         final java.lang.String strTreasuryType,
  118.         final java.util.Map<org.drip.analytics.date.JulianDate, java.util.Map<java.lang.Double,
  119.             java.lang.Double>> mapClosingMarks,
  120.         final java.lang.String strManifestMeasure,
  121.         final java.lang.String[] astrOutputBenchmarkTenor)
  122.     {
  123.         if (null == mapClosingMarks || 0 == mapClosingMarks.size() || null == astrOutputBenchmarkTenor)
  124.             return false;

  125.         java.lang.String strHeader = "Date";
  126.         int iNumOutputBenchmark = astrOutputBenchmarkTenor.length;

  127.         if (0 == iNumOutputBenchmark) return false;

  128.         for (int i = 0; i < iNumOutputBenchmark; ++i)
  129.             strHeader += "," + astrOutputBenchmarkTenor[i];

  130.         for (int i = 0; i < iNumOutputBenchmark; ++i)
  131.             strHeader += ",<<INPUT::" + astrOutputBenchmarkTenor[i] + ">>";

  132.         System.out.println (strHeader);

  133.         for (java.util.Map.Entry<org.drip.analytics.date.JulianDate, java.util.Map<java.lang.Double,
  134.             java.lang.Double>> meClosingMarks : mapClosingMarks.entrySet()) {
  135.             org.drip.analytics.date.JulianDate dtSpot = meClosingMarks.getKey();

  136.             java.util.Map<java.lang.Double, java.lang.Double> mapKeyMarks = meClosingMarks.getValue();

  137.             int iNumInputBenchmark = mapKeyMarks.size();

  138.             int i = 0;
  139.             double[] adblInputTreasuryYield = new double[iNumInputBenchmark];
  140.             org.drip.analytics.date.JulianDate[] adtInputTreasuryMaturity = new
  141.                 org.drip.analytics.date.JulianDate[iNumInputBenchmark];
  142.             org.drip.analytics.date.JulianDate[] adtInputTreasuryEffective = new
  143.                 org.drip.analytics.date.JulianDate[iNumInputBenchmark];
  144.             org.drip.product.credit.BondComponent[] aInputBenchmarkTreasury = new
  145.                 org.drip.product.credit.BondComponent[iNumInputBenchmark];
  146.             org.drip.product.credit.BondComponent[] aOutputBenchmarkTreasury = new
  147.                 org.drip.product.credit.BondComponent[iNumOutputBenchmark];

  148.             for (java.util.Map.Entry<java.lang.Double, java.lang.Double> meKeyMarks : mapKeyMarks.entrySet())
  149.             {
  150.                 double dblYear = meKeyMarks.getKey();

  151.                 if (null == (aInputBenchmarkTreasury[i] = org.drip.service.template.TreasuryBuilder.FromCode
  152.                     (strTreasuryType, adtInputTreasuryEffective[i] = dtSpot, adtInputTreasuryMaturity[i] =
  153.                         dtSpot.addYears ((int) dblYear), adblInputTreasuryYield[i] = meKeyMarks.getValue())))
  154.                     return false;

  155.                 ++i;
  156.             }

  157.             for (int j = 0; j < iNumOutputBenchmark; ++j) {
  158.                 if (null == (aOutputBenchmarkTreasury[j] = org.drip.service.template.TreasuryBuilder.FromCode
  159.                     (strTreasuryType, dtSpot, dtSpot.addTenor (astrOutputBenchmarkTenor[j]), 0.01)))
  160.                     return false;
  161.             }

  162.             org.drip.state.govvie.GovvieCurve gc =
  163.                 org.drip.service.template.LatentMarketStateBuilder.ShapePreservingGovvieCurve
  164.                     (strTreasuryType, dtSpot, adtInputTreasuryEffective, adtInputTreasuryMaturity,
  165.                         adblInputTreasuryYield, adblInputTreasuryYield, strManifestMeasure);

  166.             java.lang.String strCleansedInput = "" + dtSpot;

  167.             org.drip.param.valuation.ValuationParams valParamsSpot =
  168.                 org.drip.param.valuation.ValuationParams.Spot (dtSpot.julian());

  169.             for (int j = 0; j < iNumOutputBenchmark; ++j) {
  170.                 try {
  171.                     strCleansedInput += "," + org.drip.numerical.common.FormatUtil.FormatDouble
  172.                         (aOutputBenchmarkTreasury[j].yieldFromPrice (valParamsSpot, null, null,
  173.                             aOutputBenchmarkTreasury[j].priceFromYield (valParamsSpot, null, null, gc.yield
  174.                                 (aOutputBenchmarkTreasury[j].maturityDate()))), 1, 6, 1.);
  175.                 } catch (java.lang.Exception e) {
  176.                     // e.printStackTrace();

  177.                     continue;
  178.                 }
  179.             }

  180.             for (int j = 0; j < iNumInputBenchmark; ++j)
  181.                 strCleansedInput += "," + org.drip.numerical.common.FormatUtil.FormatDouble
  182.                     (adblInputTreasuryYield[j], 1, 6, 1.);

  183.             System.out.println (strCleansedInput);
  184.         }

  185.         return true;
  186.     }

  187.     /**
  188.      * Re-constitute the Horizon Benchmark Marks
  189.      *
  190.      * @param strTreasuryType The Treasury Type
  191.      * @param mapClosingMarks Map of the Dates Closing Marks
  192.      *
  193.      * @return The Transformed Horizon Benchmark Yield
  194.      */

  195.     public static final boolean RegularizeBenchmarkMarks (
  196.         final java.lang.String strTreasuryType,
  197.         final java.util.Map<org.drip.analytics.date.JulianDate, java.util.Map<java.lang.Double,
  198.             java.lang.Double>> mapClosingMarks)
  199.     {
  200.         return RegularizeBenchmarkMarks (strTreasuryType, mapClosingMarks, s_strCalibrationMeasure,
  201.             s_astrOutputBenchmarkTenor);
  202.     }

  203.     /**
  204.      * Re-constitute the Horizon Benchmark Marks
  205.      *
  206.      * @param strMarksLocation The Location of the CSV Marks File
  207.      * @param strTreasuryType The Treasury Type
  208.      *
  209.      * @return The Transformed Horizon Benchmark Yield
  210.      */

  211.     public static final boolean RegularizeBenchmarkMarks (
  212.         final java.lang.String strMarksLocation,
  213.         final java.lang.String strTreasuryType)
  214.     {
  215.         org.drip.feed.loader.CSVGrid csvGrid = org.drip.feed.loader.CSVParser.StringGrid (strMarksLocation,
  216.             false);

  217.         return null == csvGrid ? false : RegularizeBenchmarkMarks (strTreasuryType, csvGrid.doubleMap
  218.             (s_dblScaler), s_strCalibrationMeasure, s_astrOutputBenchmarkTenor);
  219.     }
  220. }