FixedPointSearch.java

  1. package org.drip.sample.numerical;

  2. import org.drip.function.definition.R1ToR1;
  3. import org.drip.function.r1tor1solver.*;
  4. import org.drip.numerical.common.*;
  5. import org.drip.numerical.differentiation.*;
  6. import org.drip.numerical.integration.R1ToR1Integrator;

  7. /*
  8.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  9.  */

  10. /*!
  11.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  16.  * Copyright (C) 2013 Lakshmi Krishnamurthy
  17.  * Copyright (C) 2012 Lakshmi Krishnamurthy
  18.  *
  19.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  20.  *      libraries targeting analysts and developers
  21.  *      https://lakshmidrip.github.io/DRIP/
  22.  *  
  23.  *  DRIP is composed of four main libraries:
  24.  *  
  25.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  26.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  27.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  28.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  29.  *
  30.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  31.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  32.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  33.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  34.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  35.  *
  36.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  37.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  38.  *
  39.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  40.  *
  41.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  42.  *
  43.  *  Licensed under the Apache License, Version 2.0 (the "License");
  44.  *      you may not use this file except in compliance with the License.
  45.  *  
  46.  *  You may obtain a copy of the License at
  47.  *      http://www.apache.org/licenses/LICENSE-2.0
  48.  *  
  49.  *  Unless required by applicable law or agreed to in writing, software
  50.  *      distributed under the License is distributed on an "AS IS" BASIS,
  51.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  52.  *  
  53.  *  See the License for the specific language governing permissions and
  54.  *      limitations under the License.
  55.  */

  56. /**
  57.  * FixedPointSearch contains a sample illustration of usage of the Root Finder Library. It demonstrates the
  58.  *  fixed point extraction using the following techniques:
  59.  *  - Newton-Raphson method
  60.  *  - Bisection Method
  61.  *  - False Position
  62.  *  - Quadratic Interpolation
  63.  *  - Inverse Quadratic Interpolation
  64.  *  - Ridder's method
  65.  *  - Brent's method
  66.  *  - Zheng's method
  67.  *
  68.  * @author Lakshmi Krishnamurthy
  69.  */

  70. public class FixedPointSearch {

  71.     /*
  72.      * Sample illustrating the Invocation of the Newton-Raphson Open Method
  73.      *
  74.      *  WARNING: Insufficient Error Checking, so use caution
  75.      */

  76.     private static final void InvokeNewton (
  77.         final R1ToR1 func)
  78.     {
  79.         try {
  80.             FixedPointFinderOutput fpop = new FixedPointFinderNewton (
  81.                 0.,
  82.                 func,
  83.                 true
  84.             ).findRoot();

  85.             System.out.println ("--------\nNEWTON START\n-------");

  86.             if (null != fpop && fpop.containsRoot()) {
  87.                 System.out.println ("Root: " + FormatUtil.FormatDouble (fpop.getRoot(), 1, 4, 1.));

  88.                 System.out.println (fpop.displayString());
  89.             } else
  90.                 System.out.println ("Root searched failed!");

  91.             System.out.println ("--------\nNEWTON FINISH\n-------\n");
  92.         } catch (Exception e) {
  93.             e.printStackTrace();
  94.         }
  95.     }

  96.     /*
  97.      * Sample illustrating the Invocation of the Bisection Bracketing Method
  98.      *
  99.      *  WARNING: Insufficient Error Checking, so use caution
  100.      */

  101.     private static final void InvokeBisection (
  102.         final R1ToR1 func)
  103.     {
  104.         try {
  105.             FixedPointFinderOutput fpop = new FixedPointFinderBracketing (
  106.                 0.,
  107.                 func,
  108.                 null,
  109.                 VariateIteratorPrimitive.BISECTION,
  110.                 true
  111.             ).findRoot();

  112.             System.out.println ("--------\nBISECTION START\n-------");

  113.             if (null != fpop && fpop.containsRoot()) {
  114.                 System.out.println ("Root: " + FormatUtil.FormatDouble (fpop.getRoot(), 1, 4, 1.));

  115.                 System.out.println (fpop.displayString());
  116.             } else
  117.                 System.out.println ("Root searched failed!");

  118.             System.out.println ("--------\nBISECTION FINISH\n-------\n");
  119.         } catch (Exception e) {
  120.             e.printStackTrace();
  121.         }
  122.     }

  123.     /*
  124.      * Sample illustrating the Invocation of the False Position Method
  125.      *
  126.      *  WARNING: Insufficient Error Checking, so use caution
  127.      */

  128.     private static final void InvokeFalsePosition (
  129.         final R1ToR1 func)
  130.     {
  131.         try {
  132.             FixedPointFinderOutput fpop = new FixedPointFinderBracketing (
  133.                 0.,
  134.                 func,
  135.                 null,
  136.                 VariateIteratorPrimitive.FALSE_POSITION,
  137.                 true
  138.             ).findRoot();

  139.             System.out.println ("--------\nFALSE POSITION START\n-------");

  140.             if (null != fpop && fpop.containsRoot()) {
  141.                 System.out.println ("Root: " + FormatUtil.FormatDouble (fpop.getRoot(), 1, 4, 1.));

  142.                 System.out.println (fpop.displayString());
  143.             } else
  144.                 System.out.println ("Root searched failed!");

  145.             System.out.println ("--------\nFALSE POSITION FINISH\n-------\n");
  146.         } catch (Exception e) {
  147.             e.printStackTrace();
  148.         }
  149.     }

  150.     /*
  151.      * Sample illustrating the Invocation of the Quadratic Interpolation Bracketing Method
  152.      *
  153.      *  WARNING: Insufficient Error Checking, so use caution
  154.      */

  155.     private static final void InvokeQuadraticInterpolation (
  156.         final R1ToR1 func)
  157.     {
  158.         try {
  159.             FixedPointFinderOutput fpop = new FixedPointFinderBracketing (
  160.                 0.,
  161.                 func,
  162.                 null,
  163.                 VariateIteratorPrimitive.QUADRATIC_INTERPOLATION,
  164.                 true
  165.             ).findRoot();

  166.             System.out.println ("--------\nQUADRATIC INTERPOLATION START\n-------");

  167.             if (null != fpop && fpop.containsRoot()) {
  168.                 System.out.println ("Root: " + FormatUtil.FormatDouble (fpop.getRoot(), 1, 4, 1.));

  169.                 System.out.println (fpop.displayString());
  170.             } else
  171.                 System.out.println ("Root searched failed!");

  172.             System.out.println ("--------\nQUADRATIC INTERPOLATION FINISH\n-------\n");
  173.         } catch (Exception e) {
  174.             e.printStackTrace();
  175.         }
  176.     }

  177.     /*
  178.      * Sample illustrating the Invocation of the Inverse Quadratic Interpolation Bracketing Method
  179.      *
  180.      *  WARNING: Insufficient Error Checking, so use caution
  181.      */

  182.     private static final void InvokeInverseQuadraticInterpolation (
  183.         final R1ToR1 func)
  184.     {
  185.         try {
  186.             FixedPointFinderOutput fpop = new FixedPointFinderBracketing (
  187.                 0.,
  188.                 func,
  189.                 null,
  190.                 VariateIteratorPrimitive.INVERSE_QUADRATIC_INTERPOLATION,
  191.                 true
  192.             ).findRoot();

  193.             System.out.println ("--------\nINVERSE QUADRATIC INTERPOLATION START\n-------");

  194.             if (null != fpop && fpop.containsRoot()) {
  195.                 System.out.println ("Root: " + FormatUtil.FormatDouble (fpop.getRoot(), 1, 4, 1.));

  196.                 System.out.println (fpop.displayString());
  197.             } else
  198.                 System.out.println ("Root searched failed!");

  199.             System.out.println ("--------\nINVERSE QUADRATIC INTERPOLATION FINISH\n-------\n");
  200.         } catch (Exception e) {
  201.             e.printStackTrace();
  202.         }
  203.     }

  204.     /*
  205.      * Sample illustrating the Invocation of the Ridder Bracketing Method
  206.      *
  207.      *  WARNING: Insufficient Error Checking, so use caution
  208.      */

  209.     private static final void InvokeRidder (
  210.         final R1ToR1 func)
  211.     {
  212.         try {
  213.             FixedPointFinderOutput fpop = new FixedPointFinderBracketing (
  214.                 0.,
  215.                 func,
  216.                 null,
  217.                 VariateIteratorPrimitive.RIDDER,
  218.                 true
  219.             ).findRoot();

  220.             System.out.println ("--------\nRIDDER START\n-------");

  221.             if (null != fpop && fpop.containsRoot()) {
  222.                 System.out.println ("Root: " + FormatUtil.FormatDouble (fpop.getRoot(), 1, 4, 1.));

  223.                 System.out.println (fpop.displayString());
  224.             } else
  225.                 System.out.println ("Root searched failed!");

  226.             System.out.println ("--------\nRIDDER FINISH\n-------\n");
  227.         } catch (Exception e) {
  228.             e.printStackTrace();
  229.         }
  230.     }

  231.     /*
  232.      * Sample illustrating the Invocation of the Brent's Bracketing Method
  233.      *
  234.      *  WARNING: Insufficient Error Checking, so use caution
  235.      */

  236.     private static final void InvokeBrent (
  237.         final R1ToR1 func)
  238.     {
  239.         try {
  240.             FixedPointFinderOutput fpop = new FixedPointFinderBrent (
  241.                 0.,
  242.                 func,
  243.                 true
  244.             ).findRoot();

  245.             System.out.println ("--------\nBRENT START\n-------");

  246.             if (null != fpop && fpop.containsRoot()) {
  247.                 System.out.println ("Root: " + FormatUtil.FormatDouble (fpop.getRoot(), 1, 4, 1.));

  248.                 System.out.println (fpop.displayString());
  249.             } else
  250.                 System.out.println ("Root searched failed!");

  251.             System.out.println ("--------\nBRENT FINISH\n-------\n");
  252.         } catch (Exception e) {
  253.             e.printStackTrace();
  254.         }
  255.     }

  256.     /*
  257.      * Sample illustrating the Invocation of the Zheng's Bracketing Method
  258.      *
  259.      *  WARNING: Insufficient Error Checking, so use caution
  260.      */

  261.     private static final void InvokeZheng (
  262.         final R1ToR1 func)
  263.     {
  264.         try {
  265.             FixedPointFinderOutput fpop = new FixedPointFinderZheng (
  266.                 0.,
  267.                 func,
  268.                 true
  269.             ).findRoot();

  270.             System.out.println ("--------\nZHENG START\n-------");

  271.             if (null != fpop && fpop.containsRoot()) {
  272.                 System.out.println ("Root: " + FormatUtil.FormatDouble (fpop.getRoot(), 1, 4, 1.));

  273.                 System.out.println (fpop.displayString());
  274.             } else
  275.                 System.out.println ("Root searched failed!");

  276.             System.out.println ("--------\nZHENG FINISH\n-------\n");
  277.         } catch (Exception e) {
  278.             e.printStackTrace();
  279.         }
  280.     }

  281.     public static final void main (
  282.         final String[] astrArgs)
  283.     {
  284.         /*
  285.          * Define and implement the objective function
  286.          */

  287.         R1ToR1 func = new R1ToR1 (null) {
  288.             @Override public double evaluate (
  289.                 final double dblVariate)
  290.                 throws Exception
  291.             {
  292.                 return Math.cos (dblVariate) - dblVariate * dblVariate * dblVariate;

  293.                 /* return dblVariate * dblVariate * dblVariate - 3. * dblVariate * dblVariate + 2. *
  294.                     dblVariate;

  295.                 return dblVariate * dblVariate * dblVariate + 4. * dblVariate + 4.;

  296.                 return 32. * dblVariate * dblVariate * dblVariate * dblVariate * dblVariate * dblVariate
  297.                     - 48. * dblVariate * dblVariate * dblVariate * dblVariate + 18. * dblVariate *
  298.                         dblVariate - 1.;

  299.                 return 1. + 3. * dblVariate - 2. * java.lang.Math.sin (dblVariate); */
  300.             }

  301.             @Override public Differential differential (
  302.                 final double dblVariate,
  303.                 final double dblOFBase,
  304.                 final int iOrder)
  305.             {
  306.                 if (0 >= iOrder || 2 < iOrder) return null;

  307.                 double dblVariateInfinitesimal = Double.NaN;

  308.                 try {
  309.                     dblVariateInfinitesimal = _dc.getVariateInfinitesimal (dblVariate);
  310.                 } catch (Exception e) {
  311.                     e.printStackTrace();

  312.                     return null;
  313.                 }

  314.                 if (1 != iOrder) {
  315.                     try {
  316.                         return new Differential (dblVariateInfinitesimal, (-1. * Math.cos (dblVariate) - 6. * dblVariate)
  317.                             * dblVariateInfinitesimal);

  318.                         /* return new Differential (dblVariateInfinitesimal, (6. * dblVariate - 6.) * dblVariateInfinitesimal);

  319.                         return new Differential (dblVariateInfinitesimal, (6. * dblVariate) * dblVariateInfinitesimal);

  320.                         return new Differential (dblVariateInfinitesimal, (960. * dblVariate * dblVariate * dblVariate *
  321.                             dblVariate - 576. * dblVariate * dblVariate + 36.) * dblVariateInfinitesimal);

  322.                         return new Differential (dblVariateInfinitesimal, (2. * Math.sin (dblVariate)) * dblVariateInfinitesimal); */
  323.                     } catch (Exception e) {
  324.                         e.printStackTrace();
  325.                     }

  326.                     return null;
  327.                 }

  328.                 try {
  329.                     return new Differential (dblVariateInfinitesimal, (-1. * Math.sin (dblVariate) - 3. * dblVariate * dblVariate) *
  330.                         dblVariateInfinitesimal);

  331.                     /* return new Differential (dblVariateInfinitesimal, (3. * dblVariate * dblVariate - 6. * dblVariate + 2.) *
  332.                         dblVariateInfinitesimal);

  333.                     return new Differential (dblVariateInfinitesimal, (3. * dblVariate * dblVariate + 4.) * dblVariateInfinitesimal);

  334.                     return new Differential (dblVariateInfinitesimal, (192. * dblVariate * dblVariate * dblVariate * dblVariate *
  335.                         dblVariate - 192. * dblVariate * dblVariate * dblVariate + 36. * dblVariate) * dblVariateInfinitesimal);

  336.                     return new Differential (dblVariateInfinitesimal, (3. - 2. * Math.cos (dblVariate)) * dblVariateInfinitesimal); */
  337.                 } catch (Exception e) {
  338.                     e.printStackTrace();
  339.                 }

  340.                 return null;
  341.             }

  342.             @Override public double integrate (
  343.                 final double dblBegin,
  344.                 final double dblEnd)
  345.                 throws Exception
  346.             {
  347.                 return R1ToR1Integrator.Boole (this, dblBegin, dblEnd);
  348.             }
  349.         };

  350.         InvokeNewton (func);

  351.         InvokeBisection (func);

  352.         InvokeFalsePosition (func);

  353.         InvokeQuadraticInterpolation (func);

  354.         InvokeInverseQuadraticInterpolation (func);

  355.         InvokeRidder (func);

  356.         InvokeBrent (func);

  357.         InvokeZheng (func);
  358.     }
  359. }