PositionGroupContainer.java

  1. package org.drip.exposure.holdings;

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

  75. /**
  76.  * <i>PositionGroupContainer</i> contains a Set of Position/Collateral Groups. The References are:
  77.  *  
  78.  * <br><br>
  79.  *      <ul>
  80.  *          <li>
  81.  *              Burgard, C., and M. Kjaer (2013): Funding Costs, Funding Strategies <i>Risk</i> <b>23
  82.  *                  (12)</b> 82-87
  83.  *          </li>
  84.  *          <li>
  85.  *              Burgard, C., and M. Kjaer (2014): In the Balance <i>Risk</i> <b>24 (11)</b> 72-75
  86.  *          </li>
  87.  *          <li>
  88.  *              Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-
  89.  *                  party Risk and Funding Costs <i>Journal of Credit Risk</i> <b>7 (3)</b> 1-19
  90.  *          </li>
  91.  *          <li>
  92.  *              Burgard, C., and M. Kjaer (2017): Derivatives Funding, Netting, and Accounting
  93.  *                  https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2534011 <b>eSSRN</b>
  94.  *          </li>
  95.  *          <li>
  96.  *              Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives
  97.  *                  Pricing <i>Risk</i> <b>21 (2)</b> 97-102
  98.  *          </li>
  99.  *          <li>
  100.  *      </ul>
  101.  *
  102.  *  <br><br>
  103.  *  <ul>
  104.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  105.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ExposureAnalyticsLibrary.md">Exposure Analytics</a></li>
  106.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/exposure/README.md">Exposure Group Level Collateralized/Uncollateralized Exposure</a></li>
  107.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/exposure/holdings/README.md">Holdings Exposure - Position and Dependencies</a></li>
  108.  *  </ul>
  109.  *
  110.  * @author Lakshmi Krishnamurthy
  111.  */

  112. public class PositionGroupContainer
  113. {
  114.     private org.drip.exposure.holdings.PositionGroup[] _positionGroupArray = null;

  115.     /**
  116.      * Generate a PositionGroupContainer Instance with a Solo Group
  117.      *
  118.      * @param positionGroup The PositionGroup Instance
  119.      *
  120.      * @return The Solo PositionGroupContainer
  121.      */

  122.     public static final PositionGroupContainer Solo (
  123.         final org.drip.exposure.holdings.PositionGroup positionGroup)
  124.     {
  125.         try
  126.         {
  127.             return new PositionGroupContainer (
  128.                 new org.drip.exposure.holdings.PositionGroup[]
  129.                 {
  130.                     positionGroup
  131.                 }
  132.             );
  133.         }
  134.         catch (java.lang.Exception e)
  135.         {
  136.             e.printStackTrace();
  137.         }

  138.         return null;
  139.     }

  140.     /**
  141.      * PositionGroupContainer Constructor
  142.      *
  143.      * @param positionGroupArray The Position Group Array
  144.      *
  145.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  146.      */

  147.     public PositionGroupContainer (
  148.         final org.drip.exposure.holdings.PositionGroup[] positionGroupArray)
  149.         throws java.lang.Exception
  150.     {
  151.         if (null == (_positionGroupArray = positionGroupArray))
  152.         {
  153.             throw new java.lang.Exception ("PositionGroupContainer Constructor => Invalid Inputs");
  154.         }

  155.         int positionGroupCount = _positionGroupArray.length;

  156.         if (0 == positionGroupCount)
  157.         {
  158.             throw new java.lang.Exception ("PositionGroupContainer Constructor => Invalid Inputs");
  159.         }

  160.         for (int positionGroupIndex = 0; positionGroupIndex < positionGroupCount; ++positionGroupIndex)
  161.         {
  162.             if (null == _positionGroupArray[positionGroupIndex])
  163.             {
  164.                 throw new java.lang.Exception ("PositionGroupContainer Constructor => Invalid Inputs");
  165.             }
  166.         }
  167.     }

  168.     /**
  169.      * Retrieve the Array of Position Groups
  170.      *
  171.      * @return The Array of Position Groups
  172.      */

  173.     public org.drip.exposure.holdings.PositionGroup[] positionGroupArray()
  174.     {
  175.         return _positionGroupArray;
  176.     }

  177.     /**
  178.      * Retrieve the Number of the Positions in the Container
  179.      *
  180.      * @return Number of the Positions in the Container
  181.      */

  182.     public int count()
  183.     {
  184.         return _positionGroupArray.length;
  185.     }

  186.     /**
  187.      * Set the Specific Position Group's Collateral Group Path
  188.      *
  189.      * @param positionGroupIndex The Index in the Position Group
  190.      * @param collateralGroupPath Collateral Group Path
  191.      *
  192.      * @return TRUE - The Collateral Group Path successfully set
  193.      */

  194.     public boolean setCollateralGroupPath (
  195.         final int positionGroupIndex,
  196.         final org.drip.xva.netting.CollateralGroupPath collateralGroupPath)
  197.     {
  198.         return positionGroupIndex >= count() ? false :
  199.             _positionGroupArray[positionGroupIndex].setCollateralGroupPath (collateralGroupPath);
  200.     }

  201.     /**
  202.      * Retrieve the Position Groups Sorted into Credit Debt Group Segments
  203.      *
  204.      * @return Map of the Position Groups Sorted into Credit Debt Group Segments
  205.      */

  206.     public java.util.Map<java.lang.String, org.drip.exposure.holdings.PositionGroupSegment> creditDebtSegments()
  207.     {
  208.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.exposure.holdings.PositionGroupSegment>
  209.             creditDebtPositionGroupSegment = new
  210.                 org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.exposure.holdings.PositionGroupSegment>();

  211.         int positionGroupCount = _positionGroupArray.length;

  212.         for (int positionGroupIndex = 0; positionGroupIndex < positionGroupCount; ++positionGroupIndex)
  213.         {
  214.             org.drip.exposure.holdings.PositionGroup positionGroup = _positionGroupArray[positionGroupIndex];

  215.             java.lang.String groupID =
  216.                 positionGroup.positionGroupSpecification().creditDebtGroupSpecification().id();

  217.             boolean segmentPresent = creditDebtPositionGroupSegment.containsKey (groupID);

  218.             org.drip.exposure.holdings.PositionGroupSegment positionGroupSegment = segmentPresent ?
  219.                 creditDebtPositionGroupSegment.get (groupID) : new org.drip.exposure.holdings.PositionGroupSegment();

  220.             positionGroupSegment.add (positionGroup);

  221.             if (!segmentPresent)
  222.             {
  223.                 creditDebtPositionGroupSegment.put (
  224.                     groupID,
  225.                     positionGroupSegment
  226.                 );
  227.             }
  228.         }

  229.         return creditDebtPositionGroupSegment;
  230.     }

  231.     /**
  232.      * Retrieve the Array of Position Groups Collected into Credit Debt Group Collateral Vertex Paths
  233.      *
  234.      * @return Array of the Position Groups Collected into Credit Debt Group Collateral Vertex Paths
  235.      */

  236.     public org.drip.xva.netting.CollateralGroupPath[][] creditDebtSegmentPaths()
  237.     {
  238.         java.util.Map<java.lang.String, org.drip.exposure.holdings.PositionGroupSegment>
  239.             creditDebtPositionGroupSegment = creditDebtSegments();

  240.         int creditDebtPositionGroupSegmentCount = creditDebtPositionGroupSegment.size();

  241.         int creditDebtPositionGroupSegmentIndex = 0;
  242.         org.drip.xva.netting.CollateralGroupPath[][] creditDebtSegmentPathArray = new
  243.             org.drip.xva.netting.CollateralGroupPath[creditDebtPositionGroupSegmentCount][];

  244.         for (java.util.Map.Entry<java.lang.String, org.drip.exposure.holdings.PositionGroupSegment>
  245.             creditDebtPositionGroupSegmentEntry : creditDebtPositionGroupSegment.entrySet())
  246.         {
  247.             creditDebtSegmentPathArray[creditDebtPositionGroupSegmentIndex++] =
  248.                 creditDebtPositionGroupSegmentEntry.getValue().collateralGroupPathArray();
  249.         }

  250.         return creditDebtSegmentPathArray;
  251.     }

  252.     /**
  253.      * Retrieve the Position Groups Sorted into Funding Group Segments
  254.      *
  255.      * @return Map of the Position Groups Sorted into Funding Group Segments
  256.      */

  257.     public java.util.Map<java.lang.String, org.drip.exposure.holdings.PositionGroupSegment> fundingSegments()
  258.     {
  259.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.exposure.holdings.PositionGroupSegment>
  260.             fundingPositionGroupSegment = new
  261.                 org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.exposure.holdings.PositionGroupSegment>();

  262.         int positionGroupCount = _positionGroupArray.length;

  263.         for (int positionGroupIndex = 0; positionGroupIndex < positionGroupCount; ++positionGroupIndex)
  264.         {
  265.             org.drip.exposure.holdings.PositionGroup positionGroup = _positionGroupArray[positionGroupIndex];

  266.             java.lang.String groupID =
  267.                 positionGroup.positionGroupSpecification().fundingGroupSpecification().id();

  268.             boolean segmentPresent = fundingPositionGroupSegment.containsKey (groupID);

  269.             org.drip.exposure.holdings.PositionGroupSegment positionGroupSegment = segmentPresent ?
  270.                 fundingPositionGroupSegment.get (groupID) : new org.drip.exposure.holdings.PositionGroupSegment();

  271.             positionGroupSegment.add (positionGroup);

  272.             if (!segmentPresent)
  273.             {
  274.                 fundingPositionGroupSegment.put (
  275.                     groupID,
  276.                     positionGroupSegment
  277.                 );
  278.             }
  279.         }

  280.         return fundingPositionGroupSegment;
  281.     }

  282.     /**
  283.      * Retrieve the Array of Position Groups Collected into Funding Group Collateral Vertex Paths
  284.      *
  285.      * @return Array of the Position Groups Collected into Funding Group Collateral Vertex Paths
  286.      */

  287.     public org.drip.xva.netting.CollateralGroupPath[][] fundingSegmentPaths()
  288.     {
  289.         java.util.Map<java.lang.String, org.drip.exposure.holdings.PositionGroupSegment>
  290.             fundingPositionGroupSegment = fundingSegments();

  291.         int fundingPositionGroupSegmentCount = fundingPositionGroupSegment.size();

  292.         int fundingPositionGroupSegmentIndex = 0;
  293.         org.drip.xva.netting.CollateralGroupPath[][] fundingSegmentPathArray = new
  294.             org.drip.xva.netting.CollateralGroupPath[fundingPositionGroupSegmentCount][];

  295.         for (java.util.Map.Entry<java.lang.String, org.drip.exposure.holdings.PositionGroupSegment>
  296.             fundingPositionGroupSegmentEntry : fundingPositionGroupSegment.entrySet())
  297.         {
  298.             fundingSegmentPathArray[fundingPositionGroupSegmentIndex++] =
  299.                 fundingPositionGroupSegmentEntry.getValue().collateralGroupPathArray();
  300.         }

  301.         return fundingSegmentPathArray;
  302.     }
  303. }