FixedPointFinderBracketing.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>FixedPointFinderBracketing</i> customizes the FixedPointFinder for bracketing based fixed point finder
  83.  *  functionality.
  84.  * <br><br>
  85.  * FixedPointFinderBracketing applies the following customization:
  86.  * <br>
  87.  * <ul>
  88.  *  <li>
  89.  *      Initializes the fixed point finder by computing the starting brackets
  90.  *  </li>
  91.  *  <li>
  92.  *      Iterating the next search variate using one of the specified variate iterator primitives.
  93.  *  </li>
  94.  * </ul>
  95.  * <br><br>
  96.  * By default, FixedPointFinderBracketing does not do compound iterations of the variate using any schemes -
  97.  * that is done by classes that extend it.
  98.  *
  99.  *  <br><br>
  100.  *  <ul>
  101.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  102.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  103.  *      <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>
  104.  *      <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>
  105.  *  </ul>
  106.  *
  107.  * @author Lakshmi Krishnamurthy
  108.  */

  109. public class FixedPointFinderBracketing extends org.drip.function.r1tor1solver.FixedPointFinder {
  110.     protected int _iIteratorPrimitive = -1;
  111.     protected org.drip.function.r1tor1solver.IteratedBracket _ib = null;

  112.     private org.drip.function.r1tor1solver.ExecutionInitializer _ei = null;

  113.     protected final double calcNextVariate (
  114.         final double dblCurrentVariate,
  115.         final double dblContraVariate,
  116.         final double dblCurrentOF,
  117.         final double dblContraPointOF,
  118.         final int iIteratorPrimitive,
  119.         final org.drip.function.r1tor1solver.FixedPointFinderOutput rfop)
  120.         throws java.lang.Exception
  121.     {
  122.         if (org.drip.function.r1tor1solver.VariateIteratorPrimitive.BISECTION == iIteratorPrimitive)
  123.             return org.drip.function.r1tor1solver.VariateIteratorPrimitive.Bisection (dblCurrentVariate,
  124.                 dblContraVariate);

  125.         if (org.drip.function.r1tor1solver.VariateIteratorPrimitive.FALSE_POSITION == iIteratorPrimitive)
  126.             return org.drip.function.r1tor1solver.VariateIteratorPrimitive.FalsePosition (dblCurrentVariate,
  127.                 dblContraVariate, dblCurrentOF, dblContraPointOF);

  128.         double dblIntermediateVariate = org.drip.function.r1tor1solver.VariateIteratorPrimitive.Bisection
  129.             (dblCurrentVariate, dblContraVariate);

  130.         if (!rfop.incrOFCalcs())
  131.             throw new java.lang.Exception
  132.                 ("FixedPointFinderBracketing::calcNextVariate => Cannot increment rfop!");

  133.         if (org.drip.function.r1tor1solver.VariateIteratorPrimitive.QUADRATIC_INTERPOLATION == iIteratorPrimitive)
  134.             return org.drip.function.r1tor1solver.VariateIteratorPrimitive.QuadraticInterpolation (dblCurrentVariate,
  135.                 dblIntermediateVariate, dblContraVariate, dblCurrentOF, _of.evaluate
  136.                     (dblIntermediateVariate), dblContraPointOF);

  137.         if (org.drip.function.r1tor1solver.VariateIteratorPrimitive.INVERSE_QUADRATIC_INTERPOLATION ==
  138.             iIteratorPrimitive)
  139.             return org.drip.function.r1tor1solver.VariateIteratorPrimitive.InverseQuadraticInterpolation
  140.                 (dblCurrentVariate, dblIntermediateVariate, dblContraVariate, dblCurrentOF, _of.evaluate
  141.                     (dblIntermediateVariate), dblContraPointOF);

  142.         if (org.drip.function.r1tor1solver.VariateIteratorPrimitive.RIDDER == iIteratorPrimitive)
  143.             return org.drip.function.r1tor1solver.VariateIteratorPrimitive.Ridder (dblCurrentVariate,
  144.                 dblIntermediateVariate, dblContraVariate, dblCurrentOF, _of.evaluate
  145.                     (dblIntermediateVariate), dblContraPointOF);

  146.         throw new java.lang.Exception
  147.             ("FixedPointFinderBracketing.calcNextVariate => Unknown Iterator Primitive");
  148.     }

  149.     protected double iterateCompoundVariate (
  150.         final double dblCurrentVariate,
  151.         final double dblContraVariate,
  152.         final double dblCurrentOF,
  153.         final double dblContraPointOF,
  154.         final org.drip.function.r1tor1solver.FixedPointFinderOutput rfop)
  155.         throws java.lang.Exception
  156.     {
  157.         return calcNextVariate (dblCurrentVariate, dblContraVariate, dblCurrentOF, dblContraPointOF,
  158.             _iIteratorPrimitive, rfop);
  159.     }

  160.     @Override protected boolean iterateVariate (
  161.         final org.drip.function.r1tor1solver.IteratedVariate iv,
  162.         final org.drip.function.r1tor1solver.FixedPointFinderOutput rfop)
  163.     {
  164.         if (null == iv || null == rfop) return false;

  165.         double dblContraRoot = java.lang.Double.NaN;
  166.         double dblContraRootOF = java.lang.Double.NaN;

  167.         double dblOF = iv.getOF();

  168.         double dblOFLeft = _ib.getOFLeft();

  169.         double dblOFRight = _ib.getOFRight();

  170.         double dblVariate = iv.getVariate();

  171.         double dblVariateLeft = _ib.getVariateLeft();

  172.         double dblVariateRight = _ib.getVariateRight();

  173.         if (((dblOFLeft - _dblOFGoal) * (dblOF - _dblOFGoal)) > 0.) {
  174.             if (!_ib.setOFLeft (dblOF) || !_ib.setVariateLeft (dblVariate)) return false;

  175.             dblContraRootOF = dblOFRight;
  176.             dblContraRoot = dblVariateRight;
  177.         } else if (((dblOFRight - _dblOFGoal) * (dblOF - _dblOFGoal)) > 0.) {
  178.             if (!_ib.setOFRight (dblOF) || !_ib.setVariateRight (dblVariate)) return false;

  179.             dblContraRootOF = dblOFLeft;
  180.             dblContraRoot = dblVariateLeft;
  181.         }

  182.         try {
  183.             dblVariate = iterateCompoundVariate (dblVariate, dblContraRoot, dblOF, dblContraRootOF, rfop);

  184.             return iv.setVariate (dblVariate) && iv.setOF (_of.evaluate (dblVariate)) && rfop.incrOFCalcs();
  185.         } catch (java.lang.Exception e) {
  186.             if (_bWhine) e.printStackTrace();
  187.         }

  188.         return false;
  189.     }

  190.     @Override protected org.drip.function.r1tor1solver.ExecutionInitializationOutput initializeVariateZone (
  191.         final org.drip.function.r1tor1solver.InitializationHeuristics ih)
  192.     {
  193.         org.drip.function.r1tor1solver.BracketingOutput bop = null;

  194.         if (null != ih && org.drip.function.r1tor1solver.InitializationHeuristics.SEARCH_HARD_BRACKETS ==
  195.             ih.getDeterminant())
  196.             bop = _ei.verifyHardSearchEdges (ih, _dblOFGoal);
  197.         else
  198.             bop = _ei.initializeBracket (ih, _dblOFGoal);

  199.         if (null == bop || !bop.isDone()) return null;

  200.         try {
  201.             _ib = new org.drip.function.r1tor1solver.IteratedBracket (bop);

  202.             return bop;
  203.         } catch (java.lang.Exception e) {
  204.             if (_bWhine) e.printStackTrace();
  205.         }

  206.         return null;
  207.     }

  208.     /**
  209.      * FixedPointFinderBracketing constructor
  210.      *
  211.      * @param dblOFGoal OF Goal
  212.      * @param of Objective Function
  213.      * @param ec Execution Control
  214.      * @param iIteratorPrimitive Iterator Primitive
  215.      * @param bWhine TRUE - Balk on Encountering Exception
  216.      *
  217.      * @throws java.lang.Exception Thrown if inputs are invalid
  218.      */

  219.     public FixedPointFinderBracketing (
  220.         final double dblOFGoal,
  221.         final org.drip.function.definition.R1ToR1 of,
  222.         final org.drip.function.r1tor1solver.ExecutionControl ec,
  223.         final int iIteratorPrimitive,
  224.         final boolean bWhine)
  225.         throws java.lang.Exception
  226.     {
  227.         super (dblOFGoal, of, ec, bWhine);

  228.         if (org.drip.function.r1tor1solver.VariateIteratorPrimitive.BISECTION != (_iIteratorPrimitive =
  229.             iIteratorPrimitive) && org.drip.function.r1tor1solver.VariateIteratorPrimitive.FALSE_POSITION !=
  230.                 _iIteratorPrimitive &&
  231.                     org.drip.function.r1tor1solver.VariateIteratorPrimitive.QUADRATIC_INTERPOLATION !=
  232.                         _iIteratorPrimitive &&
  233.                             org.drip.function.r1tor1solver.VariateIteratorPrimitive.INVERSE_QUADRATIC_INTERPOLATION
  234.                                 != _iIteratorPrimitive &&
  235.                                     org.drip.function.r1tor1solver.VariateIteratorPrimitive.RIDDER !=
  236.                                         _iIteratorPrimitive)
  237.             throw new java.lang.Exception ("FixedPointFinderBracketing constructor: Invalid inputs!");

  238.         _ei = new org.drip.function.r1tor1solver.ExecutionInitializer (_of, null, true);
  239.     }
  240. }