ReturnsConstrainedVarianceMinimizer.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>ReturnsConstrainedVarianceMinimizer</i> demonstrates the Construction of an Optimal Portfolio using the
  88.  * Variance Minimizing Allocator with Weight Normalization Constraints and Design Returns 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 ReturnsConstrainedVarianceMinimizer
  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 designReturn = 0.0026;
  117.         double assetHoldingsLowerBound = 0.05;
  118.         double assetHoldingsUpperBound = 0.65;
  119.         String[] assetIDArray = new String[variateHeaderArray.length - 1];
  120.         double[][] variateSampleGrid = new double[variateHeaderArray.length - 1][];

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

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

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

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

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

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

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

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

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

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

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

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

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

  165.             System.out.println (strDump + "||");
  166.         }

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

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

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

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

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

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

  182.         InteriorPointBarrierControl interiorPointBarrierControl = InteriorPointBarrierControl.Standard();

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

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

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

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

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

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

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

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

  199.         BoundedHoldingsAllocationControl boundedPortfolioConstructionParameters =
  200.             new BoundedHoldingsAllocationControl (
  201.                 assetIDArray,
  202.                 CustomRiskUtilitySettings.VarianceMinimizer(),
  203.                 new EqualityConstraintSettings (
  204.                     EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT |
  205.                         EqualityConstraintSettings.RETURNS_CONSTRAINT,
  206.                     designReturn
  207.                 )
  208.             );

  209.         for (int assetIndex = 0;
  210.             assetIndex < assetIDArray.length;
  211.             ++assetIndex)
  212.         {
  213.             boundedPortfolioConstructionParameters.addBound (
  214.                 assetIDArray[assetIndex],
  215.                 assetHoldingsLowerBound,
  216.                 assetHoldingsUpperBound
  217.             );
  218.         }

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

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

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

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

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

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

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

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

  246.         System.out.println (
  247.             "\t| Portfolio Design Return      : " + FormatUtil.FormatDouble (
  248.                 designReturn, 1, 4, 100.
  249.             ) + "% ||"
  250.         );

  251.         System.out.println (
  252.             "\t| Portfolio Expected Return    : " + FormatUtil.FormatDouble (
  253.                 optimizationOutput.optimalMetrics().excessReturnsMean(), 1, 4, 100.
  254.             ) + "% ||"
  255.         );

  256.         System.out.println (
  257.             "\t| Portfolio Standard Deviation : " + FormatUtil.FormatDouble (
  258.                 optimizationOutput.optimalMetrics().excessReturnsStandardDeviation(), 1, 4, 100.
  259.             ) + "% ||"
  260.         );

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

  262.         EnvManager.TerminateEnv();
  263.     }
  264. }