DualConstrainedVariateConvergence.java

  1. package org.drip.sample.assetallocation;

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

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

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

  84. /**
  85.  * <i>DualConstrainedVariateConvergence</i> demonstrates the Sequential Convergence of the Constrained
  86.  * Optimal R<sup>d</sup> Space.
  87.  *  
  88.  * <br><br>
  89.  *  <ul>
  90.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  91.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  92.  *      <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>
  93.  *      <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>
  94.  *  </ul>
  95.  * <br><br>
  96.  *
  97.  * @author Lakshmi Krishnamurthy
  98.  */

  99. public class DualConstrainedVariateConvergence
  100. {

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

  109.         VariateInequalityConstraintMultiplier.s_preBoundBlog = true;
  110.         VariateInequalityConstraintMultiplier.s_postBoundBlog = true;

  111.         String[] assetIDArray = new String[]
  112.         {
  113.             "TOK",
  114.             "EWJ",
  115.             "HYG",
  116.             "LQD",
  117.             "EMD",
  118.             "GSG",
  119.             "BWX"
  120.         };
  121.         double[] assetHoldingsLowerBoundArray = new double[]
  122.         {
  123.             0.05,
  124.             0.04,
  125.             0.06,
  126.             0.03,
  127.             0.03,
  128.             0.03,
  129.             0.13
  130.         };
  131.         double[] assetHoldingsUpperBoundArray = new double[]
  132.         {
  133.             0.43,
  134.             0.27,
  135.             0.44,
  136.             0.32,
  137.             0.66,
  138.             0.32,
  139.             0.88
  140.         };
  141.         double[] assetExpectedReturnsArray = new double[]
  142.         {
  143.             0.1300,
  144.             0.0700,
  145.             0.0400,
  146.             0.0300,
  147.             0.0800,
  148.             0.1000,
  149.             0.0100
  150.         };
  151.         double portfolioDesignReturn = 0.06000;
  152.         double[][] assetReturnsCovarianceMatrix = new double[][]
  153.         {
  154.             {0.002733 * 12, 0.002083 * 12, 0.001593 * 12, 0.000488 * 12, 0.001172 * 12, 0.002312 * 12, 0.000710 * 12},
  155.             {0.002083 * 12, 0.002768 * 12, 0.001302 * 12, 0.000457 * 12, 0.001105 * 12, 0.001647 * 12, 0.000563 * 12},
  156.             {0.001593 * 12, 0.001302 * 12, 0.001463 * 12, 0.000639 * 12, 0.001050 * 12, 0.001110 * 12, 0.000519 * 12},
  157.             {0.000488 * 12, 0.000457 * 12, 0.000639 * 12, 0.000608 * 12, 0.000663 * 12, 0.000042 * 12, 0.000370 * 12},
  158.             {0.001172 * 12, 0.001105 * 12, 0.001050 * 12, 0.000663 * 12, 0.001389 * 12, 0.000825 * 12, 0.000661 * 12},
  159.             {0.002312 * 12, 0.001647 * 12, 0.001110 * 12, 0.000042 * 12, 0.000825 * 12, 0.005211 * 12, 0.000749 * 12},
  160.             {0.000710 * 12, 0.000563 * 12, 0.000519 * 12, 0.000370 * 12, 0.000661 * 12, 0.000749 * 12, 0.000703 * 12}
  161.         };

  162.         AssetUniverseStatisticalProperties assetUniverseStatisticalProperties =
  163.             AssetUniverseStatisticalProperties.FromMultivariateMetrics (
  164.                 MultivariateMoments.Standard (
  165.                     assetIDArray,
  166.                     assetExpectedReturnsArray,
  167.                     assetReturnsCovarianceMatrix
  168.                 )
  169.             );

  170.         double[][] covarianceMatrix = assetUniverseStatisticalProperties.covariance (
  171.             assetIDArray
  172.         );

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

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

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

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

  177.         for (int assetIndex = 0;
  178.             assetIndex < assetIDArray.length;
  179.             ++assetIndex)
  180.         {
  181.             header += "    " + assetIDArray[assetIndex] + "     |";
  182.         }

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

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

  185.         for (int assetIndexI = 0;
  186.             assetIndexI < assetIDArray.length;
  187.             ++assetIndexI)
  188.         {
  189.             String dump = "\t| " + assetIDArray[assetIndexI] + " ";

  190.             for (int assetIndexJ = 0;
  191.                 assetIndexJ < assetIDArray.length;
  192.                 ++assetIndexJ)
  193.             {
  194.                 dump += "|" + FormatUtil.FormatDouble (
  195.                     covarianceMatrix[assetIndexI][assetIndexJ], 1, 8, 1.
  196.                 ) + " ";
  197.             }

  198.             System.out.println (dump + "||");
  199.         }

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

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

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

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

  204.         for (int assetIndex = 0;
  205.             assetIndex < assetIDArray.length;
  206.             ++assetIndex)
  207.         {
  208.             System.out.println (
  209.                 "\t| " + assetIDArray[assetIndex] + " | " +
  210.                 FormatUtil.FormatDouble (assetHoldingsLowerBoundArray[assetIndex], 2, 0, 100.) + "% | " +
  211.                 FormatUtil.FormatDouble (assetHoldingsUpperBoundArray[assetIndex], 2, 0, 100.) + "% ||"
  212.             );
  213.         }

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

  215.         InteriorPointBarrierControl interiorPointBarrierControl = InteriorPointBarrierControl.Standard();

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

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

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

  219.         System.out.println (
  220.             "\t|    Barrier Decay Velocity        : " + 1. / interiorPointBarrierControl.decayVelocity()
  221.         );

  222.         System.out.println (
  223.             "\t|    Barrier Decay Steps           : " + interiorPointBarrierControl.decayStepCount()
  224.         );

  225.         System.out.println (
  226.             "\t|    Initial Barrier Strength      : " + interiorPointBarrierControl.initialStrength()
  227.         );

  228.         System.out.println (
  229.             "\t|    Barrier Convergence Tolerance : " + interiorPointBarrierControl.relativeTolerance()
  230.         );

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

  232.         BoundedHoldingsAllocationControl boundedPortfolioConstructionParameters =
  233.             new BoundedHoldingsAllocationControl (
  234.                 assetIDArray,
  235.                 CustomRiskUtilitySettings.VarianceMinimizer(),
  236.                 new EqualityConstraintSettings (
  237.                     EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT |
  238.                         EqualityConstraintSettings.RETURNS_CONSTRAINT,
  239.                     portfolioDesignReturn
  240.                 )
  241.             );

  242.         for (int assetIndex = 0;
  243.             assetIndex < assetIDArray.length;
  244.             ++assetIndex)
  245.         {
  246.             boundedPortfolioConstructionParameters.addBound (
  247.                 assetIDArray[assetIndex],
  248.                 assetHoldingsLowerBoundArray[assetIndex],
  249.                 assetHoldingsUpperBoundArray[assetIndex]
  250.             );
  251.         }

  252.         new ConstrainedMeanVarianceOptimizer (
  253.             interiorPointBarrierControl,
  254.             LineStepEvolutionControl.NocedalWrightStrongWolfe (
  255.                 false
  256.             )
  257.         ).allocate (
  258.             boundedPortfolioConstructionParameters,
  259.             assetUniverseStatisticalProperties
  260.         );

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