NSphereSurfaceExtremization.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/
  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.  * NSphereSurfaceExtremization computes the Equality-Constrained Extrema of the Specified Function along the
  55.  *  Surface of an N-Sphere using Lagrange Multipliers.
  56.  *
  57.  * @author Lakshmi Krishnamurthy
  58.  */

  59. public class NSphereSurfaceExtremization
  60. {

  61.     private static final void Solve (
  62.         final NewtonFixedPointFinder newtonFixedPointFinder,
  63.         final double[] startingVariateArray)
  64.         throws Exception
  65.     {
  66.         System.out.println ("\n\t|------------------------------------||");

  67.         String strDump = "\t| STARTER: [";

  68.         strDump += FormatUtil.FormatDouble (startingVariateArray[0], 1, 4, 1.) + ",";

  69.         strDump += FormatUtil.FormatDouble (startingVariateArray[1], 1, 4, 1.) + ",";

  70.         strDump += FormatUtil.FormatDouble (startingVariateArray[2], 1, 4, 1.);

  71.         System.out.println (strDump + "] ||");

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

  73.         double[] variateArray = newtonFixedPointFinder.convergeVariate (
  74.             new VariateInequalityConstraintMultiplier (
  75.                 false,
  76.                 startingVariateArray,
  77.                 null
  78.             )
  79.         ).variateArray();

  80.         System.out.println (
  81.             "\t| Optimal X      : " + FormatUtil.FormatDouble (variateArray[0], 1, 4, 1.) + "           ||"
  82.         );

  83.         System.out.println (
  84.             "\t| Optimal Y      : " + FormatUtil.FormatDouble (variateArray[1], 1, 4, 1.) + "           ||"
  85.         );

  86.         System.out.println (
  87.             "\t| Optimal Lambda : " + FormatUtil.FormatDouble (variateArray[2], 1, 4, 1.) + "           ||"
  88.         );

  89.         System.out.println ("\t|------------------------------------||");
  90.     }

  91.     public static final void main (
  92.         final String[] argumentArray)
  93.         throws Exception
  94.     {
  95.         EnvManager.InitEnv (
  96.             ""
  97.         );

  98.         RdToR1 variateSumObjectiveFunction = new RdToR1 (
  99.             null
  100.         )
  101.         {
  102.             @Override public double evaluate (
  103.                 final double[] variateArray)
  104.                 throws Exception
  105.             {
  106.                 return variateArray[0] * variateArray[0] * variateArray[1];
  107.             }

  108.             @Override public int dimension()
  109.             {
  110.                 return 2;
  111.             }

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

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

  131.         RdToR1 sphereSurfaceConstraintFunction = new RdToR1 (
  132.             null
  133.         )
  134.         {
  135.             @Override public double evaluate (
  136.                 final double[] variateArray)
  137.                 throws Exception
  138.             {
  139.                 return variateArray[0] * variateArray[0] + variateArray[1] * variateArray[1] - 3.;
  140.             }

  141.             @Override public int dimension()
  142.             {
  143.                 return 2;
  144.             }

  145.             @Override public double[] jacobian (
  146.                 final double[] variateArray)
  147.             {
  148.                 double[] jacobian = new double[2];
  149.                 jacobian[0] = 2. * variateArray[0];
  150.                 jacobian[1] = 2. * variateArray[1];
  151.                 return jacobian;
  152.             }

  153.             @Override public double[][] hessian (
  154.                 final double[] variateArray)
  155.             {
  156.                 double[][] hessian = new double[2][2];
  157.                 hessian[0][0] = 2.;
  158.                 hessian[0][1] = 0.;
  159.                 hessian[1][0] = 0.;
  160.                 hessian[1][1] = 2.;
  161.                 return hessian;
  162.             }
  163.         };

  164.         NewtonFixedPointFinder newtonFixedPointFinder = new NewtonFixedPointFinder (
  165.             new LagrangianMultivariate (
  166.                 variateSumObjectiveFunction,
  167.                 new RdToR1[]
  168.                 {
  169.                     sphereSurfaceConstraintFunction
  170.                 }
  171.             ),
  172.             LineStepEvolutionControl.NocedalWrightStrongWolfe (
  173.                 false
  174.             ),
  175.             ConvergenceControl.Standard()
  176.         );

  177.         Solve (
  178.             newtonFixedPointFinder,
  179.             new double[]
  180.             {
  181.                 2.,
  182.                 1.,
  183.                 1.
  184.             }
  185.         );

  186.         Solve (
  187.             newtonFixedPointFinder,
  188.             new double[]
  189.             {
  190.                 -2.,
  191.                  1.,
  192.                  1.
  193.             }
  194.         );

  195.         Solve (
  196.             newtonFixedPointFinder,
  197.             new double[] {
  198.                  2.,
  199.                 -1.,
  200.                  1.
  201.             }
  202.         );

  203.         Solve (
  204.             newtonFixedPointFinder,
  205.             new double[]
  206.             {
  207.                 -2.,
  208.                 -1.,
  209.                  1.
  210.             }
  211.         );

  212.         Solve (
  213.             newtonFixedPointFinder,
  214.             new double[]
  215.             {
  216.                 0.,
  217.                 1.,
  218.                 0.
  219.             }
  220.         );

  221.         Solve (
  222.             newtonFixedPointFinder,
  223.             new double[]
  224.             {
  225.                  0.,
  226.                 -1.,
  227.                  0.
  228.             }
  229.         );
  230.     }
  231. }