Holdings.java

  1. package org.drip.portfolioconstruction.composite;

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

  76. /**
  77.  * <i>Holdings</i> is a Portfolio of Holdings in the specified Set of Assets.
  78.  *
  79.  *  <br><br>
  80.  *  <ul>
  81.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  82.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  83.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/portfolioconstruction/README.md">Portfolio Construction under Allocation Constraints</a></li>
  84.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/portfolioconstruction/composite/README.md">Portfolio Construction Component Groups Suite</a></li>
  85.  *  </ul>
  86.  *
  87.  * @author Lakshmi Krishnamurthy
  88.  */

  89. public class Holdings extends org.drip.portfolioconstruction.core.Block
  90. {
  91.     private java.lang.String _currency = "";

  92.     private org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> _quantityMap =
  93.         new org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double>();

  94.     /**
  95.      * Holdings Constructor
  96.      *
  97.      * @param name The Asset Name
  98.      * @param id The Asset ID
  99.      * @param description The Asset Description
  100.      * @param currency The Account Currency
  101.      *
  102.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  103.      */

  104.     public Holdings (
  105.         final java.lang.String name,
  106.         final java.lang.String id,
  107.         final java.lang.String description,
  108.         final java.lang.String currency)
  109.         throws java.lang.Exception
  110.     {
  111.         super (
  112.             name,
  113.             id,
  114.             description
  115.         );

  116.         if (null == (_currency = currency))
  117.         {
  118.             throw new java.lang.Exception ("Holdings Constructor => Invalid Inputs");
  119.         }
  120.     }

  121.     /**
  122.      * Retrieve the Set of Asset IDs
  123.      *
  124.      * @return The Set of Asset IDs
  125.      */

  126.     public java.util.Set<java.lang.String> assetIDSet()
  127.     {
  128.         return _quantityMap.keySet();
  129.     }

  130.     /**
  131.      * Retrieve the Map of Holdings Amount
  132.      *
  133.      * @return The Map of Holdings Amount
  134.      */

  135.     public java.util.Map<java.lang.String, java.lang.Double> quantityMap()
  136.     {
  137.         return _quantityMap;
  138.     }

  139.     /**
  140.      * Add an Asset/Amount Pair
  141.      *
  142.      * @param assetID The Asset ID
  143.      * @param quantity The Amount in the Portfolio
  144.      *
  145.      * @return TRUE - The Asset/Amount has been successfully added
  146.      */

  147.     public boolean add (
  148.         final java.lang.String assetID,
  149.         final double quantity)
  150.     {
  151.         if (null == assetID || assetID.isEmpty() ||
  152.             !org.drip.numerical.common.NumberUtil.IsValid (quantity))
  153.         {
  154.             return false;
  155.         }

  156.         _quantityMap.put (
  157.             assetID,
  158.             quantity
  159.         );

  160.         return true;
  161.     }

  162.     /**
  163.      * Indicates if an Asset exists in the Holdings
  164.      *
  165.      * @param assetID The Asset ID
  166.      *
  167.      * @return TRUE - The Asset is Part of the Holdings (may have Zero Value though)
  168.      */

  169.     public boolean contains (
  170.         final java.lang.String assetID)
  171.     {
  172.         return null != assetID && !_quantityMap.containsKey (assetID);
  173.     }

  174.     /**
  175.      * Retrieves the Holdings Quantity for the Asset (if it exists)
  176.      *
  177.      * @param assetID The Asset ID
  178.      *
  179.      * @return The Holdings Quantity for the Asset (if it exists)
  180.      *
  181.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  182.      */

  183.     public double quantity (
  184.         final java.lang.String assetID)
  185.         throws java.lang.Exception
  186.     {
  187.         if (!contains (assetID))
  188.         {
  189.             throw new java.lang.Exception ("Holdings::quantity => Invalid Inputs");
  190.         }

  191.         return _quantityMap.get (assetID);
  192.     }

  193.     /**
  194.      * Retrieve the Currency
  195.      *
  196.      * @return The Currency
  197.      */

  198.     public java.lang.String currency()
  199.     {
  200.         return _currency;
  201.     }

  202.     /**
  203.      * Retrieves the Cash Holdings
  204.      *
  205.      * @return The Cash Holdings
  206.      */

  207.     public double cash()
  208.     {
  209.         try
  210.         {
  211.             return quantity ("CASH::" + _currency);
  212.         }
  213.         catch (java.lang.Exception e)
  214.         {
  215.         }

  216.         return 0.;
  217.     }

  218.     /**
  219.      * Retrieve the Array Form of the Holdings Quantity
  220.      *
  221.      * @return Array Form of the Holdings Quantity
  222.      */

  223.     public double[] toArray()
  224.     {
  225.         int size = _quantityMap.size();

  226.         if (0 == size)
  227.         {
  228.             return null;
  229.         }

  230.         int assetIndex = 0;
  231.         double[] quantityArray = new double[size];

  232.         for (java.util.Map.Entry<java.lang.String, java.lang.Double> quantityMapEntry :
  233.             _quantityMap.entrySet())
  234.         {
  235.             quantityArray[assetIndex++] = quantityMapEntry.getValue();
  236.         }

  237.         return quantityArray;
  238.     }

  239.     /**
  240.      * Constrict "This" Holdings to those of the Assets in the "Other" Holdings
  241.      *
  242.      * @param holdingsOther The Other Holdings Instance
  243.      *
  244.      * @return Constriction of "This" Holdings
  245.      */

  246.     public double[] constrict (
  247.         final org.drip.portfolioconstruction.composite.Holdings holdingsOther)
  248.     {
  249.         if (null == holdingsOther)
  250.         {
  251.             return null;
  252.         }

  253.         java.util.Set<java.lang.String> assetIDSet = holdingsOther.assetIDSet();

  254.         java.util.List<java.lang.Double> quantityList = new java.util.ArrayList<java.lang.Double>();

  255.         for (java.lang.String assetID : assetIDSet)
  256.         {
  257.             try
  258.             {
  259.                 quantityList.add (
  260.                     contains (
  261.                         assetID
  262.                     ) ? quantity (
  263.                         assetID
  264.                     ) : 0.
  265.                 );
  266.             }
  267.             catch (java.lang.Exception e)
  268.             {
  269.                 e.printStackTrace();

  270.                 return null;
  271.             }
  272.         }

  273.         int assetCount = assetIDSet.size();

  274.         if (quantityList.size() != assetCount)
  275.         {
  276.             return null;
  277.         }

  278.         int assetIndex = 0;
  279.         double[] assetQuantityArray = new double[assetCount];

  280.         for (double quantity : quantityList)
  281.         {
  282.             assetQuantityArray[assetIndex++] = quantity;
  283.         }

  284.         return assetQuantityArray;
  285.     }
  286. }