RegressionEngine.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>RegressionEngine</i> provides the control and frame-work functionality for the General Purpose
  83.  * Regression Suite. It invokes the following steps as part of the execution:
  84.  *  <ul>
  85.  *      <li>
  86.  *          Initialize the regression environment. This step sets up the regression sets, and adds individual
  87.  *              regressors to the set.
  88.  *      </li>
  89.  *      <li>
  90.  *          Invoke the regressors in each set one by one.
  91.  *      </li>
  92.  *      <li>
  93.  *          Collect the results and details of the regression runs.
  94.  *      </li>
  95.  *      <li>
  96.  *          Compile the regression statistics.
  97.  *      </li>
  98.  *      <li>
  99.  *          Optionally display the regression statistics.
  100.  *      </li>
  101.  *  </ul>
  102.  *
  103.  * <br><br>
  104.  *  <ul>
  105.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  106.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  107.  *      <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>
  108.  *      <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>
  109.  *  </ul>
  110.  * <br><br>
  111.  *
  112.  * @author Lakshmi Krishnamurthy
  113.  */

  114. public class RegressionEngine {

  115.     /**
  116.      * Regression outputs decomposed at individual Module Units
  117.      */

  118.     public static final int REGRESSION_DETAIL_MODULE_UNIT_DECOMPOSED = 1;

  119.     /**
  120.      * Regression outputs rolled up to Module Units
  121.      */

  122.     public static final int REGRESSION_DETAIL_MODULE_UNIT_AGGREGATED = 2;

  123.     /**
  124.      * Regression outputs rolled up to Modules
  125.      */

  126.     public static final int REGRESSION_DETAIL_MODULE_AGGREGATED = 4;

  127.     /**
  128.      * Regression Output: Statistics
  129.      */

  130.     public static final int REGRESSION_DETAIL_STATS = 8;

  131.     private int _iNumRuns = 0;
  132.     private int _iRegressionDetail = REGRESSION_DETAIL_MODULE_UNIT_DECOMPOSED;

  133.     protected java.util.Set<org.drip.regression.core.RegressorSet> _setRS = new
  134.         java.util.HashSet<org.drip.regression.core.RegressorSet>();

  135.     org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.regression.core.UnitRegressionStat> _mapURS = new
  136.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.regression.core.UnitRegressionStat>();

  137.     private boolean executeRegressionSet (
  138.         final org.drip.regression.core.RegressorSet rs)
  139.     {
  140.         if (null == rs || null == rs.getSetName() || rs.getSetName().isEmpty()) return false;

  141.         java.util.List<org.drip.regression.core.UnitRegressor> lsRegressor = rs.getRegressorSet();

  142.         if (null == lsRegressor || 0 == lsRegressor.size()) {
  143.             System.out.println ("Cannot get the " + rs.getSetName() + " scenarios!");

  144.             return false;
  145.         }

  146.         long lModuleTime = 0;
  147.         int iNumRegressionsSucceeded = 0;
  148.         boolean bRegressionDetail = 0 != (REGRESSION_DETAIL_MODULE_UNIT_DECOMPOSED & _iRegressionDetail) ?
  149.             true : false;

  150.         System.out.println ("\t" + rs.getSetName() + " starts at " + new java.util.Date());

  151.         for (org.drip.regression.core.UnitRegressor r : lsRegressor) {
  152.             if (null == r) continue;

  153.             org.drip.regression.core.RegressionRunOutput ro = r.regress();

  154.             if (null != ro && ro._bStatus) {
  155.                 ++iNumRegressionsSucceeded;
  156.                 lModuleTime += ro._lExecTime;

  157.                 java.lang.String strScenarioQualifiedRegressor = rs.getSetName() + "." + r.getName();

  158.                 org.drip.regression.core.UnitRegressionStat urs = _mapURS.get
  159.                     (strScenarioQualifiedRegressor);

  160.                 if (null == urs) urs = new org.drip.regression.core.UnitRegressionStat();

  161.                 urs.addExecTime (ro._lExecTime);

  162.                 _mapURS.put (strScenarioQualifiedRegressor, urs);

  163.                 if (0 != (REGRESSION_DETAIL_MODULE_UNIT_DECOMPOSED & _iRegressionDetail) ||
  164.                     0 != (REGRESSION_DETAIL_MODULE_UNIT_AGGREGATED & _iRegressionDetail))
  165.                     System.out.println (ro.displayString (bRegressionDetail));
  166.             }
  167.         }

  168.         System.out.println ("\t" + rs.getSetName() + "=> " + lModuleTime+ " (mu-s): " +
  169.             iNumRegressionsSucceeded + " / " + lsRegressor.size() + " succeeded.");

  170.         System.out.println ("\t" + rs.getSetName() + " ends at " + new java.util.Date() + "\n");

  171.         return true;
  172.     }

  173.     protected RegressionEngine (
  174.         final int iNumRuns,
  175.         final int iRegressionDetail)
  176.         throws java.lang.Exception
  177.     {
  178.         if (0 >= (_iNumRuns = iNumRuns))
  179.             throw new java.lang.Exception ("RegressionEngine ctr: Invalid inputs");

  180.         _iRegressionDetail = iRegressionDetail;
  181.     }

  182.     /**
  183.      * Add the regressor set to the framework
  184.      *
  185.      * @param rs Regressor Set
  186.      *
  187.      * @return TRUE - Regressor Set successfully added
  188.      */

  189.     protected final boolean addRegressorSet (
  190.         final org.drip.regression.core.RegressorSet rs)
  191.     {
  192.         if (null == rs || !rs.setupRegressors()) return false;

  193.         _setRS.add (rs);

  194.         return true;
  195.     }

  196.     /**
  197.      * One-time initialization of the regression engine environment
  198.      *
  199.      * @return TRUE - Regression Environment initialized successfully
  200.      */

  201.     public boolean initRegressionEnv()
  202.     {
  203.         return true;
  204.     }

  205.     /**
  206.      * Launch the Regression Engine and execute the regression sets
  207.      *
  208.      * @return TRUE - Launch Successful
  209.      */

  210.     protected final boolean launch()
  211.     {
  212.         if (0 == _setRS.size() || !initRegressionEnv()) return false;

  213.         boolean bLaunchSuccessful = true;

  214.         for (int i = 0; i < _iNumRuns; ++i) {
  215.             for (org.drip.regression.core.RegressorSet rs : _setRS)
  216.                 bLaunchSuccessful = executeRegressionSet (rs);
  217.         }

  218.         if (0 != _mapURS.size() && null != _mapURS.entrySet()) {
  219.             for (java.util.Map.Entry<java.lang.String, org.drip.regression.core.UnitRegressionStat>
  220.                 meURS : _mapURS.entrySet()) {
  221.                 if (null == meURS || null == meURS.getKey() || meURS.getKey().isEmpty()) continue;

  222.                 org.drip.regression.core.UnitRegressionStat urs = meURS.getValue();

  223.                 if (null == urs || !urs.generateStat()) continue;

  224.                 if (0 != (REGRESSION_DETAIL_STATS & _iRegressionDetail)) {
  225.                     System.out.println ("\n--------\nStats for " + meURS.getKey() + "\n--------");

  226.                     System.out.println (urs.displayString (meURS.getKey()));
  227.                 }
  228.             }
  229.         }

  230.         return bLaunchSuccessful;
  231.     }
  232. }