BracketingRegressorSet.java

  1. package org.drip.regression.fixedpointfinder;

  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>BracketingRegressorSet</i> implements regression run for the Primitive Bracketing Fixed Point Search
  83.  * Method. It implements the following 4 primitive bracketing schemes: Bisection, False Position, Quadratic,
  84.  * and Inverse Quadratic.
  85.  *
  86.  * <br><br>
  87.  *  <ul>
  88.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  89.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  90.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/regression/README.md">Regression Engine Core and the Unit Regressors</a></li>
  91.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/regression/fixedpointfinder/README.md">Fixed Point Finder Regression Engine</a></li>
  92.  *  </ul>
  93.  * <br><br>
  94.  *
  95.  * @author Lakshmi Krishnamurthy
  96.  */

  97. public class BracketingRegressorSet implements org.drip.regression.core.RegressorSet {
  98.     private org.drip.function.definition.R1ToR1 _of = null;
  99.     private java.lang.String _strRegressionScenario = "org.drip.math.solver1D.FixedPointFinderPrimitive";

  100.     private java.util.List<org.drip.regression.core.UnitRegressor> _setRegressors = new
  101.         java.util.ArrayList<org.drip.regression.core.UnitRegressor>();

  102.     public BracketingRegressorSet()
  103.     {
  104.         _of = new org.drip.function.definition.R1ToR1 (null)
  105.         {
  106.             public double evaluate (
  107.                 final double dblVariate)
  108.                 throws java.lang.Exception
  109.             {
  110.                 if (java.lang.Double.isNaN (dblVariate))
  111.                     throw new java.lang.Exception
  112.                         ("FixedPointFinderRegressorOF.evalTarget => Invalid variate!");

  113.                 /* return java.lang.Math.cos (dblVariate) - dblVariate * dblVariate * dblVariate;

  114.                 return dblVariate * dblVariate * dblVariate - 3. * dblVariate * dblVariate + 2. *
  115.                     dblVariate;

  116.                 return dblVariate * dblVariate * dblVariate + 4. * dblVariate + 4.;

  117.                 return 32. * dblVariate * dblVariate * dblVariate * dblVariate * dblVariate * dblVariate
  118.                     - 48. * dblVariate * dblVariate * dblVariate * dblVariate + 18. * dblVariate *
  119.                         dblVariate - 1.; */

  120.                 return 1. + 3. * dblVariate - 2. * java.lang.Math.sin (dblVariate);
  121.             }

  122.             @Override public double integrate (
  123.                 final double dblBegin,
  124.                 final double dblEnd)
  125.                 throws java.lang.Exception
  126.             {
  127.                 return org.drip.numerical.integration.R1ToR1Integrator.Boole (this, dblBegin, dblEnd);
  128.             }
  129.         };
  130.     }

  131.     @Override public boolean setupRegressors()
  132.     {
  133.         try {
  134.             _setRegressors.add (new org.drip.regression.core.UnitRegressionExecutor
  135.                 ("BisectionFixedPointFinder", _strRegressionScenario)
  136.             {
  137.                 org.drip.function.r1tor1solver.FixedPointFinderOutput fpfopBisect = null;
  138.                 org.drip.function.r1tor1solver.FixedPointFinderBracketing fpfbBisect = null;

  139.                 @Override public boolean preRegression()
  140.                 {
  141.                     try {
  142.                         fpfbBisect = new org.drip.function.r1tor1solver.FixedPointFinderBracketing (0., _of, null,
  143.                             org.drip.function.r1tor1solver.VariateIteratorPrimitive.BISECTION, true);

  144.                         return true;
  145.                     } catch (java.lang.Exception e) {
  146.                         e.printStackTrace();
  147.                     }

  148.                     return false;
  149.                 }

  150.                 @Override public boolean execRegression()
  151.                 {
  152.                     if (null == (fpfopBisect = fpfbBisect.findRoot())) return false;

  153.                     return true;
  154.                 }

  155.                 @Override public boolean postRegression (
  156.                     final org.drip.regression.core.RegressionRunDetail rnvd)
  157.                 {
  158.                     rnvd.set ("FixedPoint", "" + fpfopBisect.getRoot());

  159.                     return true;
  160.                 }
  161.             });

  162.             _setRegressors.add (new org.drip.regression.core.UnitRegressionExecutor
  163.                 ("FalsePositionFixedPointFinder", _strRegressionScenario)
  164.             {
  165.                 org.drip.function.r1tor1solver.FixedPointFinderOutput fpfopFalsePosition = null;
  166.                 org.drip.function.r1tor1solver.FixedPointFinderBracketing fpfbFalsePosition = null;

  167.                 @Override public boolean preRegression()
  168.                 {
  169.                     try {
  170.                         fpfbFalsePosition = new org.drip.function.r1tor1solver.FixedPointFinderBracketing (0., _of,
  171.                             null, org.drip.function.r1tor1solver.VariateIteratorPrimitive.FALSE_POSITION, true);

  172.                         return true;
  173.                     } catch (java.lang.Exception e) {
  174.                         e.printStackTrace();
  175.                     }

  176.                     return false;
  177.                 }

  178.                 @Override public boolean execRegression()
  179.                 {
  180.                     if (null == (fpfopFalsePosition = fpfbFalsePosition.findRoot())) return false;

  181.                     return true;
  182.                 }

  183.                 @Override public boolean postRegression (
  184.                     final org.drip.regression.core.RegressionRunDetail rnvd)
  185.                 {
  186.                     rnvd.set ("FixedPoint", "" + fpfopFalsePosition.getRoot());

  187.                     return true;
  188.                 }
  189.             });

  190.             _setRegressors.add (new org.drip.regression.core.UnitRegressionExecutor
  191.                 ("QuadraticFixedPointFinder", _strRegressionScenario) {
  192.                 org.drip.function.r1tor1solver.FixedPointFinderOutput fpfopQuadratic = null;
  193.                 org.drip.function.r1tor1solver.FixedPointFinderBracketing fpfbQuadratic = null;

  194.                 @Override public boolean preRegression()
  195.                 {
  196.                     try {
  197.                         fpfbQuadratic = new org.drip.function.r1tor1solver.FixedPointFinderBracketing (0., _of,
  198.                             null, org.drip.function.r1tor1solver.VariateIteratorPrimitive.QUADRATIC_INTERPOLATION,
  199.                                 true);

  200.                         return true;
  201.                     } catch (java.lang.Exception e) {
  202.                         e.printStackTrace();
  203.                     }

  204.                     return false;
  205.                 }

  206.                 @Override public boolean execRegression()
  207.                 {
  208.                     if (null == (fpfopQuadratic = fpfbQuadratic.findRoot())) return false;

  209.                     return true;
  210.                 }

  211.                 @Override public boolean postRegression (
  212.                     final org.drip.regression.core.RegressionRunDetail rnvd)
  213.                 {
  214.                     rnvd.set ("FixedPoint", "" + fpfopQuadratic.getRoot());

  215.                     return true;
  216.                 }
  217.             });

  218.             _setRegressors.add (new org.drip.regression.core.UnitRegressionExecutor
  219.                 ("InverseQuadraticFixedPointFinder", _strRegressionScenario)
  220.             {
  221.                 org.drip.function.r1tor1solver.FixedPointFinderOutput fpfopInverseQuadratic = null;
  222.                 org.drip.function.r1tor1solver.FixedPointFinderBracketing fpfbInverseQuadratic = null;

  223.                 @Override public boolean preRegression()
  224.                 {
  225.                     try {
  226.                         fpfbInverseQuadratic = new org.drip.function.r1tor1solver.FixedPointFinderBracketing (0.,
  227.                             _of, null,
  228.                                 org.drip.function.r1tor1solver.VariateIteratorPrimitive.INVERSE_QUADRATIC_INTERPOLATION,
  229.                             true);

  230.                         return true;
  231.                     } catch (java.lang.Exception e) {
  232.                         e.printStackTrace();
  233.                     }

  234.                     return false;
  235.                 }

  236.                 @Override public boolean execRegression()
  237.                 {
  238.                     if (null == (fpfopInverseQuadratic = fpfbInverseQuadratic.findRoot())) return false;

  239.                     return true;
  240.                 }

  241.                 @Override public boolean postRegression (
  242.                     final org.drip.regression.core.RegressionRunDetail rnvd)
  243.                 {
  244.                     rnvd.set ("FixedPoint", "" + fpfopInverseQuadratic.getRoot());

  245.                     return true;
  246.                 }
  247.             });

  248.             _setRegressors.add (new org.drip.regression.core.UnitRegressionExecutor
  249.                 ("RidderFixedPointFinder", _strRegressionScenario)
  250.             {
  251.                 org.drip.function.r1tor1solver.FixedPointFinderOutput fpfopRidder = null;
  252.                 org.drip.function.r1tor1solver.FixedPointFinderBracketing fpfbRidder = null;

  253.                 @Override public boolean preRegression()
  254.                 {
  255.                     try {
  256.                         fpfbRidder = new org.drip.function.r1tor1solver.FixedPointFinderBracketing (0., _of, null,
  257.                             org.drip.function.r1tor1solver.VariateIteratorPrimitive.RIDDER, true);

  258.                         return true;
  259.                     } catch (java.lang.Exception e) {
  260.                         e.printStackTrace();
  261.                     }

  262.                     return false;
  263.                 }

  264.                 @Override public boolean execRegression()
  265.                 {
  266.                     if (null == (fpfopRidder = fpfbRidder.findRoot())) return false;

  267.                     return true;
  268.                 }

  269.                 @Override public boolean postRegression (
  270.                     final org.drip.regression.core.RegressionRunDetail rnvd) {
  271.                     rnvd.set ("FixedPoint", "" + fpfopRidder.getRoot());

  272.                     return true;
  273.                 }
  274.             });
  275.         } catch (java.lang.Exception e) {
  276.             e.printStackTrace();
  277.         }

  278.         return true;
  279.     }

  280.     @Override public java.util.List<org.drip.regression.core.UnitRegressor> getRegressorSet()
  281.     {
  282.         return _setRegressors;
  283.     }

  284.     @Override public java.lang.String getSetName()
  285.     {
  286.         return _strRegressionScenario;
  287.     }
  288. }