QQTest5.java

  1. package org.drip.sample.quantile;

  2. import org.drip.measure.gaussian.R1UnivariateNormal;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;
  5. import org.drip.validation.evidence.Ensemble;
  6. import org.drip.validation.evidence.Sample;
  7. import org.drip.validation.evidence.TestStatisticEvaluator;
  8. import org.drip.validation.hypothesis.ProbabilityIntegralTransformTest;
  9. import org.drip.validation.quantile.PlottingPositionGeneratorHeuristic;
  10. import org.drip.validation.quantile.QQTestOutcome;
  11. import org.drip.validation.quantile.QQVertex;

  12. /*
  13.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  14.  */

  15. /*!
  16.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  17.  *
  18.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  19.  *      calculations, and portfolio construction within and across fixed income, credit, commodity, equity,
  20.  *      FX, and structured products.
  21.  *  
  22.  *      https://lakshmidrip.github.io/DROP/
  23.  *  
  24.  *  DROP is composed of three main modules:
  25.  *  
  26.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  27.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  28.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  29.  *
  30.  *  DROP Analytics Core implements libraries for the following:
  31.  *  - Fixed Income Analytics
  32.  *  - Asset Backed Analytics
  33.  *  - XVA Analytics
  34.  *  - Exposure and Margin Analytics
  35.  *
  36.  *  DROP Portfolio Core implements libraries for the following:
  37.  *  - Asset Allocation Analytics
  38.  *  - Transaction Cost Analytics
  39.  *
  40.  *  DROP Numerical Core implements libraries for the following:
  41.  *  - Statistical Learning Library
  42.  *  - Numerical Optimizer Library
  43.  *  - Machine Learning Library
  44.  *  - Spline Builder Library
  45.  *
  46.  *  Documentation for DROP is Spread Over:
  47.  *
  48.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  49.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  50.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  51.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  52.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  53.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  54.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  55.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  56.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  57.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  58.  *
  59.  *  Licensed under the Apache License, Version 2.0 (the "License");
  60.  *      you may not use this file except in compliance with the License.
  61.  *  
  62.  *  You may obtain a copy of the License at
  63.  *      http://www.apache.org/licenses/LICENSE-2.0
  64.  *  
  65.  *  Unless required by applicable law or agreed to in writing, software
  66.  *      distributed under the License is distributed on an "AS IS" BASIS,
  67.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  68.  *  
  69.  *  See the License for the specific language governing permissions and
  70.  *      limitations under the License.
  71.  */

  72. /**
  73.  * <i>QQTest5</i> compares the Order Statistics between 2 Similar Normal Distributions using the Filliben
  74.  * (1975) Mean Based Plotting Position Generator.
  75.  *
  76.  *  <br><br>
  77.  *  <ul>
  78.  *      <li>
  79.  *          Gibbons, J. D., and S. Chakraborti (2003): <i>Non-parametric Statistical Inference 4th
  80.  *              Edition</i> <b>CRC Press</b>
  81.  *      </li>
  82.  *      <li>
  83.  *          Filliben, J. J. (1975): The Probability Plot Correlation Coefficient Test for Normality
  84.  *              <i>Technometrics, American Society for Quality</i> <b>17 (1)</b> 111-117
  85.  *      </li>
  86.  *      <li>
  87.  *          Gnanadesikan, R. (1977): <i>Methods for Statistical Analysis of Multivariate Observations</i>
  88.  *              <b>Wiley</b>
  89.  *      </li>
  90.  *      <li>
  91.  *          Thode, H. C. (2002): <i>Testing for Normality</i> <b>Marcel Dekker</b> New York
  92.  *      </li>
  93.  *      <li>
  94.  *          Wikipedia (2018): Q-Q Plot https://en.wikipedia.org/wiki/Q%E2%80%93Q_plot
  95.  *      </li>
  96.  *  </ul>
  97.  *
  98.  *  <br><br>
  99.  *  <ul>
  100.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  101.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ModelValidationAnalyticsLibrary.md">Model Validation Analytics Library</a></li>
  102.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/validation">Model Validation Suite</a></li>
  103.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/validation/quantile">Quantile Based Graphical Numerical Validators</a></li>
  104.  *  </ul>
  105.  * <br><br>
  106.  *
  107.  * @author Lakshmi Krishnamurthy
  108.  */

  109. public class QQTest5
  110. {

  111.     private static final double UnivariateRandom (
  112.         final double mean,
  113.         final double sigma)
  114.         throws Exception
  115.     {
  116.         return new R1UnivariateNormal (
  117.             mean,
  118.             sigma
  119.         ).random();
  120.     }

  121.     private static final Sample GenerateSample (
  122.         final double mean,
  123.         final double sigma,
  124.         final int drawCount)
  125.         throws Exception
  126.     {
  127.         double[] univariateRandomArray = new double[drawCount];

  128.         for (int drawIndex = 0; drawIndex < drawCount; ++drawIndex)
  129.         {
  130.             univariateRandomArray[drawIndex] = UnivariateRandom (
  131.                 mean,
  132.                 sigma
  133.             );
  134.         }

  135.         return new Sample (univariateRandomArray);
  136.     }

  137.     private static final Sample[] GenerateSampleArray (
  138.         final double mean,
  139.         final double sigma,
  140.         final int drawCount,
  141.         final int sampleCount)
  142.         throws Exception
  143.     {
  144.         Sample[] sampleArray = new Sample[sampleCount];

  145.         for (int sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex)
  146.         {
  147.             sampleArray[sampleIndex] = GenerateSample (
  148.                 mean,
  149.                 sigma,
  150.                 drawCount
  151.             );
  152.         }

  153.         return sampleArray;
  154.     }

  155.     private static final Ensemble GenerateEnsemble (
  156.         final double mean,
  157.         final double sigma,
  158.         final int drawCount,
  159.         final int sampleCount)
  160.         throws Exception
  161.     {
  162.         return new Ensemble (
  163.             GenerateSampleArray (
  164.                 mean,
  165.                 sigma,
  166.                 drawCount,
  167.                 sampleCount
  168.             ),
  169.             new TestStatisticEvaluator[]
  170.             {
  171.                 new TestStatisticEvaluator()
  172.                 {
  173.                     public double evaluate (
  174.                         final double[] drawArray)
  175.                         throws Exception
  176.                     {
  177.                         return 1.;
  178.                     }
  179.                 }
  180.             }
  181.         );
  182.     }

  183.     private static final void QQPlot (
  184.         final QQTestOutcome qqTestOutcome)
  185.         throws Exception
  186.     {
  187.         QQVertex[] qqVertexArray = qqTestOutcome.qqVertexArray();

  188.         System.out.println ("\t|------------------------------------||");

  189.         System.out.println ("\t|          Q-Q TEST OUTCOME          ||");

  190.         System.out.println ("\t|------------------------------------||");

  191.         System.out.println ("\t|    L -> R:                         ||");

  192.         System.out.println ("\t|        - Order Statistic Ordinal   ||");

  193.         System.out.println ("\t|        - Order Statistic Quantile  ||");

  194.         System.out.println ("\t|        - Order Statistic X         ||");

  195.         System.out.println ("\t|        - Order Statistic Y         ||");

  196.         System.out.println ("\t|------------------------------------||");

  197.         for (QQVertex qqVertex : qqVertexArray)
  198.         {
  199.             System.out.println (
  200.                 "\t| " + FormatUtil.FormatDouble (
  201.                     qqVertex.plottingPosition().orderStatisticOrdinal(), 2, 0, 1.
  202.                 ) + " => " +
  203.                 FormatUtil.FormatDouble (qqVertex.plottingPosition().quantile(), 1, 4, 1.) + " | " +
  204.                 FormatUtil.FormatDouble (qqVertex.orderStatisticX(), 1, 4, 1.) + " | " +
  205.                 FormatUtil.FormatDouble (qqVertex.orderStatisticY(), 1, 4, 1.) + " ||"
  206.             );
  207.         }

  208.         System.out.println ("\t|------------------------------------||");

  209.         System.out.println ("\t|-----------------------------------------------------||");

  210.         System.out.println (
  211.             "\t| Probability Plot Correlation Coefficient => " +
  212.             FormatUtil.FormatDouble (qqTestOutcome.probabilityPlotCorrelationCoefficient(), 1, 4, 1.) + " ||"
  213.         );

  214.         System.out.println ("\t|-----------------------------------------------------||");
  215.     }

  216.     public static final void main (
  217.         final String[] argumentArray)
  218.         throws Exception
  219.     {
  220.         EnvManager.InitEnv ("");

  221.         int drawCount = 1000000;
  222.         int sampleCount = 1;
  223.         double sampleMean = 0.;
  224.         double hypothesisMean = 1.;
  225.         int orderStatisticCount = 25;
  226.         double sampleVolatility = 1.0;
  227.         double hypothesisVolatility = 0.5;

  228.         PlottingPositionGeneratorHeuristic plottingPositionGenerator =
  229.             PlottingPositionGeneratorHeuristic.Filliben1975 (orderStatisticCount);

  230.         Sample sample = GenerateSample (
  231.             sampleMean,
  232.             sampleVolatility,
  233.             drawCount
  234.         );

  235.         Ensemble hypothesis = GenerateEnsemble (
  236.             hypothesisMean,
  237.             hypothesisVolatility,
  238.             drawCount,
  239.             sampleCount
  240.         );

  241.         QQPlot (
  242.             new ProbabilityIntegralTransformTest (
  243.                 hypothesis.nativeProbabilityIntegralTransform()
  244.             ).qqTest (
  245.                 sample.nativeProbabilityIntegralTransform(),
  246.                 plottingPositionGenerator
  247.             )
  248.         );

  249.         EnvManager.TerminateEnv();
  250.     }
  251. }