WeightConstrainedEllipsoidVariance.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.  * WeightConstrainedEllipsoidVariance demonstrates the Application of the Interior Point Method for
  55.  *  Minimizing the Variance Across The Specified Ellipsoid under the Normalization Constraint.
  56.  *
  57.  * @author Lakshmi Krishnamurthy
  58.  */

  59. public class WeightConstrainedEllipsoidVariance
  60. {

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

  68.         String[] entityNameArray = new String[]
  69.         {
  70.             "IBM",
  71.             "ATT",
  72.             "ALU",
  73.             "QCO",
  74.             "INT",
  75.             "MSF",
  76.             "VER"
  77.         };

  78.         double[][] covarianceMatrix = new double[][]
  79.         {
  80.             {1.00, 0.76, 0.80, 0.38, 0.60, 0.61, 0.51},
  81.             {0.76, 1.00, 0.65, 0.35, 0.56, 0.43, 0.40},
  82.             {0.80, 0.65, 1.00, 0.68, 0.74, 0.40, 0.51},
  83.             {0.38, 0.35, 0.68, 1.00, 0.72, 0.02, 0.57},
  84.             {0.60, 0.56, 0.74, 0.72, 1.00, 0.31, 0.67},
  85.             {0.61, 0.43, 0.40, 0.02, 0.31, 1.00, 0.39},
  86.             {0.51, 0.40, 0.51, 0.57, 0.67, 0.39, 1.00}
  87.         };

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

  89.         int entityCount = covarianceMatrix.length;
  90.         double equalityConstraintConstant = -1.;
  91.         String header = "\t|     |";

  92.         for (int entityIndex = 0;
  93.             entityIndex < entityCount;
  94.             ++entityIndex)
  95.         {
  96.             header += " " + entityNameArray[entityIndex] + "  |";
  97.         }

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

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

  100.         for (int entityIndexI = 0;
  101.             entityIndexI < entityCount;
  102.             ++entityIndexI)
  103.         {
  104.             String dump = "\t| " + entityNameArray[entityIndexI] + " ";

  105.             for (int entityIndexJ = 0;
  106.                 entityIndexJ < entityCount;
  107.                 ++entityIndexJ)
  108.             {
  109.                 dump += "|" + FormatUtil.FormatDouble (
  110.                     covarianceMatrix[entityIndexI][entityIndexJ], 1, 2, 1.
  111.                 ) + " ";
  112.             }

  113.             System.out.println (dump + "||");
  114.         }

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

  116.         InteriorPointBarrierControl interiorPointBarrierControl = InteriorPointBarrierControl.Standard();

  117.         RdToR1[] equalityConstraintArray = new RdToR1[]
  118.         {
  119.             new AffineMultivariate (
  120.                 ObjectiveConstraintVariateSet.Unitary (
  121.                     entityCount
  122.                 ),
  123.                 equalityConstraintConstant
  124.             )
  125.         };

  126.         int equalityConstraintCount = equalityConstraintArray.length;

  127.         RdToR1[] inequalityConstraintArray = new RdToR1[]
  128.         {
  129.             new AffineBoundMultivariate (false, 0, entityCount + equalityConstraintCount, 0.05),
  130.             new AffineBoundMultivariate (true, 0, entityCount + equalityConstraintCount, 0.65),
  131.             new AffineBoundMultivariate (false, 1, entityCount + equalityConstraintCount, 0.05),
  132.             new AffineBoundMultivariate (true, 1, entityCount + equalityConstraintCount, 0.65),
  133.             new AffineBoundMultivariate (false, 2, entityCount + equalityConstraintCount, 0.05),
  134.             new AffineBoundMultivariate (true, 2, entityCount + equalityConstraintCount, 0.65),
  135.             new AffineBoundMultivariate (false, 3, entityCount + equalityConstraintCount, 0.05),
  136.             new AffineBoundMultivariate (true, 3, entityCount + equalityConstraintCount, 0.65),
  137.             new AffineBoundMultivariate (false, 4, entityCount + equalityConstraintCount, 0.05),
  138.             new AffineBoundMultivariate (true, 4, entityCount + equalityConstraintCount, 0.65),
  139.             new AffineBoundMultivariate (false, 5, entityCount + equalityConstraintCount, 0.05),
  140.             new AffineBoundMultivariate (true, 5, entityCount + equalityConstraintCount, 0.65),
  141.             new AffineBoundMultivariate (false, 6, entityCount + equalityConstraintCount, 0.05),
  142.             new AffineBoundMultivariate (true, 6, entityCount + equalityConstraintCount, 0.65)
  143.         };

  144.         LagrangianMultivariate lagrangianMultivariate = new LagrangianMultivariate (
  145.             new CovarianceEllipsoidMultivariate (
  146.                 covarianceMatrix
  147.             ),
  148.             equalityConstraintArray
  149.         );

  150.         double[] optimalVariateArray = new BarrierFixedPointFinder (
  151.             lagrangianMultivariate,
  152.             inequalityConstraintArray,
  153.             interiorPointBarrierControl,
  154.             LineStepEvolutionControl.NocedalWrightStrongWolfe (
  155.                 false
  156.             )
  157.         ).solve (
  158.             ObjectiveConstraintVariateSet.Uniform (
  159.                 entityCount,
  160.                 equalityConstraintCount
  161.             )
  162.         ).variateArray();

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

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

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

  166.         for (int entityIndex = 0;
  167.             entityIndex < entityCount;
  168.             ++entityIndex)
  169.         {
  170.             System.out.println (
  171.                 "\t|   " + entityNameArray[entityIndex] + "   =>  " + FormatUtil.FormatDouble (
  172.                     optimalVariateArray[entityIndex], 2, 2, 100.
  173.                 ) + "%  ||"
  174.             );
  175.         }

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

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

  178.         System.out.println (
  179.             "\t| OPTIMAL VARIANCE => " + FormatUtil.FormatDouble (
  180.                 lagrangianMultivariate.evaluate (
  181.                     optimalVariateArray
  182.                 ), 1, 5, 1.
  183.             ) + " ||"
  184.         );

  185.         System.out.println ("\t|------------------------------||\n");
  186.     }
  187. }