LagrangianMultivariate.java

  1. package org.drip.function.rdtor1;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  *
  12.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  13.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  14.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  15.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  16.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  17.  *      and computational support.
  18.  *  
  19.  *      https://lakshmidrip.github.io/DROP/
  20.  *  
  21.  *  DROP is composed of three modules:
  22.  *  
  23.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  24.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  25.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  26.  *
  27.  *  DROP Product Core implements libraries for the following:
  28.  *  - Fixed Income Analytics
  29.  *  - Loan Analytics
  30.  *  - Transaction Cost Analytics
  31.  *
  32.  *  DROP Portfolio Core implements libraries for the following:
  33.  *  - Asset Allocation Analytics
  34.  *  - Asset Liability Management Analytics
  35.  *  - Capital Estimation Analytics
  36.  *  - Exposure Analytics
  37.  *  - Margin Analytics
  38.  *  - XVA Analytics
  39.  *
  40.  *  DROP Computational Core implements libraries for the following:
  41.  *  - Algorithm Support
  42.  *  - Computation Support
  43.  *  - Function Analysis
  44.  *  - Model Validation
  45.  *  - Numerical Analysis
  46.  *  - Numerical Optimizer
  47.  *  - Spline Builder
  48.  *  - Statistical Learning
  49.  *
  50.  *  Documentation for DROP is Spread Over:
  51.  *
  52.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  53.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  54.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  55.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  56.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  57.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  58.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  59.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  60.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  61.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  62.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  63.  *
  64.  *  Licensed under the Apache License, Version 2.0 (the "License");
  65.  *      you may not use this file except in compliance with the License.
  66.  *  
  67.  *  You may obtain a copy of the License at
  68.  *      http://www.apache.org/licenses/LICENSE-2.0
  69.  *  
  70.  *  Unless required by applicable law or agreed to in writing, software
  71.  *      distributed under the License is distributed on an "AS IS" BASIS,
  72.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  73.  *  
  74.  *  See the License for the specific language governing permissions and
  75.  *      limitations under the License.
  76.  */

  77. /**
  78.  * <i>LagrangianMultivariate</i> implements a R<sup>d</sup> To R<sup>1</sup> Multivariate Function along
  79.  * with the specified Set of Equality Constraints.
  80.  *
  81.  *  <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/function/README.md">R<sup>d</sup> To R<sup>d</sup> Function Analysis</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/function/rdtor1/README.md">Built-in R<sup>d</sup> To R<sup>1</sup> Functions</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class LagrangianMultivariate extends org.drip.function.definition.RdToR1 {
  92.     private org.drip.function.definition.RdToR1 _RdToR1Objective = null;
  93.     private org.drip.function.definition.RdToR1[] _aRdToR1EqualityConstraint = null;

  94.     /**
  95.      * LagrangianMultivariate Constructor
  96.      *
  97.      * @param RdToR1Objective The Objective Function
  98.      * @param aRdToR1EqualityConstraint Array of Equality Constraint Functions
  99.      *
  100.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  101.      */

  102.     public LagrangianMultivariate (
  103.         final org.drip.function.definition.RdToR1 RdToR1Objective,
  104.         final org.drip.function.definition.RdToR1[] aRdToR1EqualityConstraint)
  105.         throws java.lang.Exception
  106.     {
  107.         super (null);

  108.         if (null == (_RdToR1Objective = RdToR1Objective))
  109.             throw new java.lang.Exception ("LagrangianMultivariate Constructor => Invalid Inputs");

  110.         _aRdToR1EqualityConstraint = aRdToR1EqualityConstraint;
  111.     }

  112.     /**
  113.      * Retrieve the Objective R^d To R^1 Function Instance
  114.      *
  115.      * @return The Objective R^d To R^1 Function Instance
  116.      */

  117.     public org.drip.function.definition.RdToR1 objectiveFunction()
  118.     {
  119.         return _RdToR1Objective;
  120.     }

  121.     /**
  122.      * Retrieve the Array of the Constraint R^d To R^1 Function Instances
  123.      *
  124.      * @return The Array of Constraint R^d To R^1 Function Instances
  125.      */

  126.     public org.drip.function.definition.RdToR1[] constraintFunctions()
  127.     {
  128.         return _aRdToR1EqualityConstraint;
  129.     }

  130.     /**
  131.      * Retrieve the Objective Function Dimension
  132.      *
  133.      * @return The Objective Function Dimension
  134.      */

  135.     public int objectiveFunctionDimension()
  136.     {
  137.         return _RdToR1Objective.dimension();
  138.     }

  139.     /**
  140.      * Retrieve the Constraint Function Dimension
  141.      *
  142.      * @return The Constraint Function Dimension
  143.      */

  144.     public int constraintFunctionDimension()
  145.     {
  146.         return null == _aRdToR1EqualityConstraint ? 0 : _aRdToR1EqualityConstraint.length;
  147.     }

  148.     @Override public int dimension()
  149.     {
  150.         return objectiveFunctionDimension() + constraintFunctionDimension();
  151.     }

  152.     @Override public double evaluate (
  153.         final double[] adblVariate)
  154.         throws java.lang.Exception
  155.     {
  156.         org.drip.function.rdtor1.ObjectiveConstraintVariateSet ocvs =
  157.             org.drip.function.rdtor1.ObjectiveConstraintVariateSet.Partition (adblVariate,
  158.                 objectiveFunctionDimension());

  159.         if (null == ocvs)
  160.             throw new java.lang.Exception ("LagrangianMultivariate::evaluate => Invalid Inputs");

  161.         double[] adblConstraintVariate = ocvs.constraintVariates();

  162.         double[] adblObjectiveVariate = ocvs.objectiveVariates();

  163.         int iNumConstraint = adblConstraintVariate.length;

  164.         double dblValue = _RdToR1Objective.evaluate (adblObjectiveVariate);

  165.         for (int i = 0; i < iNumConstraint; ++i)
  166.             dblValue += adblConstraintVariate[i] * _aRdToR1EqualityConstraint[i].evaluate
  167.                 (adblObjectiveVariate);

  168.         return dblValue;
  169.     }

  170.     @Override public double[] jacobian (
  171.         final double[] adblVariate)
  172.     {
  173.         int iObjectiveDimension = objectiveFunctionDimension();

  174.         int iConstraintDimension = constraintFunctionDimension();

  175.         double[] adblObjectiveVariate = null;
  176.         double[] adblConstraintVariate = null;
  177.         double[][] aadblConstraintJacobian = null;
  178.         double[] adblJacobian = new double[iObjectiveDimension + iConstraintDimension];

  179.         if (0 == iConstraintDimension)
  180.             adblObjectiveVariate = adblVariate;
  181.         else {
  182.             org.drip.function.rdtor1.ObjectiveConstraintVariateSet ocvs =
  183.                 org.drip.function.rdtor1.ObjectiveConstraintVariateSet.Partition (adblVariate,
  184.                     iObjectiveDimension);

  185.             if (null == ocvs) return null;

  186.             adblObjectiveVariate = ocvs.objectiveVariates();

  187.             adblConstraintVariate = ocvs.constraintVariates();
  188.         }

  189.         double[] adblObjectiveJacobian = _RdToR1Objective.jacobian (adblObjectiveVariate);

  190.         if (null == adblObjectiveJacobian) return null;

  191.         if (0 != iConstraintDimension) aadblConstraintJacobian = new double[iConstraintDimension][];

  192.         for (int i = 0; i < iConstraintDimension; ++i) {
  193.             if (null == (aadblConstraintJacobian[i] = _aRdToR1EqualityConstraint[i].jacobian
  194.                 (adblObjectiveVariate)))
  195.                 return null;

  196.             try {
  197.                 adblJacobian[iObjectiveDimension + i] = _aRdToR1EqualityConstraint[i].evaluate
  198.                     (adblObjectiveVariate);
  199.             } catch (java.lang.Exception e) {
  200.                 e.printStackTrace();

  201.                 return null;
  202.             }
  203.         }

  204.         for (int i = 0; i < iObjectiveDimension; ++i) {
  205.             adblJacobian[i] = adblObjectiveJacobian[i];

  206.             for (int j = 0; j < iConstraintDimension; ++j)
  207.                 adblJacobian[i] += adblConstraintVariate[j] * aadblConstraintJacobian[j][i];
  208.         }

  209.         return adblJacobian;
  210.     }

  211.     @Override public double[][] hessian (
  212.         final double[] adblVariate)
  213.     {
  214.         int iObjectiveDimension = objectiveFunctionDimension();

  215.         int iConstraintDimension = constraintFunctionDimension();

  216.         double[] adblObjectiveVariate = null;
  217.         double[] adblConstraintVariate = null;

  218.         if (0 == iConstraintDimension)
  219.             adblObjectiveVariate = adblVariate;
  220.         else {
  221.             org.drip.function.rdtor1.ObjectiveConstraintVariateSet ocvs =
  222.                 org.drip.function.rdtor1.ObjectiveConstraintVariateSet.Partition (adblVariate,
  223.                     iObjectiveDimension);

  224.             if (null == ocvs) return null;

  225.             adblObjectiveVariate = ocvs.objectiveVariates();

  226.             adblConstraintVariate = ocvs.constraintVariates();
  227.         }

  228.         double[][] aadblObjectiveHessian = _RdToR1Objective.hessian (adblObjectiveVariate);

  229.         double[][] aadblConstraintJacobian = null;
  230.         double[][][] aaadblConstraintHessian = null;
  231.         int iDimension = iObjectiveDimension + iConstraintDimension;
  232.         double[][] aadblHessian = new double[iDimension][iDimension];

  233.         if (0 != iConstraintDimension) {
  234.             aadblConstraintJacobian = new double[iConstraintDimension][];
  235.             aaadblConstraintHessian = new double[iConstraintDimension][][];
  236.         }

  237.         for (int i = 0; i < iConstraintDimension; ++i) {
  238.             if (null == (aaadblConstraintHessian[i] = _aRdToR1EqualityConstraint[i].hessian
  239.                 (adblObjectiveVariate)))
  240.                 return null;
  241.         }

  242.         for (int i = 0; i < iObjectiveDimension; ++i) {
  243.             for (int j = 0; j < iObjectiveDimension; ++j) {
  244.                 aadblHessian[i][j] = aadblObjectiveHessian[i][j];

  245.                 for (int k = 0; k < iConstraintDimension; ++k)
  246.                     aadblHessian[i][j] += adblConstraintVariate[k] * aaadblConstraintHessian[k][i][j];
  247.             }
  248.         }

  249.         for (int i = 0; i < iConstraintDimension; ++i) {
  250.             for (int j = 0; j < iConstraintDimension; ++j)
  251.                 aadblHessian[i + iObjectiveDimension][j + iObjectiveDimension] = 0.;

  252.             if (null == (aadblConstraintJacobian[i] = _aRdToR1EqualityConstraint[i].jacobian
  253.                 (adblObjectiveVariate)))
  254.                 return null;
  255.         }

  256.         for (int i = 0; i < iConstraintDimension; ++i) {
  257.             for (int j = 0; j < iObjectiveDimension; ++j) {
  258.                 aadblHessian[iObjectiveDimension + i][j] = aadblConstraintJacobian[i][j];
  259.                 aadblHessian[j][iObjectiveDimension + i] = aadblConstraintJacobian[i][j];
  260.             }
  261.         }

  262.         return aadblHessian;
  263.     }
  264. }