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

  102. public class AllocatedPnLAttribution
  103.     extends org.drip.capital.explain.PnLAttribution
  104. {
  105.     private org.drip.capital.explain.PnLAttribution _standalonePnLAttribution = null;
  106.     private org.drip.capital.allocation.EntityComponentCapital _entityComponentCapital = null;

  107.     /**
  108.      * AllocatedPnLAttribution Constructor
  109.      *
  110.      * @param standalonePnLAttribution The Stand-alone PnL Attribution
  111.      * @param entityComponentCapital The Entity Component Capital
  112.      *
  113.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  114.      */

  115.     public AllocatedPnLAttribution (
  116.         final org.drip.capital.explain.PnLAttribution standalonePnLAttribution,
  117.         final org.drip.capital.allocation.EntityComponentCapital entityComponentCapital)
  118.         throws java.lang.Exception
  119.     {
  120.         if (null == (_standalonePnLAttribution = standalonePnLAttribution) ||
  121.             null == (_entityComponentCapital = entityComponentCapital)
  122.         )
  123.         {
  124.             throw new java.lang.Exception (
  125.                 "AllocatedPnLAttribution Constructor => Invalid Inputs"
  126.             );
  127.         }
  128.     }

  129.     /**
  130.      * Retrieve the Entity Component Capital
  131.      *
  132.      * @return The Entity Component Capital
  133.      */

  134.     public org.drip.capital.allocation.EntityComponentCapital entityComponentCapital()
  135.     {
  136.         return _entityComponentCapital;
  137.     }

  138.     @Override public java.util.Map<java.lang.String, java.lang.Double> fsPnLDecompositionExplainMap()
  139.     {
  140.         java.util.Map<java.lang.String, java.lang.Double> standaloneFSPnLDecompositionExplainMap =
  141.             _standalonePnLAttribution.fsPnLDecompositionExplainMap();

  142.         if (null == standaloneFSPnLDecompositionExplainMap ||
  143.             0 == standaloneFSPnLDecompositionExplainMap.size())
  144.         {
  145.             return null;
  146.         }

  147.         double allocationScale = _entityComponentCapital.noStressStandaloneMultiplier();

  148.         java.util.Map<java.lang.String, java.lang.Double> fsPnLDecompositionExplainMap = new
  149.             org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>();

  150.         for (java.util.Map.Entry<java.lang.String, java.lang.Double>
  151.             standaloneFSPnLDecompositionExplainMapEntry : standaloneFSPnLDecompositionExplainMap.entrySet())
  152.         {
  153.             fsPnLDecompositionExplainMap.put (
  154.                 standaloneFSPnLDecompositionExplainMapEntry.getKey(),
  155.                 standaloneFSPnLDecompositionExplainMapEntry.getValue() * allocationScale
  156.             );
  157.         }

  158.         return fsPnLDecompositionExplainMap;
  159.     }

  160.     @Override public java.util.Map<java.lang.String, java.lang.Double> paaCategoryDecompositionExplainMap()
  161.     {
  162.         java.util.Map<java.lang.String, java.lang.Double> standalonePAACategoryDecompositionExplainMap =
  163.             _standalonePnLAttribution.paaCategoryDecompositionExplainMap();

  164.         if (null == standalonePAACategoryDecompositionExplainMap ||
  165.             0 == standalonePAACategoryDecompositionExplainMap.size())
  166.         {
  167.             return null;
  168.         }

  169.         double allocationScale = _entityComponentCapital.systemicStandaloneMultiplier();

  170.         java.util.Map<java.lang.String, java.lang.Double> paaCategoryDecompositionExplainMap = new
  171.             org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>();

  172.         for (java.util.Map.Entry<java.lang.String, java.lang.Double>
  173.             standalonePAACategoryDecompositionExplainMapEntry :
  174.                 standalonePAACategoryDecompositionExplainMap.entrySet())
  175.         {
  176.             paaCategoryDecompositionExplainMap.put (
  177.                 standalonePAACategoryDecompositionExplainMapEntry.getKey(),
  178.                 standalonePAACategoryDecompositionExplainMapEntry.getValue() * allocationScale
  179.             );
  180.         }

  181.         return paaCategoryDecompositionExplainMap;
  182.     }

  183.     @Override public java.util.Map<java.lang.String, java.lang.Double> systemicPnLExplainMap()
  184.     {
  185.         java.util.Map<java.lang.String, java.lang.Double> standaloneSystemicPnLExplainMap =
  186.             _standalonePnLAttribution.systemicPnLExplainMap();

  187.         if (null == standaloneSystemicPnLExplainMap || 0 == standaloneSystemicPnLExplainMap.size())
  188.         {
  189.             return null;
  190.         }

  191.         double allocationScale = _entityComponentCapital.systemicStandaloneMultiplier();

  192.         java.util.Map<java.lang.String, java.lang.Double> systemicPnLExplainMap = new
  193.             org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>();

  194.         for (java.util.Map.Entry<java.lang.String, java.lang.Double> standaloneSystemicPnLExplainMapEntry :
  195.             standaloneSystemicPnLExplainMap.entrySet())
  196.         {
  197.             systemicPnLExplainMap.put (
  198.                 standaloneSystemicPnLExplainMapEntry.getKey(),
  199.                 standaloneSystemicPnLExplainMapEntry.getValue() * allocationScale
  200.             );
  201.         }

  202.         return systemicPnLExplainMap;
  203.     }

  204.     @Override public java.util.Map<java.lang.String, java.lang.Double> systemicGrossPnLExplainMap()
  205.     {
  206.         return null;
  207.     }

  208.     @Override public java.util.Map<java.lang.String, java.lang.Double> correlatedPnLExplainMap()
  209.     {
  210.         java.util.Map<java.lang.String, java.lang.Double> standaloneCorrelatedPnLExplainMap =
  211.             _standalonePnLAttribution.correlatedPnLExplainMap();

  212.         if (null == standaloneCorrelatedPnLExplainMap || 0 == standaloneCorrelatedPnLExplainMap.size())
  213.         {
  214.             return null;
  215.         }

  216.         double allocationScale = _entityComponentCapital.correlatedStandaloneMultiplier();

  217.         java.util.Map<java.lang.String, java.lang.Double> correlatedPnLExplainMap = new
  218.             org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>();

  219.         for (java.util.Map.Entry<java.lang.String, java.lang.Double> standaloneCorrelatedPnLExplainMapEntry :
  220.             standaloneCorrelatedPnLExplainMap.entrySet())
  221.         {
  222.             correlatedPnLExplainMap.put (
  223.                 standaloneCorrelatedPnLExplainMapEntry.getKey(),
  224.                 standaloneCorrelatedPnLExplainMapEntry.getValue() * allocationScale
  225.             );
  226.         }

  227.         return correlatedPnLExplainMap;
  228.     }

  229.     @Override public java.util.Map<java.lang.String, java.lang.Double> correlatedPnLWorstMap()
  230.     {
  231.         java.util.Map<java.lang.String, java.lang.Double> standaloneCorrelatedPnLWorstMap =
  232.             _standalonePnLAttribution.correlatedPnLWorstMap();

  233.         if (null == standaloneCorrelatedPnLWorstMap || 0 == standaloneCorrelatedPnLWorstMap.size())
  234.         {
  235.             return null;
  236.         }

  237.         double allocationScale = _entityComponentCapital.correlatedStandaloneMultiplier();

  238.         java.util.Map<java.lang.String, java.lang.Double> correlatedPnLWorstMap = new
  239.             org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>();

  240.         for (java.util.Map.Entry<java.lang.String, java.lang.Double> standaloneCorrelatedPnLWorstMapEntry :
  241.             standaloneCorrelatedPnLWorstMap.entrySet())
  242.         {
  243.             correlatedPnLWorstMap.put (
  244.                 standaloneCorrelatedPnLWorstMapEntry.getKey(),
  245.                 standaloneCorrelatedPnLWorstMapEntry.getValue() * allocationScale
  246.             );
  247.         }

  248.         return correlatedPnLWorstMap;
  249.     }

  250.     @Override public java.util.Map<java.lang.String, java.lang.Double> idiosyncraticPnLExplainMap()
  251.     {
  252.         java.util.Map<java.lang.String, java.lang.Double> standaloneIdiosyncraticPnLExplainMap =
  253.             _standalonePnLAttribution.idiosyncraticPnLExplainMap();

  254.         if (null == standaloneIdiosyncraticPnLExplainMap || 0 == standaloneIdiosyncraticPnLExplainMap.size())
  255.         {
  256.             return null;
  257.         }

  258.         double allocationScale = _entityComponentCapital.idiosyncraticStandaloneMultiplier();

  259.         java.util.Map<java.lang.String, java.lang.Double> idiosyncraticPnLExplainMap = new
  260.             org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>();

  261.         for (java.util.Map.Entry<java.lang.String, java.lang.Double>
  262.             standaloneIdiosyncraticPnLExplainMapEntry : standaloneIdiosyncraticPnLExplainMap.entrySet())
  263.         {
  264.             idiosyncraticPnLExplainMap.put (
  265.                 standaloneIdiosyncraticPnLExplainMapEntry.getKey(),
  266.                 standaloneIdiosyncraticPnLExplainMapEntry.getValue() * allocationScale
  267.             );
  268.         }

  269.         return idiosyncraticPnLExplainMap;
  270.     }

  271.     @Override public java.util.Map<java.lang.String, java.lang.Double> idiosyncraticPnLWorstMap()
  272.     {
  273.         java.util.Map<java.lang.String, java.lang.Double> standaloneIdiosyncraticPnLWorstMap =
  274.             _standalonePnLAttribution.idiosyncraticPnLWorstMap();

  275.         if (null == standaloneIdiosyncraticPnLWorstMap || 0 == standaloneIdiosyncraticPnLWorstMap.size())
  276.         {
  277.             return null;
  278.         }

  279.         double allocationScale = _entityComponentCapital.idiosyncraticStandaloneMultiplier();

  280.         java.util.Map<java.lang.String, java.lang.Double> idiosyncraticPnLWorstMap = new
  281.             org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>();

  282.         for (java.util.Map.Entry<java.lang.String, java.lang.Double> standaloneIdiosyncraticPnLWorstMapEntry
  283.             : standaloneIdiosyncraticPnLWorstMap.entrySet())
  284.         {
  285.             idiosyncraticPnLWorstMap.put (
  286.                 standaloneIdiosyncraticPnLWorstMapEntry.getKey(),
  287.                 standaloneIdiosyncraticPnLWorstMapEntry.getValue() * allocationScale
  288.             );
  289.         }

  290.         return idiosyncraticPnLWorstMap;
  291.     }

  292.     @Override public double var()
  293.     {
  294.         return _entityComponentCapital.noStressStandaloneMultiplier() * super.var();
  295.     }

  296.     @Override public double expectedShortfall()
  297.     {
  298.         return systemicPnL() + correlatedPnL() + idiosyncraticGrossPnL() + fsGrossPnL();
  299.     }

  300.     @Override public java.util.Map<java.lang.String, java.lang.Integer> systemicInstanceCountMap()
  301.     {
  302.         return _standalonePnLAttribution.systemicInstanceCountMap();
  303.     }

  304.     @Override public java.util.Map<java.lang.String, java.lang.Integer> correlatedInstanceCountMap()
  305.     {
  306.         return _standalonePnLAttribution.correlatedInstanceCountMap();
  307.     }

  308.     @Override public java.util.Map<java.lang.String, java.lang.Integer> idiosyncraticInstanceCountMap()
  309.     {
  310.         return _standalonePnLAttribution.idiosyncraticInstanceCountMap();
  311.     }

  312.     @Override public java.util.List<java.lang.Integer> pathIndexList()
  313.     {
  314.         return _standalonePnLAttribution.pathIndexList();
  315.     }

  316.     @Override public int pathCount()
  317.     {
  318.         return _standalonePnLAttribution.pathCount();
  319.     }

  320.     @Override public double systemicPnL()
  321.     {
  322.         return _entityComponentCapital.systemicStandaloneMultiplier() *
  323.             _standalonePnLAttribution.systemicPnL();
  324.     }

  325.     @Override public double systemicGrossPnL()
  326.     {
  327.         return _entityComponentCapital.systemicStandaloneMultiplier() *
  328.             _standalonePnLAttribution.systemicGrossPnL();
  329.     }

  330.     @Override public double correlatedPnL()
  331.     {
  332.         return _entityComponentCapital.correlatedStandaloneMultiplier() *
  333.             _standalonePnLAttribution.correlatedPnL();
  334.     }

  335.     @Override public double idiosyncraticGrossPnL()
  336.     {
  337.         return _entityComponentCapital.idiosyncraticStandaloneMultiplier() *
  338.             _standalonePnLAttribution.idiosyncraticGrossPnL();
  339.     }

  340.     @Override public double fsGrossPnL()
  341.     {
  342.         return _entityComponentCapital.noStressStandaloneMultiplier() *
  343.             _standalonePnLAttribution.fsGrossPnL();
  344.     }
  345. }