R1ToR1.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.  * 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>R1ToR1</i> provides the evaluation of the objective function and its derivatives for a specified
  83.  * variate. Default implementation of the derivatives are for non-analytical black box objective functions.
  84.  *
  85.  *  <br><br>
  86.  *  <ul>
  87.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  88.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  89.  *      <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>
  90.  *      <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>
  91.  *  </ul>
  92.  *
  93.  * @author Lakshmi Krishnamurthy
  94.  */

  95. public abstract class R1ToR1 {
  96.     protected org.drip.numerical.differentiation.DerivativeControl _dc = null;

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

  102.     /**
  103.      * Evaluate for the given variate
  104.      *
  105.      * @param dblVariate Variate
  106.      *  
  107.      * @return Returns the calculated value
  108.      *
  109.      * @throws java.lang.Exception Thrown if evaluation cannot be done
  110.      */

  111.     public abstract double evaluate (
  112.         final double dblVariate)
  113.         throws java.lang.Exception;

  114.     /**
  115.      * Calculate the Differential
  116.      *
  117.      * @param dblVariate Variate at which the derivative is to be calculated
  118.      * @param dblOFBase Base Value for the Objective Function
  119.      * @param iOrder Order of the derivative to be computed
  120.      *
  121.      * @return The Derivative
  122.      */

  123.     public org.drip.numerical.differentiation.Differential differential (
  124.         final double dblVariate,
  125.         final double dblOFBase,
  126.         final int iOrder)
  127.     {
  128.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblVariate) || 0 >= iOrder) return null;

  129.         double dblDerivative = 0.;
  130.         double dblOrderedVariateInfinitesimal = 1.;
  131.         double dblVariateInfinitesimal = java.lang.Double.NaN;

  132.         try {
  133.             dblVariateInfinitesimal = _dc.getVariateInfinitesimal (dblVariate);
  134.         } catch (java.lang.Exception e) {
  135.             e.printStackTrace();

  136.             return null;
  137.         }

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

  140.             try {
  141.                 dblDerivative += (i % 2 == 0 ? 1 : -1) * org.drip.numerical.common.NumberUtil.NCK (iOrder, i) *
  142.                     evaluate (dblVariate + dblVariateInfinitesimal * (iOrder - 2. * i));
  143.             } catch (java.lang.Exception e) {
  144.                 e.printStackTrace();

  145.                 return null;
  146.             }
  147.         }

  148.         try {
  149.             return new org.drip.numerical.differentiation.Differential (dblOrderedVariateInfinitesimal, dblDerivative);
  150.         } catch (java.lang.Exception e) {
  151.             e.printStackTrace();
  152.         }

  153.         return null;
  154.     }

  155.     /**
  156.      * Calculate the Differential
  157.      *
  158.      * @param dblVariate Variate at which the derivative is to be calculated
  159.      * @param iOrder Order of the derivative to be computed
  160.      *
  161.      * @return The Derivative
  162.      */

  163.     public org.drip.numerical.differentiation.Differential differential (
  164.         final double dblVariate,
  165.         final int iOrder)
  166.     {
  167.         try {
  168.             return differential (dblVariate, evaluate (dblVariate), iOrder);
  169.         } catch (java.lang.Exception e) {
  170.             e.printStackTrace();
  171.         }

  172.         return null;
  173.     }

  174.     /**
  175.      * Calculate the derivative as a double
  176.      *
  177.      * @param dblVariate Variate at which the derivative is to be calculated
  178.      * @param iOrder Order of the derivative to be computed
  179.      *
  180.      * @return The Derivative
  181.      *
  182.      * @throws java.lang.Exception Thrown if Inputs are Invalid
  183.      */

  184.     public double derivative (
  185.         final double dblVariate,
  186.         final int iOrder)
  187.         throws java.lang.Exception
  188.     {
  189.         return differential (dblVariate, evaluate (dblVariate), iOrder).calcSlope (true);
  190.     }

  191.     /**
  192.      * Integrate over the given range
  193.      *
  194.      * @param dblBegin Range Begin
  195.      * @param dblEnd Range End
  196.      *  
  197.      * @return The Integrated Value
  198.      *
  199.      * @throws java.lang.Exception Thrown if evaluation cannot be done
  200.      */

  201.     public double integrate (
  202.         final double dblBegin,
  203.         final double dblEnd)
  204.         throws java.lang.Exception
  205.     {
  206.         return org.drip.numerical.integration.R1ToR1Integrator.Boole (this, dblBegin, dblEnd);
  207.     }

  208.     /**
  209.      * Compute the Maximal Variate and the Corresponding Function Value
  210.      *
  211.      * @return The Maximal Variate and the Corresponding Function Value
  212.      */

  213.     public org.drip.function.definition.VariateOutputPair maxima()
  214.     {
  215.         R1ToR1 auDerivative = new R1ToR1 (null) {
  216.             @Override public double evaluate (
  217.                 final double dblX)
  218.                 throws java.lang.Exception
  219.             {
  220.                 return derivative (dblX, 1);
  221.             }
  222.         };

  223.         try {
  224.             org.drip.function.r1tor1solver.FixedPointFinder fpf = new
  225.                 org.drip.function.r1tor1solver.FixedPointFinderZheng (0., auDerivative, false);

  226.             org.drip.function.r1tor1solver.FixedPointFinderOutput fpfo = fpf.findRoot();

  227.             if (null == fpfo) return null;

  228.             double dblExtrema = fpfo.getRoot();

  229.             if (0. <= derivative (dblExtrema, 2)) return null;

  230.             return new org.drip.function.definition.VariateOutputPair (new double[] {dblExtrema}, new
  231.                 double[] {evaluate (dblExtrema)});
  232.         } catch (java.lang.Exception e) {
  233.             e.printStackTrace();
  234.         }

  235.         return null;
  236.     }

  237.     /**
  238.      * Compute the Maximum VOP within the Variate Range
  239.      *
  240.      * @param dblVariateLeft The Range Left End
  241.      * @param dblVariateRight The Range Right End
  242.      *
  243.      * @return The Maximum VOP
  244.      */

  245.     public org.drip.function.definition.VariateOutputPair maxima (
  246.         final double dblVariateLeft,
  247.         final double dblVariateRight)
  248.     {
  249.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblVariateLeft) ||
  250.             !org.drip.numerical.common.NumberUtil.IsValid (dblVariateRight) || dblVariateLeft >= dblVariateRight)
  251.             return null;

  252.         org.drip.function.definition.VariateOutputPair vop = maxima();

  253.         if (null != vop) {
  254.             double dblRoot = vop.variates()[0];

  255.             if (dblVariateLeft <= dblRoot && dblVariateRight >= dblRoot) return vop;
  256.         }

  257.         try {
  258.             double dblLeftOutput = evaluate (dblVariateLeft);

  259.             double dblRightOutput = evaluate (dblVariateRight);

  260.             return dblLeftOutput > dblRightOutput ? new org.drip.function.definition.VariateOutputPair
  261.                 (new double[] {dblVariateLeft}, new double[] {dblLeftOutput}) : new
  262.                     org.drip.function.definition.VariateOutputPair (new double[] {dblVariateRight}, new
  263.                         double[] {dblRightOutput});
  264.         } catch (java.lang.Exception e) {
  265.             e.printStackTrace();
  266.         }

  267.         return null;
  268.     }

  269.     /**
  270.      * Compute the Minimal Variate and the Corresponding Function Value
  271.      *
  272.      * @return The Minimal Variate and the Corresponding Function Value
  273.      */

  274.     public org.drip.function.definition.VariateOutputPair minima()
  275.     {
  276.         R1ToR1 auDerivative = new R1ToR1 (null) {
  277.             @Override public double evaluate (
  278.                 final double dblX)
  279.                 throws java.lang.Exception
  280.             {
  281.                 return derivative (dblX, 1);
  282.             }
  283.         };

  284.         try {
  285.             org.drip.function.r1tor1solver.FixedPointFinder fpf = new
  286.                 org.drip.function.r1tor1solver.FixedPointFinderZheng (0., auDerivative, false);

  287.             org.drip.function.r1tor1solver.FixedPointFinderOutput fpfo = fpf.findRoot();

  288.             if (null == fpfo) return null;

  289.             double dblExtrema = fpfo.getRoot();

  290.             if (0. >= derivative (dblExtrema, 2)) return null;

  291.             return new org.drip.function.definition.VariateOutputPair (new double[] {dblExtrema}, new
  292.                 double[] {evaluate (dblExtrema)});
  293.         } catch (java.lang.Exception e) {
  294.             e.printStackTrace();
  295.         }

  296.         return null;
  297.     }

  298.     /**
  299.      * Compute the Minimum VOP within the Variate Range
  300.      *
  301.      * @param dblVariateLeft The Range Left End
  302.      * @param dblVariateRight The Range Right End
  303.      *
  304.      * @return The Minimum VOP
  305.      */

  306.     public org.drip.function.definition.VariateOutputPair minima (
  307.         final double dblVariateLeft,
  308.         final double dblVariateRight)
  309.     {
  310.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblVariateLeft) ||
  311.             !org.drip.numerical.common.NumberUtil.IsValid (dblVariateRight) || dblVariateLeft >= dblVariateRight)
  312.             return null;

  313.         org.drip.function.definition.VariateOutputPair vop = minima();

  314.         if (null != vop) {
  315.             double dblRoot = vop.variates()[0];

  316.             if (dblVariateLeft <= dblRoot && dblVariateRight >= dblRoot) return vop;
  317.         }

  318.         try {
  319.             double dblLeftOutput = evaluate (dblVariateLeft);

  320.             double dblRightOutput = evaluate (dblVariateRight);

  321.             return dblLeftOutput < dblRightOutput ? new org.drip.function.definition.VariateOutputPair
  322.                 (new double[] {dblVariateLeft}, new double[] {dblLeftOutput}) : new
  323.                     org.drip.function.definition.VariateOutputPair (new double[] {dblVariateRight}, new
  324.                         double[] {dblRightOutput});
  325.         } catch (java.lang.Exception e) {
  326.             e.printStackTrace();
  327.         }

  328.         return null;
  329.     }

  330.     /**
  331.      * Compute the Anti-Derivative Function
  332.      *
  333.      * @return The Anti-Derivative Function
  334.      */

  335.     public R1ToR1 antiDerivative()
  336.     {
  337.         return null;
  338.     }

  339.     /**
  340.      * Compute the Residue if the Variate is a Pole
  341.      *
  342.      * @param x Variate
  343.      *
  344.      * @return The Residue if the Variate is a Pole
  345.      */

  346.     public org.drip.function.definition.PoleResidue poleResidue (
  347.         final double x)
  348.     {
  349.         return null;
  350.     }
  351. }