StandardExponentialPIT.java

  1. package org.drip.sample.samplestatistics;

  2. import org.drip.measure.continuous.R1UnivariateExponential;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;
  5. import org.drip.validation.evidence.TestStatisticAccumulator;
  6. import org.drip.validation.hypothesis.ProbabilityIntegralTransform;

  7. /*
  8.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  9.  */

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

  67. /**
  68.  * <i>StandardExponentialPIT</i> illustrates the Probability Integral Transform and the p-Value for an
  69.  * Empirical Standard Exponential Distribution.
  70.  *
  71.  *  <br><br>
  72.  *  <ul>
  73.  *      <li>
  74.  *          Anfuso, F., D. Karyampas, and A. Nawroth (2017): A Sound Basel III Compliant Framework for
  75.  *              Back-testing Credit Exposure Models
  76.  *              https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2264620 <b>eSSRN</b>
  77.  *      </li>
  78.  *      <li>
  79.  *          Diebold, F. X., T. A. Gunther, and A. S. Tay (1998): Evaluating Density Forecasts with
  80.  *              Applications to Financial Risk Management, International Economic Review 39 (4) 863-883
  81.  *      </li>
  82.  *      <li>
  83.  *          Kenyon, C., and R. Stamm (2012): Discounting, LIBOR, CVA, and Funding: Interest Rate and Credit
  84.  *              Pricing, Palgrave Macmillan
  85.  *      </li>
  86.  *      <li>
  87.  *          Wikipedia (2018): Probability Integral Transform
  88.  *              https://en.wikipedia.org/wiki/Probability_integral_transform
  89.  *      </li>
  90.  *      <li>
  91.  *          Wikipedia (2019): p-value https://en.wikipedia.org/wiki/P-value
  92.  *      </li>
  93.  *  </ul>
  94.  *
  95.  *  <br><br>
  96.  *  <ul>
  97.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  98.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ModelValidationAnalyticsLibrary.md">Model Validation Analytics Library</a></li>
  99.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample">Sample</a></li>
  100.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/hypothesistest">Statistical Hypothesis Tests</a></li>
  101.  *  </ul>
  102.  * <br><br>
  103.  *
  104.  * @author Lakshmi Krishnamurthy
  105.  */

  106. public class StandardExponentialPIT
  107. {

  108.     private static final double UnivariateRandom()
  109.         throws Exception
  110.     {
  111.         return R1UnivariateExponential.Standard().random();
  112.     }

  113.     public static final void main (
  114.         final String[] argumentArray)
  115.         throws Exception
  116.     {
  117.         EnvManager.InitEnv ("");

  118.         int pValueTestCount = 25;
  119.         int instanceCount = 1000000;

  120.         TestStatisticAccumulator responseAccumulator = new TestStatisticAccumulator();

  121.         for (int instanceIndex = 0; instanceIndex < instanceCount; ++instanceIndex)
  122.         {
  123.             responseAccumulator.addTestStatistic (UnivariateRandom());
  124.         }

  125.         ProbabilityIntegralTransform pit = responseAccumulator.probabilityIntegralTransform();

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

  127.         System.out.println ("\t| Empirical p-Value ||");

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

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

  130.         System.out.println ("\t|    Test Response  ||");

  131.         System.out.println ("\t|    Test p-Value   ||");

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

  133.         for (int pValueTestIndex = 0; pValueTestIndex < pValueTestCount; ++pValueTestIndex)
  134.         {
  135.             double testResponse = UnivariateRandom();

  136.             System.out.println (
  137.                 "\t|" +
  138.                 FormatUtil.FormatDouble (testResponse, 1, 4, 1.) + " => " +
  139.                 FormatUtil.FormatDouble (pit.pValue (testResponse), 1, 4, 1.) + " ||"
  140.             );
  141.         }

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

  143.         EnvManager.TerminateEnv();
  144.     }
  145. }