LongOnlyMarkovitzBullet.java

  1. package org.drip.sample.efficientfrontier;

  2. import java.util.*;

  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.mpt.MarkovitzBullet;
  10. import org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties;
  11. import org.drip.service.env.EnvManager;

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

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

  87. /**
  88.  * <i>LongOnlyMarkovitzBullet</i> demonstrates the Construction of the Efficient Frontier using the
  89.  * Constrained Mean Variance Optimizer for a Long-Only Portfolio.
  90.  *
  91.  * <br><br>
  92.  *  <ul>
  93.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  94.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  95.  *      <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>
  96.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/efficientfrontier/README.md">Efficient Frontier Markovitz Bullet Variants</a></li>
  97.  *  </ul>
  98.  * <br><br>
  99.  *
  100.  * @author Lakshmi Krishnamurthy
  101.  */

  102. public class LongOnlyMarkovitzBullet
  103. {

  104.     private static void DisplayPortfolioMetrics (
  105.         final HoldingsAllocation optimizationOutput)
  106.         throws Exception
  107.     {
  108.         AssetComponent[] globalMinimumAssetComponentArray =
  109.             optimizationOutput.optimalPortfolio().assetComponentArray();

  110.         String dump = "\t|" + FormatUtil.FormatDouble (
  111.                 optimizationOutput.optimalMetrics().excessReturnsMean(), 1, 4, 100.
  112.             ) + "% |" + FormatUtil.FormatDouble (
  113.                 optimizationOutput.optimalMetrics().excessReturnsStandardDeviation(), 1, 4, 100.
  114.             ) + " |";

  115.         for (AssetComponent assetComponent : globalMinimumAssetComponentArray)
  116.         {
  117.             dump += " " + FormatUtil.FormatDouble (
  118.                 assetComponent.amount(), 3, 2, 100.
  119.             ) + "% |";
  120.         }

  121.         System.out.println (dump + "|");
  122.     }

  123.     public static final void main (
  124.         final String[] agrumentArray)
  125.         throws Exception
  126.     {
  127.         EnvManager.InitEnv ("");

  128.         String[] assetNameArray = new String[]
  129.         {
  130.             "TOK",
  131.             "EWJ",
  132.             "HYG",
  133.             "LQD",
  134.             "EMD",
  135.             "GSG",
  136.             "BWX"
  137.         };
  138.         double[] assetHoldingsLowerBoundArray = new double[]
  139.         {
  140.             0.00,
  141.             0.00,
  142.             0.00,
  143.             0.00,
  144.             0.00,
  145.             0.00,
  146.             0.00
  147.         };
  148.         double[] assetHoldingsUpperBoundArray = new double[]
  149.         {
  150.             1.00,
  151.             1.00,
  152.             1.00,
  153.             1.00,
  154.             1.00,
  155.             1.00,
  156.             1.00
  157.         };
  158.         double[] expectedAssetReturnsArray = new double[]
  159.         {
  160.             0.008430,
  161.             0.007230,
  162.             0.006450,
  163.             0.002560,
  164.             0.004480,
  165.             0.006840,
  166.             0.001670
  167.         };

  168.         double[][] assetReturnsCovarianceMatrix = new double[][]
  169.         {
  170.             {0.002733, 0.002083, 0.001593, 0.000488, 0.001172, 0.002312, 0.000710},
  171.             {0.002083, 0.002768, 0.001302, 0.000457, 0.001105, 0.001647, 0.000563},
  172.             {0.001593, 0.001302, 0.001463, 0.000639, 0.001050, 0.001110, 0.000519},
  173.             {0.000488, 0.000457, 0.000639, 0.000608, 0.000663, 0.000042, 0.000370},
  174.             {0.001172, 0.001105, 0.001050, 0.000663, 0.001389, 0.000825, 0.000661},
  175.             {0.002312, 0.001647, 0.001110, 0.000042, 0.000825, 0.005211, 0.000749},
  176.             {0.000710, 0.000563, 0.000519, 0.000370, 0.000661, 0.000749, 0.000703}
  177.         };
  178.         int frontierSampleUnits = 20;

  179.         AssetUniverseStatisticalProperties ausp = AssetUniverseStatisticalProperties.FromMultivariateMetrics (
  180.             MultivariateMoments.Standard (
  181.                 assetNameArray,
  182.                 expectedAssetReturnsArray,
  183.                 assetReturnsCovarianceMatrix
  184.             )
  185.         );

  186.         double[][] aadblCovarianceMatrix = ausp.covariance (
  187.             assetNameArray
  188.         );

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

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

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

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

  193.         for (int assetIndex = 0;
  194.             assetIndex < assetNameArray.length;
  195.             ++assetIndex)
  196.         {
  197.             header += "    " + assetNameArray[assetIndex] + "     |";
  198.         }

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

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

  201.         for (int assetIndexI = 0;
  202.             assetIndexI < assetNameArray.length;
  203.             ++assetIndexI)
  204.         {
  205.             String strDump = "\t| " + assetNameArray[assetIndexI] + " ";

  206.             for (int assetIndexJ = 0;
  207.                 assetIndexJ < assetNameArray.length;
  208.                 ++assetIndexJ)
  209.             {
  210.                 strDump += "|" + FormatUtil.FormatDouble (
  211.                     aadblCovarianceMatrix[assetIndexI][assetIndexJ], 1, 8, 1.
  212.                 ) + " ";
  213.             }

  214.             System.out.println (strDump + "||");
  215.         }

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

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

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

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

  220.         for (int assetIndex = 0;
  221.             assetIndex < assetNameArray.length;
  222.             ++assetIndex)
  223.         {
  224.             System.out.println (
  225.                 "\t| " + assetNameArray[assetIndex] + " | " +
  226.                 FormatUtil.FormatDouble (
  227.                     assetHoldingsLowerBoundArray[assetIndex], 2, 0, 100.
  228.                 ) + "% | " + FormatUtil.FormatDouble (
  229.                     assetHoldingsUpperBoundArray[assetIndex], 2, 0, 100.
  230.                 ) + "% ||"
  231.             );
  232.         }

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

  234.         InteriorPointBarrierControl interiorPointBarrierControl = InteriorPointBarrierControl.Standard();

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

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

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

  238.         System.out.println (
  239.             "\t|    Barrier Decay Velocity        : " + 1. / interiorPointBarrierControl.decayVelocity()
  240.         );

  241.         System.out.println (
  242.             "\t|    Barrier Decay Steps           : " + interiorPointBarrierControl.decayStepCount()
  243.         );

  244.         System.out.println (
  245.             "\t|    Initial Barrier Strength      : " + interiorPointBarrierControl.initialStrength()
  246.         );

  247.         System.out.println (
  248.             "\t|    Barrier Convergence Tolerance : " + interiorPointBarrierControl.relativeTolerance()
  249.         );

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

  251.         BoundedHoldingsAllocationControl boundedPortfolioConstructionParameters =
  252.             new BoundedHoldingsAllocationControl (
  253.                 assetNameArray,
  254.                 CustomRiskUtilitySettings.VarianceMinimizer(),
  255.                 new EqualityConstraintSettings (
  256.                     EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT,
  257.                     Double.NaN
  258.                 )
  259.             );

  260.         for (int assetIndex = 0;
  261.             assetIndex < assetNameArray.length;
  262.             ++assetIndex)
  263.         {
  264.             boundedPortfolioConstructionParameters.addBound (
  265.                 assetNameArray[assetIndex],
  266.                 assetHoldingsLowerBoundArray[assetIndex],
  267.                 assetHoldingsUpperBoundArray[assetIndex]
  268.             );
  269.         }

  270.         MarkovitzBullet markovitzBullet = new ConstrainedMeanVarianceOptimizer (
  271.             interiorPointBarrierControl,
  272.             LineStepEvolutionControl.NocedalWrightStrongWolfe (
  273.                 false
  274.             )
  275.         ).efficientFrontier (
  276.             boundedPortfolioConstructionParameters,
  277.             ausp,
  278.             frontierSampleUnits
  279.         );

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

  281.         System.out.println ("\t|                     GLOBAL MINIMUM VARIANCE AND MAXIMUM RETURNS PORTFOLIOS                    ||");

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

  283.         header = "\t| RETURNS | RISK % |";

  284.         for (int assetIndex = 0;
  285.             assetIndex < assetNameArray.length;
  286.             ++assetIndex)
  287.         {
  288.             header += "   " + assetNameArray[assetIndex] + "    |";
  289.         }

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

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

  292.         DisplayPortfolioMetrics (
  293.             markovitzBullet.globalMinimumVariance()
  294.         );

  295.         DisplayPortfolioMetrics (
  296.             markovitzBullet.longOnlyMaximumReturns()
  297.         );

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

  299.         TreeMap<Double, HoldingsAllocation> frontierPortfolioMap = markovitzBullet.optimalPortfolioMap();

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

  301.         System.out.println ("\t|         EFFICIENT FRONTIER: PORTFOLIO RISK & RETURNS + CORRESPONDING ASSET ALLOCATION         ||");

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

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

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

  305.         for (Map.Entry<Double, HoldingsAllocation> me : frontierPortfolioMap.entrySet())
  306.         {
  307.             DisplayPortfolioMetrics (
  308.                 me.getValue()
  309.             );
  310.         }

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

  312.         EnvManager.TerminateEnv();
  313.     }
  314. }