LongestCommonSubsequenceBound.java

  1. package org.drip.sample.efronstein;

  2. import org.drip.numerical.common.FormatUtil;
  3. import org.drip.sequence.custom.LongestCommonSubsequence;
  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>LongestCommonSubsequenceBound</i> demonstrates the Computation of the Probabilistic Bounds for the
  75.  * Longest Common Subsequence across each half over the Random Sequence Values using Variants of the
  76.  * 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 LongestCommonSubsequenceBound {

  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 void MartingaleDifferencesRun (
  104.         final UnivariateSequenceGenerator rsg,
  105.         final MultivariateRandom func,
  106.         final int iNumSample,
  107.         final int iNumSet)
  108.         throws Exception
  109.     {
  110.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  199.     private static final void BoundedDifferencesRun (
  200.         final UnivariateSequenceGenerator rsg,
  201.         final MultivariateRandom func,
  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.                 rsg,
  210.                 iNumSample
  211.             );

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

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

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

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

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

  226.         int iNumSet = 5;

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

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

  234.         MultivariateRandom func = new LongestCommonSubsequence();

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

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

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

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

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

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

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

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

  249.         for (int iSampleSize : aiSampleSize)
  250.             GhostVariateVarianceRun (
  251.                 bu,
  252.                 func,
  253.                 iSampleSize,
  254.                 iNumSet
  255.             );

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

  257.         aiSampleSize = new int[] {
  258.             3, 10, 25, 50, 75, 99
  259.         };

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

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

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

  263.         for (int iSampleSize : aiSampleSize)
  264.             EfronSteinSteeleRun (
  265.                 bu,
  266.                 func,
  267.                 iSampleSize,
  268.                 iNumSet
  269.             );

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

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

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

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

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

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

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

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

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

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

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

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