WengertJacobian.java

  1. package org.drip.numerical.differentiation;

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

  81. /**
  82.  * <i>WengertJacobian</i> contains the Jacobian of the given set of Wengert variables to the set of
  83.  * parameters. It exposes the following functionality:
  84.  *
  85.  * <br><br>
  86.  *  <ul>
  87.  *      <li>
  88.  *          Set/Retrieve the Wengert variables
  89.  *      </li>
  90.  *      <li>
  91.  *          Accumulate the Partials
  92.  *      </li>
  93.  *      <li>
  94.  *          Scale the partial entries
  95.  *      </li>
  96.  *      <li>
  97.  *          Merge the Jacobian with another
  98.  *      </li>
  99.  *      <li>
  100.  *          Retrieve the WengertJacobian elements
  101.  *      </li>
  102.  *      <li>
  103.  *          Display the contents of the WengertJacobian
  104.  *      </li>
  105.  *  </ul>
  106.  *
  107.  * <br><br>
  108.  *  <ul>
  109.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  110.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  111.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical">Numerical Quadrature, Differentiation, Eigenization, Linear Algebra, and Utilities</a></li>
  112.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/differentiation">R<sup>1</sup> To R<sup>1</sup> Numerical Differentiation</a></li>
  113.  *  </ul>
  114.  * <br><br>
  115.  *
  116.  * @author Lakshmi Krishnamurthy
  117.  */

  118. public class WengertJacobian {
  119.     private double[] _adblWengert = null;
  120.     private double[][] _aadblDWengertDParameter = null;

  121.     /**
  122.      * WengertJacobian constructor
  123.      *
  124.      * @param iNumWengerts Number of Wengert variables
  125.      * @param iNumParameters Number of Parameters
  126.      *
  127.      * @throws java.lang.Exception Thrown if the inputs are invalid
  128.      */

  129.     public WengertJacobian (
  130.         final int iNumWengerts,
  131.         final int iNumParameters)
  132.         throws java.lang.Exception
  133.     {
  134.         if (0 >= iNumWengerts || 0 >= iNumParameters)
  135.             throw new java.lang.Exception ("WengertJacobian constructor: Invalid inputs");

  136.         _adblWengert = new double[iNumWengerts];
  137.         _aadblDWengertDParameter = new double[iNumWengerts][iNumParameters];

  138.         for (int iWengertIndex = 0; iWengertIndex < _aadblDWengertDParameter.length; ++iWengertIndex) {
  139.             for (int iParameterIndex = 0; iParameterIndex < _aadblDWengertDParameter[0].length;
  140.                 ++iParameterIndex)
  141.                 _aadblDWengertDParameter[iWengertIndex][iParameterIndex] = 0.;
  142.         }
  143.     }

  144.     /**
  145.      * Retrieve the number of Wengert Variables
  146.      *
  147.      * @return Number of Wengert Variables
  148.      */

  149.     public int numWengerts()
  150.     {
  151.         return null == _adblWengert ? 0 : _adblWengert.length;
  152.     }

  153.     /**
  154.      * Retrieve the number of Parameters
  155.      *
  156.      * @return Number of Parameters
  157.      */

  158.     public int numParameters()
  159.     {
  160.         return (null == _aadblDWengertDParameter || null == _aadblDWengertDParameter[0]) ? 0 :
  161.             _aadblDWengertDParameter[0].length;
  162.     }

  163.     /**
  164.      * Set the Value for the Wengert variable
  165.      *
  166.      * @param iWengertIndex Wengert Variable Index
  167.      * @param dblWengert The Value for the Wengert Variable
  168.      *
  169.      * @return TRUE - Successfully set
  170.      */

  171.     public boolean setWengert (
  172.         final int iWengertIndex,
  173.         final double dblWengert)
  174.     {
  175.         if (0 > iWengertIndex || iWengertIndex >= _adblWengert.length ||
  176.             !org.drip.numerical.common.NumberUtil.IsValid (dblWengert))
  177.             return false;

  178.         _adblWengert[iWengertIndex] = dblWengert;
  179.         return true;
  180.     }

  181.     /**
  182.      * Get the Value for the Wengert Variable
  183.      *
  184.      * @param iIndex Wengert Variable Index
  185.      *
  186.      * @return The Value for the Wengert variable
  187.      *
  188.      * @throws java.lang.Exception Thrown if the inputs are invalid
  189.      */

  190.     public double wengert (
  191.         final int iIndex)
  192.         throws java.lang.Exception
  193.     {
  194.         if (0 > iIndex || iIndex >= _adblWengert.length)
  195.             throw new java.lang.Exception ("WengertJacobian::wengert => Invalid Wengert Variable Index!");

  196.         return _adblWengert[iIndex];
  197.     }

  198.     /**
  199.      * Accumulate {D(Wengert)}/{D(Parameter)}
  200.      *
  201.      * @param iWengertIndex Wengert Variable Index
  202.      * @param iParameterIndex Parameter Index
  203.      * @param dblDWengertDParameter The incremental {D(Wengert)}/{D(Parameter)}
  204.      *
  205.      * @return TRUE - Successfully set
  206.      */

  207.     public boolean accumulatePartialFirstDerivative (
  208.         final int iWengertIndex,
  209.         final int iParameterIndex,
  210.         final double dblDWengertDParameter)
  211.     {
  212.         if (0 > iParameterIndex || iParameterIndex >= _aadblDWengertDParameter[0].length || 0 > iWengertIndex
  213.             || iWengertIndex >= _adblWengert.length || !org.drip.numerical.common.NumberUtil.IsValid
  214.                 (dblDWengertDParameter))
  215.             return false;

  216.         _aadblDWengertDParameter[iWengertIndex][iParameterIndex] += dblDWengertDParameter;
  217.         return true;
  218.     }

  219.     /**
  220.      * Retrieve {D(Wengert)}/{D(Parameter)} for the Wengert and the parameter identified by their indices
  221.      *
  222.      * @param iWengertIndex Wengert Variable Index
  223.      * @param iParameterIndex Parameter Index
  224.      *
  225.      * @return {D(Wengert)}/{D(Parameter)}
  226.      */

  227.     public double firstDerivative (
  228.         final int iWengertIndex,
  229.         final int iParameterIndex)
  230.     {
  231.         return _aadblDWengertDParameter[iWengertIndex][iParameterIndex];
  232.     }

  233.     /**
  234.      * Accumulate and merge partial entries from the other CurveWengertJacobian
  235.      *
  236.      * @param wjOther CurveWengertJacobian to be accumulated and merged
  237.      *
  238.      * @return TRUE - Successfully accumulated and merged
  239.      */

  240.     public boolean cumulativeMerge (
  241.         final org.drip.numerical.differentiation.WengertJacobian wjOther)
  242.     {
  243.         if (null == wjOther) return false;

  244.         for (int iWengertIndex = 0; iWengertIndex < _aadblDWengertDParameter.length; ++iWengertIndex) {
  245.             for (int iParameterIndex = 0; iParameterIndex < _aadblDWengertDParameter[0].length;
  246.                 ++iParameterIndex)
  247.                 _aadblDWengertDParameter[iWengertIndex][iParameterIndex] += wjOther.firstDerivative
  248.                     (iWengertIndex, iParameterIndex);
  249.         }

  250.         return true;
  251.     }

  252.     /**
  253.      * Accumulate and merge the weighted partial entries from the other CurveWengertJacobian
  254.      *
  255.      * @param wjOther CurveWengertJacobian to be accumulated and merged
  256.      * @param dblWeight The Weight
  257.      *
  258.      * @return TRUE - Successfully accumulated and merged
  259.      */

  260.     public boolean cumulativeMerge (
  261.         final org.drip.numerical.differentiation.WengertJacobian wjOther,
  262.         final double dblWeight)
  263.     {
  264.         if (null == wjOther || !org.drip.numerical.common.NumberUtil.IsValid (dblWeight)) return false;

  265.         for (int iWengertIndex = 0; iWengertIndex < _aadblDWengertDParameter.length; ++iWengertIndex) {
  266.             for (int iParameterIndex = 0; iParameterIndex < _aadblDWengertDParameter[0].length;
  267.                 ++iParameterIndex)
  268.                 _aadblDWengertDParameter[iWengertIndex][iParameterIndex] += wjOther.firstDerivative
  269.                     (iWengertIndex, iParameterIndex) * dblWeight;
  270.         }

  271.         return true;
  272.     }

  273.     /**
  274.      * Scale the partial entries
  275.      *
  276.      * @param dblScale Factor by which the partials are to be scaled by
  277.      *
  278.      * @return TRUE - Scaling down successful
  279.      */

  280.     public boolean scale (
  281.         final double dblScale)
  282.     {
  283.         if (0 >= dblScale) return false;

  284.         for (int iWengertIndex = 0; iWengertIndex < _aadblDWengertDParameter.length; ++iWengertIndex) {
  285.             for (int iParameterIndex = 0; iParameterIndex < _aadblDWengertDParameter[0].length;
  286.                 ++iParameterIndex)
  287.                 _aadblDWengertDParameter[iWengertIndex][iParameterIndex] *= dblScale;
  288.         }

  289.         return true;
  290.     }

  291.     /**
  292.      * Stringifies the contents of WengertJacobian
  293.      *
  294.      * @return Stringified WengertJacobian
  295.      */

  296.     public java.lang.String displayString()
  297.     {
  298.         java.lang.StringBuffer sb = new java.lang.StringBuffer();

  299.         for (int iWengertIndex = 0; iWengertIndex < _aadblDWengertDParameter.length; ++iWengertIndex) {
  300.             java.lang.StringBuffer sbDWengertDParameter = new java.lang.StringBuffer();

  301.             sbDWengertDParameter.append ("Wengert{" + iWengertIndex + "} => [");

  302.             for (int iParameterIndex = 0; iParameterIndex < _aadblDWengertDParameter[0].length;
  303.                 ++iParameterIndex) {
  304.                 if (0 != iParameterIndex) sbDWengertDParameter.append (", ");

  305.                 sbDWengertDParameter.append (org.drip.numerical.common.FormatUtil.FormatDouble
  306.                     (_aadblDWengertDParameter[iWengertIndex][iParameterIndex], 1, 3, 1.));
  307.             }

  308.             sb.append (sbDWengertDParameter).append ("]\n");
  309.         }

  310.         return sb.toString();
  311.     }
  312. }