BinaryVariateSumBound.java

  1. package org.drip.sample.efronstein;

  2. import org.drip.numerical.common.FormatUtil;
  3. import org.drip.sequence.custom.GlivenkoCantelliUniformDeviation;
  4. import org.drip.sequence.functional.*;
  5. import org.drip.sequence.metrics.SingleSequenceAgnosticMetrics;
  6. import org.drip.sequence.random.*;
  7. import org.drip.service.env.EnvManager;

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

  11. /*!
  12.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  16.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  17.  *
  18.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  19.  *      calculations, valuation adjustment, and portfolio construction within and across fixed income,
  20.  *      credit, commodity, equity, FX, and structured products.
  21.  *  
  22.  *      https://lakshmidrip.github.io/DROP/
  23.  *  
  24.  *  DROP is composed of three 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
  42.  *  - Numerical Optimizer
  43.  *  - Spline Builder
  44.  *  - Algorithm Support
  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.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  52.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  53.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  54.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  55.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  56.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  57.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  58.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  59.  *
  60.  *  Licensed under the Apache License, Version 2.0 (the "License");
  61.  *      you may not use this file except in compliance with the License.
  62.  *  
  63.  *  You may obtain a copy of the License at
  64.  *      http://www.apache.org/licenses/LICENSE-2.0
  65.  *  
  66.  *  Unless required by applicable law or agreed to in writing, software
  67.  *      distributed under the License is distributed on an "AS IS" BASIS,
  68.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  69.  *  
  70.  *  See the License for the specific language governing permissions and
  71.  *      limitations under the License.
  72.  */

  73. /**
  74.  * <i>BinaryVariateSumBound</i> demonstrates the Computation of the Probabilistic Bounds for the Realization
  75.  * of the Values of a Multivariate Function over Random Sequence Values (in this case, sum of the independent
  76.  * Random Variates) using Variants of the Efron-Stein Methodology.
  77.  *  
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  81.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  82.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  83.  *      <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>
  84.  *  </ul>
  85.  * <br><br>
  86.  *
  87.  * @author Lakshmi Krishnamurthy
  88.  */

  89. public class BinaryVariateSumBound {

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

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

  101.         return aSSAM;
  102.     }

  103.     private static final GlivenkoCantelliUniformDeviation BinarySumFunction (
  104.         final Binary binarySequenceGenerator,
  105.         final int iNumVariate)
  106.         throws Exception
  107.     {
  108.         double[] adblWeight = new double[iNumVariate];

  109.         for (int i = 0; i < iNumVariate; ++i)
  110.             adblWeight[i] = 1. / iNumVariate;

  111.         return new GlivenkoCantelliUniformDeviation (
  112.             new BinaryIdempotentUnivariateRandom (
  113.                 0.,
  114.                 null,
  115.                 1.,
  116.                 binarySequenceGenerator.positiveProbability()
  117.             ),
  118.             adblWeight
  119.         );
  120.     }

  121.     private static final void MartingaleDifferencesRun (
  122.         final Binary binarySequenceGenerator,
  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.                 binarySequenceGenerator,
  131.                 iNumSample
  132.             );

  133.             EfronSteinMetrics esam = new EfronSteinMetrics (
  134.                 BinarySumFunction (
  135.                     binarySequenceGenerator,
  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 binarySequenceGenerator,
  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.                 binarySequenceGenerator,
  155.                 iNumSample
  156.             );

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

  164.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  165.                 binarySequenceGenerator,
  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 binarySequenceGenerator,
  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.                 binarySequenceGenerator,
  183.                 iNumSample
  184.             );

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

  192.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  193.                 binarySequenceGenerator,
  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 binarySequenceGenerator,
  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 = BinarySumFunction (
  210.                 binarySequenceGenerator,
  211.                 iNumSample
  212.             );

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

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

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

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

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

  226.     private static final void BoundedDifferencesRun (
  227.         final Binary binarySequenceGenerator,
  228.         final int iNumSample,
  229.         final int iNumSet)
  230.         throws Exception
  231.     {
  232.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  233.         for (int j = 0; j < iNumSet; ++j) {
  234.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  235.                 binarySequenceGenerator,
  236.                 iNumSample
  237.             );

  238.             EfronSteinMetrics esam = new EfronSteinMetrics (
  239.                 BinarySumFunction (
  240.                     binarySequenceGenerator,
  241.                     iNumSample
  242.                 ),
  243.                 aSSAM
  244.             );

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

  246.             strDump += FormatUtil.FormatDouble (esam.boundedVarianceUpperBound(), 1, 3, 1.);
  247.         }

  248.         System.out.println (strDump + " |");
  249.     }

  250.     public static final void main (
  251.         final String[] astrArgs)
  252.         throws Exception
  253.     {
  254.         EnvManager.InitEnv ("");

  255.         int iNumSet = 5;

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

  259.         Binary bin = new Binary (0.7);

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

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

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

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

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

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

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

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

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

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

  280.         aiSampleSize = new int[] {
  281.             3, 10, 25, 50, 75, 99
  282.         };

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

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

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

  286.         for (int iSampleSize : aiSampleSize)
  287.             EfronSteinSteeleRun (
  288.                 bin,
  289.                 iSampleSize,
  290.                 iNumSet
  291.             );

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

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

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

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

  296.         for (int iSampleSize : aiSampleSize)
  297.             PivotDifferencesRun (
  298.                 bin,
  299.                 iSampleSize,
  300.                 iNumSet
  301.             );

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

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

  304.         System.out.println ("\t|   Bounded Differences Variance Upper Bound    |");

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

  306.         for (int iSampleSize : aiSampleSize)
  307.             BoundedDifferencesRun (
  308.                 bin,
  309.                 iSampleSize,
  310.                 iNumSet
  311.             );

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

  313.         EnvManager.TerminateEnv();
  314.     }
  315. }