KKTNecessarySufficientConditions.java

  1. package org.drip.sample.optimizer;

  2. import org.drip.function.definition.RdToR1;
  3. import org.drip.function.rdtor1solver.*;
  4. import org.drip.optimization.constrained.*;
  5. import org.drip.service.env.EnvManager;

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

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

  51. /**
  52.  * KKTNecessarySufficientConditions carries out the Zero and the First Order Necessary and the Second Order
  53.  *  Sufficiency Checks for a Constrained KKT Optimization Problem. The References are:
  54.  *
  55.  *  - Boyd, S., and L. van den Berghe (2009): Convex Optimization, Cambridge University Press, Cambridge UK.
  56.  *
  57.  *  - Eustaquio, R., E. Karas, and A. Ribeiro (2008): Constraint Qualification for Nonlinear Programming,
  58.  *      Technical Report, Federal University of Parana.
  59.  *
  60.  *  - Karush, A. (1939): Minima of Functions of Several Variables with Inequalities as Side Constraints,
  61.  *      M. Sc., University of Chicago, Chicago IL.
  62.  *
  63.  *  - Kuhn, H. W., and A. W. Tucker (1951): Nonlinear Programming, Proceedings of the Second Berkeley
  64.  *      Symposium, University of California, Berkeley CA 481-492.
  65.  *
  66.  *  - Ruszczynski, A. (2006): Nonlinear Optimization, Princeton University Press, Princeton NJ.
  67.  *
  68.  * @author Lakshmi Krishnamurthy
  69.  */

  70. public class KKTNecessarySufficientConditions
  71. {

  72.     private static final RdToR1 ObjectiveFunction (
  73.         final double x0,
  74.         final double x1,
  75.         final double x2)
  76.         throws Exception
  77.     {
  78.         return new RdToR1 (
  79.             null
  80.         )
  81.         {
  82.             @Override public int dimension()
  83.             {
  84.                 return 3;
  85.             }

  86.             @Override public double evaluate (
  87.                 final double[] variateArray)
  88.                 throws Exception
  89.             {
  90.                 return (variateArray[0] - x0) * (variateArray[0] - x0) +
  91.                     (variateArray[1] - x1) * (variateArray[1] - x1) +
  92.                     (variateArray[2] - x2) * (variateArray[2] - x2);
  93.             }

  94.             @Override public double[] jacobian (
  95.                 final double[] variateArray)
  96.             {
  97.                 return new double[]
  98.                 {
  99.                     2. * (x0 - variateArray[0]),
  100.                     2. * (x1 - variateArray[1]),
  101.                     2. * (x2 - variateArray[2])
  102.                 };
  103.             }

  104.             @Override public double[][] hessian (
  105.                 final double[] variateArray)
  106.             {
  107.                 return new double[][]
  108.                 {
  109.                     {2., 0., 0.},
  110.                     {0., 2., 0.},
  111.                     {0., 0., 2.}
  112.                 };
  113.             }
  114.         };
  115.     }

  116.     private static final RdToR1 RightConstraintFunction (
  117.         final double deadCenter,
  118.         final int dimension,
  119.         final double halfWidth,
  120.         final boolean signFlip)
  121.         throws Exception
  122.     {
  123.         return new RdToR1 (
  124.             null
  125.         )
  126.         {
  127.             @Override public int dimension()
  128.             {
  129.                 return 3;
  130.             }

  131.             @Override public double evaluate (
  132.                 final double[] variateArray)
  133.                 throws Exception
  134.             {
  135.                 return (signFlip ? -1. : 1.) * (deadCenter + halfWidth - variateArray[dimension]);
  136.             }

  137.             @Override public double[] jacobian (
  138.                 final double[] variateArray)
  139.             {
  140.                 return new double[]
  141.                 {
  142.                     dimension == 0 ? (signFlip ? -1. : 1.) * -1. : 0.,
  143.                     dimension == 1 ? (signFlip ? -1. : 1.) * -1. : 0.,
  144.                     dimension == 2 ? (signFlip ? -1. : 1.) * -1. : 0.
  145.                 };
  146.             }

  147.             @Override public double[][] hessian (
  148.                 final double[] variateArray)
  149.             {
  150.                 return new double[][]
  151.                 {
  152.                     {0., 0., 0.},
  153.                     {0., 0., 0.},
  154.                     {0., 0., 0.}
  155.                 };
  156.             }
  157.         };
  158.     }

  159.     private static final RdToR1 LeftConstraintFunction (
  160.         final double deadCenter,
  161.         final int dimension,
  162.         final double halfWidth,
  163.         final boolean signFlip)
  164.         throws Exception
  165.     {
  166.         return new RdToR1 (
  167.             null
  168.         )
  169.         {
  170.             @Override public int dimension()
  171.             {
  172.                 return 3;
  173.             }

  174.             @Override public double evaluate (
  175.                 final double[] variateArray)
  176.                 throws Exception
  177.             {
  178.                 return (signFlip ? -1. : 1.) * (variateArray[dimension] - deadCenter + halfWidth);
  179.             }

  180.             @Override public double[] jacobian (
  181.                 final double[] variateArray)
  182.             {
  183.                 return new double[]
  184.                 {
  185.                     dimension == 0 ? (signFlip ? -1. : 1.) * 1. : 0.,
  186.                     dimension == 1 ? (signFlip ? -1. : 1.) * 1. : 0.,
  187.                     dimension == 2 ? (signFlip ? -1. : 1.) * 1. : 0.
  188.                 };
  189.             }

  190.             @Override public double[][] hessian (
  191.                 final double[] variateArray)
  192.             {
  193.                 return new double[][]
  194.                 {
  195.                     {0., 0., 0.},
  196.                     {0., 0., 0.},
  197.                     {0., 0., 0.}
  198.                 };
  199.             }
  200.         };
  201.     }

  202.     private static final RdToR1[] ConstraintFunctionArray (
  203.         final double x0,
  204.         final double x1,
  205.         final double x2,
  206.         final double halfWidth,
  207.         final boolean signFlip)
  208.         throws Exception
  209.     {
  210.         return new RdToR1[] {
  211.             LeftConstraintFunction (
  212.                 x0,
  213.                 0,
  214.                 halfWidth,
  215.                 signFlip
  216.             ),
  217.             RightConstraintFunction (
  218.                 x0,
  219.                 0,
  220.                 halfWidth,
  221.                 signFlip
  222.             ),
  223.             LeftConstraintFunction (
  224.                 x1,
  225.                 1,
  226.                 halfWidth,
  227.                 signFlip
  228.             ),
  229.             RightConstraintFunction (
  230.                 x1,
  231.                 1,
  232.                 halfWidth,
  233.                 signFlip
  234.             ),
  235.             LeftConstraintFunction (
  236.                 x2,
  237.                 2,
  238.                 halfWidth,
  239.                 signFlip
  240.             ),
  241.             RightConstraintFunction (
  242.                 x2,
  243.                 2,
  244.                 halfWidth,
  245.                 signFlip
  246.             )
  247.         };
  248.     }
  249.     public static final void main (
  250.         final String[] argumentArray)
  251.         throws Exception
  252.     {
  253.         EnvManager.InitEnv (
  254.             ""
  255.         );

  256.         double x0 = 1.;
  257.         double x1 = 2.;
  258.         double x2 = 3.;
  259.         double halfWidth = 1.;

  260.         RdToR1 objectiveFunction = ObjectiveFunction (
  261.             x0,
  262.             x1,
  263.             x2
  264.         );

  265.         double[] variateArray = new double[]
  266.         {
  267.             x0,
  268.             x1,
  269.             x2
  270.         };

  271.         FritzJohnMultipliers karushKuhnTuckerMultipliers = FritzJohnMultipliers.KarushKuhnTucker (
  272.             null,
  273.             new BarrierFixedPointFinder (
  274.                 objectiveFunction,
  275.                 ConstraintFunctionArray (
  276.                     x0,
  277.                     x1,
  278.                     x2,
  279.                     halfWidth,
  280.                     false
  281.                 ),
  282.                 new InteriorPointBarrierControl (
  283.                     InteriorPointBarrierControl.VARIATE_CONSTRAINT_SEQUENCE_CONVERGENCE,
  284.                     5.0e-06,
  285.                     1.0e-07,
  286.                     1.0e+10,
  287.                     0.5,
  288.                     20
  289.                 ),
  290.                 null
  291.             ).solve (
  292.                 new double[]
  293.                 {
  294.                     x0 + 0.25 * halfWidth,
  295.                     x1 + 0.25 * halfWidth,
  296.                     x2 + 0.25 * halfWidth
  297.                 }
  298.             ).constraintMultiplierArray()
  299.         );

  300.         OptimizationFramework optimizationFramework = new OptimizationFramework (
  301.             objectiveFunction,
  302.             null,
  303.             ConstraintFunctionArray (
  304.                 x0,
  305.                 x1,
  306.                 x2,
  307.                 halfWidth,
  308.                 true
  309.             )
  310.         );

  311.         System.out.println();

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

  313.         System.out.println ("\t||    KKT NECESSARY & SUFFICIENT CONDITIONS CHECK    ||");

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

  315.         System.out.println ("\t|| KKT Multiplier Compatibility             : " +
  316.             optimizationFramework.isCompatible (
  317.                 karushKuhnTuckerMultipliers
  318.             ) + "   ||"
  319.         );

  320.         System.out.println ("\t|| Dual Feasibility Check                   : " +
  321.             karushKuhnTuckerMultipliers.dualFeasibilityCheck() + "   ||"
  322.         );

  323.         System.out.println ("\t|| Primal Feasibility Check                 : " +
  324.             optimizationFramework.primalFeasibilityCheck (
  325.                 variateArray
  326.             ) + "   ||"
  327.         );

  328.         System.out.println ("\t|| Complementary Slackness Check            : " +
  329.             optimizationFramework.complementarySlacknessCheck (
  330.                 karushKuhnTuckerMultipliers,
  331.                 variateArray
  332.             ) + "  ||"
  333.         );

  334.         System.out.println ("\t|| First Order Necessary Condition Check    : " +
  335.             optimizationFramework.isFONC (
  336.                 karushKuhnTuckerMultipliers,
  337.                 variateArray
  338.             ) + "   ||"
  339.         );

  340.         System.out.println ("\t|| Second Order Sufficiency Condition Check : " +
  341.             optimizationFramework.isSOSC (
  342.                 karushKuhnTuckerMultipliers,
  343.                 variateArray,
  344.                 true
  345.             ) + "   ||"
  346.         );

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

  348.         System.out.println();

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

  350.         System.out.println ("\t||                 KKT NECESSARY & SUFFICIENT CONSTIONS - ZERO, FIRST, & SECOND ORDERS                  ||");

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

  352.         String[] necessarySufficientConditionOrderArray =
  353.             optimizationFramework.necessarySufficientQualifier (
  354.                 karushKuhnTuckerMultipliers,
  355.                 variateArray,
  356.                 true
  357.             ).conditionOrder();

  358.         for (int necessarySufficientConditionOrderIndex = 0;
  359.             necessarySufficientConditionOrderIndex < necessarySufficientConditionOrderArray.length;
  360.             ++necessarySufficientConditionOrderIndex)
  361.         {
  362.             System.out.println (
  363.                 "\t|| " +
  364.                 necessarySufficientConditionOrderArray[necessarySufficientConditionOrderIndex]
  365.             );
  366.         }

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

  368.         System.out.println();
  369.     }
  370. }