UnitRegressionExecutor.java

  1. package org.drip.regression.core;

  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>UnitRegressionExecutor</i> implements the UnitRegressor, and splits the regression execution into pre-,
  83.  * execute, and post-regression. It provides default implementations for pre-regression and post-regression.
  84.  * Most typical regressors only need to over-ride the execRegression method.
  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/core/README.md">Regression Engine Core - Unit Regressors</a></li>
  92.  *  </ul>
  93.  * <br><br>
  94.  *
  95.  * @author Lakshmi Krishnamurthy
  96.  */

  97. public abstract class UnitRegressionExecutor implements org.drip.regression.core.UnitRegressor {
  98.     private static final boolean _bDisplayStatus = false;

  99.     private java.lang.String _strRegressorSet = "";
  100.     private java.lang.String _strRegressorName = "";

  101.     /**
  102.      * Constructor for the unit regression executor
  103.      *
  104.      * @param strRegressorName Name of the unit regressor
  105.      * @param strRegressorSet Name of the regressor set
  106.      *
  107.      * @throws java.lang.Exception Thrown if inputs are invalid
  108.      */

  109.     protected UnitRegressionExecutor (
  110.         final java.lang.String strRegressorName,
  111.         final java.lang.String strRegressorSet)
  112.         throws java.lang.Exception
  113.     {
  114.         if (null == (_strRegressorName = strRegressorName) || strRegressorName.isEmpty() || null ==
  115.             (_strRegressorSet = strRegressorSet) || _strRegressorSet.isEmpty())
  116.             throw new java.lang.Exception ("UnitRegressionExecutor ctr: Invalid inputs");
  117.     }

  118.     /**
  119.      * One-time initialization to set up the objects needed for the regression
  120.      *
  121.      * @return TRUE - Initialization successful
  122.      */

  123.     public boolean preRegression()
  124.     {
  125.         return true;
  126.     }

  127.     /**
  128.      * Execute the regression call within this function
  129.      *
  130.      * @return The result of the regression
  131.      */

  132.     public abstract boolean execRegression();

  133.     /**
  134.      * Clean-up of the objects set-up for the regression
  135.      *
  136.      * @param rnvd Regression Run Detail object to capture the regression details
  137.      *
  138.      * @return TRUE - Clean-up successful
  139.      */

  140.     public boolean postRegression (
  141.         final org.drip.regression.core.RegressionRunDetail rnvd)
  142.     {
  143.         return true;
  144.     }

  145.     @Override public org.drip.regression.core.RegressionRunOutput regress()
  146.     {
  147.         org.drip.regression.core.RegressionRunOutput ro = null;

  148.         try {
  149.             ro = new org.drip.regression.core.RegressionRunOutput (_strRegressorSet + "." +
  150.                 _strRegressorName);
  151.         } catch (java.lang.Exception e) {
  152.             e.printStackTrace();

  153.             return null;
  154.         }

  155.         if (!preRegression()) {
  156.             if (_bDisplayStatus)
  157.                 System.out.println (_strRegressorSet + "." + _strRegressorName +
  158.                     ": Cannot set-up the regressor!");

  159.             return null;
  160.         }

  161.         long lStartTime = System.nanoTime();

  162.         if (!execRegression()) {
  163.             if (_bDisplayStatus)
  164.                 System.out.println (_strRegressorSet + "." + _strRegressorName + ": failed");

  165.             ro.setTerminationStatus (false);

  166.             return ro;
  167.         }

  168.         ro._lExecTime = (long) (1.e-03 * (System.nanoTime() - lStartTime));

  169.         if (!postRegression (ro.getRegressionDetail())) {
  170.             if (_bDisplayStatus)
  171.                 System.out.println (_strRegressorSet + "." + _strRegressorName +
  172.                     ": Regressor clean-up unsuccessful!");

  173.             return null;
  174.         }

  175.         if (_bDisplayStatus) System.out.println (_strRegressorSet + "." + _strRegressorName + ": succeeded");

  176.         ro.setTerminationStatus (true);

  177.         return ro;
  178.     }

  179.     @Override public java.lang.String getName()
  180.     {
  181.         return _strRegressorName;
  182.     }
  183. }