NormalAndersonDarlingGapDiscriminant.java

  1. package org.drip.sample.distancetest;

  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.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>NormalAndersonDarlingGapDiscriminant</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 Normal</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 NormalAndersonDarlingGapDiscriminant
  137. {

  138.     private static final double UnivariateRandom (
  139.         final double mean,
  140.         final double sigma)
  141.         throws Exception
  142.     {
  143.         return new R1UnivariateNormal (
  144.             mean,
  145.             sigma
  146.         ).random();
  147.     }

  148.     private static final Sample GenerateSample (
  149.         final double mean,
  150.         final double sigma,
  151.         final int drawCount)
  152.         throws Exception
  153.     {
  154.         double[] univariateRandomArray = new double[drawCount];

  155.         for (int drawIndex = 0; drawIndex < drawCount; ++drawIndex)
  156.         {
  157.             univariateRandomArray[drawIndex] = UnivariateRandom (
  158.                 mean,
  159.                 sigma
  160.             );
  161.         }

  162.         return new Sample (univariateRandomArray);
  163.     }

  164.     private static final Sample[] GenerateSampleArray (
  165.         final double mean,
  166.         final double sigma,
  167.         final int drawCount,
  168.         final int sampleCount)
  169.         throws Exception
  170.     {
  171.         Sample[] sampleArray = new Sample[sampleCount];

  172.         for (int sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex)
  173.         {
  174.             sampleArray[sampleIndex] = GenerateSample (
  175.                 mean,
  176.                 sigma,
  177.                 drawCount
  178.             );
  179.         }

  180.         return sampleArray;
  181.     }

  182.     private static final Ensemble GenerateEnsemble (
  183.         final double mean,
  184.         final double sigma,
  185.         final int drawCount,
  186.         final int sampleCount)
  187.         throws Exception
  188.     {
  189.         return new Ensemble (
  190.             GenerateSampleArray (
  191.                 mean,
  192.                 sigma,
  193.                 drawCount,
  194.                 sampleCount
  195.             ),
  196.             new TestStatisticEvaluator[]
  197.             {
  198.                 new TestStatisticEvaluator()
  199.                 {
  200.                     public double evaluate (
  201.                         final double[] drawArray)
  202.                         throws Exception
  203.                     {
  204.                         return 1.;
  205.                     }
  206.                 }
  207.             }
  208.         );
  209.     }

  210.     private static final GapTestOutcome DistanceTest (
  211.         final Sample sample,
  212.         final Ensemble ensemble,
  213.         final GapTestSetting gapTestSetting)
  214.         throws Exception
  215.     {
  216.         return new ProbabilityIntegralTransformTest (
  217.             ensemble.nativeProbabilityIntegralTransform()
  218.         ).distanceTest (
  219.             sample.nativeProbabilityIntegralTransform(),
  220.             gapTestSetting
  221.         );
  222.     }

  223.     private static final double DistanceTest (
  224.         final double hypothesisMean,
  225.         final double hypothesisSigma,
  226.         final int drawCount,
  227.         final int sampleCount,
  228.         final Sample sample,
  229.         final GapTestSetting gapTestSetting)
  230.         throws Exception
  231.     {
  232.         return DistanceTest (
  233.             sample,
  234.             GenerateEnsemble (
  235.                 hypothesisMean,
  236.                 hypothesisSigma,
  237.                 drawCount,
  238.                 sampleCount
  239.             ),
  240.             gapTestSetting
  241.         ).distance();
  242.     }

  243.     public static final void main (
  244.         final String[] argumentArray)
  245.         throws Exception
  246.     {
  247.         EnvManager.InitEnv ("");

  248.         int drawCount = 2000;
  249.         int sampleCount = 600;
  250.         double sampleMean = 0.;
  251.         double sampleSigma = 1.;
  252.         double[] hypothesisMeanArray = {
  253.             -0.50,
  254.             -0.25,
  255.              0.00,
  256.              0.25,
  257.              0.50
  258.         };
  259.         double[] hypothesisSigmaArray = {
  260.             0.50,
  261.             0.75,
  262.             1.00,
  263.             1.25,
  264.             1.50
  265.         };

  266.         GapTestSetting gapTestSetting = GapTestSetting.RiskFactorLossTest (
  267.             GapLossWeightFunction.AndersonDarling()
  268.         );

  269.         Sample sample = GenerateSample (
  270.             sampleMean,
  271.             sampleSigma,
  272.             drawCount
  273.         );

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

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

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

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

  278.         System.out.println ("\t|        - Hypothesis Mean     ||");

  279.         System.out.println ("\t|        - Hypothesis Sigma    ||");

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

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

  282.         for (double hypothesisMean : hypothesisMeanArray)
  283.         {
  284.             for (double hypothesisSigma : hypothesisSigmaArray)
  285.             {
  286.                 System.out.println (
  287.                     "\t| " +
  288.                     FormatUtil.FormatDouble (hypothesisMean, 1, 2, 1.) + " | " +
  289.                     FormatUtil.FormatDouble (hypothesisSigma, 1, 2, 1.) + " => " +
  290.                     FormatUtil.FormatDouble (
  291.                         DistanceTest (
  292.                             hypothesisMean,
  293.                             hypothesisSigma,
  294.                             drawCount,
  295.                             sampleCount,
  296.                             sample,
  297.                             gapTestSetting
  298.                         ),
  299.                         1, 8, 1.
  300.                     ) + " ||"
  301.                 );
  302.             }
  303.         }

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

  305.         EnvManager.TerminateEnv();
  306.     }
  307. }