ExponentialAndersonDarlingGapDiscriminant.java

  1. package org.drip.sample.distancetest;

  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.distance.GapTestOutcome;
  6. import org.drip.validation.distance.GapTestSetting;
  7. import org.drip.validation.distance.GapLossWeightFunction;
  8. import org.drip.validation.evidence.Ensemble;
  9. import org.drip.validation.evidence.Sample;
  10. import org.drip.validation.evidence.TestStatisticEvaluator;
  11. import org.drip.validation.hypothesis.ProbabilityIntegralTransformTest;

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

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

  84. /**
  85.  * <i>ExponentialAndersonDarlingGapDiscriminant</i> demonstrates the Generation of the Sample Distance
  86.  * Discriminant Metrics for Different Ensemble Hypotheses.
  87.  *
  88.  *  <br><br>
  89.  *  <ul>
  90.  *      <li>
  91.  *          <b>Reference Distribution  </b> - <i>Univariate Exponential</i>
  92.  *      </li>
  93.  *      <li>
  94.  *          <b>Gap Loss Function       </b> - <i>Anfuso, Karyampas, and Nawroth (2017)</i>
  95.  *      </li>
  96.  *      <li>
  97.  *          <b>Gap Loss Weight Function</b> - <i>Anderson and Darling</i>
  98.  *      </li>
  99.  *  </ul>
  100.  *
  101.  *  <br><br>
  102.  *  <ul>
  103.  *      <li>
  104.  *          Anfuso, F., D. Karyampas, and A. Nawroth (2017): A Sound Basel III Compliant Framework for
  105.  *              Back-testing Credit Exposure Models
  106.  *              https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2264620 <b>eSSRN</b>
  107.  *      </li>
  108.  *      <li>
  109.  *          Diebold, F. X., T. A. Gunther, and A. S. Tay (1998): Evaluating Density Forecasts with
  110.  *              Applications to Financial Risk Management, International Economic Review 39 (4) 863-883
  111.  *      </li>
  112.  *      <li>
  113.  *          Kenyon, C., and R. Stamm (2012): Discounting, LIBOR, CVA, and Funding: Interest Rate and Credit
  114.  *              Pricing, Palgrave Macmillan
  115.  *      </li>
  116.  *      <li>
  117.  *          Wikipedia (2018): Probability Integral Transform
  118.  *              https://en.wikipedia.org/wiki/Probability_integral_transform
  119.  *      </li>
  120.  *      <li>
  121.  *          Wikipedia (2019): p-value https://en.wikipedia.org/wiki/P-value
  122.  *      </li>
  123.  *  </ul>
  124.  *
  125.  *  <br><br>
  126.  *  <ul>
  127.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  128.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ModelValidationAnalyticsLibrary.md">Model Validation Analytics Library</a></li>
  129.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  130.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/validation">Empirical Univariate Gap Distance Tests</a></li>
  131.  *  </ul>
  132.  * <br><br>
  133.  *
  134.  * @author Lakshmi Krishnamurthy
  135.  */

  136. public class ExponentialAndersonDarlingGapDiscriminant
  137. {

  138.     private static final double UnivariateRandom (
  139.         final double lambda)
  140.         throws Exception
  141.     {
  142.         return new R1UnivariateExponential (lambda).random();
  143.     }

  144.     private static final Sample GenerateSample (
  145.         final double lambda,
  146.         final int drawCount)
  147.         throws Exception
  148.     {
  149.         double[] univariateRandomArray = new double[drawCount];

  150.         for (int drawIndex = 0; drawIndex < drawCount; ++drawIndex)
  151.         {
  152.             univariateRandomArray[drawIndex] = UnivariateRandom (lambda);
  153.         }

  154.         return new Sample (univariateRandomArray);
  155.     }

  156.     private static final Sample[] GenerateSampleArray (
  157.         final double lambda,
  158.         final int drawCount,
  159.         final int sampleCount)
  160.         throws Exception
  161.     {
  162.         Sample[] sampleArray = new Sample[sampleCount];

  163.         for (int sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex)
  164.         {
  165.             sampleArray[sampleIndex] = GenerateSample (
  166.                 lambda,
  167.                 drawCount
  168.             );
  169.         }

  170.         return sampleArray;
  171.     }

  172.     private static final Ensemble GenerateEnsemble (
  173.         final double lambda,
  174.         final int drawCount,
  175.         final int sampleCount)
  176.         throws Exception
  177.     {
  178.         return new Ensemble (
  179.             GenerateSampleArray (
  180.                 lambda,
  181.                 drawCount,
  182.                 sampleCount
  183.             ),
  184.             new TestStatisticEvaluator[]
  185.             {
  186.                 new TestStatisticEvaluator()
  187.                 {
  188.                     public double evaluate (
  189.                         final double[] drawArray)
  190.                         throws Exception
  191.                     {
  192.                         return 1.;
  193.                     }
  194.                 }
  195.             }
  196.         );
  197.     }

  198.     private static final GapTestOutcome DistanceTest (
  199.         final Sample sample,
  200.         final Ensemble ensemble,
  201.         final GapTestSetting gapTestSetting)
  202.         throws Exception
  203.     {
  204.         return new ProbabilityIntegralTransformTest (
  205.             ensemble.nativeProbabilityIntegralTransform()
  206.         ).distanceTest (
  207.             sample.nativeProbabilityIntegralTransform(),
  208.             gapTestSetting
  209.         );
  210.     }

  211.     private static final double DistanceTest (
  212.         final double hypothesisLambda,
  213.         final int drawCount,
  214.         final int sampleCount,
  215.         final Sample sample,
  216.         final GapTestSetting gapTestSetting)
  217.         throws Exception
  218.     {
  219.         return DistanceTest (
  220.             sample,
  221.             GenerateEnsemble (
  222.                 hypothesisLambda,
  223.                 drawCount,
  224.                 sampleCount
  225.             ),
  226.             gapTestSetting
  227.         ).distance();
  228.     }

  229.     public static final void main (
  230.         final String[] argumentArray)
  231.         throws Exception
  232.     {
  233.         EnvManager.InitEnv ("");

  234.         int drawCount = 2000;
  235.         int sampleCount = 600;
  236.         double sampleLambda = 1.;
  237.         double[] hypothesisLambdaArray =
  238.         {
  239.             0.20,
  240.             0.30,
  241.             0.40,
  242.             0.50,
  243.             0.60,
  244.             0.70,
  245.             0.80,
  246.             0.90,
  247.             1.00,
  248.             1.10,
  249.             1.20,
  250.             1.30,
  251.             1.40,
  252.             1.50,
  253.             1.60,
  254.             1.70,
  255.             1.80,
  256.             1.90,
  257.             2.00,
  258.             2.10,
  259.             2.20,
  260.             2.30,
  261.             2.40,
  262.             2.50
  263.         };

  264.         GapTestSetting gapTestSetting = GapTestSetting.RiskFactorLossTest (
  265.             GapLossWeightFunction.AndersonDarling()
  266.         );

  267.         Sample sample = GenerateSample (
  268.             sampleLambda,
  269.             drawCount
  270.         );

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

  272.         System.out.println ("\t|    DISCRIMINANT GRID SCAN    ||");

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

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

  275.         System.out.println ("\t|        - Hypothesis Lambda   ||");

  276.         System.out.println ("\t|        - Distance Metric     ||");

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

  278.         for (double hypothesisLambda : hypothesisLambdaArray)
  279.         {
  280.             System.out.println (
  281.                 "\t| " +
  282.                 FormatUtil.FormatDouble (hypothesisLambda, 1, 2, 1.) + " => " +
  283.                 FormatUtil.FormatDouble (
  284.                     DistanceTest (
  285.                         hypothesisLambda,
  286.                         drawCount,
  287.                         sampleCount,
  288.                         sample,
  289.                         gapTestSetting
  290.                     ),
  291.                     1, 8, 1.
  292.                 ) + "         ||"
  293.             );
  294.         }

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

  296.         EnvManager.TerminateEnv();
  297.     }
  298. }