HoldingsAllocationControl.java

  1. package org.drip.portfolioconstruction.allocator;

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

  77. /**
  78.  * <i>HoldingsAllocationControl</i> holds the Parameters needed to control the Portfolio Allocation.
  79.  *
  80.  *  <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  84.  *      <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>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/portfolioconstruction/allocator/README.md">MVO Based Portfolio Allocation Construction</a></li>
  86.  *  </ul>
  87.  *
  88.  * @author Lakshmi Krishnamurthy
  89.  */

  90. public class HoldingsAllocationControl
  91. {
  92.     private java.lang.String[] _assetIDArray = null;
  93.     private org.drip.portfolioconstruction.allocator.CustomRiskUtilitySettings _customRiskUtilitySettings =
  94.         null;
  95.     private org.drip.portfolioconstruction.allocator.EqualityConstraintSettings
  96.         _equalityConstraintSettings = null;

  97.     /**
  98.      * HoldingsAllocationControl Constructor
  99.      *
  100.      * @param assetIDArray Array of Asset IDs
  101.      * @param customRiskUtilitySettings The Custom Risk Utility Settings
  102.      * @param equalityConstraintSettings The Portfolio Equality Constraint Settings
  103.      *
  104.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  105.      */

  106.     public HoldingsAllocationControl (
  107.         final java.lang.String[] assetIDArray,
  108.         final org.drip.portfolioconstruction.allocator.CustomRiskUtilitySettings customRiskUtilitySettings,
  109.         final org.drip.portfolioconstruction.allocator.EqualityConstraintSettings equalityConstraintSettings)
  110.         throws java.lang.Exception
  111.     {
  112.         if (null == (_assetIDArray = assetIDArray) || 0 == _assetIDArray.length ||
  113.             null == (_customRiskUtilitySettings = customRiskUtilitySettings) ||
  114.             null == (_equalityConstraintSettings = equalityConstraintSettings))
  115.         {
  116.             throw new java.lang.Exception ("HoldingsAllocationControl Constructor => Invalid Inputs");
  117.         }
  118.     }

  119.     /**
  120.      * Retrieve the Asset ID Array
  121.      *
  122.      * @return The Asset ID Array
  123.      */

  124.     public java.lang.String[] assetIDArray()
  125.     {
  126.         return _assetIDArray;
  127.     }

  128.     /**
  129.      * Retrieve the Instance of the Custom Risk Utility Settings
  130.      *
  131.      * @return The Custom Risk Utility Settings
  132.      */

  133.     public org.drip.portfolioconstruction.allocator.CustomRiskUtilitySettings customRiskUtilitySettings()
  134.     {
  135.         return _customRiskUtilitySettings;
  136.     }

  137.     /**
  138.      * Retrieve the Instance of the Portfolio Equality Constraint Settings
  139.      *
  140.      * @return The Portfolio Equality Constraint Settings
  141.      */

  142.     public org.drip.portfolioconstruction.allocator.EqualityConstraintSettings equalityConstraintSettings()
  143.     {
  144.         return _equalityConstraintSettings;
  145.     }

  146.     /**
  147.      * Retrieve the Fully Invested Equality Constraint

  148.      * @return The Fully Invested Equality Constraint
  149.      */

  150.     public org.drip.function.definition.RdToR1 fullyInvestedConstraint()
  151.     {
  152.         try
  153.         {
  154.             return new org.drip.function.rdtor1.AffineMultivariate (
  155.                 org.drip.function.rdtor1.ObjectiveConstraintVariateSet.Unitary (_assetIDArray.length),
  156.                 -1.
  157.             );
  158.         }
  159.         catch (java.lang.Exception e)
  160.         {
  161.             e.printStackTrace();
  162.         }

  163.         return null;
  164.     }

  165.     /**
  166.      * Retrieve the Mandatory Returns Constraint
  167.      *
  168.      * @param assetUniverseStatisticalProperties The Asset Universe Statistical Properties Instance
  169.      *
  170.      * @return The Mandatory Returns Constraint
  171.      */

  172.     public org.drip.function.definition.RdToR1 returnsConstraint (
  173.         final org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties
  174.             assetUniverseStatisticalProperties)
  175.     {
  176.         if (null == assetUniverseStatisticalProperties)
  177.         {
  178.             return null;
  179.         }

  180.         int assetCount = _assetIDArray.length;
  181.         double[] assetReturnsArray = new double[assetCount];

  182.         for (int assetIndex = 0; assetIndex < assetCount; ++assetIndex)
  183.         {
  184.             org.drip.portfolioconstruction.params.AssetStatisticalProperties assetStatisticalProperties =
  185.                 assetUniverseStatisticalProperties.assetStatisticalProperties (_assetIDArray[assetIndex]);

  186.             if (null == assetStatisticalProperties)
  187.             {
  188.                 return null;
  189.             }

  190.             assetReturnsArray[assetIndex] = assetStatisticalProperties.expectedReturn();
  191.         }

  192.         try
  193.         {
  194.             return new org.drip.function.rdtor1.AffineMultivariate (
  195.                 assetReturnsArray,
  196.                 -1. * _equalityConstraintSettings.returnsConstraint()
  197.             );
  198.         }
  199.         catch (java.lang.Exception e)
  200.         {
  201.             e.printStackTrace();
  202.         }

  203.         return null;
  204.     }

  205.     /**
  206.      * Retrieve the Equality Constraint R<sup>d</sup> To R<sup>1</sup> Corresponding to the Specified
  207.      *  Constraint Type
  208.      *
  209.      * @param assetUniverseStatisticalProperties The Asset Universe Statistical Properties Instance
  210.      *
  211.      * @return The Equality Constraint R<sup>d</sup> To R<sup>1</sup> Corresponding to the Specified
  212.      *  Constraint Type
  213.      */

  214.     public org.drip.function.definition.RdToR1[] equalityConstraintArray (
  215.         final org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties
  216.             assetUniverseStatisticalProperties)
  217.     {
  218.         int constraintType = _equalityConstraintSettings.constraintType();

  219.         if (org.drip.portfolioconstruction.allocator.EqualityConstraintSettings.NO_CONSTRAINT ==
  220.             constraintType)
  221.         {
  222.             return null;
  223.         }

  224.         java.util.List<org.drip.function.definition.RdToR1> rdToR1ConstraintList = new
  225.             java.util.ArrayList<org.drip.function.definition.RdToR1>();

  226.         if (0 !=
  227.             (org.drip.portfolioconstruction.allocator.EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT
  228.             & constraintType))
  229.         {
  230.             rdToR1ConstraintList.add (fullyInvestedConstraint());
  231.         }

  232.         if (0 !=
  233.             (org.drip.portfolioconstruction.allocator.EqualityConstraintSettings.RETURNS_CONSTRAINT
  234.             & constraintType))
  235.         {
  236.             org.drip.function.definition.RdToR1 rdToR1ReturnsConstraint = returnsConstraint (
  237.                 assetUniverseStatisticalProperties
  238.             );

  239.             if (null == rdToR1ReturnsConstraint)
  240.             {
  241.                 return null;
  242.             }

  243.             rdToR1ConstraintList.add (rdToR1ReturnsConstraint);
  244.         }

  245.         int constraintCount = rdToR1ConstraintList.size();

  246.         org.drip.function.definition.RdToR1[] equalityConstraintArray =
  247.             new org.drip.function.definition.RdToR1[constraintCount];

  248.         for (int constraintIndex = 0; constraintIndex < constraintCount; ++constraintIndex)
  249.         {
  250.             equalityConstraintArray[constraintIndex] = rdToR1ConstraintList.get (constraintIndex);
  251.         }

  252.         return equalityConstraintArray;
  253.     }

  254.     /**
  255.      * Retrieve the Equality Constraint RHS Corresponding to the Specified Constraint Type
  256.      *
  257.      * @return The Equality Constraint RHS Corresponding to the Specified Constraint Type
  258.      */

  259.     public double[] equalityConstraintRHS()
  260.     {
  261.         double returnsConstraint = _equalityConstraintSettings.returnsConstraint();

  262.         return org.drip.numerical.common.NumberUtil.IsValid (returnsConstraint) ? new double[] {
  263.             1.,
  264.             returnsConstraint
  265.         } : new double[] {
  266.             1.
  267.         };
  268.     }
  269. }