PnLAttribution.java

  1. package org.drip.capital.explain;

  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>PnLAttribution</i> exposes the Path-Level Capital Component Attributions. 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/explain/README.md">Economic Risk Capital Attribution Explain</a></li>
  97.  *  </ul>
  98.  *
  99.  * @author Lakshmi Krishnamurthy
  100.  */

  101. public abstract class PnLAttribution
  102. {
  103.     protected double _var = java.lang.Double.NaN;
  104.     protected double _expectedShortfall = java.lang.Double.NaN;
  105.     protected java.util.Map<java.lang.String, java.lang.Double> _correlatedPnLWorstMap = null;
  106.     protected java.util.Map<java.lang.String, java.lang.Double> _systemicPnLExplainMap = null;
  107.     protected java.util.Map<java.lang.String, java.lang.Double> _correlatedPnLExplainMap = null;
  108.     protected java.util.Map<java.lang.String, java.lang.Double> _idiosyncraticPnLWorstMap = null;
  109.     protected java.util.Map<java.lang.String, java.lang.Integer> _systemicInstanceCountMap = null;
  110.     protected java.util.Map<java.lang.String, java.lang.Double> _idiosyncraticPnLExplainMap = null;
  111.     protected java.util.Map<java.lang.String, java.lang.Double> _systemicGrossPnLExplainMap = null;
  112.     protected java.util.Map<java.lang.String, java.lang.Integer> _correlatedInstanceCountMap = null;
  113.     protected java.util.Map<java.lang.String, java.lang.Double> _fsPnLDecompositionExplainMap = null;
  114.     protected java.util.Map<java.lang.String, java.lang.Integer> _idiosyncraticInstanceCountMap = null;
  115.     protected java.util.Map<java.lang.String, java.lang.Double> _paaCategoryDecompositionExplainMap = null;

  116.     /**
  117.      * Retrieve the FS PnL Decomposition Explain Map
  118.      *
  119.      * @return The FS PnL Decomposition Explain Map
  120.      */

  121.     public java.util.Map<java.lang.String, java.lang.Double> fsPnLDecompositionExplainMap()
  122.     {
  123.         return _fsPnLDecompositionExplainMap;
  124.     }

  125.     /**
  126.      * Retrieve the PAA Category Decomposition Explain Map
  127.      *
  128.      * @return The PAA Category Decomposition Explain Map
  129.      */

  130.     public java.util.Map<java.lang.String, java.lang.Double> paaCategoryDecompositionExplainMap()
  131.     {
  132.         return _paaCategoryDecompositionExplainMap;
  133.     }

  134.     /**
  135.      * Retrieve the Systemic PnL Explain Map
  136.      *
  137.      * @return The Systemic PnL Explain Map
  138.      */

  139.     public java.util.Map<java.lang.String, java.lang.Double> systemicPnLExplainMap()
  140.     {
  141.         return _systemicPnLExplainMap;
  142.     }

  143.     /**
  144.      * Retrieve the Systemic Gross PnL Explain Map
  145.      *
  146.      * @return The Systemic Gross PnL Explain Map
  147.      */

  148.     public java.util.Map<java.lang.String, java.lang.Double> systemicGrossPnLExplainMap()
  149.     {
  150.         return _systemicGrossPnLExplainMap;
  151.     }

  152.     /**
  153.      * Retrieve the Correlated PnL Explain Map
  154.      *
  155.      * @return The Correlated PnL Explain Map
  156.      */

  157.     public java.util.Map<java.lang.String, java.lang.Double> correlatedPnLExplainMap()
  158.     {
  159.         return _correlatedPnLExplainMap;
  160.     }

  161.     /**
  162.      * Retrieve the Correlated Worst PnL Map
  163.      *
  164.      * @return The Correlated Worst PnL Map
  165.      */

  166.     public java.util.Map<java.lang.String, java.lang.Double> correlatedPnLWorstMap()
  167.     {
  168.         return _correlatedPnLWorstMap;
  169.     }

  170.     /**
  171.      * Retrieve the Idiosyncratic PnL Explain Map
  172.      *
  173.      * @return The Idiosyncratic PnL Explain Map
  174.      */

  175.     public java.util.Map<java.lang.String, java.lang.Double> idiosyncraticPnLExplainMap()
  176.     {
  177.         return _idiosyncraticPnLExplainMap;
  178.     }

  179.     /**
  180.      * Retrieve the Idiosyncratic Worst PnL Map
  181.      *
  182.      * @return The Idiosyncratic Worst PnL Map
  183.      */

  184.     public java.util.Map<java.lang.String, java.lang.Double> idiosyncraticPnLWorstMap()
  185.     {
  186.         return _idiosyncraticPnLWorstMap;
  187.     }

  188.     /**
  189.      * Retrieve the VaR
  190.      *
  191.      * @return VaR
  192.      */

  193.     public double var()
  194.     {
  195.         return _var;
  196.     }

  197.     /**
  198.      * Retrieve the Expected Short-fall
  199.      *
  200.      * @return Expected Short-fall
  201.      */

  202.     public double expectedShortfall()
  203.     {
  204.         return _expectedShortfall;
  205.     }

  206.     /**
  207.      * Retrieve the Systemic Instance Count Map
  208.      *
  209.      * @return The Systemic Instance Count Map
  210.      */

  211.     public java.util.Map<java.lang.String, java.lang.Integer> systemicInstanceCountMap()
  212.     {
  213.         return _systemicInstanceCountMap;
  214.     }

  215.     /**
  216.      * Retrieve the Correlated Instance Count Map
  217.      *
  218.      * @return The Correlated Instance Count Map
  219.      */

  220.     public java.util.Map<java.lang.String, java.lang.Integer> correlatedInstanceCountMap()
  221.     {
  222.         return _correlatedInstanceCountMap;
  223.     }

  224.     /**
  225.      * Retrieve the Idiosyncratic Instance Count Map
  226.      *
  227.      * @return The Idiosyncratic Instance Count Map
  228.      */

  229.     public java.util.Map<java.lang.String, java.lang.Integer> idiosyncraticInstanceCountMap()
  230.     {
  231.         return _idiosyncraticInstanceCountMap;
  232.     }

  233.     /**
  234.      * Generate the Contributing Path Index List
  235.      *
  236.      * @return Contributing Path Index List
  237.      */

  238.     public abstract java.util.List<java.lang.Integer> pathIndexList();

  239.     /**
  240.      * Retrieve the Number of Contributing Paths
  241.      *
  242.      * @return The Number of Contributing Paths
  243.      */

  244.     public abstract int pathCount();

  245.     /**
  246.      * Retrieve the Systemic PnL
  247.      *
  248.      * @return The Systemic PnL
  249.      */

  250.     public double systemicPnL()
  251.     {
  252.         if (null == _systemicPnLExplainMap || 0 == _systemicPnLExplainMap.size())
  253.         {
  254.             return 0.;
  255.         }

  256.         double systemicPnL = 0.;

  257.         for (java.util.Map.Entry<java.lang.String, java.lang.Double> systemicPnLExplainEntry :
  258.             _systemicPnLExplainMap.entrySet())
  259.         {
  260.             systemicPnL = systemicPnL + systemicPnLExplainEntry.getValue();
  261.         }

  262.         return systemicPnL;
  263.     }

  264.     /**
  265.      * Retrieve the Systemic Gross PnL
  266.      *
  267.      * @return The Systemic Gross PnL
  268.      */

  269.     public double systemicGrossPnL()
  270.     {
  271.         if (null == _systemicGrossPnLExplainMap || 0 == _systemicGrossPnLExplainMap.size())
  272.         {
  273.             return 0.;
  274.         }

  275.         double systemicGrossPnL = 0.;

  276.         for (java.util.Map.Entry<java.lang.String, java.lang.Double> systemicGrossPnLExplainEntry :
  277.             _systemicGrossPnLExplainMap.entrySet())
  278.         {
  279.             systemicGrossPnL = systemicGrossPnL + systemicGrossPnLExplainEntry.getValue();
  280.         }

  281.         return systemicGrossPnL;
  282.     }

  283.     /**
  284.      * Retrieve the Correlated PnL
  285.      *
  286.      * @return The Correlated PnL
  287.      */

  288.     public double correlatedPnL()
  289.     {
  290.         if (null == _correlatedPnLExplainMap || 0 == _correlatedPnLExplainMap.size())
  291.         {
  292.             return 0.;
  293.         }

  294.         double correlatedPnL = 0.;

  295.         for (java.util.Map.Entry<java.lang.String, java.lang.Double> correlatedPnLExplainEntry :
  296.             _correlatedPnLExplainMap.entrySet())
  297.         {
  298.             correlatedPnL = correlatedPnL + correlatedPnLExplainEntry.getValue();
  299.         }

  300.         return correlatedPnL;
  301.     }

  302.     /**
  303.      * Retrieve the Idiosyncratic Gross PnL
  304.      *
  305.      * @return The Idiosyncratic Gross PnL
  306.      */

  307.     public double idiosyncraticGrossPnL()
  308.     {
  309.         if (null == _idiosyncraticPnLExplainMap || 0 == _idiosyncraticPnLExplainMap.size())
  310.         {
  311.             return 0.;
  312.         }

  313.         double idiosyncraticGrossPnL = 0.;

  314.         for (java.util.Map.Entry<java.lang.String, java.lang.Double> idiosyncraticPnLExplainEntry :
  315.             _idiosyncraticPnLExplainMap.entrySet())
  316.         {
  317.             idiosyncraticGrossPnL = idiosyncraticGrossPnL + idiosyncraticPnLExplainEntry.getValue();
  318.         }

  319.         return idiosyncraticGrossPnL;
  320.     }

  321.     /**
  322.      * Retrieve the Gross VaR FS PnL
  323.      *
  324.      * @return The Gross VaR FS PnL
  325.      */

  326.     public double fsGrossPnL()
  327.     {
  328.         if (null == _fsPnLDecompositionExplainMap || 0 == _fsPnLDecompositionExplainMap.size())
  329.         {
  330.             return 0.;
  331.         }

  332.         double fsGrossPnL = 0.;

  333.         for (java.util.Map.Entry<java.lang.String, java.lang.Double> fsPnLDecompositionExplainEntry :
  334.             _fsPnLDecompositionExplainMap.entrySet())
  335.         {
  336.             fsGrossPnL = fsGrossPnL + fsPnLDecompositionExplainEntry.getValue();
  337.         }

  338.         return fsGrossPnL;
  339.     }
  340. }