UnboundedMarkovitzBulletExplicit.java

  1. package org.drip.sample.efficientfrontier;

  2. import org.drip.feed.loader.*;
  3. import org.drip.measure.statistics.MultivariateMoments;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.portfolioconstruction.allocator.*;
  6. import org.drip.portfolioconstruction.asset.AssetComponent;
  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>UnboundedMarkovitzBulletExplicit</i> demonstrates the Explicit Construction of the Efficient Frontier.
  86.  *
  87.  * <br><br>
  88.  *  <ul>
  89.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  90.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  91.  *      <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>
  92.  *      <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>
  93.  *  </ul>
  94.  * <br><br>
  95.  *
  96.  * @author Lakshmi Krishnamurthy
  97.  */

  98. public class UnboundedMarkovitzBulletExplicit
  99. {

  100.     private static double DisplayPortfolioMetrics (
  101.         final HoldingsAllocation optimizationOutput)
  102.         throws Exception
  103.     {
  104.         AssetComponent[] globalMinimumAssetComponentArray =
  105.             optimizationOutput.optimalPortfolio().assetComponentArray();

  106.         String dump = "\t|" + FormatUtil.FormatDouble (
  107.                 optimizationOutput.optimalMetrics().excessReturnsMean(), 1, 4, 100.
  108.             ) + "% |" + FormatUtil.FormatDouble (
  109.                 optimizationOutput.optimalMetrics().excessReturnsStandardDeviation(), 1, 4, 100.
  110.             ) + " |";

  111.         for (AssetComponent assetComponent : globalMinimumAssetComponentArray)
  112.         {
  113.             dump += " " + FormatUtil.FormatDouble (
  114.                 assetComponent.amount(), 3, 2, 100.
  115.             ) + "% |";
  116.         }

  117.         System.out.println (dump + "|");

  118.         return optimizationOutput.optimalMetrics().excessReturnsMean();
  119.     }

  120.     public static final void main (
  121.         final String[] agrumentArray)
  122.         throws Exception
  123.     {
  124.         EnvManager.InitEnv ("");

  125.         int riskReturnGranularity = 40;
  126.         double riskToleranceFactor = 0.;
  127.         String seriesLocation =
  128.             "C:\\DROP\\Daemons\\Feeds\\MeanVarianceOptimizer\\FormattedSeries1.csv";

  129.         CSVGrid csvGrid = CSVParser.NamedStringGrid (
  130.             seriesLocation
  131.         );

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

  133.         String[] assetIDArray = new String[variateHeaderArray.length - 1];
  134.         double[][] variateSampleGrid = new double[variateHeaderArray.length - 1][];

  135.         for (int assetIndex = 0;
  136.             assetIndex < assetIDArray.length;
  137.             ++assetIndex)
  138.         {
  139.             assetIDArray[assetIndex] = variateHeaderArray[assetIndex + 1];

  140.             variateSampleGrid[assetIndex] = csvGrid.doubleArrayAtColumn (assetIndex + 1);
  141.         }

  142.         AssetUniverseStatisticalProperties assetUniverseStatisticalProperties =
  143.             AssetUniverseStatisticalProperties.FromMultivariateMetrics (
  144.                 MultivariateMoments.Standard (
  145.                     assetIDArray,
  146.                     variateSampleGrid
  147.                 )
  148.             );

  149.         HoldingsAllocationControl portfolioConstructionParameters =
  150.             new HoldingsAllocationControl (
  151.                 assetIDArray,
  152.                 CustomRiskUtilitySettings.RiskTolerant (
  153.                     riskToleranceFactor
  154.                 ),
  155.                 new EqualityConstraintSettings (
  156.                     EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT,
  157.                     Double.NaN
  158.                 )
  159.             );

  160.         MeanVarianceOptimizer meanVarianceOptimizer = new QuadraticMeanVarianceOptimizer();

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

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

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

  164.         String header = "\t| RETURNS | RISK % |";

  165.         for (int assetIndex = 0;
  166.             assetIndex < assetIDArray.length;
  167.             ++assetIndex)
  168.         {
  169.             header += "   " + assetIDArray[assetIndex] + "    |";
  170.         }

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

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

  173.         double globalMinimumVarianceReturns = DisplayPortfolioMetrics (
  174.             meanVarianceOptimizer.globalMinimumVarianceAllocate (
  175.                 portfolioConstructionParameters,
  176.                 assetUniverseStatisticalProperties
  177.             )
  178.         );

  179.         double maximumReturns = DisplayPortfolioMetrics (
  180.             meanVarianceOptimizer.longOnlyMaximumReturnsAllocate (
  181.                 portfolioConstructionParameters,
  182.                 assetUniverseStatisticalProperties
  183.             )
  184.         );

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

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

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

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

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

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

  191.         double returnsGrain = (maximumReturns - globalMinimumVarianceReturns) / riskReturnGranularity;

  192.         for (int returnOffset = 0;
  193.             returnOffset <= riskReturnGranularity;
  194.             ++returnOffset)
  195.         {
  196.             DisplayPortfolioMetrics (
  197.                 meanVarianceOptimizer.allocate (
  198.                     new HoldingsAllocationControl (
  199.                         assetIDArray,
  200.                         CustomRiskUtilitySettings.VarianceMinimizer(),
  201.                         new EqualityConstraintSettings (
  202.                             EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT |
  203.                                 EqualityConstraintSettings.RETURNS_CONSTRAINT,
  204.                             globalMinimumVarianceReturns + returnOffset * returnsGrain
  205.                         )
  206.                     ),
  207.                     assetUniverseStatisticalProperties
  208.                 )
  209.             );
  210.         }

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

  212.         EnvManager.TerminateEnv();
  213.     }
  214. }