FixedPointFinderBrent.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>FixedPointFinderBrent</i> customizes FixedPointFinderBracketing by applying the Brent's scheme of
  83.  * compound variate selector.
  84.  * <br><br>
  85.  * Brent's scheme, as implemented here, is described in http://www.credit-trader.org. This implementation
  86.  *  retains absolute shifts that have happened to the variate for the past 2 iterations as the discriminant
  87.  *  that determines the next variate to be generated.
  88.  * <br><br>
  89.  * FixedPointFinderBrent uses the following parameters specified in VariateIterationSelectorParams:
  90.  * <br>
  91.  * <ul>
  92.  *  <li>
  93.  *      The Variate Primitive that is regarded as the "fast" method
  94.  *  </li>
  95.  *  <li>
  96.  *      The Variate Primitive that is regarded as the "robust" method
  97.  *  </li>
  98.  *  <li>
  99.  *      The relative variate shift that determines when the "robust" method is to be invoked over the "fast"
  100.  *  </li>
  101.  *  <li>
  102.  *      The lower bound on the variate shift between iterations that serves as the fall-back to the "robust"
  103.  *  </li>
  104.  * </ul>
  105.  *
  106.  *  <br><br>
  107.  *  <ul>
  108.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  109.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  110.  *      <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>
  111.  *      <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>
  112.  *  </ul>
  113.  *
  114.  * @author Lakshmi Krishnamurthy
  115.  */

  116. public class FixedPointFinderBrent extends org.drip.function.r1tor1solver.FixedPointFinderBracketing {
  117.     private double _dblVariateIterativeShift = java.lang.Double.NaN;
  118.     private double _dblPreviousVariateIterativeShift = java.lang.Double.NaN;
  119.     private org.drip.function.r1tor1solver.VariateIterationSelectorParams _visp = null;

  120.     @Override protected double iterateCompoundVariate (
  121.         final double dblCurrentVariate,
  122.         final double dblContraVariate,
  123.         final double dblCurrentOF,
  124.         final double dblContraPointOF,
  125.         final org.drip.function.r1tor1solver.FixedPointFinderOutput rfop)
  126.         throws java.lang.Exception
  127.     {
  128.         double dblNextVariate = calcNextVariate (dblCurrentVariate, dblContraVariate, dblCurrentOF,
  129.             dblContraPointOF, _visp.getFastVariateIteratorPrimitive(), rfop);

  130.         double dblVariateEstimateShift = java.lang.Math.abs (dblNextVariate - dblCurrentVariate);

  131.         if (org.drip.numerical.common.NumberUtil.IsValid (_dblVariateIterativeShift) ||
  132.             _visp.getRobustVariateIteratorPrimitive() == _iIteratorPrimitive) {
  133.             if (dblVariateEstimateShift < _visp.getRelativeVariateShift() * _dblVariateIterativeShift &&
  134.                 _dblVariateIterativeShift > 0.5 * _visp.getVariateShiftLowerBound()) {
  135.                 _iIteratorPrimitive = _visp.getFastVariateIteratorPrimitive();

  136.                 _dblPreviousVariateIterativeShift = _dblVariateIterativeShift;
  137.                 _dblVariateIterativeShift = dblVariateEstimateShift;
  138.                 return dblNextVariate;
  139.             }

  140.             _iIteratorPrimitive = _visp.getRobustVariateIteratorPrimitive();

  141.             _dblPreviousVariateIterativeShift = _dblVariateIterativeShift;
  142.             _dblVariateIterativeShift = dblVariateEstimateShift;

  143.             return calcNextVariate (dblCurrentVariate, dblContraVariate, dblCurrentOF, dblContraPointOF,
  144.                 _visp.getRobustVariateIteratorPrimitive(), rfop);
  145.         }

  146.         if (org.drip.numerical.common.NumberUtil.IsValid (_dblPreviousVariateIterativeShift) &&
  147.             (dblVariateEstimateShift < _visp.getRelativeVariateShift() * _dblPreviousVariateIterativeShift &&
  148.                 _dblPreviousVariateIterativeShift > 0.5 * _visp.getVariateShiftLowerBound())) {
  149.             _iIteratorPrimitive = _visp.getFastVariateIteratorPrimitive();

  150.             _dblPreviousVariateIterativeShift = _dblVariateIterativeShift;
  151.             _dblVariateIterativeShift = dblVariateEstimateShift;
  152.             return dblNextVariate;
  153.         }

  154.         _iIteratorPrimitive = _visp.getRobustVariateIteratorPrimitive();

  155.         _dblPreviousVariateIterativeShift = _dblVariateIterativeShift;
  156.         _dblVariateIterativeShift = dblVariateEstimateShift;

  157.         return calcNextVariate (dblCurrentVariate, dblContraVariate, dblCurrentOF, dblContraPointOF,
  158.             _visp.getRobustVariateIteratorPrimitive(), rfop);
  159.     }

  160.     /**
  161.      * FixedPointFinderBrent constructor
  162.      *
  163.      * @param dblOFGoal OF Goal
  164.      * @param of Objective Function
  165.      * @param bWhine TRUE - Balk on Encountering Exception
  166.      *
  167.      * @throws java.lang.Exception Propogated from below
  168.      */

  169.     public FixedPointFinderBrent (
  170.         final double dblOFGoal,
  171.         final org.drip.function.definition.R1ToR1 of,
  172.         final boolean bWhine)
  173.         throws java.lang.Exception
  174.     {
  175.         super (dblOFGoal, of, null, org.drip.function.r1tor1solver.VariateIteratorPrimitive.BISECTION, bWhine);

  176.         _visp = new org.drip.function.r1tor1solver.VariateIterationSelectorParams();
  177.     }
  178. }