InitializationHeuristics.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>InitializationHeuristics</i> implements several heuristics used to kick off the fixed point
  83.  * bracketing/search process.
  84.  * <br><br>
  85.  * The following custom heuristics are implemented as part of the heuristics based kick-off:
  86.  * <br>
  87.  * <ul>
  88.  *  <li>
  89.  *      Custom Bracketing Control Parameters: Any of the standard bracketing control parameters can be
  90.  *          customized to kick-off the bracketing search.
  91.  *  </li>
  92.  *  <li>
  93.  *      Soft Left/Right Bracketing Hints: The left/right starting bracket edges are used as soft bracketing
  94.  *          initialization hints.
  95.  *  </li>
  96.  *  <li>
  97.  *      Soft Mid Bracketing Hint: A mid bracketing level is specified to indicate the soft bracketing
  98.  *          kick-off.
  99.  *  </li>
  100.  *  <li>
  101.  *      Hard Bracketing Floor/Ceiling: A pair of hard floor and ceiling limits are specified as a constraint
  102.  *          to the bracketing.
  103.  *  </li>
  104.  *  <li>
  105.  *      Hard Search Boundaries: A pair of hard left and right boundaries are specified to kick-off the final
  106.  *          fixed point search.
  107.  *  </li>
  108.  * </ul>
  109.  * <br><br>
  110.  * These heuristics are further interpreted and developed inside the ExecutionInitializer and the
  111.  *  ExecutionControl implementations.
  112.  *
  113.  *  <br><br>
  114.  *  <ul>
  115.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  116.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  117.  *      <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>
  118.  *      <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>
  119.  *  </ul>
  120.  *
  121.  * @author Lakshmi Krishnamurthy
  122.  */

  123. public class InitializationHeuristics {

  124.     /**
  125.      * Start bracket initialization from the Generic Bracket Initializer
  126.      */

  127.     public static final int BRACKETING_GENERIC_BCP = 0;

  128.     /**
  129.      * Start bracket initialization from Pre-specified left/right edge hints
  130.      */

  131.     public static final int BRACKETING_EDGE_HINTS = 1;

  132.     /**
  133.      * Start bracket initialization from Pre-specified Starting Mid Bracketing Variate
  134.      */

  135.     public static final int BRACKETING_MID_HINT = 2;

  136.     /**
  137.      * Restrict the bracket initialization to within the specified Floor and Ceiling
  138.      */

  139.     public static final int BRACKETING_FLOOR_CEILING = 4;

  140.     /**
  141.      * Start search from Pre-specified Hard Search Brackets
  142.      */

  143.     public static final int SEARCH_HARD_BRACKETS = 8;

  144.     /**
  145.      * Start search from Custom Bracketing Control Parameters
  146.      */

  147.     public static final int BRACKETING_CUSTOM_BCP = 16;

  148.     private int _iDeterminant = BRACKETING_GENERIC_BCP;
  149.     private double _dblBracketFloor = java.lang.Double.NaN;
  150.     private double _dblBracketCeiling = java.lang.Double.NaN;
  151.     private double _dblSearchStartLeft = java.lang.Double.NaN;
  152.     private double _dblSearchStartRight = java.lang.Double.NaN;
  153.     private double _dblStartingBracketMid = java.lang.Double.NaN;
  154.     private double _dblStartingBracketLeft = java.lang.Double.NaN;
  155.     private double _dblStartingBracketRight = java.lang.Double.NaN;
  156.     private org.drip.function.r1tor1solver.BracketingControlParams _bcpCustom = null;

  157.     /**
  158.      * Construct an Initialization Heuristics Instance from the hard search edges
  159.      *
  160.      * @param dblSearchStartLeft Search Start Left Edge
  161.      * @param dblSearchStartRight Search Start Right Edge
  162.      *
  163.      * @return InitializationHeuristics instance
  164.      */

  165.     public static final InitializationHeuristics FromHardSearchEdges (
  166.         final double dblSearchStartLeft,
  167.         final double dblSearchStartRight)
  168.     {
  169.         try {
  170.             return new InitializationHeuristics (SEARCH_HARD_BRACKETS, dblSearchStartLeft,
  171.                 dblSearchStartRight, java.lang.Double.NaN, java.lang.Double.NaN, java.lang.Double.NaN,
  172.                     java.lang.Double.NaN, java.lang.Double.NaN, null);
  173.         } catch (java.lang.Exception e) {
  174.             e.printStackTrace();
  175.         }

  176.         return null;
  177.     }

  178.     /**
  179.      * Construct an Initialization Heuristics Instance from the bracketing edge soft hints
  180.      *
  181.      * @param dblStartingBracketLeft Starting Soft Left Bracketing Edge Hint
  182.      * @param dblStartingBracketRight Starting Soft Right Bracketing Edge Hint
  183.      *
  184.      * @return InitializationHeuristics instance
  185.      */

  186.     public static final InitializationHeuristics FromBracketingEdgeHints (
  187.         final double dblStartingBracketLeft,
  188.         final double dblStartingBracketRight)
  189.     {
  190.         try {
  191.             return new InitializationHeuristics (BRACKETING_EDGE_HINTS, java.lang.Double.NaN,
  192.                 java.lang.Double.NaN, dblStartingBracketLeft, dblStartingBracketRight, java.lang.Double.NaN,
  193.                     java.lang.Double.NaN, java.lang.Double.NaN, null);
  194.         } catch (java.lang.Exception e) {
  195.             e.printStackTrace();
  196.         }

  197.         return null;
  198.     }

  199.     /**
  200.      * Construct an Initialization Heuristics Instance from the bracketing mid hint
  201.      *
  202.      * @param dblStartingBracketMid Starting Soft Right Bracketing Mid Hint
  203.      *
  204.      * @return InitializationHeuristics instance
  205.      */

  206.     public static final InitializationHeuristics FromBracketingMidHint (
  207.         final double dblStartingBracketMid)
  208.     {
  209.         try {
  210.             return new InitializationHeuristics (BRACKETING_MID_HINT, java.lang.Double.NaN,
  211.                 java.lang.Double.NaN, java.lang.Double.NaN, java.lang.Double.NaN, dblStartingBracketMid,
  212.                     java.lang.Double.NaN, java.lang.Double.NaN, null);
  213.         } catch (java.lang.Exception e) {
  214.             e.printStackTrace();
  215.         }

  216.         return null;
  217.     }

  218.     /**
  219.      * Construct an Initialization Heuristics Instance from the bracketing hard floor/ceiling
  220.      *
  221.      * @param dblBracketFloor Starting Hard Left Bracketing Floor
  222.      * @param dblBracketCeiling Starting Hard Right Bracketing Ceiling
  223.      *
  224.      * @return InitializationHeuristics instance
  225.      */

  226.     public static final InitializationHeuristics FromBracketingFloorCeiling (
  227.         final double dblBracketFloor,
  228.         final double dblBracketCeiling)
  229.     {
  230.         try {
  231.             return new InitializationHeuristics (BRACKETING_FLOOR_CEILING, java.lang.Double.NaN,
  232.                 java.lang.Double.NaN, java.lang.Double.NaN, java.lang.Double.NaN, java.lang.Double.NaN,
  233.                     dblBracketFloor, dblBracketCeiling, null);
  234.         } catch (java.lang.Exception e) {
  235.             e.printStackTrace();
  236.         }

  237.         return null;
  238.     }

  239.     /**
  240.      * Construct an Initialization Heuristics Instance from Custom Bracketing Control Parameters
  241.      *
  242.      * @param bcpCustom Custom Bracketing Control Parameters
  243.      *
  244.      * @return InitializationHeuristics instance
  245.      */

  246.     public static final InitializationHeuristics FromBracketingCustomBCP (
  247.         final org.drip.function.r1tor1solver.BracketingControlParams bcpCustom)
  248.     {
  249.         try {
  250.             return new InitializationHeuristics (BRACKETING_CUSTOM_BCP, java.lang.Double.NaN,
  251.                 java.lang.Double.NaN, java.lang.Double.NaN, java.lang.Double.NaN, java.lang.Double.NaN,
  252.                     java.lang.Double.NaN, java.lang.Double.NaN, bcpCustom);
  253.         } catch (java.lang.Exception e) {
  254.             e.printStackTrace();
  255.         }

  256.         return null;
  257.     }

  258.     /**
  259.      * Construct an Initialization Heuristics Instance from the set of Heuristics Parameters
  260.      *
  261.      * @param iDeterminant Initialization Heuristics Instance Type
  262.      * @param dblSearchStartLeft Hard Search Start Left Edge
  263.      * @param dblSearchStartRight Hard Search Start Right Edge
  264.      * @param dblStartingBracketLeft Starting Soft Left Bracketing Edge Hint
  265.      * @param dblStartingBracketRight Starting Soft Right Bracketing Edge Hint
  266.      * @param dblStartingBracketMid Starting Soft Right Bracketing Mid Hint
  267.      * @param dblBracketFloor Starting Hard Left Bracketing Floor
  268.      * @param dblBracketCeiling Starting Hard Right Bracketing Ceiling
  269.      * @param bcpCustom Custom Bracketing Control Parameters
  270.      *
  271.      * @throws java.lang.Exception Thrown if the Input Determinant/parameters are unknown/invalid
  272.      */

  273.     public InitializationHeuristics (
  274.         final int iDeterminant,
  275.         final double dblSearchStartLeft,
  276.         final double dblSearchStartRight,
  277.         final double dblStartingBracketLeft,
  278.         final double dblStartingBracketRight,
  279.         final double dblStartingBracketMid,
  280.         final double dblBracketFloor,
  281.         final double dblBracketCeiling,
  282.         final org.drip.function.r1tor1solver.BracketingControlParams bcpCustom)
  283.         throws java.lang.Exception
  284.     {
  285.         if (BRACKETING_EDGE_HINTS == (_iDeterminant = iDeterminant)) {
  286.             if (!org.drip.numerical.common.NumberUtil.IsValid (_dblStartingBracketLeft = dblStartingBracketLeft)
  287.                 || !org.drip.numerical.common.NumberUtil.IsValid (_dblStartingBracketRight =
  288.                     dblStartingBracketRight))
  289.                 throw new java.lang.Exception
  290.                     ("InitializationHeuristics constructor: Invalid BRACKETING_EDGE_HINTS params!");
  291.         } else if (BRACKETING_MID_HINT == _iDeterminant) {
  292.             if (!org.drip.numerical.common.NumberUtil.IsValid (_dblStartingBracketMid = dblStartingBracketMid))
  293.                 throw new java.lang.Exception
  294.                     ("InitializationHeuristics constructor: Invalid BRACKETING_MID_HINT params!");
  295.         } else if (BRACKETING_FLOOR_CEILING == _iDeterminant) {
  296.             if (!org.drip.numerical.common.NumberUtil.IsValid (_dblBracketFloor = dblBracketFloor) ||
  297.                 !org.drip.numerical.common.NumberUtil.IsValid (_dblBracketCeiling = dblBracketCeiling))
  298.                 throw new java.lang.Exception
  299.                     ("InitializationHeuristics constructor: Invalid BRACKETING_FLOOR_CEILING params!");
  300.         } else if (SEARCH_HARD_BRACKETS == _iDeterminant) {
  301.             if (!org.drip.numerical.common.NumberUtil.IsValid (_dblSearchStartLeft = dblSearchStartLeft) ||
  302.                 !org.drip.numerical.common.NumberUtil.IsValid (_dblSearchStartRight = dblSearchStartRight))
  303.                 throw new java.lang.Exception
  304.                     ("InitializationHeuristics constructor: Invalid SEARCH_HARD_BRACKETS params!");
  305.         } else if (BRACKETING_CUSTOM_BCP == _iDeterminant) {
  306.             if (null == (_bcpCustom = bcpCustom))
  307.                 throw new java.lang.Exception
  308.                     ("InitializationHeuristics constructor: Invalid BRACKETING_CUSTOM_BCP params!");
  309.         } else if (BRACKETING_GENERIC_BCP != _iDeterminant)
  310.             throw new java.lang.Exception
  311.                 ("InitializationHeuristics constructor: Invalid BRACKETING_GENERIC_BCP params!");
  312.     }

  313.     /**
  314.      * Retrieve the Determinant
  315.      *
  316.      * @return The Determinant
  317.      */

  318.     public int getDeterminant()
  319.     {
  320.         return _iDeterminant;
  321.     }

  322.     /**
  323.      * Retrieve the Hard Left Search Start
  324.      *
  325.      * @return The Hard Left Search Start
  326.      */

  327.     public double getSearchStartLeft()
  328.     {
  329.         return _dblSearchStartLeft;
  330.     }

  331.     /**
  332.      * Retrieve the Hard Right Search Start
  333.      *
  334.      * @return The Hard Right Search Start
  335.      */

  336.     public double getSearchStartRight()
  337.     {
  338.         return _dblSearchStartRight;
  339.     }

  340.     /**
  341.      * Retrieve the Soft Bracket Start Mid
  342.      *
  343.      * @return The Soft Bracket Start Mid
  344.      */

  345.     public double getStartingBracketMid()
  346.     {
  347.         return _dblStartingBracketMid;
  348.     }

  349.     /**
  350.      * Retrieve the Soft Bracket Start Left
  351.      *
  352.      * @return The Soft Bracket Start Left
  353.      */

  354.     public double getStartingBracketLeft()
  355.     {
  356.         return _dblStartingBracketLeft;
  357.     }

  358.     /**
  359.      * Retrieve the Hard Bracket Floor
  360.      *
  361.      * @return The Hard Bracket Floor
  362.      */

  363.     public double getBracketFloor()
  364.     {
  365.         return _dblBracketFloor;
  366.     }

  367.     /**
  368.      * Retrieve the Soft Bracket Start Right
  369.      *
  370.      * @return The Soft Bracket Start Right
  371.      */

  372.     public double getStartingBracketRight()
  373.     {
  374.         return _dblStartingBracketRight;
  375.     }

  376.     /**
  377.      * Retrieve the Hard Bracket Ceiling
  378.      *
  379.      * @return The Hard Bracket Ceiling
  380.      */

  381.     public double getBracketCeiling()
  382.     {
  383.         return _dblBracketCeiling;
  384.     }

  385.     /**
  386.      * Retrieve the Custom BCP
  387.      *
  388.      * @return The Custom BCP
  389.      */

  390.     public org.drip.function.r1tor1solver.BracketingControlParams getCustomBCP()
  391.     {
  392.         return _bcpCustom;
  393.     }
  394. }