BoundedVariateSumBound.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>BoundedVariateSumBound</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 BoundedVariateSumBound {

  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 BoundedSumFunction (
  104.         final BoundedUniform bu,
  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.;

  111.         return new GlivenkoCantelliUniformDeviation (
  112.             new BoundedIdempotentUnivariateRandom (
  113.                 0.,
  114.                 null,
  115.                 bu.upperBound() - bu.lowerBound()
  116.             ),
  117.             adblWeight
  118.         );
  119.     }

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

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

  132.             EfronSteinMetrics esam = new EfronSteinMetrics (
  133.                 BoundedSumFunction (
  134.                     bu,
  135.                     iNumSample
  136.                 ),
  137.                 aSSAM
  138.             );

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

  140.             strDump += FormatUtil.FormatDouble (esam.martingaleVarianceUpperBound(), 2, 2, 1.);
  141.         }

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

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

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

  156.             EfronSteinMetrics esam = new EfronSteinMetrics (
  157.                 BoundedSumFunction (
  158.                     bu,
  159.                     iNumSample
  160.                 ),
  161.                 aSSAM
  162.             );

  163.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  164.                 bu,
  165.                 iNumSample
  166.             );

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

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

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

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

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

  184.             EfronSteinMetrics esam = new EfronSteinMetrics (
  185.                 BoundedSumFunction (
  186.                     bu,
  187.                     iNumSample
  188.                 ),
  189.                 aSSAM
  190.             );

  191.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  192.                 bu,
  193.                 iNumSample
  194.             );

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

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

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

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

  207.         for (int j = 0; j < iNumSet; ++j) {
  208.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  209.                 bu,
  210.                 iNumSample
  211.             );

  212.             MultivariateRandom func = BoundedSumFunction (
  213.                 bu,
  214.                 iNumSample
  215.             );

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

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

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

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

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

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

  237.             EfronSteinMetrics esam = new EfronSteinMetrics (
  238.                 BoundedSumFunction (
  239.                     bu,
  240.                     iNumSample
  241.                 ),
  242.                 aSSAM
  243.             );

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

  245.             strDump += FormatUtil.FormatDouble (esam.boundedVarianceUpperBound(), 2, 2, 1.);
  246.         }

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

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

  254.         int iNumSet = 5;

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

  258.         BoundedUniform bu = new BoundedUniform (
  259.             0.,
  260.             1.
  261.         );

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

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

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

  265.         for (int iSampleSize : aiSampleSize)
  266.             MartingaleDifferencesRun (
  267.                 bu,
  268.                 iSampleSize,
  269.                 iNumSet
  270.             );

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

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

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

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

  275.         for (int iSampleSize : aiSampleSize)
  276.             GhostVariateVarianceRun (
  277.                 bu,
  278.                 iSampleSize,
  279.                 iNumSet
  280.             );

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

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

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

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

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

  288.         for (int iSampleSize : aiSampleSize)
  289.             EfronSteinSteeleRun (
  290.                 bu,
  291.                 iSampleSize,
  292.                 iNumSet
  293.             );

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

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

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

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

  298.         for (int iSampleSize : aiSampleSize)
  299.             PivotDifferencesRun (
  300.                 bu,
  301.                 iSampleSize,
  302.                 iNumSet
  303.             );

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

  305.         double[] adblVariateBound = new double[aiSampleSize[aiSampleSize.length - 1]];

  306.         for (int i = 0; i < adblVariateBound.length; ++i)
  307.             adblVariateBound[i] = bu.upperBound() - bu.lowerBound();

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

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

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

  311.         for (int iSampleSize : aiSampleSize)
  312.             BoundedDifferencesRun (
  313.                 bu,
  314.                 iSampleSize,
  315.                 iNumSet
  316.             );

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

  318.         EnvManager.TerminateEnv();
  319.     }
  320. }