FixedPointFinder.java

  1. package org.drip.function.r1tor1solver;

  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>FixedPointFinder</i> is the base abstract class that is implemented by customized invocations, e.g.,
  83.  * Newton's method, or any of the bracketing methodologies.
  84.  * <br><br>
  85.  * FixedPointFinder invokes the core routine for determining the fixed point from the goal. The
  86.  *  ExecutionControl determines the execution termination. The initialization heuristics implements
  87.  *  targeted customization of the search.
  88.  * <br><br>
  89.  * FixedPointFinder main flow comprises of the following steps:
  90.  * <br>
  91.  * <ul>
  92.  *  <li>
  93.  *      Initialize the fixed point search zone by determining either a) the brackets, or b) the starting
  94.  *          variate.
  95.  *  </li>
  96.  *  <li>
  97.  *      Compute the absolute OF tolerance that establishes the attainment of the fixed point.
  98.  *  </li>
  99.  *  <li>
  100.  *      Launch the variate iterator that iterates the variate.
  101.  *  </li>
  102.  *  <li>
  103.  *      Iterate until the desired tolerance has been attained
  104.  *  </li>
  105.  *  <li>
  106.  *      Return the fixed point output.
  107.  *  </li>
  108.  * </ul>
  109.  * <br><br>
  110.  * Fixed point finders that derive from this provide implementations for the following:
  111.  * <br>
  112.  * <ul>
  113.  *  <li>
  114.  *  - Variate initialization: They may choose either bracketing initializer, or the convergence initializer -
  115.  *      functionality is provided for both in this module.
  116.  *  </li>
  117.  *  <li>
  118.  *  - Variate Iteration: Variates are iterated using a) any of the standard primitive built-in variate
  119.  *      iterators (or custom ones), or b) a variate selector scheme for each iteration.
  120.  *  </li>
  121.  * </ul>
  122.  *
  123.  *  <br><br>
  124.  *  <ul>
  125.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  126.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  127.  *      <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>
  128.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/function/r1tor1solver/README.md">Built-in R<sup>1</sup> To R<sup>1</sup> Solvers</a></li>
  129.  *  </ul>
  130.  *
  131.  * @author Lakshmi Krishnamurthy
  132.  */

  133. public abstract class FixedPointFinder {
  134.     protected boolean _bWhine = false;
  135.     protected double _dblOFGoal = java.lang.Double.NaN;
  136.     protected org.drip.function.r1tor1solver.ExecutionControl _ec = null;
  137.     protected org.drip.function.definition.R1ToR1 _of = null;

  138.     protected FixedPointFinder (
  139.         final double dblOFGoal,
  140.         final org.drip.function.definition.R1ToR1 of,
  141.         final org.drip.function.r1tor1solver.ExecutionControl ec,
  142.         final boolean bWhine)
  143.         throws java.lang.Exception
  144.     {
  145.         if (!org.drip.numerical.common.NumberUtil.IsValid (_dblOFGoal = dblOFGoal) || null == (_of = of))
  146.             throw new java.lang.Exception ("FixedPointFinder constructor: Invalid inputs");

  147.         _ec = new org.drip.function.r1tor1solver.ExecutionControl (of, null);

  148.         _bWhine = bWhine;
  149.     }

  150.     protected abstract boolean iterateVariate (
  151.         final org.drip.function.r1tor1solver.IteratedVariate vi,
  152.         final org.drip.function.r1tor1solver.FixedPointFinderOutput rfop);

  153.     protected abstract org.drip.function.r1tor1solver.ExecutionInitializationOutput initializeVariateZone (
  154.         final org.drip.function.r1tor1solver.InitializationHeuristics ih);

  155.     /**
  156.      * Invoke the solution 1D root finding sequence
  157.      *
  158.      * @param ih Optional Initialization Heuristics
  159.      *
  160.      * @return Root finder Solution Object for the variate
  161.      */

  162.     public org.drip.function.r1tor1solver.FixedPointFinderOutput findRoot (
  163.         final org.drip.function.r1tor1solver.InitializationHeuristics ih)
  164.     {
  165.         org.drip.function.r1tor1solver.FixedPointFinderOutput rfop = null;

  166.         org.drip.function.r1tor1solver.ExecutionInitializationOutput eiop = initializeVariateZone (ih);

  167.         if (null == eiop || !eiop.isDone()) return null;

  168.         try {
  169.             rfop = new org.drip.function.r1tor1solver.FixedPointFinderOutput (eiop);

  170.             if (!rfop.incrOFCalcs()) return rfop;

  171.             double dblOF = _of.evaluate (eiop.getStartingVariate());

  172.             double dblAbsoluteTolerance = _ec.calcAbsoluteOFTolerance (dblOF);

  173.             double dblAbsoluteConvergence = _ec.calcAbsoluteVariateConvergence (eiop.getStartingVariate());

  174.             org.drip.function.r1tor1solver.IteratedVariate iv = new
  175.                 org.drip.function.r1tor1solver.IteratedVariate (eiop, dblOF);

  176.             int iNumIterationsPending = _ec.getNumIterations();

  177.             while (!_ec.hasOFReachedGoal (dblAbsoluteTolerance, iv.getOF(), _dblOFGoal)) {
  178.                 double dblPrevVariate = iv.getVariate();

  179.                 if (!rfop.incrIterations() || 0 == --iNumIterationsPending || !iterateVariate (iv, rfop))
  180.                     return rfop;

  181.                 if (_ec.isVariateConvergenceCheckEnabled() && (java.lang.Math.abs (dblPrevVariate -
  182.                     iv.getVariate()) < dblAbsoluteConvergence))
  183.                     break;
  184.             }

  185.             rfop.setRoot (iv.getVariate());
  186.         } catch (java.lang.Exception e) {
  187.             if (_bWhine) e.printStackTrace();
  188.         }

  189.         return rfop;
  190.     }

  191.     /**
  192.      * Invoke the solution 1D root finding sequence
  193.      *
  194.      * @return Root finder Solution Object for the variate
  195.      */

  196.     public org.drip.function.r1tor1solver.FixedPointFinderOutput findRoot()
  197.     {
  198.         return findRoot (null);
  199.     }
  200. }