UnitRegressionStat.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>UnitRegressionStat</i> creates the statistical details for the Unit Regressor. It holds the following:
  83.  *  <ul>
  84.  *      <li>
  85.  *          Execution Initialization Delay
  86.  *      </li>
  87.  *      <li>
  88.  *          Execution time mean, variance, maximum, and minimum
  89.  *      </li>
  90.  *      <li>
  91.  *          The full list of individual execution times
  92.  *      </li>
  93.  *  </ul>
  94.  *
  95.  * <br><br>
  96.  *  <ul>
  97.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  98.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  99.  *      <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>
  100.  *      <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>
  101.  *  </ul>
  102.  * <br><br>
  103.  *
  104.  * @author Lakshmi Krishnamurthy
  105.  */

  106. public class UnitRegressionStat {
  107.     private long _lExecTimeMean = 0L;
  108.     private long _lExecTimeMaximum = 0L;
  109.     private long _lExecTimeMinimum = 0L;
  110.     private long _lExecTimeVariance = 0L;
  111.     private long _lInitializationDelay = 0L;
  112.     private java.util.List<java.lang.Long> _llExecTime = null;

  113.     /**
  114.      * Empty Constructor
  115.      */

  116.     public UnitRegressionStat()
  117.     {
  118.         _llExecTime = new java.util.ArrayList<java.lang.Long>();
  119.     }

  120.     /**
  121.      * Add another run execution time
  122.      *
  123.      * @param lExecTime Execution Time
  124.      *
  125.      * @return TRUE - Executed time run successfully added
  126.      */

  127.     public boolean addExecTime (
  128.         final long lExecTime)
  129.     {
  130.         _llExecTime.add (lExecTime);

  131.         return true;
  132.     }

  133.     /**
  134.      * Generate the statistics across all the execution times generated
  135.      *
  136.      * @return TRUE - Statistics successfully generated
  137.      */

  138.     public boolean generateStat()
  139.     {
  140.         boolean bFirstRun = true;

  141.         int iNumRuns = _llExecTime.size();

  142.         if (0 == iNumRuns) return false;

  143.         for (long lExecTime : _llExecTime) {
  144.             if (bFirstRun) {
  145.                 _lExecTimeMaximum = lExecTime;
  146.                 _lExecTimeMinimum = lExecTime;
  147.                 _lInitializationDelay = lExecTime;
  148.             } else {
  149.                 _lExecTimeMean += lExecTime;

  150.                 if (_lExecTimeMaximum < lExecTime) _lExecTimeMaximum = lExecTime;

  151.                 if (_lExecTimeMinimum > lExecTime) _lExecTimeMinimum = lExecTime;
  152.             }

  153.             bFirstRun = false;
  154.         }

  155.         _lExecTimeMean /= (iNumRuns - 1);
  156.         bFirstRun = true;

  157.         for (long lExecTime : _llExecTime) {
  158.             if (!bFirstRun)
  159.                 _lExecTimeVariance += (lExecTime - _lExecTimeMean) * (lExecTime - _lExecTimeMean);

  160.             bFirstRun = false;
  161.         }

  162.         _lExecTimeVariance = (long) java.lang.Math.sqrt (_lExecTimeVariance / (iNumRuns - 1));

  163.         _lInitializationDelay -= _lExecTimeMean;
  164.         return true;
  165.     }

  166.     /**
  167.      * Get the number of runs for the statistics
  168.      *
  169.      * @return Number of runs
  170.      */

  171.     public int getRuns()
  172.     {
  173.         return _llExecTime.size();
  174.     }

  175.     /**
  176.      * Get the Mean in the execution time
  177.      *
  178.      * @return Execution Time Mean
  179.      */

  180.     public long getMean()
  181.     {
  182.         return _lExecTimeMean;
  183.     }

  184.     /**
  185.      * Get the Minimum in the execution time
  186.      *
  187.      * @return Execution Time Minimum
  188.      */

  189.     public long getMin()
  190.     {
  191.         return _lExecTimeMinimum;
  192.     }

  193.     /**
  194.      * Get the Maximum in the execution time
  195.      *
  196.      * @return Execution Time Maximum
  197.      */

  198.     public long getMax()
  199.     {
  200.         return _lExecTimeMaximum;
  201.     }

  202.     /**
  203.      * Get the variance in the execution time
  204.      *
  205.      * @return Execution Time Variance
  206.      */

  207.     public long getVariance()
  208.     {
  209.         return _lExecTimeVariance;
  210.     }

  211.     /**
  212.      * Get the delay when the regressor is invoked for the first time
  213.      *
  214.      * @return Initialization Delay
  215.      */

  216.     public long getInitializationDelay()
  217.     {
  218.         return _lInitializationDelay;
  219.     }

  220.     /**
  221.      * Return the string version of the statistics
  222.      *
  223.      * @param strRegressionUnit Name the unit for which the regression run was done
  224.      *
  225.      * @return String holding the content of the unit regression statistics
  226.      */

  227.     public java.lang.String displayString (
  228.         final java.lang.String strRegressionUnit)
  229.     {
  230.         if (null == strRegressionUnit || strRegressionUnit.isEmpty()) return null;

  231.         java.lang.StringBuffer sb = new java.lang.StringBuffer();

  232.         sb.append ("\t" + strRegressionUnit + ".Stat.NumRuns=" + _llExecTime.size() + "\n");

  233.         sb.append ("\t" + strRegressionUnit + ".Stat.ExecTimeMean=" + _lExecTimeMean + "\n");

  234.         sb.append ("\t" + strRegressionUnit + ".Stat.ExecTimeMaximum=" + _lExecTimeMaximum + "\n");

  235.         sb.append ("\t" + strRegressionUnit + ".Stat.ExecTimeMinimum=" + _lExecTimeMinimum + "\n");

  236.         sb.append ("\t" + strRegressionUnit + ".Stat.ExecTimeVariance=" + _lExecTimeVariance + "\n");

  237.         sb.append ("\t" + strRegressionUnit + ".Stat.InitializationDelay=" + _lInitializationDelay + "\n");

  238.         sb.append ("\t" + strRegressionUnit + ".Stat.ExecTimeList=" + _llExecTime.toString() + "\n");

  239.         return sb.toString();
  240.     }
  241. }