UnitRandomSequenceBound.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.  * SingleRandomSequenceBound demonstrates the Computation of the Probabilistic Bounds for a Sample Random
  56.  *  Sequence.
  57.  *
  58.  * @author Lakshmi Krishnamurthy
  59.  */

  60. public class UnitRandomSequenceBound {

  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| SIZE ||               <-               TOLERANCES               ->               |");

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

  71.     private static final void ChernoffBinomialBounds (
  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.             UnitSequenceAgnosticMetrics ssamDist = (UnitSequenceAgnosticMetrics) iidsg.sequence (
  80.                 iSampleSize,
  81.                 dist
  82.             );

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

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

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

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

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

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

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

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

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

  120.             for (double dblTolerance : adblTolerance)
  121.                 strDump += FormatUtil.FormatDouble (ssamDist.karpHagerupRubBounds (dblTolerance).upper(), 1, 9, 1.) + " | ";

  122.             System.out.println (strDump);
  123.         }
  124.     }

  125.     private static final void KarpHagerupRubLowerBounds (
  126.         final UnivariateSequenceGenerator iidsg,
  127.         final R1Univariate dist,
  128.         final int[] aiSampleSize,
  129.         final double[] adblTolerance)
  130.         throws Exception
  131.     {
  132.         for (int iSampleSize : aiSampleSize) {
  133.             UnitSequenceAgnosticMetrics ssamDist = (UnitSequenceAgnosticMetrics) iidsg.sequence (
  134.                 iSampleSize,
  135.                 dist
  136.             );

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

  138.             for (double dblTolerance : adblTolerance)
  139.                 strDump += FormatUtil.FormatDouble (ssamDist.karpHagerupRubBounds (dblTolerance).lower(), 1, 9, 1.) + " | ";

  140.             System.out.println (strDump);
  141.         }
  142.     }

  143.     public static void main (
  144.         final String[] args)
  145.         throws Exception
  146.     {
  147.         EnvManager.InitEnv ("");

  148.         BoundedUniform uniformRandom = new BoundedUniform (
  149.             0.,
  150.             1.
  151.         );

  152.         R1Uniform uniformDistribution = new R1Uniform (
  153.             0.,
  154.             1.
  155.         );

  156.         int[] aiSampleSize = new int[] {
  157.             10, 20, 50, 100, 250
  158.         };

  159.         double[] adblTolerance = new double[] {
  160.             0.01, 0.03, 0.05, 0.07, 0.10
  161.         };

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

  163.         ChernoffBinomialBounds (
  164.             uniformRandom,
  165.             uniformDistribution,
  166.             aiSampleSize,
  167.             adblTolerance
  168.         );

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

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

  171.         ChernoffBinomialBounds (
  172.             uniformRandom,
  173.             null,
  174.             aiSampleSize,
  175.             adblTolerance
  176.         );

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

  178.         Head ("\t|       POISSON CHERNOFF-BINOMIAL BOUNDS  -   METRICS FROM UNDERLYING GENERATOR    |");

  179.         PoissonChernoffBinomialBounds (
  180.             uniformRandom,
  181.             uniformDistribution,
  182.             aiSampleSize,
  183.             adblTolerance
  184.         );

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

  186.         Head ("\t|       POISSON CHERNOFF-BINOMIAL BOUNDS  -  METRICS FROM EMPIRICAL DISTRIBUTION   |");

  187.         PoissonChernoffBinomialBounds (
  188.             uniformRandom,
  189.             null,
  190.             aiSampleSize,
  191.             adblTolerance
  192.         );

  193.         aiSampleSize = new int[] {
  194.             100, 200, 300, 500, 999
  195.         };

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

  197.         Head ("\t|         KARP-HAGERUP-RUB UPPER BOUNDS  -   METRICS FROM UNDERLYING GENERATOR     |");

  198.         KarpHagerupRubUpperBounds (
  199.             uniformRandom,
  200.             null,
  201.             aiSampleSize,
  202.             adblTolerance
  203.         );

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

  205.         Head ("\t|        KARP-HAGERUP-RUB UPPER BOUNDS  -   METRICS FROM EMPIRICAL DISTRIBUTION    |");

  206.         KarpHagerupRubUpperBounds (
  207.             uniformRandom,
  208.             null,
  209.             aiSampleSize,
  210.             adblTolerance
  211.         );

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

  213.         Head ("\t|         KARP-HAGERUP-RUB LOWER BOUNDS  -   METRICS FROM UNDERLYING GENERATOR     |");

  214.         KarpHagerupRubLowerBounds (
  215.             uniformRandom,
  216.             null,
  217.             aiSampleSize,
  218.             adblTolerance
  219.         );

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

  221.         Head ("\t|        KARP-HAGERUP-RUB LOWER BOUNDS  -   METRICS FROM EMPIRICAL DISTRIBUTION    |");

  222.         KarpHagerupRubLowerBounds (
  223.             uniformRandom,
  224.             null,
  225.             aiSampleSize,
  226.             adblTolerance
  227.         );

  228.         System.out.println ("\t|----------------------------------------------------------------------------------|");
  229.     }
  230. }