CMVReconciler4.java

  1. package org.drip.sample.assetallocationexcel;

  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.*;
  8. import org.drip.portfolioconstruction.params.AssetUniverseStatisticalProperties;
  9. import org.drip.service.env.EnvManager;

  10. /*
  11.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  12.  */

  13. /*!
  14.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  16.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  17.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  18.  * Copyright (C) 2016 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>CMVReconciler4</i> demonstrates the Execution and Reconciliation of the Dual Constrained Mean Variance
  87.  * against an XL-based Implementation for Portfolio Design Returns #4.
  88.  *  
  89.  * <br><br>
  90.  *  <ul>
  91.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  92.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  93.  *      <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>
  94.  *      <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>
  95.  *  </ul>
  96.  * <br><br>
  97.  *
  98.  * @author Lakshmi Krishnamurthy
  99.  */

  100. public class CMVReconciler4
  101. {

  102.     public static final void main (
  103.         final String[] argumentArray)
  104.         throws Exception
  105.     {
  106.         EnvManager.InitEnv (
  107.             "",
  108.             true
  109.         );

  110.         String[] assetIDArray = new String[]
  111.         {
  112.             "TOK",
  113.             "EWJ",
  114.             "HYG",
  115.             "LQD",
  116.             "EMD",
  117.             "GSG",
  118.             "BWX"
  119.         };
  120.         double[] assetHoldingsLowerBoundArray = new double[]
  121.         {
  122.             0.05,
  123.             0.05,
  124.             0.05,
  125.             0.10,
  126.             0.05,
  127.             0.05,
  128.             0.15
  129.         };
  130.         double[] assetHoldingsUpperBoundArray = new double[]
  131.         {
  132.             0.40,
  133.             0.40,
  134.             0.30,
  135.             0.30,
  136.             0.35,
  137.             0.15,
  138.             0.50
  139.         };
  140.         double[] expectedAssetReturnsArray = new double[]
  141.         {
  142.             0.1100,
  143.             0.1000,
  144.             0.0800,
  145.             0.0500,
  146.             0.0600,
  147.             0.0700,
  148.             0.0200
  149.         };
  150.         double portfolioDesignReturn = 0.08000;
  151.         double[][] assetReturnsCovarianceMatrix = new double[][]
  152.         {
  153.             {0.002733 * 12, 0.002083 * 12, 0.001593 * 12, 0.000488 * 12, 0.001172 * 12, 0.002312 * 12, 0.000710 * 12},
  154.             {0.002083 * 12, 0.002768 * 12, 0.001302 * 12, 0.000457 * 12, 0.001105 * 12, 0.001647 * 12, 0.000563 * 12},
  155.             {0.001593 * 12, 0.001302 * 12, 0.001463 * 12, 0.000639 * 12, 0.001050 * 12, 0.001110 * 12, 0.000519 * 12},
  156.             {0.000488 * 12, 0.000457 * 12, 0.000639 * 12, 0.000608 * 12, 0.000663 * 12, 0.000042 * 12, 0.000370 * 12},
  157.             {0.001172 * 12, 0.001105 * 12, 0.001050 * 12, 0.000663 * 12, 0.001389 * 12, 0.000825 * 12, 0.000661 * 12},
  158.             {0.002312 * 12, 0.001647 * 12, 0.001110 * 12, 0.000042 * 12, 0.000825 * 12, 0.005211 * 12, 0.000749 * 12},
  159.             {0.000710 * 12, 0.000563 * 12, 0.000519 * 12, 0.000370 * 12, 0.000661 * 12, 0.000749 * 12, 0.000703 * 12}
  160.         };
  161.         double[] reconcilerAssetWeightsArray = new double[]
  162.         {
  163.             0.3976,
  164.             0.1625,
  165.             0.0500,
  166.             0.1399,
  167.             0.0500,
  168.             0.0500,
  169.             0.1500
  170.         };

  171.         AssetComponent[] reconcilerAssetComponentArray =
  172.             new AssetComponent[reconcilerAssetWeightsArray.length];

  173.         for (int assetIndex = 0;
  174.             assetIndex < reconcilerAssetWeightsArray.length;
  175.             ++assetIndex)
  176.         {
  177.             reconcilerAssetComponentArray[assetIndex] = new AssetComponent (
  178.                 assetIDArray[assetIndex],
  179.                 reconcilerAssetWeightsArray[assetIndex]
  180.             );
  181.         }

  182.         AssetUniverseStatisticalProperties assetUniverseStatisticalProperties =
  183.             AssetUniverseStatisticalProperties.FromMultivariateMetrics (
  184.                 MultivariateMoments.Standard (
  185.                     assetIDArray,
  186.                     expectedAssetReturnsArray,
  187.                     assetReturnsCovarianceMatrix
  188.                 )
  189.             );

  190.         double[][] covarianceMatrix = assetUniverseStatisticalProperties.covariance (
  191.             assetIDArray
  192.         );

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

  194.         System.out.println ("\t|                                  CROSS ASSET COVARIANCE MATRIX                                 ||");

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

  196.         String header = "\t|     |";

  197.         for (int assetIndex = 0;
  198.             assetIndex < assetIDArray.length;
  199.             ++assetIndex)
  200.         {
  201.             header += "    " + assetIDArray[assetIndex] + "     |";
  202.         }

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

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

  205.         for (int assetIndexI = 0;
  206.             assetIndexI < assetIDArray.length;
  207.             ++assetIndexI)
  208.         {
  209.             String dump = "\t| " + assetIDArray[assetIndexI] + " ";

  210.             for (int assetIndexJ = 0;
  211.                 assetIndexJ < assetIDArray.length;
  212.                 ++assetIndexJ)
  213.             {
  214.                 dump += "|" + FormatUtil.FormatDouble (
  215.                     covarianceMatrix[assetIndexI][assetIndexJ], 1, 8, 1.
  216.                 ) + " ";
  217.             }

  218.             System.out.println (dump + "||");
  219.         }

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

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

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

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

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

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

  235.         InteriorPointBarrierControl interiorPointBarrierControl = InteriorPointBarrierControl.Standard();

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

  237.         System.out.println ("\t|  INTERIOR POINT METHOD BARRIER PARAMETERS  ||");

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

  239.         System.out.println (
  240.             "\t|    Barrier Decay Velocity        : " + 1. / interiorPointBarrierControl.decayVelocity()
  241.         );

  242.         System.out.println (
  243.             "\t|    Barrier Decay Steps           : " + interiorPointBarrierControl.decayStepCount()
  244.         );

  245.         System.out.println (
  246.             "\t|    Initial Barrier Strength      : " + interiorPointBarrierControl.initialStrength()
  247.         );

  248.         System.out.println (
  249.             "\t|    Barrier Convergence Tolerance : " + interiorPointBarrierControl.relativeTolerance()
  250.         );

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

  252.         BoundedHoldingsAllocationControl boundedPortfolioConstructionParameters =
  253.             new BoundedHoldingsAllocationControl (
  254.                 assetIDArray,
  255.                 CustomRiskUtilitySettings.VarianceMinimizer(),
  256.                 new EqualityConstraintSettings (
  257.                     EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT |
  258.                         EqualityConstraintSettings.RETURNS_CONSTRAINT,
  259.                     portfolioDesignReturn
  260.                 )
  261.             );

  262.         for (int assetIndex = 0;
  263.             assetIndex < assetIDArray.length;
  264.             ++assetIndex)
  265.         {
  266.             boundedPortfolioConstructionParameters.addBound (
  267.                 assetIDArray[assetIndex],
  268.                 assetHoldingsLowerBoundArray[assetIndex],
  269.                 assetHoldingsUpperBoundArray[assetIndex]
  270.             );
  271.         }

  272.         HoldingsAllocation optimizationOutput = new ConstrainedMeanVarianceOptimizer (
  273.             interiorPointBarrierControl,
  274.             LineStepEvolutionControl.NocedalWrightStrongWolfe (
  275.                 false
  276.             )
  277.         ).allocate (
  278.             boundedPortfolioConstructionParameters,
  279.             assetUniverseStatisticalProperties
  280.         );

  281.         AssetComponent[] optimalAssetComponentArray =
  282.             optimizationOutput.optimalPortfolio().assetComponentArray();

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

  284.         System.out.println ("\t|   OPTIMAL ASSET WEIGHTS  ||");

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

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

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

  288.         for (int assetIndex = 0;
  289.             assetIndex < optimalAssetComponentArray.length;
  290.             ++assetIndex)
  291.         {
  292.             System.out.println (
  293.                 "\t|  " + optimalAssetComponentArray[assetIndex].id() + "  |" +
  294.                 FormatUtil.FormatDouble (
  295.                     optimalAssetComponentArray[assetIndex].amount(), 2, 2, 100.
  296.                 ) + "% | " +
  297.                 FormatUtil.FormatDouble (
  298.                     reconcilerAssetComponentArray[assetIndex].amount(), 2, 2, 100.
  299.                 ) + "% ||"
  300.             );
  301.         }

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

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

  304.         System.out.println (
  305.             "\t| Optimal Portfolio Normalize          : " + FormatUtil.FormatDouble (
  306.                 optimizationOutput.optimalPortfolio().notional(), 1, 2, 1.
  307.             ) + "   ||"
  308.         );

  309.         System.out.println (
  310.             "\t| Optimal Portfolio Input Return       : " + FormatUtil.FormatDouble (
  311.                 portfolioDesignReturn, 1, 2, 100.
  312.             ) + "%  ||"
  313.         );

  314.         System.out.println (
  315.             "\t| Optimal Portfolio Expected Return    : " + FormatUtil.FormatDouble (
  316.                 optimizationOutput.optimalMetrics().excessReturnsMean(), 1, 2, 100.
  317.             ) + "%  ||"
  318.         );

  319.         System.out.println (
  320.             "\t| Optimal Portfolio Standard Deviation : " + FormatUtil.FormatDouble (
  321.                 optimizationOutput.optimalMetrics().excessReturnsStandardDeviation(), 2, 2, 100.
  322.             ) + "% ||"
  323.         );

  324.         System.out.println (
  325.             "\t| Excel Portfolio Standard Deviation   : " + FormatUtil.FormatDouble (
  326.                 Math.sqrt (
  327.                     new Portfolio (
  328.                         reconcilerAssetComponentArray
  329.                     ).variance (
  330.                         assetUniverseStatisticalProperties
  331.                     )
  332.                 ), 2, 2, 100.
  333.             ) + "% ||"
  334.         );

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

  336.         EnvManager.TerminateEnv();
  337.     }
  338. }