FundingCurveAPI.java

  1. package org.drip.service.state;

  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>FundingCurveAPI</i> computes the Metrics associated the Funding Curve State.
  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/state/README.md">Curve Based State Metric Generator</a></li>
  86.  *  </ul>
  87.  * <br><br>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class FundingCurveAPI {

  92.     /**
  93.      * Generate the Funding Curve Daily Metrics
  94.      *
  95.      * @param dtSpot The Spot Date
  96.      * @param astrFixFloatMaturityTenor Array of Fix Float Maturity Tenors
  97.      * @param adblFixFloatQuote Array of Fix Float Swap Rates
  98.      * @param astrInTenor Array of "In" Tenors
  99.      * @param astrForTenor Array of "For" Tenors
  100.      * @param strCurrency Funding Currency
  101.      * @param iLatentStateType Latent State Type
  102.      *
  103.      * @return The Funding Curve Daily Metrics
  104.      */

  105.     public static final org.drip.historical.state.FundingCurveMetrics DailyMetrics (
  106.         final org.drip.analytics.date.JulianDate dtSpot,
  107.         final java.lang.String[] astrFixFloatMaturityTenor,
  108.         final double[] adblFixFloatQuote,
  109.         final java.lang.String[] astrInTenor,
  110.         final java.lang.String[] astrForTenor,
  111.         final java.lang.String strCurrency,
  112.         final int iLatentStateType)
  113.     {
  114.         if (null == dtSpot || null == astrFixFloatMaturityTenor || null == adblFixFloatQuote || null ==
  115.             astrInTenor || null == astrForTenor)
  116.             return null;

  117.         int iNumInTenor = astrInTenor.length;
  118.         int iNumForTenor = astrForTenor.length;
  119.         int iNumFixFloatQuote = adblFixFloatQuote.length;
  120.         double[] adblForTenorDCF = new double[iNumForTenor];
  121.         int iNumCalibrationInstrument = astrFixFloatMaturityTenor.length;

  122.         if (0 == iNumCalibrationInstrument || iNumFixFloatQuote != iNumCalibrationInstrument || 0 ==
  123.             iNumInTenor || 0 == iNumForTenor)
  124.             return null;

  125.         for (int i = 0; i < iNumForTenor; ++i) {
  126.             try {
  127.                 adblForTenorDCF[i] = org.drip.analytics.support.Helper.TenorToYearFraction (astrForTenor[i]);
  128.             } catch (java.lang.Exception e) {
  129.                 e.printStackTrace();

  130.                 return null;
  131.             }
  132.         }

  133.         org.drip.state.discount.MergedDiscountForwardCurve dcFunding =
  134.             org.drip.service.template.LatentMarketStateBuilder.FundingCurve (dtSpot, strCurrency, null, null,
  135.                 "ForwardRate", null, "ForwardRate", astrFixFloatMaturityTenor, adblFixFloatQuote, "SwapRate",
  136.                     iLatentStateType);

  137.         if (null == dcFunding) return null;

  138.         try {
  139.             org.drip.historical.state.FundingCurveMetrics fcm = new
  140.                 org.drip.historical.state.FundingCurveMetrics (dtSpot);

  141.             for (java.lang.String strInTenor : astrInTenor) {
  142.                 org.drip.analytics.date.JulianDate dtIn = dtSpot.addTenor (strInTenor);

  143.                 for (int j = 0; j < iNumForTenor; ++j) {
  144.                     if (!fcm.addNativeForwardRate (strInTenor, astrForTenor[j], java.lang.Math.pow
  145.                         (dcFunding.df (dtIn) / dcFunding.df (dtIn.addTenor (astrForTenor[j])), 1. /
  146.                             adblForTenorDCF[j]) - 1.))
  147.                         continue;
  148.                 }
  149.             }

  150.             return fcm;
  151.         } catch (java.lang.Exception e) {
  152.             e.printStackTrace();
  153.         }

  154.         return null;
  155.     }

  156.     /**
  157.      * Generate the Funding Curve Horizon Metrics
  158.      *
  159.      * @param adtSpot Array of Spot
  160.      * @param astrFixFloatMaturityTenor Array of Fix Float Maturity Tenors
  161.      * @param aadblFixFloatQuote Array of Fix Float Swap Rates
  162.      * @param astrInTenor Array of "In" Tenors
  163.      * @param astrForTenor Array of "For" Tenors
  164.      * @param strCurrency Funding Currency
  165.      * @param iLatentStateType Latent State Type
  166.      *
  167.      * @return The Funding Curve Horizon Metrics
  168.      */

  169.     public static final java.util.Map<org.drip.analytics.date.JulianDate,
  170.         org.drip.historical.state.FundingCurveMetrics> HorizonMetrics (
  171.             final org.drip.analytics.date.JulianDate[] adtSpot,
  172.             final java.lang.String[] astrFixFloatMaturityTenor,
  173.             final double[][] aadblFixFloatQuote,
  174.             final java.lang.String[] astrInTenor,
  175.             final java.lang.String[] astrForTenor,
  176.             final java.lang.String strCurrency,
  177.             final int iLatentStateType)
  178.     {
  179.         if (null == adtSpot || null == astrFixFloatMaturityTenor || null == aadblFixFloatQuote || null ==
  180.             astrInTenor || null == astrForTenor)
  181.             return null;

  182.         int iNumClose = adtSpot.length;
  183.         int iNumInTenor = astrInTenor.length;
  184.         int iNumForTenor = astrForTenor.length;
  185.         double[] adblForTenorDCF = new double[iNumForTenor];
  186.         int iNumCalibrationInstrument = astrFixFloatMaturityTenor.length;

  187.         if (0 == iNumClose || 0 == iNumCalibrationInstrument || 0 == iNumInTenor || 0 == iNumForTenor)
  188.             return null;

  189.         for (int i = 0; i < iNumForTenor; ++i) {
  190.             try {
  191.                 adblForTenorDCF[i] = org.drip.analytics.support.Helper.TenorToYearFraction (astrForTenor[i]);
  192.             } catch (java.lang.Exception e) {
  193.                 e.printStackTrace();

  194.                 return null;
  195.             }
  196.         }

  197.         java.util.Map<org.drip.analytics.date.JulianDate, org.drip.historical.state.FundingCurveMetrics>
  198.             mapFCM = new java.util.TreeMap<org.drip.analytics.date.JulianDate,
  199.                 org.drip.historical.state.FundingCurveMetrics>();

  200.         for (int i = 0; i < iNumClose; ++i) {
  201.             org.drip.historical.state.FundingCurveMetrics fcm = null;
  202.             int iNumFixFloatQuote = null == aadblFixFloatQuote[i] ? 0 : aadblFixFloatQuote[i].length;

  203.             if (0 == iNumFixFloatQuote || iNumFixFloatQuote != iNumCalibrationInstrument) continue;

  204.             org.drip.state.discount.MergedDiscountForwardCurve dcFunding =
  205.                 org.drip.service.template.LatentMarketStateBuilder.FundingCurve (adtSpot[i], strCurrency,
  206.                     null, null, "ForwardRate", null, "ForwardRate", astrFixFloatMaturityTenor,
  207.                         aadblFixFloatQuote[i], "SwapRate", iLatentStateType);

  208.             if (null == dcFunding) continue;

  209.             try {
  210.                 fcm = new org.drip.historical.state.FundingCurveMetrics (adtSpot[i]);

  211.                 for (java.lang.String strInTenor : astrInTenor) {
  212.                     org.drip.analytics.date.JulianDate dtIn = adtSpot[i].addTenor (strInTenor);

  213.                     for (int j = 0; j < iNumForTenor; ++j) {
  214.                         if (!fcm.addNativeForwardRate (strInTenor, astrForTenor[j], java.lang.Math.pow
  215.                             (dcFunding.df (dtIn) / dcFunding.df (dtIn.addTenor (astrForTenor[j])), 1. /
  216.                                 adblForTenorDCF[j]) - 1.))
  217.                             continue;
  218.                     }
  219.                 }
  220.             } catch (java.lang.Exception e) {
  221.                 e.printStackTrace();

  222.                 continue;
  223.             }

  224.             mapFCM.put (adtSpot[i], fcm);
  225.         }

  226.         return mapFCM;
  227.     }

  228.     /**
  229.      * Generate the Funding Curve Map
  230.      *
  231.      * @param adtSpot Array of Spot
  232.      * @param astrFixFloatMaturityTenor Array of Fix Float Maturity Tenors
  233.      * @param aadblFixFloatQuote Array of Fix Float Swap Rates
  234.      * @param strCurrency Funding Currency
  235.      * @param iLatentStateType Latent State Type
  236.      *
  237.      * @return The Funding Curve Map
  238.      */

  239.     public static final java.util.Map<org.drip.analytics.date.JulianDate,
  240.         org.drip.state.discount.MergedDiscountForwardCurve> HistoricalMap (
  241.             final org.drip.analytics.date.JulianDate[] adtSpot,
  242.             final java.lang.String[] astrFixFloatMaturityTenor,
  243.             final double[][] aadblFixFloatQuote,
  244.             final java.lang.String strCurrency,
  245.             final int iLatentStateType)
  246.     {
  247.         if (null == adtSpot || null == astrFixFloatMaturityTenor || null == aadblFixFloatQuote) return null;

  248.         int iNumClose = adtSpot.length;
  249.         int iNumCalibrationInstrument = astrFixFloatMaturityTenor.length;

  250.         if (0 == iNumClose || 0 == iNumCalibrationInstrument) return null;

  251.         java.util.Map<org.drip.analytics.date.JulianDate, org.drip.state.discount.MergedDiscountForwardCurve>
  252.             mapFundingCurve = new java.util.TreeMap<org.drip.analytics.date.JulianDate,
  253.                 org.drip.state.discount.MergedDiscountForwardCurve>();

  254.         for (int i = 0; i < iNumClose; ++i) {
  255.             int iNumFixFloatQuote = null == aadblFixFloatQuote[i] ? 0 : aadblFixFloatQuote[i].length;

  256.             if (0 == iNumFixFloatQuote || iNumFixFloatQuote != iNumCalibrationInstrument) continue;

  257.             org.drip.state.discount.MergedDiscountForwardCurve dcFunding =
  258.                 org.drip.service.template.LatentMarketStateBuilder.FundingCurve (adtSpot[i], strCurrency,
  259.                     null, null, "ForwardRate", null, "ForwardRate", astrFixFloatMaturityTenor,
  260.                         aadblFixFloatQuote[i], "SwapRate", iLatentStateType);

  261.             if (null == dcFunding) continue;

  262.             mapFundingCurve.put (adtSpot[i], dcFunding);
  263.         }

  264.         return mapFundingCurve;
  265.     }
  266. }