VariateSumExtremization.java

  1. package org.drip.sample.optimizer;

  2. import org.drip.function.definition.RdToR1;
  3. import org.drip.function.rdtor1.LagrangianMultivariate;
  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/laksh
  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.  * VariateSumExtremization computes the Equality Constrained Extrema of the Sum of Variates along the Surface
  55.  *  of the Sphere using Lagrange Multipliers.
  56.  *
  57.  * @author Lakshmi Krishnamurthy
  58.  */

  59. public class VariateSumExtremization
  60. {

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

  68.         RdToR1 variateSumObjectiveFunction = new RdToR1 (
  69.             null
  70.         )
  71.         {
  72.             @Override public double evaluate (
  73.                 final double[] variateArray)
  74.                 throws Exception
  75.             {
  76.                 return variateArray[0] + variateArray[1];
  77.             }

  78.             @Override public int dimension()
  79.             {
  80.                 return 2;
  81.             }

  82.             @Override public double[] jacobian (
  83.                 final double[] variateArray)
  84.             {
  85.                 double[] jacobian = new double[2];
  86.                 jacobian[0] = 1.;
  87.                 jacobian[1] = 1.;
  88.                 return jacobian;
  89.             }

  90.             @Override public double[][] hessian (
  91.                 final double[] variateArray)
  92.             {
  93.                 double[][] hessian = new double[2][2];
  94.                 hessian[0][0] = 0.;
  95.                 hessian[0][1] = 0.;
  96.                 hessian[1][0] = 0.;
  97.                 hessian[1][1] = 0.;
  98.                 return hessian;
  99.             }
  100.         };

  101.         RdToR1 rdToR1SphereSurfaceConstraintFunction = new RdToR1 (
  102.             null
  103.         )
  104.         {
  105.             @Override public double evaluate (
  106.                 final double[] variateArray)
  107.                 throws Exception
  108.             {
  109.                 return variateArray[0] * variateArray[0] + variateArray[1] * variateArray[1] - 1.;
  110.             }

  111.             @Override public int dimension()
  112.             {
  113.                 return 2;
  114.             }

  115.             @Override public double[] jacobian (
  116.                 final double[] variateArray)
  117.             {
  118.                 double[] jacobian = new double[2];
  119.                 jacobian[0] = 2. * variateArray[0];
  120.                 jacobian[1] = 2. * variateArray[1];
  121.                 return jacobian;
  122.             }

  123.             @Override public double[][] hessian (
  124.                 final double[] variateArray)
  125.             {
  126.                 double[][] hessian = new double[2][2];
  127.                 hessian[0][0] = 2.;
  128.                 hessian[0][1] = 0.;
  129.                 hessian[1][0] = 0.;
  130.                 hessian[1][1] = 2.;
  131.                 return hessian;
  132.             }
  133.         };

  134.         VariateInequalityConstraintMultiplier vcmt = new NewtonFixedPointFinder (
  135.             new LagrangianMultivariate (
  136.                 variateSumObjectiveFunction,
  137.                 new RdToR1[]
  138.                 {
  139.                     rdToR1SphereSurfaceConstraintFunction
  140.                 }
  141.             ),
  142.             LineStepEvolutionControl.NocedalWrightStrongWolfe (
  143.                 false
  144.             ),
  145.             ConvergenceControl.Standard()
  146.         ).convergeVariate (
  147.             new VariateInequalityConstraintMultiplier (
  148.                 false,
  149.                 new double[]
  150.                 {
  151.                     1.,
  152.                     1.,
  153.                     1.
  154.                 },
  155.                 null
  156.             )
  157.         );

  158.         double[] variateArray = vcmt.variateArray();

  159.         System.out.println ("\tOptimal X      : " + FormatUtil.FormatDouble (variateArray[0], 1, 4, 1.));

  160.         System.out.println ("\tOptimal Y      : " + FormatUtil.FormatDouble (variateArray[1], 1, 4, 1.));

  161.         System.out.println ("\tOptimal Lambda : " + FormatUtil.FormatDouble (variateArray[2], 1, 4, 1.));
  162.     }
  163. }