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

  125. public class HeuristicCardinalityBoundOptimizer02
  126. {

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

  135.         Portfolio optimalPortfolio = holdingsAllocation.optimalPortfolio();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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