CMVReconciler7.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>CMVReconciler.</i> demonstrates the Execution and Reconciliation of the Dual Constrained Mean Variance
  87.  * against an XL-based Implementation for Portfolio Design Returns #7.
  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 CMVReconciler7 {

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

  109.         String[] assetIDArray = new String[]
  110.         {
  111.             "TOK",
  112.             "EWJ",
  113.             "HYG",
  114.             "LQD",
  115.             "EMD",
  116.             "GSG",
  117.             "BWX"
  118.         };
  119.         double[] assetHoldingsLowerBoundArray = new double[]
  120.         {
  121.             0.05,
  122.             0.04,
  123.             0.06,
  124.             0.03,
  125.             0.03,
  126.             0.03,
  127.             0.13
  128.         };
  129.         double[] assetHoldingsUpperBoundArray = new double[]
  130.         {
  131.             0.43,
  132.             0.27,
  133.             0.44,
  134.             0.32,
  135.             0.66,
  136.             0.32,
  137.             0.88
  138.         };
  139.         double[] expectedAssetReturnsArray = new double[]
  140.         {
  141.             0.1300,
  142.             0.0700,
  143.             0.0400,
  144.             0.0300,
  145.             0.0800,
  146.             0.1000,
  147.             0.0100
  148.         };
  149.         double portfolioDesignReturn = 0.06000;
  150.         double[][] assetReturnsCovarianceMatrix = new double[][]
  151.         {
  152.             {0.002733 * 12, 0.002083 * 12, 0.001593 * 12, 0.000488 * 12, 0.001172 * 12, 0.002312 * 12, 0.000710 * 12},
  153.             {0.002083 * 12, 0.002768 * 12, 0.001302 * 12, 0.000457 * 12, 0.001105 * 12, 0.001647 * 12, 0.000563 * 12},
  154.             {0.001593 * 12, 0.001302 * 12, 0.001463 * 12, 0.000639 * 12, 0.001050 * 12, 0.001110 * 12, 0.000519 * 12},
  155.             {0.000488 * 12, 0.000457 * 12, 0.000639 * 12, 0.000608 * 12, 0.000663 * 12, 0.000042 * 12, 0.000370 * 12},
  156.             {0.001172 * 12, 0.001105 * 12, 0.001050 * 12, 0.000663 * 12, 0.001389 * 12, 0.000825 * 12, 0.000661 * 12},
  157.             {0.002312 * 12, 0.001647 * 12, 0.001110 * 12, 0.000042 * 12, 0.000825 * 12, 0.005211 * 12, 0.000749 * 12},
  158.             {0.000710 * 12, 0.000563 * 12, 0.000519 * 12, 0.000370 * 12, 0.000661 * 12, 0.000749 * 12, 0.000703 * 12}
  159.         };
  160.         double[] reconcilerAssetWeightsArray = new double[]
  161.         {
  162.             0.1489,
  163.             0.0400,
  164.             0.0600,
  165.             0.3200,
  166.             0.2451,
  167.             0.0560,
  168.             0.1300
  169.         };

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  234.         InteriorPointBarrierControl interiorPointBarrierControl = InteriorPointBarrierControl.Standard();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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