Event.java

  1. package org.drip.capital.stress;

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

  74. /**
  75.  * <i>Event</i> holds the Coordinate-Level Parameterization of a Stress Event. The References are:
  76.  *
  77.  * <br><br>
  78.  *  <ul>
  79.  *      <li>
  80.  *          Bank for International Supervision(2005): Stress Testing at Major Financial Institutions: Survey
  81.  *              Results and Practice https://www.bis.org/publ/cgfs24.htm
  82.  *      </li>
  83.  *      <li>
  84.  *          Glasserman, P. (2004): <i>Monte Carlo Methods in Financial Engineering</i> <b>Springer</b>
  85.  *      </li>
  86.  *      <li>
  87.  *          Kupiec, P. H. (2000): Stress Tests and Risk Capital <i>Risk</i> <b>2 (4)</b> 27-39
  88.  *      </li>
  89.  *  </ul>
  90.  *
  91.  *  <br><br>
  92.  *  <ul>
  93.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  94.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/CapitalAnalyticsLibrary.md">Capital Analytics</a></li>
  95.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/capital/README.md">Basel Market Risk and Operational Capital</a></li>
  96.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/capital/stress/README.md">Economic Risk Capital Stress Event Settings</a></li>
  97.  *  </ul>
  98.  *
  99.  * @author Lakshmi Krishnamurthy
  100.  */

  101. public class Event
  102. {
  103.     private org.drip.capital.stress.PnLSeries _aggregatePnLSeries = null;
  104.     private org.drip.capital.stress.EventSpecification _specification = null;
  105.     private java.util.Map<java.lang.String, org.drip.capital.stress.PnLSeries> _attachedEventPnLSeries =
  106.         null;
  107.     private java.util.Map<java.lang.String, org.drip.capital.stress.PnLSeries> _pnlSeriesDecompositionMap =
  108.         null;

  109.     /**
  110.      * Event Constructor
  111.      *
  112.      * @param specification Stress Event Specification
  113.      * @param aggregatePnLSeries Event Aggregate PnL Series
  114.      * @param pnlSeriesDecompositionMap PnL Series Decomposition Map
  115.      *
  116.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  117.      */

  118.     public Event (
  119.         final org.drip.capital.stress.EventSpecification specification,
  120.         final org.drip.capital.stress.PnLSeries aggregatePnLSeries,
  121.         final java.util.Map<java.lang.String, org.drip.capital.stress.PnLSeries> pnlSeriesDecompositionMap)
  122.         throws java.lang.Exception
  123.     {
  124.         if (null == (_specification = specification) ||
  125.             null == (_aggregatePnLSeries = aggregatePnLSeries))
  126.         {
  127.             throw new java.lang.Exception (
  128.                 "Event Constructor => Invalid inputs"
  129.             );
  130.         }

  131.         _pnlSeriesDecompositionMap = pnlSeriesDecompositionMap;
  132.     }

  133.     /**
  134.      * Retrieve the Stress Event Specification
  135.      *
  136.      * @return Stress Event Specification
  137.      */

  138.     public org.drip.capital.stress.EventSpecification specification()
  139.     {
  140.         return _specification;
  141.     }

  142.     /**
  143.      * Retrieve the Event Aggregate PnL Series
  144.      *
  145.      * @return The Event Aggregate PnL Series
  146.      */

  147.     public org.drip.capital.stress.PnLSeries aggregatePnLSeries()
  148.     {
  149.         return _aggregatePnLSeries;
  150.     }

  151.     /**
  152.      * Retrieve the Attached Event PnL Series Map
  153.      *
  154.      * @return The Attached Event PnL Series Map
  155.      */

  156.     public java.util.Map<java.lang.String, org.drip.capital.stress.PnLSeries> attachedEventPnLSeries()
  157.     {
  158.         return _attachedEventPnLSeries;
  159.     }

  160.     /**
  161.      * Retrieve the PnL Series Decomposition Map
  162.      *
  163.      * @return The PnL Series Decomposition Map
  164.      */

  165.     public java.util.Map<java.lang.String, org.drip.capital.stress.PnLSeries> pnlSeriesDecompositionMap()
  166.     {
  167.         return _pnlSeriesDecompositionMap;
  168.     }

  169.     /**
  170.      * Attach the Specified Stress Event PnL
  171.      *
  172.      * @param stressEventName The Stress Event Name
  173.      * @param stressEventPnL The Stress Event PnL
  174.      *
  175.      * @return TRUE - The Stress Event PnL successfully attached
  176.      */

  177.     public boolean attachStressEventPnL (
  178.         final java.lang.String stressEventName,
  179.         final org.drip.capital.stress.PnLSeries stressEventPnL)
  180.     {
  181.         if (null == stressEventName || stressEventName.isEmpty() ||
  182.             null == stressEventPnL)
  183.         {
  184.             return false;
  185.         }

  186.         if (null == _attachedEventPnLSeries)
  187.         {
  188.             _attachedEventPnLSeries = new
  189.                 org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.capital.stress.PnLSeries>();
  190.         }

  191.         _attachedEventPnLSeries.put (
  192.             stressEventName,
  193.             stressEventPnL
  194.         );

  195.         return true;
  196.     }

  197.     /**
  198.      * Indicate if the Named Attached Event is available
  199.      *
  200.      * @param attachedEventName The Attached Event Name
  201.      *
  202.      * @return TRUE - The Attached Event is available
  203.      */

  204.     public boolean containsAttachedEvent (
  205.         final java.lang.String attachedEventName)
  206.     {
  207.         return null != attachedEventName && !attachedEventName.isEmpty() &&
  208.             _attachedEventPnLSeries.containsKey (
  209.                 attachedEventName
  210.             );
  211.     }

  212.     /**
  213.      * Retrieve the Specified Attached Event PnL
  214.      *
  215.      * @param attachedEventName Attached Event Name
  216.      *
  217.      * @return Attached Event PnL
  218.      */

  219.     public org.drip.capital.stress.PnLSeries attachedEventPnL (
  220.         final java.lang.String attachedEventName)
  221.     {
  222.         return !containsAttachedEvent (
  223.             attachedEventName
  224.         ) ? null : _attachedEventPnLSeries.get (
  225.             attachedEventName
  226.         );
  227.     }

  228.     /**
  229.      * Generate the PnL Decomposition Map
  230.      *
  231.      * @param scaler PnL Scaler
  232.      *
  233.      * @return PnL Decomposition Map
  234.      */

  235.     public java.util.Map<java.lang.String, java.lang.Double> generatePnLDecompositionMap (
  236.         final double scaler)
  237.     {
  238.         if (null == _pnlSeriesDecompositionMap ||
  239.             !org.drip.numerical.common.NumberUtil.IsValid (
  240.                 scaler
  241.             )
  242.         )
  243.         {
  244.             return null;
  245.         }

  246.         java.util.Map<java.lang.String, java.lang.Double> pnlDecompositionMap =
  247.             new org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>();

  248.         for (java.util.Map.Entry<java.lang.String, org.drip.capital.stress.PnLSeries>
  249.             pnlSeriesDecompositionEntry : _pnlSeriesDecompositionMap.entrySet())
  250.         {
  251.             pnlDecompositionMap.put (
  252.                 pnlSeriesDecompositionEntry.getKey(),
  253.                 scaler * pnlSeriesDecompositionEntry.getValue().composite()
  254.             );
  255.         }

  256.         return pnlDecompositionMap;
  257.     }

  258.     /**
  259.      * Generate a Stress Event Incidence of a given Type
  260.      *
  261.      * @param stressEventType The Stress Event Type
  262.      *
  263.      * @return The Generated Stress Event Incidence
  264.      */

  265.     public org.drip.capital.simulation.StressEventIncidence generateIncidence (
  266.         final java.lang.String stressEventType)
  267.     {
  268.         try
  269.         {
  270.             return new org.drip.capital.simulation.StressEventIncidence (
  271.                 _specification.name(),
  272.                 stressEventType,
  273.                 _aggregatePnLSeries.composite(),
  274.                 generatePnLDecompositionMap (
  275.                     1.
  276.                 )
  277.             );
  278.         }
  279.         catch (java.lang.Exception e)
  280.         {
  281.             e.printStackTrace();
  282.         }

  283.         return null;
  284.     }

  285.     /**
  286.      * Generate a Stress Event Ensemble of a given Type
  287.      *
  288.      * @param parentStressEventType The Parent Stress Event Type
  289.      * @param childStressEventType The Child Stress Event Type
  290.      *
  291.      * @return The Generated Stress Event Ensemble
  292.      */

  293.     public org.drip.capital.simulation.StressEventIncidenceEnsemble generateEnsemble (
  294.         final java.lang.String parentStressEventType,
  295.         final java.lang.String childStressEventType)
  296.     {
  297.         org.drip.capital.simulation.StressEventIncidenceEnsemble stressEventIncidenceEnsemble = new
  298.             org.drip.capital.simulation.StressEventIncidenceEnsemble();

  299.         try
  300.         {
  301.             if (!stressEventIncidenceEnsemble.addStressEventIncidence (
  302.                 new org.drip.capital.simulation.StressEventIncidence (
  303.                     _specification.name(),
  304.                     parentStressEventType,
  305.                     _aggregatePnLSeries.composite(),
  306.                     generatePnLDecompositionMap (
  307.                         1.
  308.                     )
  309.                 )
  310.             ))
  311.             {
  312.                 return null;
  313.             }

  314.             if (null != _attachedEventPnLSeries && 0 != _attachedEventPnLSeries.size())
  315.             {
  316.                 for (java.util.Map.Entry<java.lang.String, org.drip.capital.stress.PnLSeries>
  317.                     attachedEventPnLSeriesEntry : _attachedEventPnLSeries.entrySet())
  318.                 {
  319.                     if (!stressEventIncidenceEnsemble.addStressEventIncidence (
  320.                         new org.drip.capital.simulation.StressEventIncidence (
  321.                             attachedEventPnLSeriesEntry.getKey(),
  322.                             childStressEventType,
  323.                             attachedEventPnLSeriesEntry.getValue().composite(),
  324.                             null
  325.                         )
  326.                     ))
  327.                     {
  328.                         return null;
  329.                     }
  330.                 }
  331.             }

  332.             return stressEventIncidenceEnsemble;
  333.         }
  334.         catch (java.lang.Exception e)
  335.         {
  336.             e.printStackTrace();
  337.         }

  338.         return null;
  339.     }
  340. }