SystemicEventContainer.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>SystemicEventContainer</i> contains the Scenario Stress Events' Specifications of the Systemic Stress
  76.  *  Scenario Event Type that belong inside of a single Coordinate. The References are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Bank for International Supervision(2005): Stress Testing at Major Financial Institutions: Survey
  82.  *              Results and Practice https://www.bis.org/publ/cgfs24.htm
  83.  *      </li>
  84.  *      <li>
  85.  *          Glasserman, P. (2004): <i>Monte Carlo Methods in Financial Engineering</i> <b>Springer</b>
  86.  *      </li>
  87.  *      <li>
  88.  *          Kupiec, P. H. (2000): Stress Tests and Risk Capital <i>Risk</i> <b>2 (4)</b> 27-39
  89.  *      </li>
  90.  *  </ul>
  91.  *
  92.  *  <br><br>
  93.  *  <ul>
  94.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  95.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/CapitalAnalyticsLibrary.md">Capital Analytics</a></li>
  96.  *      <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>
  97.  *      <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>
  98.  *  </ul>
  99.  *
  100.  * @author Lakshmi Krishnamurthy
  101.  */

  102. public class SystemicEventContainer
  103. {
  104.     private org.drip.capital.stress.EventProbabilityLadder _eventProbabilityLadder =
  105.         new org.drip.capital.stress.EventProbabilityLadder();

  106.     private java.util.Map<java.lang.String, org.drip.capital.stress.Event> _eventMap =
  107.         new org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.capital.stress.Event>();

  108.     /**
  109.      * Empty SystemicEventContainer Constructor
  110.      */

  111.     public SystemicEventContainer()
  112.     {
  113.     }

  114.     /**
  115.      * Retrieve the Stress Event Type
  116.      *
  117.      * @return The Stress Event Type
  118.      */

  119.     public java.lang.String eventType()
  120.     {
  121.         return org.drip.capital.definition.StressScenarioType.SYSTEMIC;
  122.     }

  123.     /**
  124.      * Add the Specified Stress Event
  125.      *
  126.      * @param event The Stress Event
  127.      *
  128.      * @return TRUE - The Stress Event successfully added
  129.      */

  130.     public boolean addEvent (
  131.         final org.drip.capital.stress.Event event)
  132.     {
  133.         if (null == event)
  134.         {
  135.             return false;
  136.         }

  137.         _eventMap.put (
  138.             event.specification().name(),
  139.             event
  140.         );

  141.         return _eventProbabilityLadder.addEvent (
  142.             event
  143.         );
  144.     }

  145.     /**
  146.      * Check if the Stress Event Exists
  147.      *
  148.      * @param eventName The Stress Event Name
  149.      *
  150.      * @return TRUE - The Stress Event exists
  151.      */

  152.     public boolean containsEvent (
  153.         final java.lang.String eventName)
  154.     {
  155.         return null != eventName && !eventName.isEmpty() && _eventMap.containsKey (
  156.             eventName
  157.         );
  158.     }

  159.     /**
  160.      * Retrieve the Stress Event
  161.      *
  162.      * @param eventName The Stress Event Name
  163.      *
  164.      * @return The Stress Event
  165.      */

  166.     public org.drip.capital.stress.Event event (
  167.         final java.lang.String eventName)
  168.     {
  169.         return containsEvent (
  170.             eventName
  171.         ) ? _eventMap.get (
  172.             eventName
  173.         ) : null;
  174.     }

  175.     /**
  176.      * Retrieve the Stress Event Set
  177.      *
  178.      * @return The Stress Event Set
  179.      */

  180.     public java.util.Set<java.lang.String> eventSet()
  181.     {
  182.         return _eventMap.keySet();
  183.     }

  184.     /**
  185.      * Realize a Stress Event Incidence Ensemble
  186.      *
  187.      * @param stressPnLScaler The Stress PnL Scaler
  188.      * @param randomEventIndicator Random Stress Event Indicator
  189.      *
  190.      * @return Realized Stress Event Incidence Ensemble
  191.      */

  192.     public org.drip.capital.simulation.StressEventIncidenceEnsemble realizeIncidenceEnsemble (
  193.         final double stressPnLScaler,
  194.         final double randomEventIndicator)
  195.     {
  196.         if (!org.drip.numerical.common.NumberUtil.IsValid (
  197.             stressPnLScaler
  198.         ))
  199.         {
  200.             return null;
  201.         }

  202.         java.lang.String gsstEventName = _eventProbabilityLadder.realizeEvent (
  203.             randomEventIndicator
  204.         );

  205.         if (org.drip.capital.stress.EventProbabilityLadder.NO_REALIZATION.equalsIgnoreCase (
  206.             gsstEventName
  207.         ))
  208.         {
  209.             return null;
  210.         }

  211.         org.drip.capital.simulation.StressEventIncidenceEnsemble stressEventIncidenceEnsemble = new
  212.             org.drip.capital.simulation.StressEventIncidenceEnsemble();

  213.         org.drip.capital.stress.Event event = _eventMap.get (
  214.             gsstEventName
  215.         );

  216.         try
  217.         {
  218.             if (!stressEventIncidenceEnsemble.addStressEventIncidence (
  219.                 new org.drip.capital.simulation.StressEventIncidence (
  220.                     gsstEventName,
  221.                     org.drip.capital.definition.StressScenarioType.SYSTEMIC,
  222.                     stressPnLScaler * event.aggregatePnLSeries().composite(),
  223.                     event.generatePnLDecompositionMap (
  224.                         stressPnLScaler
  225.                     )
  226.                 )
  227.             ))
  228.             {
  229.                 return null;
  230.             }
  231.         }
  232.         catch (java.lang.Exception e)
  233.         {
  234.             e.printStackTrace();

  235.             return null;
  236.         }

  237.         java.util.Map<java.lang.String, org.drip.capital.stress.PnLSeries> attachedEventPnLMap =
  238.             event.attachedEventPnLSeries();

  239.         if (null == attachedEventPnLMap || 0 == attachedEventPnLMap.size())
  240.         {
  241.             return stressEventIncidenceEnsemble;
  242.         }

  243.         for (java.util.Map.Entry<java.lang.String, org.drip.capital.stress.PnLSeries> attachedEventPnLEntry
  244.             : attachedEventPnLMap.entrySet())
  245.         {
  246.             try
  247.             {
  248.                 if (!stressEventIncidenceEnsemble.addStressEventIncidence (
  249.                     new org.drip.capital.simulation.StressEventIncidence (
  250.                         gsstEventName + "::" + attachedEventPnLEntry.getKey(),
  251.                         org.drip.capital.definition.StressScenarioType.CORRELATED,
  252.                         stressPnLScaler * attachedEventPnLEntry.getValue().composite(),
  253.                         null
  254.                     )
  255.                 ))
  256.                 {
  257.                     return null;
  258.                 }
  259.             }
  260.             catch (java.lang.Exception e)
  261.             {
  262.                 e.printStackTrace();

  263.                 return null;
  264.             }
  265.         }

  266.         return stressEventIncidenceEnsemble;
  267.     }

  268.     /**
  269.      * Retrieve the Stress Event Map
  270.      *
  271.      * @return The Stress Event Map
  272.      */

  273.     public java.util.Map<java.lang.String, org.drip.capital.stress.Event> eventMap()
  274.     {
  275.         return _eventMap;
  276.     }

  277.     /**
  278.      * Retrieve the Scenario Probability Event Ladder
  279.      *
  280.      * @return The Scenario Probability Event Ladder
  281.      */

  282.     public org.drip.capital.stress.EventProbabilityLadder eventProbabilityLadder()
  283.     {
  284.         return _eventProbabilityLadder;
  285.     }
  286. }