IIDSequenceSumBound.java

  1. package org.drip.sample.sequence;

  2. import org.drip.measure.continuous.R1Univariate;
  3. import org.drip.measure.lebesgue.R1Uniform;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.sequence.metrics.*;
  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) 2018 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  16.  *
  17.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  18.  *      libraries targeting analysts and developers
  19.  *      https://lakshmidrip.github.io/DRIP/
  20.  *  
  21.  *  DRIP is composed of four main libraries:
  22.  *  
  23.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  24.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  25.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  26.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  27.  *
  28.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  29.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  30.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  31.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  32.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  33.  *
  34.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  35.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  36.  *
  37.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  38.  *
  39.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  40.  *
  41.  *  Licensed under the Apache License, Version 2.0 (the "License");
  42.  *      you may not use this file except in compliance with the License.
  43.  *  
  44.  *  You may obtain a copy of the License at
  45.  *      http://www.apache.org/licenses/LICENSE-2.0
  46.  *  
  47.  *  Unless required by applicable law or agreed to in writing, software
  48.  *      distributed under the License is distributed on an "AS IS" BASIS,
  49.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  50.  *  
  51.  *  See the License for the specific language governing permissions and
  52.  *      limitations under the License.
  53.  */

  54. /**
  55.  * IIDSequenceSumBound demonstrates the Computation of the Different Probabilistic Bounds for Sums of i.i.d.
  56.  *  Random Sequences.
  57.  *
  58.  * @author Lakshmi Krishnamurthy
  59.  */

  60. public class IIDSequenceSumBound {

  61.     private static final void Head (
  62.         final String strHeader)
  63.     {
  64.         System.out.println();

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

  66.         System.out.println (strHeader);

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

  68.         System.out.println ("\t|   SAMPLE  ||               <-               TOLERANCES               ->               |");

  69.         System.out.println ("\t|---------------------------------------------------------------------------------------|");
  70.     }

  71.     private static final void WeakLawBounds (
  72.         final UnivariateSequenceGenerator iidsg,
  73.         final R1Univariate dist,
  74.         final int[] aiSampleSize,
  75.         final double[] adblTolerance)
  76.         throws Exception
  77.     {
  78.         for (int iSampleSize : aiSampleSize) {
  79.             SingleSequenceAgnosticMetrics ssamDist = iidsg.sequence (
  80.                 iSampleSize,
  81.                 dist
  82.             );

  83.             String strDump = "\t| " + FormatUtil.FormatDouble (iSampleSize, 8, 0, 1) + " => ";

  84.             for (double dblTolerance : adblTolerance)
  85.                 strDump += FormatUtil.FormatDouble (ssamDist.weakLawAverageBounds (dblTolerance).upper(), 1, 9, 1.) + " | ";

  86.             System.out.println (strDump);
  87.         }
  88.     }

  89.     private static final void ChernoffHoeffdingBounds (
  90.         final UnivariateSequenceGenerator iidsg,
  91.         final R1Univariate dist,
  92.         final double dblSupport,
  93.         final int[] aiSampleSize,
  94.         final double[] adblTolerance)
  95.         throws Exception
  96.     {
  97.         for (int iSampleSize : aiSampleSize) {
  98.             BoundedSequenceAgnosticMetrics ssamDist = (BoundedSequenceAgnosticMetrics) iidsg.sequence (
  99.                 iSampleSize,
  100.                 dist
  101.             );

  102.             String strDump = "\t| " + FormatUtil.FormatDouble (iSampleSize, 8, 0, 1) + " => ";

  103.             for (double dblTolerance : adblTolerance)
  104.                 strDump += FormatUtil.FormatDouble (ssamDist.chernoffHoeffdingAverageBounds (dblTolerance).upper(), 1, 9, 1.) + " | ";

  105.             System.out.println (strDump);
  106.         }
  107.     }

  108.     private static final void BennettBounds (
  109.         final UnivariateSequenceGenerator iidsg,
  110.         final R1Univariate dist,
  111.         final double dblSupport,
  112.         final int[] aiSampleSize,
  113.         final double[] adblTolerance)
  114.         throws Exception
  115.     {
  116.         for (int iSampleSize : aiSampleSize) {
  117.             BoundedSequenceAgnosticMetrics ssamDist = (BoundedSequenceAgnosticMetrics) iidsg.sequence (
  118.                 iSampleSize,
  119.                 dist
  120.             );

  121.             String strDump = "\t| " + FormatUtil.FormatDouble (iSampleSize, 8, 0, 1) + " => ";

  122.             for (double dblTolerance : adblTolerance)
  123.                 strDump += FormatUtil.FormatDouble (ssamDist.bennettAverageBounds (dblTolerance).upper(), 1, 9, 1.) + " | ";

  124.             System.out.println (strDump);
  125.         }
  126.     }

  127.     private static final void BernsteinBounds (
  128.         final UnivariateSequenceGenerator iidsg,
  129.         final R1Univariate dist,
  130.         final double dblSupport,
  131.         final int[] aiSampleSize,
  132.         final double[] adblTolerance)
  133.         throws Exception
  134.     {
  135.         for (int iSampleSize : aiSampleSize) {
  136.             BoundedSequenceAgnosticMetrics ssamDist = (BoundedSequenceAgnosticMetrics) iidsg.sequence (
  137.                 iSampleSize,
  138.                 dist
  139.             );

  140.             String strDump = "\t| " + FormatUtil.FormatDouble (iSampleSize, 8, 0, 1) + " => ";

  141.             for (double dblTolerance : adblTolerance)
  142.                 strDump += FormatUtil.FormatDouble (ssamDist.bernsteinAverageBounds (dblTolerance).upper(), 1, 9, 1.) + " | ";

  143.             System.out.println (strDump);
  144.         }
  145.     }

  146.     public static void main (
  147.         final String[] args)
  148.         throws Exception
  149.     {
  150.         EnvManager.InitEnv ("");

  151.         BoundedUniform uniformRandom = new BoundedUniform (
  152.             0.,
  153.             1.
  154.         );

  155.         R1Uniform uniformDistribution = new R1Uniform (
  156.             0.,
  157.             1.
  158.         );

  159.         int[] aiSampleSize = new int[] {
  160.             50, 500, 5000, 50000, 500000, 5000000, 50000000
  161.         };

  162.         double[] adblTolerance = new double[] {
  163.             0.01, 0.03, 0.05, 0.07, 0.10
  164.         };

  165.         Head ("\t|         WEAK LAW OF LARGE NUMBERS     -      METRICS FROM UNDERLYING GENERATOR        |");

  166.         WeakLawBounds (
  167.             uniformRandom,
  168.             uniformDistribution,
  169.             aiSampleSize,
  170.             adblTolerance
  171.         );

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

  173.         Head ("\t|        WEAK LAW OF LARGE NUMBERS      -     METRICS FROM EMPIRICAL DISTRIBUTION       |");

  174.         WeakLawBounds (
  175.             uniformRandom,
  176.             null,
  177.             aiSampleSize,
  178.             adblTolerance
  179.         );

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

  181.         Head ("\t|         CHERNOFF-HOEFFDING BOUNDS      -     METRICS FROM UNDERLYING GENERATOR        |");

  182.         ChernoffHoeffdingBounds (
  183.             uniformRandom,
  184.             uniformDistribution,
  185.             uniformRandom.upperBound() - uniformRandom.lowerBound(),
  186.             aiSampleSize,
  187.             adblTolerance
  188.         );

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

  190.         Head ("\t|         CHERNOFF-HOEFFDING BOUNDS      -     METRICS FROM EMPIRICAL DISTRIBUTION      |");

  191.         ChernoffHoeffdingBounds (
  192.             uniformRandom,
  193.             null,
  194.             uniformRandom.upperBound() - uniformRandom.lowerBound(),
  195.             aiSampleSize,
  196.             adblTolerance
  197.         );

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

  199.         Head ("\t|              BENNETT BOUNDS      -      METRICS FROM UNDERLYING GENERATOR             |");

  200.         BennettBounds (
  201.             uniformRandom,
  202.             uniformDistribution,
  203.             uniformRandom.upperBound() - uniformRandom.lowerBound(),
  204.             aiSampleSize,
  205.             adblTolerance
  206.         );

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

  208.         Head ("\t|              BENNETT BOUNDS      -      METRICS FROM EMPIRICAL DISTRIBUTION           |");

  209.         BennettBounds (
  210.             uniformRandom,
  211.             null,
  212.             uniformRandom.upperBound() - uniformRandom.lowerBound(),
  213.             aiSampleSize,
  214.             adblTolerance
  215.         );

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

  217.         Head ("\t|             BERNSTEIN BOUNDS      -      METRICS FROM UNDERLYING GENERATOR            |");

  218.         BernsteinBounds (
  219.             uniformRandom,
  220.             uniformDistribution,
  221.             uniformRandom.upperBound() - uniformRandom.lowerBound(),
  222.             aiSampleSize,
  223.             adblTolerance
  224.         );

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

  226.         Head ("\t|            BERNSTEIN BOUNDS      -      METRICS FROM EMPIRICAL DISTRIBUTION           |");

  227.         BernsteinBounds (
  228.             uniformRandom,
  229.             uniformDistribution,
  230.             uniformRandom.upperBound() - uniformRandom.lowerBound(),
  231.             aiSampleSize,
  232.             adblTolerance
  233.         );

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