HeuristicCardinalityBoundOptimizer01.java

  1. package org.drip.sample.tadonkivial;

  2. import org.drip.function.rdtor1descent.LineStepEvolutionControl;
  3. import org.drip.function.rdtor1solver.InteriorPointBarrierControl;
  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.Portfolio;
  8. import org.drip.portfolioconstruction.cardinality.UpperBoundHoldingsAllocationControl;
  9. import org.drip.portfolioconstruction.cardinality.TadonkiVialHoldingsAllocation;
  10. import org.drip.portfolioconstruction.cardinality.TadonkiVialMeanVarianceOptimizer;
  11. import org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties;
  12. import org.drip.service.env.EnvManager;

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

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

  85. /**
  86.  * <i>HeuristicCardinalityBoundOptimizer01</i> demonstrates the Setup and Execution of a Cardinality Bounded
  87.  *  Portfolio Allocator with Asset Level Bounds using the Tadonki-Vial (2004) Heuristics. The References are:
  88.  *
  89.  * <br><br>
  90.  *  <ul>
  91.  *      <li>
  92.  *          Chang, T., J., N. Meade, J. E. Beasley, and Y. M. Sharaiha (2000): Heuristics for Cardinality
  93.  *              Constrained Portfolio Optimization <i>Computers and Operations Research</i> <b>27 (13)</b>
  94.  *              1271-1302
  95.  *      </li>
  96.  *      <li>
  97.  *          Chvatal, V. (1973): Edmonds Polytopes in a Hierarchy of Combinatorial Problems <i>Discrete
  98.  *              Mathematics</i> <b>4 (4)</b> 305-337
  99.  *      </li>
  100.  *      <li>
  101.  *          Jobst, N. J., M. D. Horniman, C. A. Lucas, and G. Mitra (2001): Computational Aspects of
  102.  *              Alternative Portfolio Selection Models in the Presence of Discrete Asset Choice Constraints
  103.  *              <i>Quantitative Finance</i> <b>1 (5)</b> 1-13
  104.  *      </li>
  105.  *      <li>
  106.  *          Letchford, A. N. and A. Lodi (2002): Strengthening Chvatal-Gomory Cuts and Gomory Fractional Cuts
  107.  *              <i>Operations Research Letters</i> <b>30 (2)</b> 74-82
  108.  *      </li>
  109.  *      <li>
  110.  *          Tadonki, C., and J. P. Vial (2004): Portfolio Selection with Cardinality and Bound Constraints
  111.  *              https://www.cri.ensmp.fr/~tadonki/PaperForWeb/Tadonki_PF.pdf
  112.  *      </li>
  113.  *  </ul>
  114.  *  
  115.  * <br><br>
  116.  *  <ul>
  117.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  118.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  119.  *      <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>
  120.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/assetallocationexcel/README.md">Asset-Bound Allocator Excel Reconciliation</a></li>
  121.  *  </ul>
  122.  * <br><br>
  123.  *
  124.  * @author Lakshmi Krishnamurthy
  125.  */

  126. public class HeuristicCardinalityBoundOptimizer01
  127. {

  128.     private static final void PrintPortfolio (
  129.         final String header,
  130.         final HoldingsAllocation holdingsAllocation)
  131.     {
  132.         if (null == holdingsAllocation)
  133.         {
  134.             return;
  135.         }

  136.         Portfolio optimalPortfolio = holdingsAllocation.optimalPortfolio();

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

  138.         System.out.println ("\t|  " + header);

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

  140.         System.out.println ("\t| ASSET |   DROP   ||");

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

  142.         for (int assetIndex = 0;
  143.             assetIndex < optimalPortfolio.assetComponentArray().length;
  144.             ++assetIndex)
  145.         {
  146.             System.out.println (
  147.                 "\t|  " + optimalPortfolio.assetComponentArray()[assetIndex].id() + "  |" +
  148.                 FormatUtil.FormatDouble (
  149.                     optimalPortfolio.assetComponentArray()[assetIndex].amount(), 2, 4, 100.
  150.                 ) + "% ||"
  151.             );
  152.         }

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

  154.         System.out.println ("\t| Cardinality => " + optimalPortfolio.cardinality());

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

  156.         System.out.println();
  157.     }

  158.     public static final void main (
  159.         final String[] argumentArray)
  160.         throws Exception
  161.     {
  162.         EnvManager.InitEnv (
  163.             "",
  164.             true
  165.         );

  166.         int cardinalityUpperBound = 4;
  167.         String[] assetIDArray = new String[]
  168.         {
  169.             "TOK",
  170.             "EWJ",
  171.             "HYG",
  172.             "LQD",
  173.             "EMD",
  174.             "GSG",
  175.             "BWX"
  176.         };
  177.         double[] assetHoldingsLowerBoundArray = new double[]
  178.         {
  179.             0.05,
  180.             0.05,
  181.             0.05,
  182.             0.10,
  183.             0.05,
  184.             0.05,
  185.             0.03
  186.         };
  187.         double[] assetHoldingsUpperBoundArray = new double[]
  188.         {
  189.             0.40,
  190.             0.40,
  191.             0.30,
  192.             0.60,
  193.             0.35,
  194.             0.15,
  195.             0.50
  196.         };
  197.         double[] expectedAssetReturnsArray = new double[]
  198.         {
  199.             0.008355,
  200.             0.007207,
  201.             0.006279,
  202.             0.002466,
  203.             0.004472,
  204.             0.006821,
  205.             0.001570
  206.         };
  207.         double portfolioDesignReturn = 0.005497;
  208.         double[][] assetReturnsCovarianceMatrix = new double[][]
  209.         {
  210.             {0.002733, 0.002083, 0.001593, 0.000488, 0.001172, 0.002312, 0.000710},
  211.             {0.002083, 0.002768, 0.001302, 0.000457, 0.001105, 0.001647, 0.000563},
  212.             {0.001593, 0.001302, 0.001463, 0.000639, 0.001050, 0.001110, 0.000519},
  213.             {0.000488, 0.000457, 0.000639, 0.000608, 0.000663, 0.000042, 0.000370},
  214.             {0.001172, 0.001105, 0.001050, 0.000663, 0.001389, 0.000825, 0.000661},
  215.             {0.002312, 0.001647, 0.001110, 0.000042, 0.000825, 0.005211, 0.000749},
  216.             {0.000710, 0.000563, 0.000519, 0.000370, 0.000661, 0.000749, 0.000703}
  217.         };

  218.         AssetUniverseStatisticalProperties assetUniverseStatisticalProperties =
  219.             AssetUniverseStatisticalProperties.FromMultivariateMetrics (
  220.                 MultivariateMoments.Standard (
  221.                     assetIDArray,
  222.                     expectedAssetReturnsArray,
  223.                     assetReturnsCovarianceMatrix
  224.                 )
  225.             );

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

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

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

  229.         for (int assetIndex = 0;
  230.             assetIndex < assetIDArray.length;
  231.             ++assetIndex)
  232.         {
  233.             System.out.println (
  234.                 "\t| " + assetIDArray[assetIndex] + " | " +
  235.                 FormatUtil.FormatDouble (assetHoldingsLowerBoundArray[assetIndex], 2, 0, 100.) + "% | " +
  236.                 FormatUtil.FormatDouble (assetHoldingsUpperBoundArray[assetIndex], 2, 0, 100.) + "% ||"
  237.             );
  238.         }

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

  240.         UpperBoundHoldingsAllocationControl boundedCardinalityParameters =
  241.             new UpperBoundHoldingsAllocationControl (
  242.                 assetIDArray,
  243.                 CustomRiskUtilitySettings.VarianceMinimizer(),
  244.                 new EqualityConstraintSettings (
  245.                     EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT |
  246.                         EqualityConstraintSettings.RETURNS_CONSTRAINT,
  247.                     portfolioDesignReturn
  248.                 ),
  249.                 cardinalityUpperBound
  250.             );

  251.         for (int assetIndex = 0;
  252.             assetIndex < assetIDArray.length;
  253.             ++assetIndex)
  254.         {
  255.             boundedCardinalityParameters.addBound (
  256.                 assetIDArray[assetIndex],
  257.                 assetHoldingsLowerBoundArray[assetIndex],
  258.                 assetHoldingsUpperBoundArray[assetIndex]
  259.             );
  260.         }

  261.         TadonkiVialHoldingsAllocation tadonkiVialHoldingsAllocation = new TadonkiVialMeanVarianceOptimizer (
  262.             InteriorPointBarrierControl.Standard(),
  263.             LineStepEvolutionControl.NocedalWrightStrongWolfe (
  264.                 false
  265.             )
  266.         ).allocate (
  267.             boundedCardinalityParameters,
  268.             assetUniverseStatisticalProperties
  269.         );

  270.         PrintPortfolio (
  271.             "FLOOR PASS",
  272.             tadonkiVialHoldingsAllocation.floorPassHoldingsAllocation()
  273.         );

  274.         PrintPortfolio (
  275.             "FIRST GREEDY PRUNE PASS",
  276.             tadonkiVialHoldingsAllocation.firstPrunePassHoldingsAllocation()
  277.         );

  278.         PrintPortfolio (
  279.             "SECOND GREEDY PRUNE PASS",
  280.             tadonkiVialHoldingsAllocation.secondPrunePassHoldingsAllocation()
  281.         );

  282.         EnvManager.TerminateEnv();
  283.     }
  284. }