MinimumBinPackingBound.java

  1. package org.drip.sample.efronstein;

  2. import org.drip.numerical.common.FormatUtil;
  3. import org.drip.sequence.custom.BinPacking;
  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>MinimumBinPackingBound</i> demonstrates the Computation of the Probabilistic Bounds for the Minimum
  75.  * Number of Packing Bins over a Random Sequence Values using Variants of the Efron-Stein Methodology.
  76.  *  
  77.  * <br><br>
  78.  *  <ul>
  79.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  80.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  81.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  82.  *      <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>
  83.  *  </ul>
  84.  * <br><br>
  85.  *
  86.  * @author Lakshmi Krishnamurthy
  87.  */

  88. public class MinimumBinPackingBound {

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

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

  100.         return aSSAM;
  101.     }

  102.     private static final void MartingaleDifferencesRun (
  103.         final UnivariateSequenceGenerator rsg,
  104.         final MultivariateRandom func,
  105.         final int iNumSample,
  106.         final int iNumSet)
  107.         throws Exception
  108.     {
  109.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  110.         for (int j = 0; j < iNumSet; ++j) {
  111.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  112.                 rsg,
  113.                 iNumSample
  114.             );

  115.             EfronSteinMetrics esam = new EfronSteinMetrics (
  116.                 func,
  117.                 aSSAM
  118.             );

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

  120.             strDump += FormatUtil.FormatDouble (esam.martingaleVarianceUpperBound(), 2, 2, 1.);
  121.         }

  122.         System.out.println (strDump + " |");
  123.     }

  124.     private static final void GhostVariateVarianceRun (
  125.         final UnivariateSequenceGenerator rsg,
  126.         final MultivariateRandom func,
  127.         final int iNumSample,
  128.         final int iNumSet)
  129.         throws Exception
  130.     {
  131.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  132.         for (int j = 0; j < iNumSet; ++j) {
  133.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  134.                 rsg,
  135.                 iNumSample
  136.             );

  137.             EfronSteinMetrics esam = new EfronSteinMetrics (
  138.                 func,
  139.                 aSSAM
  140.             );

  141.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  142.                 rsg,
  143.                 iNumSample
  144.             );

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

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

  148.         System.out.println (strDump + " |");
  149.     }

  150.     private static final void EfronSteinSteeleRun (
  151.         final UnivariateSequenceGenerator rsg,
  152.         final MultivariateRandom func,
  153.         final int iNumSample,
  154.         final int iNumSet)
  155.         throws Exception
  156.     {
  157.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  158.         for (int j = 0; j < iNumSet; ++j) {
  159.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  160.                 rsg,
  161.                 iNumSample
  162.             );

  163.             EfronSteinMetrics esam = new EfronSteinMetrics (
  164.                 func,
  165.                 aSSAM
  166.             );

  167.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  168.                 rsg,
  169.                 iNumSample
  170.             );

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

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

  174.         System.out.println (strDump + " |");
  175.     }

  176.     private static final void PivotDifferencesRun (
  177.         final UnivariateSequenceGenerator rsg,
  178.         final MultivariateRandom func,
  179.         final int iNumSample,
  180.         final int iNumSet)
  181.         throws Exception
  182.     {
  183.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  184.         for (int j = 0; j < iNumSet; ++j) {
  185.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  186.                 rsg,
  187.                 iNumSample
  188.             );

  189.             EfronSteinMetrics esam = new EfronSteinMetrics (
  190.                 func,
  191.                 aSSAM
  192.             );

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

  194.             strDump += FormatUtil.FormatDouble (esam.pivotVarianceUpperBound (func), 2, 2, 1.);
  195.         }

  196.         System.out.println (strDump + " |");
  197.     }

  198.     private static final void BoundedDifferencesRun (
  199.         final UnivariateSequenceGenerator rsg,
  200.         final MultivariateRandom func,
  201.         final int iNumSample,
  202.         final int iNumSet)
  203.         throws Exception
  204.     {
  205.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

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

  211.             EfronSteinMetrics esam = new EfronSteinMetrics (
  212.                 func,
  213.                 aSSAM
  214.             );

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

  216.             strDump += FormatUtil.FormatDouble (esam.boundedVarianceUpperBound(), 2, 2, 1.);
  217.         }

  218.         System.out.println (strDump + " |");
  219.     }

  220.     public static final void main (
  221.         final String[] astrArgs)
  222.         throws Exception
  223.     {
  224.         EnvManager.InitEnv ("");

  225.         int iNumSet = 5;

  226.         int[] aiSampleSize = new int[] {
  227.             3, 10, 25
  228.         };

  229.         BoundedUniform bu = new BoundedUniform (
  230.             0.,
  231.             1.
  232.         );

  233.         MultivariateRandom func = BinPacking.MinimumNumberOfBins();

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

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

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

  237.         for (int iSampleSize : aiSampleSize)
  238.             MartingaleDifferencesRun (
  239.                 bu,
  240.                 func,
  241.                 iSampleSize,
  242.                 iNumSet
  243.             );

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

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

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

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

  248.         for (int iSampleSize : aiSampleSize)
  249.             GhostVariateVarianceRun (
  250.                 bu,
  251.                 func,
  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.                 bu,
  265.                 func,
  266.                 iSampleSize,
  267.                 iNumSet
  268.             );

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

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

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

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

  273.         for (int iSampleSize : aiSampleSize)
  274.             PivotDifferencesRun (
  275.                 bu,
  276.                 func,
  277.                 iSampleSize,
  278.                 iNumSet
  279.             );

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

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

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

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

  284.         for (int iSampleSize : aiSampleSize)
  285.             BoundedDifferencesRun (
  286.                 bu,
  287.                 func,
  288.                 iSampleSize,
  289.                 iNumSet
  290.             );

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

  292.         EnvManager.TerminateEnv();
  293.     }
  294. }