ExecutionControl.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>ExecutionControl</i> implements the core fixed point search execution control and customization
  83.  * functionality.
  84.  * <br><br>
  85.  * ExecutionControl is used for a) calculating the absolute tolerance, and b) determining whether the OF has
  86.  * reached the goal.
  87.  * <br><br>
  88.  * ExecutionControl determines the execution termination using its ExecutionControlParams instance.
  89.  *
  90.  *  <br><br>
  91.  *  <ul>
  92.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  93.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  94.  *      <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>
  95.  *      <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>
  96.  *  </ul>
  97.  *
  98.  * @author Lakshmi Krishnamurthy
  99.  */

  100. public class ExecutionControl {
  101.     private org.drip.function.r1tor1solver.ExecutionControlParams _ecp = null;

  102.     protected org.drip.function.definition.R1ToR1 _of = null;

  103.     /**
  104.      * ExecutionControl constructor
  105.      *
  106.      * @param of Objective Function
  107.      * @param ecp Execution Control Parameters
  108.      *
  109.      * @throws java.lang.Exception Thrown if inputs are invalid
  110.      */

  111.     public ExecutionControl (
  112.         final org.drip.function.definition.R1ToR1 of,
  113.         final org.drip.function.r1tor1solver.ExecutionControlParams ecp)
  114.         throws java.lang.Exception
  115.     {
  116.         if (null == (_of = of))
  117.             throw new java.lang.Exception ("ExecutionControl constructor: Invalid inputs");

  118.         if (null == (_ecp = ecp)) _ecp = new org.drip.function.r1tor1solver.ExecutionControlParams();
  119.     }

  120.     /**
  121.      * Retrieve the Number of Iterations
  122.      *
  123.      * @return Number of solver iterations
  124.      */

  125.     public int getNumIterations()
  126.     {
  127.         return _ecp.getNumIterations();
  128.     }

  129.     /**
  130.      * Calculate the absolute OF tolerance using the initial OF value
  131.      *
  132.      * @param dblOFInitial Initial OF Value
  133.      *
  134.      * @return The absolute OF Tolerance
  135.      *
  136.      * @throws java.lang.Exception Thrown if absolute tolerance cannot be calculated
  137.      */

  138.     public double calcAbsoluteOFTolerance (
  139.         final double dblOFInitial)
  140.         throws java.lang.Exception
  141.     {
  142.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblOFInitial))
  143.             throw new java.lang.Exception ("ExecutionControl::calcAbsoluteOFTolerance => Invalid inputs!");

  144.         double dblAbsoluteTolerance = java.lang.Math.abs (dblOFInitial) * _ecp.getOFGoalToleranceFactor();

  145.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblAbsoluteTolerance) || dblAbsoluteTolerance <
  146.             _ecp.getAbsoluteOFToleranceFallback())
  147.             dblAbsoluteTolerance = _ecp.getAbsoluteOFToleranceFallback();

  148.         return dblAbsoluteTolerance;
  149.     }

  150.     /**
  151.      * Calculate the absolute variate convergence amount using the initial variate
  152.      *
  153.      * @param dblInitialVariate Initial Variate
  154.      *
  155.      * @return The Absolute Variate Convergence Amount
  156.      *
  157.      * @throws java.lang.Exception Thrown if Absolute Variate Convergence Amount cannot be calculated
  158.      */

  159.     public double calcAbsoluteVariateConvergence (
  160.         final double dblInitialVariate)
  161.         throws java.lang.Exception
  162.     {
  163.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblInitialVariate))
  164.             throw new java.lang.Exception
  165.                 ("ExecutionControl::calcAbsoluteVariateConvergence => Invalid inputs!");

  166.         double dblAbsoluteConvergence = java.lang.Math.abs (dblInitialVariate) *
  167.             _ecp.getVariateConvergenceFactor();

  168.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblAbsoluteConvergence) || dblAbsoluteConvergence <
  169.             _ecp.getAbsoluteVariateConvergenceFallback())
  170.             dblAbsoluteConvergence = _ecp.getAbsoluteVariateConvergenceFallback();

  171.         return dblAbsoluteConvergence;
  172.     }

  173.     /**
  174.      * Check to see if the OF has reached the goal
  175.      *
  176.      * @param dblAbsoluteTolerance Absolute Tolerance
  177.      * @param dblOF OF Value
  178.      * @param dblOFGoal OF Goal
  179.      *
  180.      * @return TRUE - If the OF has reached the goal
  181.      *
  182.      * @throws java.lang.Exception Thrown if the inputs are invalid
  183.      */

  184.     public boolean hasOFReachedGoal (
  185.         final double dblAbsoluteTolerance,
  186.         final double dblOF,
  187.         final double dblOFGoal)
  188.         throws java.lang.Exception
  189.     {
  190.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblAbsoluteTolerance) ||
  191.             !org.drip.numerical.common.NumberUtil.IsValid (dblOF) || !org.drip.numerical.common.NumberUtil.IsValid
  192.                 (dblOFGoal))
  193.             throw new java.lang.Exception ("ExecutionControl::hasOFReachedGoal => Invalid inputs!");

  194.         return dblAbsoluteTolerance > java.lang.Math.abs (dblOF - dblOFGoal);
  195.     }

  196.     /**
  197.      * Indicate if the variate convergence check has been turned on
  198.      *
  199.      * @return TRUE - Variate convergence check has been turned on
  200.      */

  201.     public boolean isVariateConvergenceCheckEnabled()
  202.     {
  203.         return _ecp.isVariateConvergenceCheckEnabled();
  204.     }
  205. }