UnifiedShapePreserving1YStart.java

  1. package org.drip.sample.fundingfeed;

  2. import java.io.BufferedWriter;
  3. import java.util.Map;

  4. import org.drip.analytics.date.JulianDate;
  5. import org.drip.feed.loader.*;
  6. import org.drip.historical.state.FundingCurveMetrics;
  7. import org.drip.numerical.common.FormatUtil;
  8. import org.drip.param.market.CurveSurfaceQuoteContainer;
  9. import org.drip.param.valuation.ValuationParams;
  10. import org.drip.product.rates.*;
  11. import org.drip.service.env.EnvManager;
  12. import org.drip.service.state.FundingCurveAPI;
  13. import org.drip.service.template.*;
  14. import org.drip.state.discount.MergedDiscountForwardCurve;

  15. /*
  16.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  17.  */

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

  79. /**
  80.  * <i>UnifiedShapePreserving1YStart</i> demonstrates the unified re-constitution and Metrics Generation.
  81.  *  
  82.  * <br><br>
  83.  *  <ul>
  84.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  85.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics Library</a></li>
  86.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/fundingfeed/README.md">Funding Marks Feed ETL</a></li>
  88.  *  </ul>
  89.  * <br><br>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class UnifiedShapePreserving1YStart {

  94.     public static final void main (
  95.         final String[] args)
  96.         throws Exception
  97.     {
  98.         EnvManager.InitEnv ("");

  99.         String strCurrency = "USD";
  100.         String strFundingMarksLocation = "C:\\DROP\\Daemons\\Feeds\\FundingMarks\\" + strCurrency + "Formatted.csv";
  101.         String strFundingMetricsLocation = "C:\\DROP\\Daemons\\Metrics\\FundingCurve\\Unified\\" + strCurrency + ".csv";

  102.         String[] astrPreFixFloatTenor = new String[] {
  103.             "1Y",
  104.             "2Y",
  105.             "3Y",
  106.             "4Y",
  107.             "5Y",
  108.             "6Y",
  109.             "7Y",
  110.             "8Y",
  111.             "9Y",
  112.             "10Y",
  113.             "11Y",
  114.             "12Y",
  115.             "15Y",
  116.             "20Y",
  117.             "25Y",
  118.             "30Y",
  119.             "40Y",
  120.             "50Y"
  121.         };
  122.         String[] astrInTenor = new String[] {
  123.             "1Y"
  124.         };
  125.         String[] astrForTenor = new String[] {
  126.             "1Y",
  127.             "2Y",
  128.             "3Y",
  129.             "4Y",
  130.             "5Y",
  131.             "6Y",
  132.             "7Y",
  133.             "8Y",
  134.             "9Y",
  135.             "10Y",
  136.             "11Y",
  137.             "12Y",
  138.             "15Y",
  139.             "20Y",
  140.             "25Y",
  141.         };
  142.         String[] astrPostFixFloatMaturityTenor = new String[] {
  143.             "1Y",
  144.             "2Y",
  145.             "3Y",
  146.             "4Y",
  147.             "5Y",
  148.             "6Y",
  149.             "7Y",
  150.             "8Y",
  151.             "9Y",
  152.             "10Y",
  153.             "11Y",
  154.             "12Y",
  155.             "15Y",
  156.             "20Y",
  157.             "25Y",
  158.             "30Y",
  159.             "40Y",
  160.             "50Y"
  161.         };

  162.         CSVGrid csvGrid = CSVParser.StringGrid (
  163.             strFundingMarksLocation,
  164.             false
  165.         );

  166.         Map<JulianDate, InstrumentSetTenorQuote> mapISTQ = csvGrid.groupedOrderedDouble (0.01);

  167.         int iNumClose = mapISTQ.size();

  168.         int iCloseDate = 0;
  169.         String strDump = "Date";
  170.         JulianDate[] adtClose = new JulianDate[iNumClose];
  171.         double[][] aadblFixFloatQuote = new double[iNumClose][18];

  172.         for (String strInTenor : astrInTenor) {
  173.             for (String strForTenor : astrForTenor)
  174.                 strDump += "," + strInTenor + strForTenor;
  175.         }

  176.         System.out.println (strDump);

  177.         BufferedWriter bwMetrics = new BufferedWriter (new java.io.FileWriter (strFundingMetricsLocation));

  178.         bwMetrics.write (strDump + "\n");

  179.         for (Map.Entry<JulianDate, InstrumentSetTenorQuote> meISTQ : mapISTQ.entrySet()) {
  180.             if (null == meISTQ) continue;

  181.             JulianDate dtSpot = meISTQ.getKey();

  182.             InstrumentSetTenorQuote istq = meISTQ.getValue();

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

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

  185.             String[] astrDepositMaturityTenor = istq.instrumentTenor ("DEPOSIT");

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

  187.             String[] astrFixFloatMaturityTenor = istq.instrumentTenor ("FIXFLOAT");

  188.             int iNumDepositQuote = null == adblDepositQuote ? 0 : adblDepositQuote.length;
  189.             int iNumFixFloatQuote = null == adblFixFloatQuote ? 0 : adblFixFloatQuote.length;
  190.             int iNumDepositTenor = null == astrDepositMaturityTenor ? 0 : astrDepositMaturityTenor.length;
  191.             int iNumFixFloatTenor = null == astrFixFloatMaturityTenor ? 0 : astrFixFloatMaturityTenor.length;

  192.             if (0 == iNumFixFloatQuote || iNumDepositQuote != iNumDepositTenor || iNumFixFloatQuote !=
  193.                 iNumFixFloatTenor)
  194.                 continue;

  195.             MergedDiscountForwardCurve dcFunding = LatentMarketStateBuilder.FundingCurve (
  196.                 dtSpot,
  197.                 strCurrency,
  198.                 astrDepositMaturityTenor,
  199.                 adblDepositQuote,
  200.                 "ForwardRate",
  201.                 null,
  202.                 "ForwardRate",
  203.                 astrFixFloatMaturityTenor,
  204.                 adblFixFloatQuote,
  205.                 "SwapRate",
  206.                 LatentMarketStateBuilder.SHAPE_PRESERVING
  207.             );

  208.             CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

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

  210.             ValuationParams valParams = ValuationParams.Spot (dtSpot.julian());

  211.             FixFloatComponent[] aFFC = OTCInstrumentBuilder.FixFloatStandard (
  212.                 dtSpot,
  213.                 strCurrency,
  214.                 "ALL",
  215.                 astrPreFixFloatTenor,
  216.                 "MAIN",
  217.                 0.
  218.             );

  219.             for (int j = 0; j < aFFC.length; ++j)
  220.                 aadblFixFloatQuote[iCloseDate][j] = aFFC[j].measureValue (
  221.                     valParams,
  222.                     null,
  223.                     csqc,
  224.                     null,
  225.                     "SwapRate"
  226.                 );

  227.             adtClose[iCloseDate] = dtSpot;

  228.             if (++iCloseDate >= iNumClose) break;
  229.         }

  230.         Map<JulianDate, FundingCurveMetrics> mapFCM = FundingCurveAPI.HorizonMetrics (
  231.             adtClose,
  232.             astrPostFixFloatMaturityTenor,
  233.             aadblFixFloatQuote,
  234.             astrInTenor,
  235.             astrForTenor,
  236.             strCurrency,
  237.             LatentMarketStateBuilder.SHAPE_PRESERVING
  238.         );

  239.         for (int i = 0; i < iNumClose; ++i) {
  240.             FundingCurveMetrics fcm = mapFCM.get (adtClose[i]);

  241.             strDump = adtClose[i].toString();

  242.             for (String strInTenor : astrInTenor) {
  243.                 for (String strForTenor : astrForTenor)
  244.                     strDump += "," + FormatUtil.FormatDouble (
  245.                         fcm.nativeForwardRate (
  246.                             strInTenor,
  247.                             strForTenor
  248.                         ), 1, 5, 100.
  249.                     );
  250.             }

  251.             System.out.println (strDump);

  252.             bwMetrics.write (strDump + "\n");
  253.         }

  254.         bwMetrics.close();

  255.         EnvManager.TerminateEnv();
  256.     }
  257. }