SingleRandomSequenceBound.java

  1. package org.drip.sample.sequence;

  2. import org.drip.function.definition.R1ToR1;
  3. import org.drip.function.r1tor1.*;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.sequence.metrics.*;
  6. import org.drip.sequence.random.BoundedUniform;
  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 SingleRandomSequenceBound {

  61.     private static final void MarkovBound (
  62.         final SingleSequenceAgnosticMetrics sm,
  63.         final double dblLevel,
  64.         final R1ToR1 au)
  65.         throws Exception
  66.     {
  67.         System.out.println (
  68.             (null == au ? "\tMarkov Base Bound        [" :  "\tMarkov Function Bound    [") +
  69.             FormatUtil.FormatDouble (dblLevel, 1, 2, 1.) + "] : " +
  70.             FormatUtil.FormatDouble (sm.markovUpperProbabilityBound (dblLevel, au), 1, 4, 1.) + " |" +
  71.             FormatUtil.FormatDouble (1. - dblLevel, 1, 4, 1.)
  72.         );
  73.     }

  74.     private static final void ChebyshevBound (
  75.         final SingleSequenceAgnosticMetrics sm,
  76.         final double dblLevel)
  77.         throws Exception
  78.     {
  79.         System.out.println (
  80.             "\tChebyshev Bound          [" +
  81.             FormatUtil.FormatDouble (dblLevel, 1, 2, 1.) + "] : " +
  82.             FormatUtil.FormatDouble (sm.chebyshevBound (dblLevel).lower(), 1, 4, 1.) + " |" +
  83.             FormatUtil.FormatDouble (sm.chebyshevBound (dblLevel).upper(), 1, 4, 1.) + " |" +
  84.             FormatUtil.FormatDouble (1. - 2. * dblLevel, 1, 4, 1.)
  85.         );
  86.     }

  87.     private static final void ChebyshevCantelliBound (
  88.         final SingleSequenceAgnosticMetrics sm,
  89.         final double dblLevel)
  90.         throws Exception
  91.     {
  92.         System.out.println (
  93.             "\tChebyshev Cantelli Bound [" +
  94.             FormatUtil.FormatDouble (dblLevel, 1, 2, 1.) + "] : " +
  95.             "        |" +
  96.             FormatUtil.FormatDouble (sm.chebyshevCantelliBound (dblLevel).upper(), 1, 4, 1.) + " |" +
  97.             FormatUtil.FormatDouble (1. - dblLevel, 1, 4, 1.)
  98.         );
  99.     }

  100.     private static final void CentralMomentBound (
  101.         final SingleSequenceAgnosticMetrics sm,
  102.         final double dblLevel,
  103.         final int iMoment)
  104.         throws Exception
  105.     {
  106.         System.out.println (
  107.             "\tMoment #" + iMoment + " Bound          [" +
  108.             FormatUtil.FormatDouble (dblLevel, 1, 2, 1.) + "] : " +
  109.             FormatUtil.FormatDouble (sm.centralMomentBound (dblLevel, iMoment).lower(), 1, 4, 1.) + " |" +
  110.             FormatUtil.FormatDouble (sm.centralMomentBound (dblLevel, iMoment).upper(), 1, 4, 1.) + " |" +
  111.             FormatUtil.FormatDouble (1. - 2. * dblLevel, 1, 4, 1.)
  112.         );
  113.     }

  114.     private static final void MarkovBound (
  115.         final SingleSequenceAgnosticMetrics sm,
  116.         final R1ToR1 au)
  117.         throws Exception
  118.     {
  119.         MarkovBound (sm, 0.20, au);

  120.         MarkovBound (sm, 0.40, au);

  121.         MarkovBound (sm, 0.59, au);

  122.         MarkovBound (sm, 0.79, au);

  123.         MarkovBound (sm, 0.99, au);
  124.     }

  125.     private static final void ChebyshevBound (
  126.         final SingleSequenceAgnosticMetrics sm)
  127.         throws Exception
  128.     {
  129.         ChebyshevBound (sm, 0.20);

  130.         ChebyshevBound (sm, 0.25);

  131.         ChebyshevBound (sm, 0.30);

  132.         ChebyshevBound (sm, 0.35);

  133.         ChebyshevBound (sm, 0.40);
  134.     }

  135.     private static final void ChebyshevCantelliBound (
  136.         final SingleSequenceAgnosticMetrics sm)
  137.         throws Exception
  138.     {
  139.         ChebyshevCantelliBound (sm, 0.20);

  140.         ChebyshevCantelliBound (sm, 0.25);

  141.         ChebyshevCantelliBound (sm, 0.30);

  142.         ChebyshevCantelliBound (sm, 0.35);

  143.         ChebyshevCantelliBound (sm, 0.40);
  144.     }

  145.     private static final void CentralMomentBound (
  146.         final SingleSequenceAgnosticMetrics sm,
  147.         final int iMoment)
  148.         throws Exception
  149.     {
  150.         CentralMomentBound (sm, 0.20, iMoment);

  151.         CentralMomentBound (sm, 0.25, iMoment);

  152.         CentralMomentBound (sm, 0.30, iMoment);

  153.         CentralMomentBound (sm, 0.35, iMoment);

  154.         CentralMomentBound (sm, 0.40, iMoment);
  155.     }

  156.     private static final void SequenceGenerationRun (
  157.         final SingleSequenceAgnosticMetrics sm)
  158.         throws Exception
  159.     {
  160.         System.out.println ("\tExpectation                      : " + FormatUtil.FormatDouble (sm.empiricalExpectation(), 1, 4, 1.));

  161.         System.out.println ("\tVariance                         : " + FormatUtil.FormatDouble (sm.empiricalVariance(), 1, 4, 1.));

  162.         System.out.println ("\t---------------------------------------------------");

  163.         MarkovBound (sm, new ExponentialTension (Math.E, 0.1));

  164.         System.out.println ("\t---------------------------------------------------");

  165.         MarkovBound (sm, new ExponentialTension (Math.E, 1.0));

  166.         System.out.println ("\t---------------------------------------------------");

  167.         MarkovBound (sm, new ExponentialTension (Math.E, 5.0));

  168.         System.out.println ("\t---------------------------------------------------");

  169.         MarkovBound (sm, null);

  170.         System.out.println ("\t---------------------------------------------------");

  171.         ChebyshevBound (sm);

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

  173.         ChebyshevCantelliBound (sm);

  174.         System.out.println ("\t---------------------------------------------------");

  175.         CentralMomentBound (sm, 3);

  176.         System.out.println ("\t---------------------------------------------------");

  177.         CentralMomentBound (sm, 4);

  178.         System.out.println ("\t---------------------------------------------------");

  179.         CentralMomentBound (sm, 5);
  180.     }

  181.     public static void main (
  182.         final String[] astrArgs)
  183.         throws Exception
  184.     {
  185.         EnvManager.InitEnv ("");

  186.         /* System.out.println ("\t---------------------------------------------------");

  187.         System.out.println ("\t|              BOUNDED GAUSSIAN RUN               |");

  188.         System.out.println ("\t---------------------------------------------------");

  189.         SequenceGenerationRun (new BoundedGaussian (0.5, 1., 0., 1.).sequence (50000, null));

  190.         System.out.println ("\t---------------------------------------------------");

  191.         System.out.println(); */

  192.         System.out.println ("\t---------------------------------------------------");

  193.         System.out.println ("\t|              BOUNDED UNIFORM RUN                |");

  194.         System.out.println ("\t---------------------------------------------------");

  195.         SequenceGenerationRun (new BoundedUniform (0., 1.).sequence (50000, null));

  196.         System.out.println ("\t---------------------------------------------------");
  197.     }
  198. }