UnboundedMarkovitzBullet.java

  1. package org.drip.sample.efficientfrontier;

  2. import java.util.*;

  3. import org.drip.feed.loader.*;
  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.asset.AssetComponent;
  8. import org.drip.portfolioconstruction.mpt.MarkovitzBullet;
  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>UnboundedMarkovitzBullet</i> demonstrates the Construction of the Efficient Frontier using the
  88.  * Unconstrained Quadratic Mean Variance Optimizer.
  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/efficientfrontier/README.md">Efficient Frontier Markovitz Bullet Variants</a></li>
  96.  *  </ul>
  97.  * <br><br>
  98.  *
  99.  * @author Lakshmi Krishnamurthy
  100.  */

  101. public class UnboundedMarkovitzBullet
  102. {

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

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

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

  120.         System.out.println (strDump + "|");
  121.     }

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

  127.         int frontierSampleUnits = 20;
  128.         double riskToleranceFactor = 0.;
  129.         String seriesLocation =
  130.             "C:\\DROP\\Daemons\\Feeds\\MeanVarianceOptimizer\\FormattedSeries1.csv";

  131.         CSVGrid csvGrid = CSVParser.NamedStringGrid (
  132.             seriesLocation
  133.         );

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

  135.         String[] assetIDArray = new String[variateHeaderArray.length - 1];
  136.         double[][] variateSampleGrid = new double[variateHeaderArray.length - 1][];

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

  142.             variateSampleGrid[assetIndex] = csvGrid.doubleArrayAtColumn (assetIndex + 1);
  143.         }

  144.         MarkovitzBullet markovitzBullet = new QuadraticMeanVarianceOptimizer().efficientFrontier (
  145.             new HoldingsAllocationControl (
  146.                 assetIDArray,
  147.                 CustomRiskUtilitySettings.RiskTolerant (riskToleranceFactor),
  148.                 new EqualityConstraintSettings (
  149.                     EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT,
  150.                     Double.NaN
  151.                 )
  152.             ),
  153.             AssetUniverseStatisticalProperties.FromMultivariateMetrics (
  154.                 MultivariateMoments.Standard (
  155.                     assetIDArray,
  156.                     variateSampleGrid
  157.                 )
  158.             ),
  159.             frontierSampleUnits
  160.         );

  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.         DisplayPortfolioMetrics (markovitzBullet.globalMinimumVariance());

  174.         DisplayPortfolioMetrics (markovitzBullet.longOnlyMaximumReturns());

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

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

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

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

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

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

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

  182.         for (Map.Entry<Double, HoldingsAllocation> me : frontierPortfolioMap.entrySet())
  183.         {
  184.             DisplayPortfolioMetrics (
  185.                 me.getValue()
  186.             );
  187.         }

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

  189.         EnvManager.TerminateEnv();
  190.     }
  191. }