CMVMonthlyReconciler05.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>CMVMonthlyReconciler05</i> demonstrates the Execution and Reconciliation of the Dual Constrained Mean
  87.  * Variance against an XL-based Monthly Series Implementation for Portfolio Design Returns #5.
  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 CMVMonthlyReconciler05
  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.00,
  123.             0.00,
  124.             0.00,
  125.             0.00,
  126.             0.00,
  127.             0.00,
  128.             0.00
  129.         };
  130.         double[] assetHoldingsUpperBoundArray = new double[]
  131.         {
  132.             0.30,
  133.             0.30,
  134.             0.30,
  135.             0.50,
  136.             0.30,
  137.             0.30,
  138.             0.50
  139.         };
  140.         double[] expectedAssetReturnsArray = new double[]
  141.         {
  142.             0.008930,
  143.             0.007730,
  144.             0.006950,
  145.             0.003660,
  146.             0.006980,
  147.             0.007140,
  148.             0.003870
  149.         };
  150.         double portfolioDesignReturn = 0.005319;
  151.         double portfolioRiskExcel = 0.026572;
  152.         double[][] assetReturnsCovarianceMatrix = new double[][]
  153.         {
  154.             {0.002733, 0.002083, 0.001593, 0.000488, 0.001172, 0.002312, 0.000710},
  155.             {0.002083, 0.002768, 0.001302, 0.000457, 0.001105, 0.001647, 0.000563},
  156.             {0.001593, 0.001302, 0.001463, 0.000639, 0.001050, 0.001110, 0.000519},
  157.             {0.000488, 0.000457, 0.000639, 0.000608, 0.000663, 0.000042, 0.000370},
  158.             {0.001172, 0.001105, 0.001050, 0.000663, 0.001389, 0.000825, 0.000661},
  159.             {0.002312, 0.001647, 0.001110, 0.000042, 0.000825, 0.005211, 0.000749},
  160.             {0.000710, 0.000563, 0.000519, 0.000370, 0.000661, 0.000749, 0.000703}
  161.         };
  162.         double[] assetWeightsReconcilerArray = new double[]
  163.         {
  164.             0.101541,
  165.             0.050369,
  166.             0.057521,
  167.             0.391489,
  168.             0.162554,
  169.             0.042900,
  170.             0.193626
  171.         };

  172.         AssetComponent[] assetComponentReconcilerArray =
  173.             new AssetComponent[assetWeightsReconcilerArray.length];

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  236.         InteriorPointBarrierControl interiorPointBarrierControl = InteriorPointBarrierControl.Standard();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  336.         System.out.println (
  337.             "\t| Excel Portfolio Standard Deviation (Input)      : " + FormatUtil.FormatDouble (
  338.                 portfolioRiskExcel, 1, 4, 100.
  339.             ) + "%  ||"
  340.         );

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

  342.         EnvManager.TerminateEnv();
  343.     }
  344. }