MeanVarianceOptimizer.java

  1. package org.drip.portfolioconstruction.allocator;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

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

  77. /**
  78.  * <i>MeanVarianceOptimizer</i> exposes Portfolio Construction using Mean Variance Optimization Techniques.
  79.  *
  80.  *  <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  84.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/portfolioconstruction/README.md">Portfolio Construction under Allocation Constraints</a></li>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/portfolioconstruction/allocator/README.md">MVO Based Portfolio Allocation Construction</a></li>
  86.  *  </ul>
  87.  *
  88.  * @author Lakshmi Krishnamurthy
  89.  */

  90. public abstract class MeanVarianceOptimizer
  91. {

  92.     protected abstract org.drip.portfolioconstruction.allocator.HoldingsAllocationControl
  93.         constrainedPCP (
  94.             final org.drip.portfolioconstruction.allocator.HoldingsAllocationControl
  95.                 designPortfolioConstructionParameters,
  96.             final double returnsConstraint);

  97.     /**
  98.      * Allocate the Long-Only Maximum Returns Portfolio
  99.      *
  100.      * @param portfolioConstructionParameters The Portfolio Construction Parameters
  101.      * @param assetUniverseStatisticalProperties The Asset Universe Statistical Properties Instance
  102.      *
  103.      * @return The Long-Only Maximum Returns Portfolio
  104.      */

  105.     public abstract org.drip.portfolioconstruction.allocator.HoldingsAllocation
  106.         longOnlyMaximumReturnsAllocate (
  107.             final org.drip.portfolioconstruction.allocator.HoldingsAllocationControl
  108.                 portfolioConstructionParameters,
  109.             final org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties
  110.                 assetUniverseStatisticalProperties);

  111.     /**
  112.      * Allocate the Global Minimum Variance Portfolio without any Returns Constraints in the Parameters
  113.      *
  114.      * @param portfolioConstructionParameters The Portfolio Construction Parameters
  115.      * @param assetUniverseStatisticalProperties The Asset Universe Statistical Properties Instance
  116.      *
  117.      * @return The Global Minimum Variance Portfolio
  118.      */

  119.     public abstract org.drip.portfolioconstruction.allocator.HoldingsAllocation
  120.         globalMinimumVarianceAllocate (
  121.             final org.drip.portfolioconstruction.allocator.HoldingsAllocationControl
  122.                 portfolioConstructionParameters,
  123.             final org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties
  124.                 assetUniverseStatisticalProperties);

  125.     /**
  126.      * Allocate the Optimal Portfolio Weights given the Portfolio Construction Parameters
  127.      *
  128.      * @param portfolioConstructionParameters The Portfolio Construction Parameters
  129.      * @param assetUniverseStatisticalProperties The Asset Universe Statistical Properties Instance
  130.      *
  131.      * @return The Optimal Portfolio
  132.      */

  133.     public abstract org.drip.portfolioconstruction.allocator.HoldingsAllocation allocate (
  134.         final org.drip.portfolioconstruction.allocator.HoldingsAllocationControl
  135.             portfolioConstructionParameters,
  136.         final org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties
  137.             assetUniverseStatisticalProperties);

  138.     /**
  139.      * Generate the Efficient Frontier given the Portfolio Construction Parameters
  140.      *
  141.      * @param portfolioConstructionParameters The Portfolio Construction Parameters
  142.      * @param assetUniverseStatisticalProperties The Asset Universe Statistical Properties Instance
  143.      * @param frontierSampleUnits The Number of Frontier Sample Units
  144.      *
  145.      * @return The Efficient Frontier
  146.      */

  147.     public org.drip.portfolioconstruction.mpt.MarkovitzBullet efficientFrontier (
  148.         final org.drip.portfolioconstruction.allocator.HoldingsAllocationControl
  149.             portfolioConstructionParameters,
  150.         final org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties
  151.             assetUniverseStatisticalProperties,
  152.         final int frontierSampleUnits)
  153.     {
  154.         if (0 >= frontierSampleUnits)
  155.         {
  156.             return null;
  157.         }

  158.         org.drip.portfolioconstruction.allocator.HoldingsAllocation globalMinimumVarianceOptimizationOutput =
  159.             globalMinimumVarianceAllocate (
  160.                 portfolioConstructionParameters,
  161.                 assetUniverseStatisticalProperties
  162.             );

  163.         if (null == globalMinimumVarianceOptimizationOutput)
  164.         {
  165.             return null;
  166.         }

  167.         org.drip.portfolioconstruction.allocator.HoldingsAllocation longOnlyMaximumReturnsOptimizationOutput
  168.             = longOnlyMaximumReturnsAllocate (
  169.                 portfolioConstructionParameters,
  170.                 assetUniverseStatisticalProperties
  171.             );

  172.         if (null == longOnlyMaximumReturnsOptimizationOutput)
  173.         {
  174.             return null;
  175.         }

  176.         double globalMinimumVarianceReturns =
  177.             globalMinimumVarianceOptimizationOutput.optimalMetrics().excessReturnsMean();

  178.         double longOnlyMaximumReturns =
  179.             longOnlyMaximumReturnsOptimizationOutput.optimalMetrics().excessReturnsMean();

  180.         double returnsConstraintGridWidth = (longOnlyMaximumReturns - globalMinimumVarianceReturns) /
  181.             frontierSampleUnits;
  182.         double returnsConstraint = globalMinimumVarianceReturns + returnsConstraintGridWidth;
  183.         org.drip.portfolioconstruction.mpt.MarkovitzBullet markovitzBullet = null;

  184.         try
  185.         {
  186.             markovitzBullet = new org.drip.portfolioconstruction.mpt.MarkovitzBullet (
  187.                 globalMinimumVarianceOptimizationOutput,
  188.                 longOnlyMaximumReturnsOptimizationOutput
  189.             );
  190.         }
  191.         catch (java.lang.Exception e)
  192.         {
  193.             e.printStackTrace();

  194.             return null;
  195.         }

  196.         while (returnsConstraint <= longOnlyMaximumReturns)
  197.         {
  198.             try
  199.             {
  200.                 markovitzBullet.addOptimalPortfolio (
  201.                     allocate (
  202.                         constrainedPCP (
  203.                             portfolioConstructionParameters,
  204.                             returnsConstraint
  205.                         ),
  206.                         assetUniverseStatisticalProperties
  207.                     )
  208.                 );
  209.             }
  210.             catch (java.lang.Exception e)
  211.             {
  212.                 e.printStackTrace();

  213.                 return null;
  214.             }

  215.             returnsConstraint += returnsConstraintGridWidth;
  216.         }

  217.         return markovitzBullet;
  218.     }
  219. }