BudgetConstrainedVarianceMinimizer.java

  1. package org.drip.sample.assetallocation;

  2. import org.drip.feed.loader.*;
  3. import org.drip.function.rdtor1descent.LineStepEvolutionControl;
  4. import org.drip.function.rdtor1solver.InteriorPointBarrierControl;
  5. import org.drip.measure.statistics.MultivariateMoments;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.portfolioconstruction.allocator.*;
  8. import org.drip.portfolioconstruction.asset.*;
  9. import org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties;
  10. import org.drip.service.env.EnvManager;

  11. /*
  12.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  13.  */

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

  86. /**
  87.  * <i>BudgetConstrainedVarianceMinimizer</i> demonstrates the Construction of an Optimal Portfolio using the
  88.  * Variance Minimizing Allocator with Budget/Weight Constraints.
  89.  *  
  90.  * <br><br>
  91.  *  <ul>
  92.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  93.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  94.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  95.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/assetallocation/README.md">MVO Based Constrained Optimal Allocator</a></li>
  96.  *  </ul>
  97.  * <br><br>
  98.  *
  99.  * @author Lakshmi Krishnamurthy
  100.  */

  101. public class BudgetConstrainedVarianceMinimizer
  102. {

  103.     public static final void main (
  104.         final String[] argumentArray)
  105.         throws Exception
  106.     {
  107.         EnvManager.InitEnv (
  108.             "",
  109.             true
  110.         );

  111.         String seriesPath = "C:\\DROP\\Daemons\\Feeds\\MeanVarianceOptimizer\\FormattedSeries1.csv";

  112.         CSVGrid csvGrid = CSVParser.NamedStringGrid (
  113.             seriesPath
  114.         );

  115.         String[] variateHeaderArray = csvGrid.headers();

  116.         double assetHoldingLowerBound = 0.05;
  117.         double assetHoldingUpperBound = 0.65;
  118.         String[] assetIDArray = new String[variateHeaderArray.length - 1];
  119.         double[][] variateSampleGrid = new double[variateHeaderArray.length - 1][];

  120.         for (int assetIndex = 0;
  121.             assetIndex < assetIDArray.length;
  122.             ++assetIndex)
  123.         {
  124.             assetIDArray[assetIndex] = variateHeaderArray[assetIndex + 1];

  125.             variateSampleGrid[assetIndex] = csvGrid.doubleArrayAtColumn (
  126.                 assetIndex + 1
  127.             );
  128.         }

  129.         AssetUniverseStatisticalProperties assetUniverseStatisticalProperties =
  130.             AssetUniverseStatisticalProperties.FromMultivariateMetrics (
  131.                 MultivariateMoments.Standard (
  132.                     assetIDArray,
  133.                     variateSampleGrid
  134.                 )
  135.             );

  136.         double[][] covarianceMatrix = assetUniverseStatisticalProperties.covariance (
  137.             assetIDArray
  138.         );

  139.         System.out.println ("\n\n\t|------------------------------------------------------------------------------------------------||");

  140.         System.out.println ("\t|                                  CROSS ASSET COVARIANCE MATRIX                                 ||");

  141.         System.out.println ("\t|------------------------------------------------------------------------------------------------||");

  142.         String header = "\t|     |";

  143.         for (int assetIndex = 0;
  144.             assetIndex < assetIDArray.length;
  145.             ++assetIndex)
  146.         {
  147.             header += "    " + assetIDArray[assetIndex] + "     |";
  148.         }

  149.         System.out.println (header + "|");

  150.         System.out.println ("\t|------------------------------------------------------------------------------------------------||");

  151.         for (int assetIndexI = 0;
  152.             assetIndexI < assetIDArray.length;
  153.             ++assetIndexI)
  154.         {
  155.             String dump = "\t| " + assetIDArray[assetIndexI] + " ";

  156.             for (int assetIndexJ = 0;
  157.                 assetIndexJ < assetIDArray.length;
  158.                 ++assetIndexJ)
  159.             {
  160.                 dump += "|" + FormatUtil.FormatDouble (
  161.                     covarianceMatrix[assetIndexI][assetIndexJ], 1, 8, 1.
  162.                 ) + " ";
  163.             }

  164.             System.out.println (dump + "||");
  165.         }

  166.         System.out.println ("\t|------------------------------------------------------------------------------------------------||\n\n");

  167.         System.out.println ("\t|------------------||");

  168.         System.out.println ("\t|   ASSET BOUNDS   ||");

  169.         System.out.println ("\t|------------------||");

  170.         for (int assetIndex = 0;
  171.             assetIndex < assetIDArray.length;
  172.             ++assetIndex)
  173.         {
  174.             System.out.println (
  175.                 "\t| " + assetIDArray[assetIndex] + " | " +
  176.                 FormatUtil.FormatDouble (assetHoldingLowerBound, 1, 0, 100.) + "% | " +
  177.                 FormatUtil.FormatDouble (assetHoldingUpperBound, 2, 0, 100.) + "% ||"
  178.             );
  179.         }

  180.         System.out.println ("\t|------------------||\n\n");

  181.         InteriorPointBarrierControl interiorPointBarrierControl = InteriorPointBarrierControl.Standard();

  182.         System.out.println ("\t|--------------------------------------------||");

  183.         System.out.println ("\t|  INTERIOR POINT METHOD BARRIER PARAMETERS  ||");

  184.         System.out.println ("\t|--------------------------------------------||");

  185.         System.out.println (
  186.             "\t|    Barrier Decay Velocity        : " + 1. / interiorPointBarrierControl.decayVelocity()
  187.         );

  188.         System.out.println (
  189.             "\t|    Barrier Decay Steps           : " + interiorPointBarrierControl.decayStepCount()
  190.         );

  191.         System.out.println (
  192.             "\t|    Initial Barrier Strength      : " + interiorPointBarrierControl.initialStrength()
  193.         );

  194.         System.out.println (
  195.             "\t|    Barrier Convergence Tolerance : " + interiorPointBarrierControl.relativeTolerance()
  196.         );

  197.         System.out.println ("\t|--------------------------------------------||\n\n");

  198.         BoundedHoldingsAllocationControl boundedPortfolioConstructionParameters =
  199.             new BoundedHoldingsAllocationControl (
  200.                 assetIDArray,
  201.                 CustomRiskUtilitySettings.VarianceMinimizer(),
  202.                 new EqualityConstraintSettings (
  203.                     EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT,
  204.                     Double.NaN
  205.                 )
  206.             );

  207.         for (int assetIndex = 0;
  208.             assetIndex < assetIDArray.length;
  209.             ++assetIndex)
  210.         {
  211.             boundedPortfolioConstructionParameters.addBound (
  212.                 assetIDArray[assetIndex],
  213.                 assetHoldingLowerBound,
  214.                 assetHoldingUpperBound
  215.             );
  216.         }

  217.         HoldingsAllocation optimizationOutput = new ConstrainedMeanVarianceOptimizer (
  218.             interiorPointBarrierControl,
  219.             LineStepEvolutionControl.NocedalWrightStrongWolfe (
  220.                 false
  221.             )
  222.         ).allocate (
  223.             boundedPortfolioConstructionParameters,
  224.             assetUniverseStatisticalProperties
  225.         );

  226.         System.out.println ("\t|---------------||");

  227.         System.out.println ("\t| ASSET WEIGHTS ||");

  228.         System.out.println ("\t|---------------||");

  229.         for (AssetComponent assetComponent : optimizationOutput.optimalPortfolio().assetComponentArray())
  230.         {
  231.             System.out.println (
  232.                 "\t| " + assetComponent.id() + " | " + FormatUtil.FormatDouble (
  233.                     assetComponent.amount(), 2, 2, 100.
  234.                 ) + "% ||"
  235.             );
  236.         }

  237.         System.out.println ("\t|---------------||\n\n");

  238.         System.out.println ("\t|-----------------------------------------||");

  239.         System.out.println (
  240.             "\t| Portfolio Notional           : " + FormatUtil.FormatDouble (
  241.                 optimizationOutput.optimalPortfolio().notional(), 1, 4, 1.
  242.             ) + "  ||"
  243.         );

  244.         System.out.println (
  245.             "\t| Portfolio Expected Return    : " + FormatUtil.FormatDouble (
  246.                 optimizationOutput.optimalMetrics().excessReturnsMean(), 1, 4, 100.
  247.             ) + "% ||"
  248.         );

  249.         System.out.println (
  250.             "\t| Portfolio Standard Deviation : " + FormatUtil.FormatDouble (
  251.                 optimizationOutput.optimalMetrics().excessReturnsStandardDeviation(), 1, 4, 100.
  252.             ) + "% ||"
  253.         );

  254.         System.out.println ("\t|-----------------------------------------||\n");

  255.         EnvManager.TerminateEnv();
  256.     }
  257. }