LossQuadratureMetrics.java

  1. package org.drip.analytics.cashflow;

  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>LossPeriodCurveFactors</i> is an Implementation of the Period Class enhanced by the Loss Period
  84.  * Measures. It exports the following Functionality:
  85.  *
  86.  *  <br><br>
  87.  *  <ul>
  88.  *      <li>
  89.  *          Start/End Survival Probabilities
  90.  *      </li>
  91.  *      <li>
  92.  *          Period Effective Notional/Recovery/Discount Factor
  93.  *      </li>
  94.  *      <li>
  95.  *          Serialization into and De-serialization out of Byte Arrays
  96.  *      </li>
  97.  *  </ul>
  98.  *
  99.  *  <br><br>
  100.  *  <ul>
  101.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  102.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  103.  *      <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>
  104.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/cashflow/README.md">Unit and Composite Cash Flow Periods</a></li>
  105.  *  </ul>
  106.  *
  107.  * @author Lakshmi Krishnamurthy
  108.  */

  109. public class LossQuadratureMetrics {
  110.     private int _iEndDate = java.lang.Integer.MIN_VALUE;
  111.     private int _iStartDate = java.lang.Integer.MIN_VALUE;
  112.     private double _dblAccrualDCF = java.lang.Double.NaN;
  113.     private double _dblEffectiveDF = java.lang.Double.NaN;
  114.     private double _dblEndSurvival = java.lang.Double.NaN;
  115.     private double _dblStartSurvival = java.lang.Double.NaN;
  116.     private double _dblEffectiveNotional = java.lang.Double.NaN;
  117.     private double _dblEffectiveRecovery = java.lang.Double.NaN;

  118.     /**
  119.      * Create an Instance of the LossPeriodCurveFactors using the Period's Dates and Curves to generate the
  120.      *  Curve Measures
  121.      *
  122.      * @param iStartDate Period Start Date
  123.      * @param iEndDate Period End Date
  124.      * @param dblAccrualDCF Period's Accrual Day Count Fraction
  125.      * @param dblEffectiveNotional Period's Effective Notional
  126.      * @param dblEffectiveRecovery Period's Effective Recovery
  127.      * @param dc Discount Curve
  128.      * @param cc Credit Curve
  129.      * @param iDefaultLag Default Pay Lag
  130.      *
  131.      * @return LossPeriodCurveFactors Instance
  132.      */

  133.     public static final LossQuadratureMetrics MakeDefaultPeriod (
  134.         final int iStartDate,
  135.         final int iEndDate,
  136.         final double dblAccrualDCF,
  137.         final double dblEffectiveNotional,
  138.         final double dblEffectiveRecovery,
  139.         final org.drip.state.discount.MergedDiscountForwardCurve dc,
  140.         final org.drip.state.credit.CreditCurve cc,
  141.         final int iDefaultLag)
  142.     {
  143.         if (
  144.             !org.drip.numerical.common.NumberUtil.IsValid (dblAccrualDCF) ||
  145.             !org.drip.numerical.common.NumberUtil.IsValid (dblEffectiveNotional) ||
  146.             !org.drip.numerical.common.NumberUtil.IsValid (dblEffectiveRecovery) ||
  147.             null == dc ||
  148.             null == cc
  149.         )
  150.             return null;

  151.         try {
  152.             return new LossQuadratureMetrics (
  153.                 iStartDate,
  154.                 iEndDate,
  155.                 cc.survival (iStartDate),
  156.                 cc.survival (iEndDate),
  157.                 dblAccrualDCF,
  158.                 dblEffectiveNotional,
  159.                 dblEffectiveRecovery,
  160.                 dc.effectiveDF (
  161.                     iStartDate + iDefaultLag,
  162.                     iEndDate + iDefaultLag
  163.                 )
  164.             );
  165.         } catch (java.lang.Exception e) {
  166.             e.printStackTrace();
  167.         }

  168.         return null;
  169.     }

  170.     /**
  171.      * Create a LossPeriodCurveFactors Instance from the Period Dates and the Curve Measures
  172.      *
  173.      * @param iStartDate Period Start Date
  174.      * @param iEndDate Period End Date
  175.      * @param dblAccrualDCF Period's Accrual Day Count Fraction
  176.      * @param dblEffectiveNotional Period's Effective Notional
  177.      * @param dc Discount Curve
  178.      * @param cc Credit Curve
  179.      * @param iDefaultLag Default Pay Lag
  180.      *
  181.      * @return LossPeriodCurveFactors instance
  182.      */

  183.     public static final LossQuadratureMetrics MakeDefaultPeriod (
  184.         final int iStartDate,
  185.         final int iEndDate,
  186.         final double dblAccrualDCF,
  187.         final double dblEffectiveNotional,
  188.         final org.drip.state.discount.MergedDiscountForwardCurve dc,
  189.         final org.drip.state.credit.CreditCurve cc,
  190.         final int iDefaultLag)
  191.     {
  192.         if (
  193.             !org.drip.numerical.common.NumberUtil.IsValid (dblAccrualDCF) ||
  194.             !org.drip.numerical.common.NumberUtil.IsValid (dblEffectiveNotional) ||
  195.             null == dc ||
  196.             null == cc
  197.         )
  198.             return null;

  199.         try {
  200.             return new LossQuadratureMetrics (
  201.                 iStartDate,
  202.                 iEndDate,
  203.                 cc.survival (iStartDate),
  204.                 cc.survival (iEndDate),
  205.                 dblAccrualDCF,
  206.                 dblEffectiveNotional,
  207.                 cc.effectiveRecovery (
  208.                     iStartDate + iDefaultLag,
  209.                     iEndDate + iDefaultLag
  210.                 ),
  211.                 dc.effectiveDF (
  212.                     iStartDate + iDefaultLag,
  213.                     iEndDate + iDefaultLag
  214.                 )
  215.             );
  216.         } catch (java.lang.Exception e) {
  217.             e.printStackTrace();
  218.         }

  219.         return null;
  220.     }

  221.     /**
  222.      * LossPeriodCurveFactors Constructor
  223.      *
  224.      * @param iStartDate Period Start Date
  225.      * @param iEndDate Period End Date
  226.      * @param dblStartSurvival Period Start Survival
  227.      * @param dblEndSurvival Period End Survival
  228.      * @param dblAccrualDCF Period Accrual DCF
  229.      * @param dblEffectiveNotional Period Effective Notional
  230.      * @param dblEffectiveRecovery Period Effective Recovery
  231.      * @param dblEffectiveDF Period Effective Discount Factor
  232.      *
  233.      * @throws java.lang.Exception Thrown if inputs are invalid
  234.      */

  235.     public LossQuadratureMetrics (
  236.         final int iStartDate,
  237.         final int iEndDate,
  238.         final double dblStartSurvival,
  239.         final double dblEndSurvival,
  240.         final double dblAccrualDCF,
  241.         final double dblEffectiveNotional,
  242.         final double dblEffectiveRecovery,
  243.         final double dblEffectiveDF)
  244.         throws java.lang.Exception
  245.     {
  246.         if (
  247.             !org.drip.numerical.common.NumberUtil.IsValid (_dblStartSurvival = dblStartSurvival) ||
  248.             !org.drip.numerical.common.NumberUtil.IsValid (_dblEndSurvival = dblEndSurvival) ||
  249.             !org.drip.numerical.common.NumberUtil.IsValid (_dblAccrualDCF = dblAccrualDCF) ||
  250.             !org.drip.numerical.common.NumberUtil.IsValid (_dblEffectiveNotional = dblEffectiveNotional) ||
  251.             !org.drip.numerical.common.NumberUtil.IsValid (_dblEffectiveRecovery = dblEffectiveRecovery) ||
  252.             !org.drip.numerical.common.NumberUtil.IsValid (_dblEffectiveDF = dblEffectiveDF)
  253.         )
  254.             throw new java.lang.Exception ("LossPeriodCurveFactors Constructor => Invalid params");

  255.         _iEndDate = iEndDate;
  256.         _iStartDate = iStartDate;
  257.     }

  258.     /**
  259.      * Period Start Date
  260.      *
  261.      * @return Period Start Date
  262.      */

  263.     public int startDate()
  264.     {
  265.         return _iStartDate;
  266.     }

  267.     /**
  268.      * Survival Probability at the Period Beginning
  269.      *
  270.      * @return Survival Probability at the Period Beginning
  271.      */

  272.     public double startSurvival()
  273.     {
  274.         return _dblStartSurvival;
  275.     }

  276.     /**
  277.      * Period End Date
  278.      *
  279.      * @return Period End Date
  280.      */

  281.     public int endDate()
  282.     {
  283.         return _iEndDate;
  284.     }

  285.     /**
  286.      * Survival at the Period End
  287.      *
  288.      * @return Survival at the Period End
  289.      */

  290.     public double endSurvival()
  291.     {
  292.         return _dblEndSurvival;
  293.     }

  294.     /**
  295.      * Get the Period Effective Notional
  296.      *
  297.      * @return Period Effective Notional
  298.      */

  299.     public double effectiveNotional()
  300.     {
  301.         return _dblEffectiveNotional;
  302.     }

  303.     /**
  304.      * Get the Period Effective Recovery
  305.      *
  306.      * @return Period Effective Recovery
  307.      */

  308.     public double effectiveRecovery()
  309.     {
  310.         return _dblEffectiveRecovery;
  311.     }

  312.     /**
  313.      * Get the Period Effective Discount Factor
  314.      *
  315.      * @return Period Effective Discount Factor
  316.      */

  317.     public double effectiveDF()
  318.     {
  319.         return _dblEffectiveDF;
  320.     }

  321.     /**
  322.      * Get the Period Accrual Day Count Fraction
  323.      *
  324.      * @return Period Accrual Day Count Fraction
  325.      */

  326.     public double accrualDCF()
  327.     {
  328.         return _dblAccrualDCF;
  329.     }
  330. }