GlivenkoCantelliSupremumBound.java

  1. package org.drip.sample.efronstein;

  2. import org.drip.function.definition.R1ToR1;
  3. import org.drip.function.r1tor1.OffsetIdempotent;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.sequence.custom.GlivenkoCantelliFunctionSupremum;
  6. import org.drip.sequence.functional.*;
  7. import org.drip.sequence.metrics.SingleSequenceAgnosticMetrics;
  8. import org.drip.sequence.random.*;
  9. import org.drip.service.env.EnvManager;

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

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

  75. /**
  76.  * <i>GlivenkoCantelliSupremumBound</i> demonstrates the Computation of the Probabilistic Bounds for the
  77.  * Supremum among the Class of Functions for an Empirical Sample from its Population Mean using Variants of
  78.  * the Efron-Stein Methodology.
  79.  *  
  80.  * <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  84.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/efronstein/README.md">Efron-Stein Semi-Agnostic Sequence Bounds</a></li>
  86.  *  </ul>
  87.  * <br><br>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class GlivenkoCantelliSupremumBound {

  92.     private static final SingleSequenceAgnosticMetrics[] IIDDraw (
  93.         final UnivariateSequenceGenerator rsg,
  94.         final int iNumSample)
  95.         throws Exception
  96.     {
  97.         SingleSequenceAgnosticMetrics[] aSSAM = new SingleSequenceAgnosticMetrics[iNumSample];

  98.         for (int i = 0; i < iNumSample; ++i)
  99.             aSSAM[i] = rsg.sequence (
  100.                 iNumSample,
  101.                 null
  102.             );

  103.         return aSSAM;
  104.     }

  105.     private static final GlivenkoCantelliFunctionSupremum GlivenkoCantelliSupremum (
  106.         final Binary bsg,
  107.         final int iNumVariate)
  108.         throws Exception
  109.     {
  110.         return GlivenkoCantelliFunctionSupremum.Create (
  111.             new FunctionSupremumUnivariateRandom (
  112.                 new R1ToR1[] {
  113.                     new OffsetIdempotent (0.),
  114.                     new OffsetIdempotent (2. * bsg.positiveProbability())
  115.                 },
  116.                 null
  117.             ),
  118.             iNumVariate
  119.         );
  120.     }

  121.     private static final void MartingaleDifferencesRun (
  122.         final Binary bsg,
  123.         final int iNumSample,
  124.         final int iNumSet)
  125.         throws Exception
  126.     {
  127.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  128.         for (int j = 0; j < iNumSet; ++j) {
  129.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  130.                 bsg,
  131.                 iNumSample
  132.             );

  133.             EfronSteinMetrics esam = new EfronSteinMetrics (
  134.                 GlivenkoCantelliSupremum (
  135.                     bsg,
  136.                     iNumSample
  137.                 ),
  138.                 aSSAM
  139.             );

  140.             if (0 != j) strDump += " |";

  141.             strDump += FormatUtil.FormatDouble (esam.martingaleVarianceUpperBound(), 1, 3, 1.);
  142.         }

  143.         System.out.println (strDump + " |");
  144.     }

  145.     private static final void GhostVariateVarianceRun (
  146.         final Binary bsg,
  147.         final int iNumSample,
  148.         final int iNumSet)
  149.         throws Exception
  150.     {
  151.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  152.         for (int j = 0; j < iNumSet; ++j) {
  153.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  154.                 bsg,
  155.                 iNumSample
  156.             );

  157.             EfronSteinMetrics esam = new EfronSteinMetrics (
  158.                 GlivenkoCantelliSupremum (
  159.                     bsg,
  160.                     iNumSample
  161.                 ),
  162.                 aSSAM
  163.             );

  164.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  165.                 bsg,
  166.                 iNumSample
  167.             );

  168.             if (0 != j) strDump += " |";

  169.             strDump += FormatUtil.FormatDouble (esam.ghostVarianceUpperBound (aSSAMGhost), 1, 3, 1.);
  170.         }

  171.         System.out.println (strDump + " |");
  172.     }

  173.     private static final void EfronSteinSteeleRun (
  174.         final Binary bsg,
  175.         final int iNumSample,
  176.         final int iNumSet)
  177.         throws Exception
  178.     {
  179.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  180.         for (int j = 0; j < iNumSet; ++j) {
  181.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  182.                 bsg,
  183.                 iNumSample
  184.             );

  185.             EfronSteinMetrics esam = new EfronSteinMetrics (
  186.                 GlivenkoCantelliSupremum (
  187.                     bsg,
  188.                     iNumSample
  189.                 ),
  190.                 aSSAM
  191.             );

  192.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  193.                 bsg,
  194.                 iNumSample
  195.             );

  196.             if (0 != j) strDump += " |";

  197.             strDump += FormatUtil.FormatDouble (esam.efronSteinSteeleBound (aSSAMGhost), 1, 3, 1.);
  198.         }

  199.         System.out.println (strDump + " |");
  200.     }

  201.     private static final void PivotDifferencesRun (
  202.         final Binary bsg,
  203.         final int iNumSample,
  204.         final int iNumSet)
  205.         throws Exception
  206.     {
  207.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  208.         for (int j = 0; j < iNumSet; ++j) {
  209.             MultivariateRandom func = GlivenkoCantelliSupremum (
  210.                 bsg,
  211.                 iNumSample
  212.             );

  213.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  214.                 bsg,
  215.                 iNumSample
  216.             );

  217.             EfronSteinMetrics esam = new EfronSteinMetrics (
  218.                 func,
  219.                 aSSAM
  220.             );

  221.             if (0 != j) strDump += " |";

  222.             strDump += FormatUtil.FormatDouble (esam.pivotVarianceUpperBound (func), 1, 3, 1.);
  223.         }

  224.         System.out.println (strDump + " |");
  225.     }

  226.     public static final void main (
  227.         final String[] astrArgs)
  228.         throws Exception
  229.     {
  230.         EnvManager.InitEnv ("");

  231.         int iNumSet = 5;

  232.         int[] aiSampleSize = new int[] {
  233.             3, 10, 25, 50
  234.         };

  235.         Binary bin = new Binary (0.7);

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

  237.         System.out.println ("\t|  Martingale Differences Variance Upper Bound  |");

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

  239.         for (int iSampleSize : aiSampleSize)
  240.             MartingaleDifferencesRun (
  241.                 bin,
  242.                 iSampleSize,
  243.                 iNumSet
  244.             );

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

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

  247.         System.out.println ("\t|   Symmetrized Variate Variance Upper Bound    |");

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

  249.         for (int iSampleSize : aiSampleSize)
  250.             GhostVariateVarianceRun (
  251.                 bin,
  252.                 iSampleSize,
  253.                 iNumSet
  254.             );

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

  256.         aiSampleSize = new int[] {
  257.             3, 10, 25, 50, 75, 99
  258.         };

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

  260.         System.out.println ("\t|    Efron-Stein-Steele Variance Upper Bound    |");

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

  262.         for (int iSampleSize : aiSampleSize)
  263.             EfronSteinSteeleRun (
  264.                 bin,
  265.                 iSampleSize,
  266.                 iNumSet
  267.             );

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

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

  270.         System.out.println ("\t|    Pivoted Differences Variance Upper Bound   |");

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

  272.         for (int iSampleSize : aiSampleSize)
  273.             PivotDifferencesRun (
  274.                 bin,
  275.                 iSampleSize,
  276.                 iNumSet
  277.             );

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

  279.         EnvManager.TerminateEnv();
  280.     }
  281. }