WeakWolfeEvolutionMetrics.java

  1. package org.drip.sample.descentverifier;

  2. import org.drip.function.rdtor1descent.LineStepEvolutionControl;
  3. import org.drip.function.rdtor1solver.*;
  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.params.AssetUniverseStatisticalProperties;
  8. import org.drip.service.env.EnvManager;

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

  12. /*!
  13.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  16.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  17.  * Copyright (C) 2016 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>WeakWolfeEvolutionMetrics</i> demonstrates the Impact of applying the Weak Wolfe Criterion on the
  86.  * Evolution of the R<sup>d</sup> Fixed Point of a Constrained Minimization Search.
  87.  *
  88.  * <br><br>
  89.  *  <ul>
  90.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  91.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  92.  *      <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>
  93.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/descentverifier/README.md">Armijo/Wolfe Strong/Weak Curvature</a></li>
  94.  *  </ul>
  95.  * <br><br>
  96.  *
  97.  * @author Lakshmi Krishnamurthy
  98.  */

  99. public class WeakWolfeEvolutionMetrics
  100. {

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

  108.         FixedRdFinder.s_verifierIncrementBlog = true;

  109.         String[] assetNameArray = new String[]
  110.         {
  111.             "TOK",
  112.             "EWJ",
  113.             "HYG",
  114.             "LQD",
  115.             "EMD",
  116.             "GSG",
  117.             "BWX"
  118.         };
  119.         double[] assetLowerBoundArray = 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[] assetUpperBoundArray = 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[] assetExpectedReturnsArray = 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.         AssetUniverseStatisticalProperties assetUniverseStatisticalProperties =
  161.             AssetUniverseStatisticalProperties.FromMultivariateMetrics (
  162.                 MultivariateMoments.Standard (
  163.                     assetNameArray,
  164.                     assetExpectedReturnsArray,
  165.                     assetReturnsCovarianceMatrix
  166.                 )
  167.             );

  168.         double[][] covarianceMatrix = assetUniverseStatisticalProperties.covariance (
  169.             assetNameArray
  170.         );

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

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

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

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

  175.         for (int assetNameIndex = 0;
  176.             assetNameIndex < assetNameArray.length;
  177.             ++assetNameIndex)
  178.         {
  179.             header += "    " + assetNameArray[assetNameIndex] + "     |";
  180.         }

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

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

  183.         for (int assetNameIndexI = 0;
  184.             assetNameIndexI < assetNameArray.length;
  185.             ++assetNameIndexI)
  186.         {
  187.             String dump = "\t| " + assetNameArray[assetNameIndexI] + " ";

  188.             for (int assetNameIndexJ = 0;
  189.                 assetNameIndexJ < assetNameArray.length;
  190.                 ++assetNameIndexJ)
  191.             {
  192.                 dump += "|" + FormatUtil.FormatDouble (
  193.                     covarianceMatrix[assetNameIndexI][assetNameIndexJ], 1, 8, 1.
  194.                 ) + " ";
  195.             }

  196.             System.out.println (dump + "||");
  197.         }

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

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

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

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

  202.         for (int assetNameIndex = 0;
  203.             assetNameIndex < assetNameArray.length;
  204.             ++assetNameIndex)
  205.         {
  206.             System.out.println (
  207.                 "\t| " + assetNameArray[assetNameIndex] + " | " +
  208.                 FormatUtil.FormatDouble (assetLowerBoundArray[assetNameIndex], 2, 0, 100.) + "% | " +
  209.                 FormatUtil.FormatDouble (assetUpperBoundArray[assetNameIndex], 2, 0, 100.) + "% ||"
  210.             );
  211.         }

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

  213.         InteriorPointBarrierControl interiorPointBarrierControl = InteriorPointBarrierControl.Standard();

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

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

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

  217.         System.out.println (
  218.             "\t|    Barrier Decay Velocity        : " + 1. / interiorPointBarrierControl.decayVelocity()
  219.         );

  220.         System.out.println (
  221.             "\t|    Barrier Decay Steps           : " + interiorPointBarrierControl.decayStepCount()
  222.         );

  223.         System.out.println (
  224.             "\t|    Initial Barrier Strength      : " + interiorPointBarrierControl.initialStrength()
  225.         );

  226.         System.out.println (
  227.             "\t|    Barrier Convergence Tolerance : " + interiorPointBarrierControl.relativeTolerance()
  228.         );

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

  230.         BoundedHoldingsAllocationControl boundedPortfolioConstructionParameters =
  231.             new BoundedHoldingsAllocationControl (
  232.                 assetNameArray,
  233.                 CustomRiskUtilitySettings.VarianceMinimizer(),
  234.                 new EqualityConstraintSettings (
  235.                     EqualityConstraintSettings.FULLY_INVESTED_CONSTRAINT |
  236.                         EqualityConstraintSettings.RETURNS_CONSTRAINT,
  237.                     portfolioDesignReturn
  238.                 )
  239.             );

  240.         for (int assetNameIndex = 0;
  241.             assetNameIndex < assetNameArray.length;
  242.             ++assetNameIndex)
  243.         {
  244.             boundedPortfolioConstructionParameters.addBound (
  245.                 assetNameArray[assetNameIndex],
  246.                 assetLowerBoundArray[assetNameIndex],
  247.                 assetUpperBoundArray[assetNameIndex]
  248.             );
  249.         }

  250.         new ConstrainedMeanVarianceOptimizer (
  251.             interiorPointBarrierControl,
  252.             LineStepEvolutionControl.NocedalWrightWeakWolfe (
  253.                 false
  254.             )
  255.         ).allocate (
  256.             boundedPortfolioConstructionParameters,
  257.             assetUniverseStatisticalProperties
  258.         );

  259.         EnvManager.TerminateEnv();
  260.     }
  261. }