RdToRd.java

  1. package org.drip.function.definition;

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

  78. /**
  79.  * <i>RdToRd</i> provides the evaluation of the R<sup>d</sup> To R<sup>d</sup> objective function and its
  80.  * derivatives for a specified set of R<sup>d</sup> variates. Default implementation of the derivatives are
  81.  * for non-analytical black box objective functions.
  82.  *
  83.  *  <br><br>
  84.  *  <ul>
  85.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  86.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  87.  *      <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>
  88.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/function/definition/README.md">Function Implementation Ancillary Support Objects</a></li>
  89.  *  </ul>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public abstract class RdToRd {
  94.     private static final int QUADRATURE_SAMPLING = 10000;

  95.     protected org.drip.numerical.differentiation.DerivativeControl _dc = null;

  96.     protected RdToRd (
  97.         final org.drip.numerical.differentiation.DerivativeControl dc)
  98.     {
  99.         if (null == (_dc = dc)) _dc = new org.drip.numerical.differentiation.DerivativeControl();
  100.     }

  101.     /**
  102.      * Evaluate for the given Input R^d Variates
  103.      *
  104.      * @param adblVariate Array of Input R^d Variates
  105.      *  
  106.      * @return The Output R^d Variates
  107.      */

  108.     public abstract double[] evaluate (
  109.         final double[] adblVariate);

  110.     /**
  111.      * Calculate the Array of Differentials
  112.      *
  113.      * @param adblVariate Variate Array at which the derivative is to be calculated
  114.      * @param iVariateIndex Index of the Variate whose Derivative is to be computed
  115.      * @param iOrder Order of the derivative to be computed
  116.      *
  117.      * @return The Array of Differentials
  118.      */

  119.     public org.drip.numerical.differentiation.Differential[] differential (
  120.         final double[] adblVariate,
  121.         final int iVariateIndex,
  122.         final int iOrder)
  123.     {
  124.         if (!org.drip.numerical.common.NumberUtil.IsValid (adblVariate) || 0 >= iOrder) return null;

  125.         int iOutputDimension = -1;
  126.         double[] adblDerivative = null;
  127.         int iNumVariate = adblVariate.length;
  128.         double dblOrderedVariateInfinitesimal = 1.;
  129.         double dblVariateInfinitesimal = java.lang.Double.NaN;

  130.         if (iNumVariate <= iVariateIndex) return null;

  131.         try {
  132.             dblVariateInfinitesimal = _dc.getVariateInfinitesimal (adblVariate[iVariateIndex]);
  133.         } catch (java.lang.Exception e) {
  134.             e.printStackTrace();

  135.             return null;
  136.         }

  137.         for (int i = 0; i <= iOrder; ++i) {
  138.             if (0 != i) dblOrderedVariateInfinitesimal *= (2. * dblVariateInfinitesimal);

  139.             double[] adblVariateIncremental = new double[iNumVariate];

  140.             for (int j = 0; i < iNumVariate; ++j)
  141.                 adblVariateIncremental[j] = j == iVariateIndex ? adblVariate[j] + dblVariateInfinitesimal *
  142.                     (iOrder - 2. * i) : adblVariate[j];

  143.             double[] adblValue = evaluate (adblVariateIncremental);

  144.             if (null == adblValue || 0 == (iOutputDimension = adblValue.length)) return null;

  145.             if (null == adblDerivative) {
  146.                 adblDerivative = new double[iOutputDimension];

  147.                 for (int j = 0; j < iOutputDimension; ++j)
  148.                     adblDerivative[j] = 0.;
  149.             }

  150.             try {
  151.                 for (int j = 0; j < iOutputDimension; ++j)
  152.                     adblDerivative[j] += (i % 2 == 0 ? 1 : -1) * org.drip.numerical.common.NumberUtil.NCK
  153.                         (iOrder, i) * adblValue[j];
  154.             } catch (java.lang.Exception e) {
  155.                 e.printStackTrace();

  156.                 return null;
  157.             }
  158.         }

  159.         org.drip.numerical.differentiation.Differential[] aDiff = new
  160.             org.drip.numerical.differentiation.Differential[iOutputDimension];

  161.         try {
  162.             for (int j = 0; j < iOutputDimension; ++j)
  163.                 aDiff[j] = new org.drip.numerical.differentiation.Differential (dblOrderedVariateInfinitesimal,
  164.                     adblDerivative[j]);
  165.         } catch (java.lang.Exception e) {
  166.             e.printStackTrace();

  167.             return null;
  168.         }

  169.         return aDiff;
  170.     }

  171.     /**
  172.      * Calculate the Derivative Array as a double
  173.      *
  174.      * @param adblVariate Variate Array at which the derivative is to be calculated
  175.      * @param iVariateIndex Index of the Variate whose Derivative is to be computed
  176.      * @param iOrder Order of the derivative to be computed
  177.      *
  178.      * @return The Derivative Array
  179.      */

  180.     public double[] derivative (
  181.         final double[] adblVariate,
  182.         final int iVariateIndex,
  183.         final int iOrder)
  184.     {
  185.         org.drip.numerical.differentiation.Differential[] aDiff = differential (adblVariate, iVariateIndex, iOrder);

  186.         if (null == aDiff) return null;

  187.         int iOutputDimension = aDiff.length;
  188.         double[] adblDerivative = new double[iOutputDimension];

  189.         if (0 == iOutputDimension) return null;

  190.         for (int i = 0; i < iOutputDimension; ++i)
  191.             adblDerivative[i] = aDiff[i].calcSlope (true);

  192.         return adblDerivative;
  193.     }

  194.     /**
  195.      * Integrate over the given Input Range Using Uniform Monte-Carlo
  196.      *
  197.      * @param adblLeftEdge Array of Input Left Edge
  198.      * @param adblRightEdge Array of Input Right Edge
  199.      *  
  200.      * @return The Array Containing the Result of the Integration over the specified Range
  201.      */

  202.     public double[] integrate (
  203.         final double[] adblLeftEdge,
  204.         final double[] adblRightEdge)
  205.     {
  206.         if (!org.drip.numerical.common.NumberUtil.IsValid (adblLeftEdge) ||
  207.             !org.drip.numerical.common.NumberUtil.IsValid (adblRightEdge))
  208.             return null;

  209.         int iOutputDimension = -1;
  210.         double[] adblIntegrand = null;
  211.         int iNumVariate = adblLeftEdge.length;
  212.         double[] adblVariate = new double[iNumVariate];
  213.         double[] adblVariateWidth = new double[iNumVariate];

  214.         if (adblRightEdge.length != iNumVariate) return null;

  215.         for (int j = 0; j < iNumVariate; ++j)
  216.             adblVariateWidth[j] = adblRightEdge[j] - adblLeftEdge[j];

  217.         for (int i = 0; i < QUADRATURE_SAMPLING; ++i) {
  218.             for (int j = 0; j < iNumVariate; ++j)
  219.                 adblVariate[j] = adblLeftEdge[j] + java.lang.Math.random() * adblVariateWidth[j];

  220.             double[] adblValue = evaluate (adblVariate);

  221.             if (null == adblValue || 0 == (iOutputDimension = adblValue.length)) return null;

  222.             if (null == adblIntegrand) adblIntegrand = new double[iOutputDimension];

  223.             for (int j = 0; j < iOutputDimension; ++j)
  224.                 adblIntegrand[j] += adblValue[j];
  225.         }

  226.         for (int i = 0; i < iOutputDimension; ++i) {
  227.             for (int j = 0; j < iNumVariate; ++j)
  228.                 adblIntegrand[i] = adblIntegrand[i] * adblVariateWidth[j];

  229.             adblIntegrand[i] /= QUADRATURE_SAMPLING;
  230.         }

  231.         return adblIntegrand;
  232.     }
  233. }