Ensemble.java

  1. package org.drip.validation.evidence;

  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.  *
  9.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  10.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  11.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  12.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  13.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  14.  *      and computational support.
  15.  *  
  16.  *      https://lakshmidrip.github.io/DROP/
  17.  *  
  18.  *  DROP is composed of three modules:
  19.  *  
  20.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  21.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  22.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  23.  *
  24.  *  DROP Product Core implements libraries for the following:
  25.  *  - Fixed Income Analytics
  26.  *  - Loan Analytics
  27.  *  - Transaction Cost Analytics
  28.  *
  29.  *  DROP Portfolio Core implements libraries for the following:
  30.  *  - Asset Allocation Analytics
  31.  *  - Asset Liability Management Analytics
  32.  *  - Capital Estimation Analytics
  33.  *  - Exposure Analytics
  34.  *  - Margin Analytics
  35.  *  - XVA Analytics
  36.  *
  37.  *  DROP Computational Core implements libraries for the following:
  38.  *  - Algorithm Support
  39.  *  - Computation Support
  40.  *  - Function Analysis
  41.  *  - Model Validation
  42.  *  - Numerical Analysis
  43.  *  - Numerical Optimizer
  44.  *  - Spline Builder
  45.  *  - Statistical Learning
  46.  *
  47.  *  Documentation for DROP is Spread Over:
  48.  *
  49.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  50.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  51.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  52.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  53.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  54.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  55.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  56.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  57.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  58.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  59.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  60.  *
  61.  *  Licensed under the Apache License, Version 2.0 (the "License");
  62.  *      you may not use this file except in compliance with the License.
  63.  *  
  64.  *  You may obtain a copy of the License at
  65.  *      http://www.apache.org/licenses/LICENSE-2.0
  66.  *  
  67.  *  Unless required by applicable law or agreed to in writing, software
  68.  *      distributed under the License is distributed on an "AS IS" BASIS,
  69.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  70.  *  
  71.  *  See the License for the specific language governing permissions and
  72.  *      limitations under the License.
  73.  */

  74. /**
  75.  * <i>Ensemble</i> contains the Ensemble Collection of Statistical Samples and their Test Statistic
  76.  * Evaluators.
  77.  *
  78.  *  <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Bhattacharya, B., and D. Habtzghi (2002): Median of the p-value under the Alternate Hypothesis
  82.  *              <i>American Statistician</i> <b>56 (3)</b> 202-206
  83.  *      </li>
  84.  *      <li>
  85.  *          Head, M. L., L. Holman, R, Lanfear, A. T. Kahn, and M. D. Jennions (2015): The Extent and
  86.  *              <i>Consequences of p-Hacking in Science PLoS Biology</i> <b>13 (3)</b> e1002106
  87.  *      </li>
  88.  *      <li>
  89.  *          Wasserstein, R. L., and N. A. Lazar (2016): The ASA’s Statement on p-values: Context, Process,
  90.  *              and Purpose <i>American Statistician</i> <b>70 (2)</b> 129-133
  91.  *      </li>
  92.  *      <li>
  93.  *          Wetzels, R., D. Matzke, M. D. Lee, J. N. Rouder, G, J, Iverson, and E. J. Wagenmakers (2011):
  94.  *              Statistical Evidence in Experimental Psychology: An Empirical Comparison using 855 t-Tests
  95.  *              <i>Perspectives in Psychological Science</i> <b>6 (3)</b> 291-298
  96.  *      </li>
  97.  *      <li>
  98.  *          Wikipedia (2019): p-value https://en.wikipedia.org/wiki/P-value
  99.  *      </li>
  100.  *  </ul>
  101.  *
  102.  *  <br><br>
  103.  *  <ul>
  104.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  105.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ModelValidationAnalyticsLibrary.md">Model Validation Analytics Library</a></li>
  106.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/validation/README.md">Risk Factor and Hypothesis Validation, Evidence Processing, and Model Testing</a></li>
  107.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/validation/evidence/README.md">Sample and Ensemble Evidence Processors</a></li>
  108.  *  </ul>
  109.  * <br><br>
  110.  *
  111.  * @author Lakshmi Krishnamurthy
  112.  */

  113. public class Ensemble implements org.drip.validation.evidence.NativePITGenerator
  114. {
  115.     private double[][] _evaluatedSampleTestStatistic = null;
  116.     private org.drip.validation.evidence.Sample[] _sampleArray = null;
  117.     private org.drip.validation.evidence.TestStatisticEvaluator[] _testStatisticEvaluatorArray =
  118.         null;
  119.     private org.drip.validation.hypothesis.ProbabilityIntegralTransform[] _probabilityIntegralTransformArray
  120.         = null;

  121.     /**
  122.      * Ensemble Constructor
  123.      *
  124.      * @param sampleArray Array of the Statistical Hypothesis Samples
  125.      * @param testStatisticEvaluatorArray Array of the Test Statistic Evaluators
  126.      *
  127.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  128.      */

  129.     public Ensemble (
  130.         final org.drip.validation.evidence.Sample[] sampleArray,
  131.         final org.drip.validation.evidence.TestStatisticEvaluator[] testStatisticEvaluatorArray)
  132.         throws java.lang.Exception
  133.     {
  134.         if (null == (_sampleArray = sampleArray) ||
  135.             null == (_testStatisticEvaluatorArray = testStatisticEvaluatorArray))
  136.         {
  137.             throw new java.lang.Exception ("Ensemble Constructor => Invalid Inputs");
  138.         }

  139.         int sampleCount = _sampleArray.length;
  140.         int testStatisticEvaluatorCount = _testStatisticEvaluatorArray.length;
  141.         _evaluatedSampleTestStatistic = new double[testStatisticEvaluatorCount][sampleCount];
  142.         _probabilityIntegralTransformArray = new
  143.             org.drip.validation.hypothesis.ProbabilityIntegralTransform[testStatisticEvaluatorCount];

  144.         if (0 == sampleCount || 0 == testStatisticEvaluatorCount)
  145.         {
  146.             throw new java.lang.Exception ("Ensemble Constructor => Invalid Inputs");
  147.         }

  148.         for (int sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex)
  149.         {
  150.             if (null == _sampleArray[sampleIndex])
  151.             {
  152.                 throw new java.lang.Exception ("Ensemble Constructor => Invalid Inputs");
  153.             }
  154.         }

  155.         for (int testStatisticEvaluatorIndex = 0;
  156.             testStatisticEvaluatorIndex < testStatisticEvaluatorCount;
  157.             ++testStatisticEvaluatorIndex)
  158.         {
  159.             if (null == _testStatisticEvaluatorArray[testStatisticEvaluatorIndex])
  160.             {
  161.                 throw new java.lang.Exception ("Ensemble Constructor => Invalid Inputs");
  162.             }

  163.             org.drip.validation.evidence.TestStatisticAccumulator testStatisticAccumulator = new
  164.                 org.drip.validation.evidence.TestStatisticAccumulator();

  165.             for (int sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex)
  166.             {
  167.                 if (!testStatisticAccumulator.addTestStatistic
  168.                     (_evaluatedSampleTestStatistic[testStatisticEvaluatorIndex][sampleIndex] =
  169.                         _sampleArray[sampleIndex].applyTestStatistic
  170.                             (_testStatisticEvaluatorArray[testStatisticEvaluatorIndex])))
  171.                 {
  172.                     throw new java.lang.Exception ("Ensemble Constructor => Invalid Inputs");
  173.                 }
  174.             }

  175.             if (null == (_probabilityIntegralTransformArray[testStatisticEvaluatorIndex] =
  176.                 testStatisticAccumulator.probabilityIntegralTransform()))
  177.             {
  178.                 throw new java.lang.Exception ("Ensemble Constructor => Invalid Inputs");
  179.             }
  180.         }
  181.     }

  182.     /**
  183.      * Retrieve the Computed Ensemble Test Statistics
  184.      *
  185.      * @return The Computed Ensemble Test Statistics
  186.      */

  187.     public double[][] evaluatedSampleTestStatistic()
  188.     {
  189.         return _evaluatedSampleTestStatistic;
  190.     }

  191.     /**
  192.      * Retrieve the Array of the Statistical Hypothesis Samples
  193.      *
  194.      * @return The Array of the Statistical Hypothesis Samples
  195.      */

  196.     public org.drip.validation.evidence.Sample[] sampleArray()
  197.     {
  198.         return _sampleArray;
  199.     }

  200.     /**
  201.      * Retrieve the Array of the Test Statistic Evaluators
  202.      *
  203.      * @return The Array of the Test Statistic Evaluators
  204.      */

  205.     public org.drip.validation.evidence.TestStatisticEvaluator[] testStatisticEvaluatorArray()
  206.     {
  207.         return _testStatisticEvaluatorArray;
  208.     }

  209.     /**
  210.      * Retrieve the Array of Probability Integral Transforms, one for each Test Statistic
  211.      *
  212.      * @return The Array of Probability Integral Transforms
  213.      */

  214.     public org.drip.validation.hypothesis.ProbabilityIntegralTransform[] probabilityIntegralTransformArray()
  215.     {
  216.         return _probabilityIntegralTransformArray;
  217.     }

  218.     /**
  219.      * Construct the Test Statistic Based Significance Test Hypothesis Array
  220.      *
  221.      * @return The Test Statistic Based Significance Test Hypothesis Array
  222.      */

  223.     public org.drip.validation.hypothesis.ProbabilityIntegralTransformTest[] significanceTest()
  224.     {
  225.         int probabilityIntegralTransformCount = _testStatisticEvaluatorArray.length;
  226.         org.drip.validation.hypothesis.ProbabilityIntegralTransformTest[]
  227.             probabilityIntegralTransformTestArray = new
  228.                 org.drip.validation.hypothesis.ProbabilityIntegralTransformTest[probabilityIntegralTransformCount];

  229.         for (int probabilityIntegralTransformIndex = 0;
  230.             probabilityIntegralTransformIndex < probabilityIntegralTransformCount;
  231.             ++probabilityIntegralTransformIndex)
  232.         {
  233.             try
  234.             {
  235.                 probabilityIntegralTransformTestArray[probabilityIntegralTransformIndex] = new
  236.                     org.drip.validation.hypothesis.ProbabilityIntegralTransformTest
  237.                         (_probabilityIntegralTransformArray[probabilityIntegralTransformIndex]);
  238.             }
  239.             catch (java.lang.Exception e)
  240.             {
  241.                 e.printStackTrace();

  242.                 return null;
  243.             }
  244.         }

  245.         return probabilityIntegralTransformTestArray;
  246.     }

  247.     /**
  248.      * Compute the Array of t-Test Results
  249.      *
  250.      * @param testStatistic The Test Statistic
  251.      *
  252.      * @return The Array of t-Test Results
  253.      */

  254.     public org.drip.validation.hypothesis.TTestOutcome[] tTest (
  255.         final double testStatistic)
  256.     {
  257.         int sampleCount = _sampleArray.length;
  258.         int testStatisticEvaluatorCount = _testStatisticEvaluatorArray.length;
  259.         org.drip.validation.hypothesis.TTestOutcome[] tTestArray = new
  260.             org.drip.validation.hypothesis.TTestOutcome[testStatisticEvaluatorCount];

  261.         for (int testStatisticEvaluatorIndex = 0;
  262.             testStatisticEvaluatorIndex < testStatisticEvaluatorCount;
  263.             ++testStatisticEvaluatorIndex)
  264.         {
  265.             org.drip.measure.statistics.UnivariateMoments ensembleUnivariateMoments =
  266.                 org.drip.measure.statistics.UnivariateMoments.Standard (
  267.                     "UnivariateMoments",
  268.                     _evaluatedSampleTestStatistic[testStatisticEvaluatorIndex],
  269.                     null
  270.                 );

  271.             if (null == ensembleUnivariateMoments)
  272.             {
  273.                 return null;
  274.             }

  275.             try
  276.             {
  277.                 tTestArray[testStatisticEvaluatorIndex] = new org.drip.validation.hypothesis.TTestOutcome (
  278.                     testStatistic,
  279.                     sampleCount,
  280.                     ensembleUnivariateMoments.mean(),
  281.                     ensembleUnivariateMoments.variance(),
  282.                     ensembleUnivariateMoments.stdDev(),
  283.                     ensembleUnivariateMoments.stdError(),
  284.                     ensembleUnivariateMoments.degreesOfFreedom(),
  285.                     ensembleUnivariateMoments.predictiveConfidenceLevel(),
  286.                     ensembleUnivariateMoments.tStatistic (testStatistic),
  287.                     ensembleUnivariateMoments.standardErrorOffset (testStatistic)
  288.                 );
  289.             }
  290.             catch (java.lang.Exception e)
  291.             {
  292.                 e.printStackTrace();

  293.                 return null;
  294.             }
  295.         }

  296.         return tTestArray;
  297.     }

  298.     /**
  299.      * Compute the Array of Statistical Test Outcomes
  300.      *
  301.      * @param testStatistic The Realized Test Statistic
  302.      * @param pTestSetting The P-Test Setting
  303.      *
  304.      * @return The Array of Statistical Test Outcomes
  305.      */

  306.     public org.drip.validation.hypothesis.StatisticalTestOutcome[] statisticalTest (
  307.         final double testStatistic,
  308.         final org.drip.validation.hypothesis.SignificanceTestSetting pTestSetting)
  309.     {
  310.         if (!org.drip.numerical.common.NumberUtil.IsValid (testStatistic) || null == pTestSetting)
  311.         {
  312.             return null;
  313.         }

  314.         int sampleCount = _sampleArray.length;
  315.         int testStatisticEvaluatorCount = _testStatisticEvaluatorArray.length;
  316.         org.drip.validation.hypothesis.StatisticalTestOutcome[] statisticalTestOutcomeArray = new
  317.             org.drip.validation.hypothesis.StatisticalTestOutcome[testStatisticEvaluatorCount];

  318.         org.drip.validation.hypothesis.ProbabilityIntegralTransformTest[]
  319.             probabilityIntegralTransformTestArray = significanceTest();

  320.         for (int testStatisticEvaluatorIndex = 0;
  321.             testStatisticEvaluatorIndex < testStatisticEvaluatorCount;
  322.             ++testStatisticEvaluatorIndex)
  323.         {
  324.             org.drip.measure.statistics.UnivariateMoments ensembleUnivariateMoments =
  325.                 org.drip.measure.statistics.UnivariateMoments.Standard (
  326.                     "UnivariateMoments",
  327.                     _evaluatedSampleTestStatistic[testStatisticEvaluatorIndex],
  328.                     null
  329.                 );

  330.             if (null == ensembleUnivariateMoments)
  331.             {
  332.                 return null;
  333.             }

  334.             try
  335.             {
  336.                 statisticalTestOutcomeArray[testStatisticEvaluatorIndex] = new
  337.                     org.drip.validation.hypothesis.StatisticalTestOutcome (
  338.                         probabilityIntegralTransformTestArray[testStatisticEvaluatorIndex].significanceTest (
  339.                             testStatistic,
  340.                             pTestSetting
  341.                         ),
  342.                         new org.drip.validation.hypothesis.TTestOutcome (
  343.                             testStatistic,
  344.                             sampleCount,
  345.                             ensembleUnivariateMoments.mean(),
  346.                             ensembleUnivariateMoments.variance(),
  347.                             ensembleUnivariateMoments.stdDev(),
  348.                             ensembleUnivariateMoments.stdError(),
  349.                             ensembleUnivariateMoments.degreesOfFreedom(),
  350.                             ensembleUnivariateMoments.predictiveConfidenceLevel(),
  351.                             ensembleUnivariateMoments.tStatistic (testStatistic),
  352.                             ensembleUnivariateMoments.standardErrorOffset (testStatistic)
  353.                         )
  354.                     );
  355.             }
  356.             catch (java.lang.Exception e)
  357.             {
  358.                 e.printStackTrace();

  359.                 return null;
  360.             }
  361.         }

  362.         return statisticalTestOutcomeArray;
  363.     }

  364.     @Override public org.drip.validation.hypothesis.ProbabilityIntegralTransform
  365.         nativeProbabilityIntegralTransform()
  366.     {
  367.         org.drip.validation.evidence.TestStatisticAccumulator testStatisticAccumulator = new
  368.             org.drip.validation.evidence.TestStatisticAccumulator();

  369.         int sampleCount = _sampleArray.length;

  370.         for (int sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex)
  371.         {
  372.             for (double realization : _sampleArray[sampleIndex].realizationArray())
  373.             {
  374.                 if (!testStatisticAccumulator.addTestStatistic (realization))
  375.                 {
  376.                     return null;
  377.                 }
  378.             }
  379.         }

  380.         return testStatisticAccumulator.probabilityIntegralTransform();
  381.     }
  382. }