CreditCurveAPI.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>CreditCurveAPI</i> computes the Metrics associated the Credit 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 CreditCurveAPI {

  92.     /**
  93.      * Generate the Horizon Metrics for the Specified Inputs
  94.      *
  95.      * @param dtSpot The Spot Date
  96.      * @param astrFundingFixingMaturityTenor Array of the Funding Fixing Curve Calibration Instrument Tenors
  97.      * @param adblFundingFixingQuote Array of the Funding Fixing Curve Calibration Instrument Quotes
  98.      * @param strFullCreditIndexName The Full Credit Index Name
  99.      * @param dblCreditIndexQuotedSpread The Credit Index Quoted Spread
  100.      * @param astrForTenor Array of the "For" Tenors
  101.      *
  102.      * @return Map of the Dated Credit Curve Metrics
  103.      */

  104.     public static final org.drip.historical.state.CreditCurveMetrics DailyMetrics (
  105.         final org.drip.analytics.date.JulianDate dtSpot,
  106.         final java.lang.String[] astrFundingFixingMaturityTenor,
  107.         final double[] adblFundingFixingQuote,
  108.         final java.lang.String strFullCreditIndexName,
  109.         final double dblCreditIndexQuotedSpread,
  110.         final java.lang.String[] astrForTenor)
  111.     {
  112.         if (null == dtSpot || null == astrFundingFixingMaturityTenor || null == adblFundingFixingQuote ||
  113.             null == astrForTenor)
  114.             return null;

  115.         int iNumForTenor = astrForTenor.length;
  116.         int iNumFundingFixingInstrument = astrFundingFixingMaturityTenor.length;

  117.         if (0 == iNumFundingFixingInstrument || iNumFundingFixingInstrument != adblFundingFixingQuote.length
  118.             || 0 == iNumForTenor)
  119.             return null;

  120.         org.drip.market.otc.CreditIndexConvention cic =
  121.             org.drip.market.otc.CreditIndexConventionContainer.ConventionFromFullName
  122.                 (strFullCreditIndexName);

  123.         if (null == cic) return null;

  124.         org.drip.state.discount.MergedDiscountForwardCurve dcFundingFixing =
  125.             org.drip.service.template.LatentMarketStateBuilder.FundingCurve (dtSpot, cic.currency(), null,
  126.                 null, "ForwardRate", null, "ForwardRate", astrFundingFixingMaturityTenor,
  127.                     adblFundingFixingQuote, "SwapRate",
  128.                         org.drip.service.template.LatentMarketStateBuilder.SHAPE_PRESERVING);

  129.         if (null == dcFundingFixing) return null;

  130.         org.drip.state.credit.CreditCurve cc = org.drip.service.template.LatentMarketStateBuilder.CreditCurve
  131.             (dtSpot, new org.drip.product.definition.CreditDefaultSwap[] {cic.indexCDS()}, new double[]
  132.                 {dblCreditIndexQuotedSpread}, "FairPremium", dcFundingFixing);

  133.         if (null == cc) return null;

  134.         try {
  135.             org.drip.historical.state.CreditCurveMetrics ccm = new
  136.                 org.drip.historical.state.CreditCurveMetrics (dtSpot);

  137.             for (int j = 0; j < iNumForTenor; ++j) {
  138.                 org.drip.analytics.date.JulianDate dtFor = dtSpot.addTenor (astrForTenor[j]);

  139.                 if (null == dtFor || !ccm.addSurvivalProbability (dtFor, cc.survival (dtFor)) ||
  140.                     !ccm.addRecoveryRate (dtFor, cc.recovery (dtFor)))
  141.                     continue;
  142.             }

  143.             return ccm;
  144.         } catch (java.lang.Exception e) {
  145.             e.printStackTrace();
  146.         }

  147.         return null;
  148.     }

  149.     /**
  150.      * Generate the Horizon Metrics for the Specified Inputs
  151.      *
  152.      * @param adtSpot Array of Horizon Dates
  153.      * @param astrFundingFixingMaturityTenor Array of the Funding Fixing Curve Calibration Instrument Tenors
  154.      * @param aadblFundingFixingQuote Array of the Funding Fixing Curve Calibration Instrument Quotes
  155.      * @param astrFullCreditIndexName Array of the Full Credit Index Names
  156.      * @param adblCreditIndexQuotedSpread Array of the Credit Index Quoted Spreads
  157.      * @param astrForTenor Array of the "For" Tenors
  158.      *
  159.      * @return Map of the Dated Credit Curve Metrics
  160.      */

  161.     public static final java.util.TreeMap<org.drip.analytics.date.JulianDate,
  162.         org.drip.historical.state.CreditCurveMetrics> HorizonMetrics (
  163.             final org.drip.analytics.date.JulianDate[] adtSpot,
  164.             final java.lang.String[] astrFundingFixingMaturityTenor,
  165.             final double[][] aadblFundingFixingQuote,
  166.             final java.lang.String[] astrFullCreditIndexName,
  167.             final double[] adblCreditIndexQuotedSpread,
  168.             final java.lang.String[] astrForTenor)
  169.     {
  170.         if (null == adtSpot || null == astrFundingFixingMaturityTenor || null == aadblFundingFixingQuote ||
  171.             null == astrFullCreditIndexName || null == adblCreditIndexQuotedSpread || null == astrForTenor)
  172.             return null;

  173.         int iNumSpot = adtSpot.length;
  174.         int iNumForTenor = astrForTenor.length;
  175.         int iNumFundingFixingInstrument = astrFundingFixingMaturityTenor.length;

  176.         if (0 == iNumSpot || iNumSpot != aadblFundingFixingQuote.length || iNumSpot !=
  177.             astrFullCreditIndexName.length || iNumSpot != adblCreditIndexQuotedSpread.length || 0 ==
  178.                 iNumFundingFixingInstrument || 0 == iNumForTenor)
  179.             return null;

  180.         java.util.TreeMap<org.drip.analytics.date.JulianDate, org.drip.historical.state.CreditCurveMetrics>
  181.             mapCCM = new java.util.TreeMap<org.drip.analytics.date.JulianDate,
  182.                 org.drip.historical.state.CreditCurveMetrics>();

  183.         for (int i = 0; i < iNumSpot; ++i) {
  184.             org.drip.market.otc.CreditIndexConvention cic =
  185.                 org.drip.market.otc.CreditIndexConventionContainer.ConventionFromFullName
  186.                     (astrFullCreditIndexName[i]);

  187.             if (null == cic) continue;

  188.             org.drip.state.discount.MergedDiscountForwardCurve dcFundingFixing =
  189.                 org.drip.service.template.LatentMarketStateBuilder.FundingCurve (adtSpot[i], cic.currency(),
  190.                     null, null, "ForwardRate", null, "ForwardRate", astrFundingFixingMaturityTenor,
  191.                         aadblFundingFixingQuote[i], "SwapRate",
  192.                             org.drip.service.template.LatentMarketStateBuilder.SHAPE_PRESERVING);

  193.             if (null == dcFundingFixing) continue;

  194.             org.drip.state.credit.CreditCurve cc =
  195.                 org.drip.service.template.LatentMarketStateBuilder.CreditCurve (adtSpot[i], new
  196.                     org.drip.product.definition.CreditDefaultSwap[] {cic.indexCDS()}, new double[]
  197.                         {adblCreditIndexQuotedSpread[i]}, "FairPremium", dcFundingFixing);

  198.             if (null == cc) continue;

  199.             try {
  200.                 org.drip.historical.state.CreditCurveMetrics ccm = new
  201.                     org.drip.historical.state.CreditCurveMetrics (adtSpot[i]);

  202.                 for (int j = 0; j < iNumForTenor; ++j) {
  203.                     org.drip.analytics.date.JulianDate dtFor = adtSpot[i].addTenor (astrForTenor[j]);

  204.                     if (null == dtFor) continue;

  205.                     if (!ccm.addSurvivalProbability (dtFor, cc.survival (dtFor)) || !ccm.addRecoveryRate
  206.                         (dtFor, cc.recovery (dtFor)))
  207.                         continue;
  208.                 }

  209.                 mapCCM.put (adtSpot[i], ccm);
  210.             } catch (java.lang.Exception e) {
  211.                 e.printStackTrace();

  212.                 continue;
  213.             }
  214.         }

  215.         return mapCCM;
  216.     }
  217. }