DualConstrainedEllipsoidVariance.java

  1. package org.drip.sample.semidefinite;

  2. import org.drip.function.definition.RdToR1;
  3. import org.drip.function.rdtor1.*;
  4. import org.drip.function.rdtor1descent.LineStepEvolutionControl;
  5. import org.drip.function.rdtor1solver.*;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.service.env.EnvManager;

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

  11. /*!
  12.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  15.  *
  16.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  17.  *      libraries targeting analysts and developers
  18.  *      https://lakshmidrip.github.io/DRIP/
  19.  *  
  20.  *  DRIP is composed of four main libraries:
  21.  *  
  22.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  23.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  24.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  25.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  26.  *
  27.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  28.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  29.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  30.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  31.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  32.  *
  33.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  34.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  35.  *
  36.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  37.  *
  38.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  39.  *
  40.  *  Licensed under the Apache License, Version 2.0 (the "License");
  41.  *      you may not use this file except in compliance with the License.
  42.  *  
  43.  *  You may obtain a copy of the License at
  44.  *      http://www.apache.org/licenses/LICENSE-2.0
  45.  *  
  46.  *  Unless required by applicable law or agreed to in writing, software
  47.  *      distributed under the License is distributed on an "AS IS" BASIS,
  48.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  49.  *  
  50.  *  See the License for the specific language governing permissions and
  51.  *      limitations under the License.
  52.  */

  53. /**
  54.  * DualConstrainedEllipsoidVariance demonstrates the Application of the Interior Point Method for Minimizing
  55.  *  the Variance Across The Specified Ellipsoid under both Normalization and first Moment Constraints.
  56.  *
  57.  * @author Lakshmi Krishnamurthy
  58.  */

  59. public class DualConstrainedEllipsoidVariance
  60. {

  61.     public static final void main (
  62.         final String[] argumentArray)
  63.         throws Exception
  64.     {
  65.         EnvManager.InitEnv ("");

  66.         String[] entityNameArray = new String[]
  67.         {
  68.             "IBM",
  69.             "ATT",
  70.             "ALU",
  71.             "QCO",
  72.             "INT",
  73.             "MSF",
  74.             "VER"
  75.         };
  76.         double[] entityReturnsArray = new double[]
  77.         {
  78.             0.0264,
  79.             0.0332,
  80.             0.0400,
  81.             0.0468,
  82.             0.0536,
  83.             0.0604,
  84.             0.0672
  85.         };
  86.         double entityDesignReturn = 0.0468;
  87.         double[][] entityCovarianceMatrix = new double[][]
  88.         {
  89.             {1.00, 0.76, 0.80, 0.38, 0.60, 0.61, 0.51},
  90.             {0.76, 1.00, 0.65, 0.35, 0.56, 0.43, 0.40},
  91.             {0.80, 0.65, 1.00, 0.68, 0.74, 0.40, 0.51},
  92.             {0.38, 0.35, 0.68, 1.00, 0.72, 0.02, 0.57},
  93.             {0.60, 0.56, 0.74, 0.72, 1.00, 0.31, 0.67},
  94.             {0.61, 0.43, 0.40, 0.02, 0.31, 1.00, 0.39},
  95.             {0.51, 0.40, 0.51, 0.57, 0.67, 0.39, 1.00}
  96.         };

  97.         InteriorPointBarrierControl interiorPointBarrierControl = InteriorPointBarrierControl.Standard();

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

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

  100.         for (int entityIndex = 0;
  101.             entityIndex < entityNameArray.length;
  102.             ++entityIndex)
  103.         {
  104.             header += " " + entityNameArray[entityIndex] + "  |";
  105.         }

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

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

  108.         for (int entityIndexI = 0;
  109.             entityIndexI < entityNameArray.length;
  110.             ++entityIndexI)
  111.         {
  112.             String dump = "\t| " + entityNameArray[entityIndexI] + " ";

  113.             for (int entityIndexJ = 0;
  114.                 entityIndexJ < entityNameArray.length;
  115.                 ++entityIndexJ)
  116.             {
  117.                 dump += "|" + FormatUtil.FormatDouble (
  118.                     entityCovarianceMatrix[entityIndexI][entityIndexJ], 1, 2, 1.
  119.                 ) + " ";
  120.             }

  121.             System.out.println (dump + "||");
  122.         }

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

  124.         double equalityConstraintConstant = -1.;
  125.         int entityCount = entityCovarianceMatrix.length;

  126.         RdToR1[] equalityConstraintFunctionArray = new RdToR1[]
  127.         {
  128.             new AffineMultivariate (
  129.                 ObjectiveConstraintVariateSet.Unitary (
  130.                     entityCount
  131.                 ),
  132.                 equalityConstraintConstant
  133.             ),
  134.             new AffineMultivariate (
  135.                 entityReturnsArray,
  136.                 -1. * entityDesignReturn
  137.             )
  138.         };

  139.         int equalityConstraintCount = equalityConstraintFunctionArray.length;

  140.         LagrangianMultivariate lagrangianMultivariate = new LagrangianMultivariate (
  141.             new CovarianceEllipsoidMultivariate (
  142.                 entityCovarianceMatrix
  143.             ),
  144.             equalityConstraintFunctionArray
  145.         );

  146.         double[] optimalVariateArray = new BarrierFixedPointFinder (
  147.             lagrangianMultivariate,
  148.             new RdToR1[]
  149.             {
  150.                 new AffineBoundMultivariate (
  151.                     false,
  152.                     0,
  153.                     entityCount + equalityConstraintCount,
  154.                     0.05
  155.                 ),
  156.                 new AffineBoundMultivariate (
  157.                     true,
  158.                     0,
  159.                     entityCount + equalityConstraintCount,
  160.                     0.65
  161.                 ),
  162.                 new AffineBoundMultivariate (false, 1, entityCount + equalityConstraintCount, 0.05),
  163.                 new AffineBoundMultivariate (true, 1, entityCount + equalityConstraintCount, 0.65),
  164.                 new AffineBoundMultivariate (false, 2, entityCount + equalityConstraintCount, 0.05),
  165.                 new AffineBoundMultivariate (true, 2, entityCount + equalityConstraintCount, 0.65),
  166.                 new AffineBoundMultivariate (false, 3, entityCount + equalityConstraintCount, 0.05),
  167.                 new AffineBoundMultivariate (true, 3, entityCount + equalityConstraintCount, 0.65),
  168.                 new AffineBoundMultivariate (false, 4, entityCount + equalityConstraintCount, 0.05),
  169.                 new AffineBoundMultivariate (true, 4, entityCount + equalityConstraintCount, 0.65),
  170.                 new AffineBoundMultivariate (false, 5, entityCount + equalityConstraintCount, 0.05),
  171.                 new AffineBoundMultivariate (true, 5, entityCount + equalityConstraintCount, 0.65),
  172.                 new AffineBoundMultivariate (false, 6, entityCount + equalityConstraintCount, 0.05),
  173.                 new AffineBoundMultivariate (true, 6, entityCount + equalityConstraintCount, 0.65)
  174.             },
  175.             interiorPointBarrierControl,
  176.             LineStepEvolutionControl.NocedalWrightStrongWolfe (
  177.                 false
  178.             )
  179.         ).solve (
  180.             ObjectiveConstraintVariateSet.Uniform (
  181.                 entityCount,
  182.                 lagrangianMultivariate.constraintFunctionDimension()
  183.             )
  184.         ).variateArray();

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

  186.         System.out.println ("\t|   OPTIMAL ENTITIES   ||");

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

  188.         double expectedReturn = 0.;

  189.         for (int entityIndex = 0;
  190.             entityIndex < entityCount;
  191.             ++entityIndex)
  192.         {
  193.             System.out.println (
  194.                 "\t|   " + entityNameArray[entityIndex] + "   =>  " + FormatUtil.FormatDouble (
  195.                     optimalVariateArray[entityIndex], 2, 2, 100.
  196.                 ) + "%  ||"
  197.             );

  198.             expectedReturn += optimalVariateArray[entityIndex] * entityReturnsArray[entityIndex];
  199.         }

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

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

  202.         System.out.println (
  203.             "\t| DESIGN RETURN    => " + FormatUtil.FormatDouble (
  204.                 entityDesignReturn, 1, 5, 1.
  205.             ) + " ||"
  206.         );

  207.         System.out.println (
  208.             "\t| EXPECTED RETURN  => " + FormatUtil.FormatDouble (
  209.                 expectedReturn, 1, 5, 1.
  210.             ) + " ||"
  211.         );

  212.         System.out.println (
  213.             "\t| OPTIMAL VARIANCE => " + FormatUtil.FormatDouble (
  214.                 lagrangianMultivariate.evaluate (
  215.                     optimalVariateArray
  216.                 ), 1, 5, 1.
  217.             ) + " ||"
  218.         );

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