LossQuadratureGenerator.java

  1. package org.drip.analytics.support;

  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.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2013 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2012 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2011 Lakshmi Krishnamurthy
  16.  *
  17.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  18.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  19.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  20.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  21.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  22.  *      and computational support.
  23.  *  
  24.  *      https://lakshmidrip.github.io/DROP/
  25.  *  
  26.  *  DROP is composed of three modules:
  27.  *  
  28.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  29.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  30.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  31.  *
  32.  *  DROP Product Core implements libraries for the following:
  33.  *  - Fixed Income Analytics
  34.  *  - Loan Analytics
  35.  *  - Transaction Cost Analytics
  36.  *
  37.  *  DROP Portfolio Core implements libraries for the following:
  38.  *  - Asset Allocation Analytics
  39.  *  - Asset Liability Management Analytics
  40.  *  - Capital Estimation Analytics
  41.  *  - Exposure Analytics
  42.  *  - Margin Analytics
  43.  *  - XVA Analytics
  44.  *
  45.  *  DROP Computational Core implements libraries for the following:
  46.  *  - Algorithm Support
  47.  *  - Computation Support
  48.  *  - Function Analysis
  49.  *  - Model Validation
  50.  *  - Numerical Analysis
  51.  *  - Numerical Optimizer
  52.  *  - Spline Builder
  53.  *  - Statistical Learning
  54.  *
  55.  *  Documentation for DROP is Spread Over:
  56.  *
  57.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  58.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  59.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  60.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  61.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  62.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  63.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  64.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  65.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  66.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  67.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  68.  *
  69.  *  Licensed under the Apache License, Version 2.0 (the "License");
  70.  *      you may not use this file except in compliance with the License.
  71.  *  
  72.  *  You may obtain a copy of the License at
  73.  *      http://www.apache.org/licenses/LICENSE-2.0
  74.  *  
  75.  *  Unless required by applicable law or agreed to in writing, software
  76.  *      distributed under the License is distributed on an "AS IS" BASIS,
  77.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  78.  *  
  79.  *  See the License for the specific language governing permissions and
  80.  *      limitations under the License.
  81.  */

  82. /**
  83.  * <i>LossQuadratureGenerator</i> generates the decomposed Integrand Quadrature for the Loss Steps.
  84.  *
  85.  *  <br><br>
  86.  *  <ul>
  87.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  88.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  89.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/README.md">Date, Cash Flow, and Cash Flow Period Measure Generation Utilities</a></li>
  90.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/support/README.md">Assorted Support and Helper Utilities</a></li>
  91.  *  </ul>
  92.  *
  93.  * @author Lakshmi Krishnamurthy
  94.  */

  95. public class LossQuadratureGenerator {

  96.     /**
  97.      * Generate the Set of Loss Quadrature Metrics from the Day Step Loss Periods
  98.      *
  99.      * @param comp Component for which the measures are to be generated
  100.      * @param valParams ValuationParams from which the periods are generated
  101.      * @param period The enveloping coupon period
  102.      * @param iWorkoutDate Date representing the absolute end of all the generated periods
  103.      * @param iPeriodUnit Day Step Size Unit of the generated Loss Quadrature Periods
  104.      * @param csqs The Market Parameters Curves/Quotes
  105.      *  
  106.      * @return List of the generated LossQuadratureMetrics
  107.      */

  108.     public static final java.util.List<org.drip.analytics.cashflow.LossQuadratureMetrics>
  109.         GenerateDayStepLossPeriods (
  110.             final org.drip.product.definition.CreditComponent comp,
  111.             final org.drip.param.valuation.ValuationParams valParams,
  112.             final org.drip.analytics.cashflow.CompositePeriod period,
  113.             final int iWorkoutDate,
  114.             final int iPeriodUnit,
  115.             final org.drip.param.market.CurveSurfaceQuoteContainer csqs)
  116.     {
  117.         if (null == comp || null == valParams || null == period || null == csqs) return null;

  118.         org.drip.state.discount.MergedDiscountForwardCurve dc = csqs.fundingState
  119.             (org.drip.state.identifier.FundingLabel.Standard (comp.payCurrency()));

  120.         if (null == dc) return null;

  121.         org.drip.state.credit.CreditCurve cc = csqs.creditState (comp.creditLabel());

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

  123.         int iLossPayLag = comp.creditValuationParams().lossPayLag();

  124.         int iSubPeriodStartDate = period.startDate();

  125.         if (iSubPeriodStartDate > iWorkoutDate) return null;

  126.         int iPeriodEndDate = period.endDate();

  127.         int iValueDate = valParams.valueDate();

  128.         boolean bPeriodDone = false;
  129.         iPeriodEndDate = iPeriodEndDate < iWorkoutDate ? iPeriodEndDate : iWorkoutDate;
  130.         iSubPeriodStartDate = iSubPeriodStartDate < iValueDate ? iValueDate : iSubPeriodStartDate;

  131.         java.util.List<org.drip.analytics.cashflow.LossQuadratureMetrics> sLP = new
  132.             java.util.ArrayList<org.drip.analytics.cashflow.LossQuadratureMetrics>();

  133.         while (!bPeriodDone) {
  134.             int iSubPeriodEndDate = iSubPeriodStartDate + iPeriodUnit;

  135.             if (iSubPeriodEndDate < iValueDate) return null;

  136.             if (iSubPeriodEndDate >= iPeriodEndDate) {
  137.                 bPeriodDone = true;
  138.                 iSubPeriodEndDate = iPeriodEndDate;
  139.             }

  140.             try {
  141.                 org.drip.analytics.cashflow.LossQuadratureMetrics lp =
  142.                     org.drip.analytics.cashflow.LossQuadratureMetrics.MakeDefaultPeriod (iSubPeriodStartDate,
  143.                         iSubPeriodEndDate, period.accrualDCF ((iSubPeriodStartDate + iSubPeriodEndDate) / 2),
  144.                             comp.notional (iSubPeriodStartDate, iSubPeriodEndDate), comp.recovery
  145.                                 (iSubPeriodStartDate, iSubPeriodEndDate, cc), dc, cc, iLossPayLag);

  146.                 if (null != lp) sLP.add (lp);
  147.             } catch (java.lang.Exception e) {
  148.                 e.printStackTrace();

  149.                 return null;
  150.             }

  151.             iSubPeriodStartDate = iSubPeriodEndDate;
  152.         }

  153.         return sLP;
  154.     }

  155.     /**
  156.      * Generate the Set of Loss Quadrature Metrics from the Day Step Loss Periods
  157.      *
  158.      * @param comp Component for which the measures are to be generated
  159.      * @param valParams ValuationParams from which the periods are generated
  160.      * @param period The enveloping coupon period
  161.      * @param iWorkoutDate The absolute end of all the generated periods
  162.      * @param iPeriodUnit Loss Grid Size Unit of the generated Loss Quadrature Periods
  163.      * @param csqs The Market Parameters Curves/Quotes
  164.      *  
  165.      * @return List of the generated LossQuadratureMetrics
  166.      */

  167.     public static final java.util.List<org.drip.analytics.cashflow.LossQuadratureMetrics>
  168.         GeneratePeriodUnitLossPeriods (
  169.             final org.drip.product.definition.CreditComponent comp,
  170.             final org.drip.param.valuation.ValuationParams valParams,
  171.             final org.drip.analytics.cashflow.CompositePeriod period,
  172.             final int iWorkoutDate,
  173.             final int iPeriodUnit,
  174.             final org.drip.param.market.CurveSurfaceQuoteContainer csqs)
  175.     {
  176.         if (null == comp || null == valParams || null == period || null == csqs) return null;

  177.         org.drip.state.discount.MergedDiscountForwardCurve dc = csqs.fundingState
  178.             (org.drip.state.identifier.FundingLabel.Standard (comp.payCurrency()));

  179.         if (null == dc) return null;

  180.         org.drip.state.credit.CreditCurve cc = csqs.creditState (comp.creditLabel());

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

  182.         int iValueDate = valParams.valueDate();

  183.         int iPeriodEndDate = period.endDate();

  184.         int iSubPeriodStartDate = period.startDate();

  185.         boolean bPeriodDone = false;
  186.         iPeriodEndDate = iPeriodEndDate < iWorkoutDate ? iPeriodEndDate : iWorkoutDate;
  187.         iSubPeriodStartDate = iSubPeriodStartDate < iValueDate ? iValueDate : iSubPeriodStartDate;
  188.         int iDayStep = (iPeriodEndDate - iSubPeriodStartDate) / iPeriodUnit;

  189.         if (iSubPeriodStartDate > iWorkoutDate || iPeriodEndDate < iValueDate) return null;

  190.         if (iDayStep < org.drip.param.pricer.CreditPricerParams.PERIOD_DAY_STEPS_MINIMUM)
  191.             iDayStep = org.drip.param.pricer.CreditPricerParams.PERIOD_DAY_STEPS_MINIMUM;

  192.         int iLossPayLag = comp.creditValuationParams().lossPayLag();

  193.         java.util.List<org.drip.analytics.cashflow.LossQuadratureMetrics> sLP = new
  194.             java.util.ArrayList<org.drip.analytics.cashflow.LossQuadratureMetrics>();

  195.         while (!bPeriodDone) {
  196.             int iSubPeriodEndDate = iSubPeriodStartDate + iDayStep;

  197.             if (iSubPeriodEndDate < iValueDate) return null;

  198.             try {
  199.                 if (iSubPeriodEndDate >= iPeriodEndDate) {
  200.                     bPeriodDone = true;
  201.                     iSubPeriodEndDate = iPeriodEndDate;
  202.                 }

  203.                 org.drip.analytics.cashflow.LossQuadratureMetrics lp =
  204.                     org.drip.analytics.cashflow.LossQuadratureMetrics.MakeDefaultPeriod (iSubPeriodStartDate,
  205.                         iSubPeriodEndDate, period.accrualDCF ((iSubPeriodStartDate + iSubPeriodEndDate) / 2),
  206.                             comp.notional (iSubPeriodStartDate, iSubPeriodEndDate), comp.recovery
  207.                                 (iSubPeriodStartDate, iSubPeriodEndDate, cc),  dc, cc, iLossPayLag);

  208.                 if (null != lp) sLP.add (lp);
  209.             } catch (java.lang.Exception e) {
  210.                 e.printStackTrace();

  211.                 return null;
  212.             }

  213.             iSubPeriodStartDate = iSubPeriodEndDate;
  214.         }

  215.         return sLP;
  216.     }


  217.     /**
  218.      * Generate the Set of Loss Quadrature Metrics from the Day Step Loss Periods
  219.      *
  220.      * @param comp Component for which the measures are to be generated
  221.      * @param valParams ValuationParams from which the periods are generated
  222.      * @param period The Enveloping Coupon period
  223.      * @param iWorkoutDate The Absolute End of all the generated periods
  224.      * @param csqs The Market Parameters Curves/Quotes
  225.      *  
  226.      * @return List of the generated LossQuadratureMetrics
  227.      */

  228.     public static final java.util.List<org.drip.analytics.cashflow.LossQuadratureMetrics>
  229.         GenerateWholeLossPeriods (
  230.             final org.drip.product.definition.CreditComponent comp,
  231.             final org.drip.param.valuation.ValuationParams valParams,
  232.             final org.drip.analytics.cashflow.CompositePeriod period,
  233.             final int iWorkoutDate,
  234.             final org.drip.param.market.CurveSurfaceQuoteContainer csqs)
  235.     {
  236.         if (null == comp || null == valParams || null == period || null == csqs) return null;

  237.         org.drip.state.discount.MergedDiscountForwardCurve dc = csqs.fundingState
  238.             (org.drip.state.identifier.FundingLabel.Standard (comp.payCurrency()));

  239.         if (null == dc) return null;

  240.         org.drip.state.credit.CreditCurve cc = csqs.creditState (comp.creditLabel());

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

  242.         int iPeriodStartDate = period.startDate();

  243.         if (iPeriodStartDate > iWorkoutDate) return null;

  244.         int iPeriodEndDate = period.endDate();

  245.         int iValueDate = valParams.valueDate();

  246.         iPeriodStartDate = iPeriodStartDate < iValueDate ? iValueDate : iPeriodStartDate;
  247.         iPeriodEndDate = iPeriodEndDate < iWorkoutDate ? iPeriodEndDate : iWorkoutDate;

  248.         int iLossPayLag = comp.creditValuationParams().lossPayLag();

  249.         java.util.List<org.drip.analytics.cashflow.LossQuadratureMetrics> sLP = new
  250.             java.util.ArrayList<org.drip.analytics.cashflow.LossQuadratureMetrics>();

  251.         try {
  252.             org.drip.analytics.cashflow.LossQuadratureMetrics lp =
  253.                 org.drip.analytics.cashflow.LossQuadratureMetrics.MakeDefaultPeriod (iPeriodStartDate,
  254.                     iPeriodEndDate, period.accrualDCF ((iPeriodStartDate + iPeriodEndDate) / 2),
  255.                         comp.notional (iPeriodStartDate, iPeriodEndDate), comp.recovery (iPeriodStartDate,
  256.                             iPeriodEndDate, cc), dc, cc, iLossPayLag);

  257.             if (null != lp) sLP.add (lp);
  258.         } catch (java.lang.Exception e) {
  259.             e.printStackTrace();

  260.             return null;
  261.         }

  262.         return sLP;
  263.     }
  264. }