KernelDensityL1Bound.java

  1. package org.drip.sample.efronstein;

  2. import org.drip.function.r1tor1.FlatUnivariate;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.sequence.custom.KernelDensityEstimationL1;
  5. import org.drip.sequence.functional.*;
  6. import org.drip.sequence.metrics.SingleSequenceAgnosticMetrics;
  7. import org.drip.sequence.random.*;
  8. import org.drip.service.env.EnvManager;

  9. /*
  10.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  11.  */

  12. /*!
  13.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  16.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  17.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  18.  *
  19.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  20.  *      calculations, valuation adjustment, and portfolio construction within and across fixed income,
  21.  *      credit, commodity, equity, FX, and structured products.
  22.  *  
  23.  *      https://lakshmidrip.github.io/DROP/
  24.  *  
  25.  *  DROP is composed of three modules:
  26.  *  
  27.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  28.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  29.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  30.  *
  31.  *  DROP Analytics Core implements libraries for the following:
  32.  *  - Fixed Income Analytics
  33.  *  - Asset Backed Analytics
  34.  *  - XVA Analytics
  35.  *  - Exposure and Margin Analytics
  36.  *
  37.  *  DROP Portfolio Core implements libraries for the following:
  38.  *  - Asset Allocation Analytics
  39.  *  - Transaction Cost Analytics
  40.  *
  41.  *  DROP Numerical Core implements libraries for the following:
  42.  *  - Statistical Learning
  43.  *  - Numerical Optimizer
  44.  *  - Spline Builder
  45.  *  - Algorithm Support
  46.  *
  47.  *  Documentation for DROP is Spread Over:
  48.  *
  49.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  50.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  51.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  52.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  53.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  54.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  55.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  56.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  57.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  58.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  59.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  60.  *
  61.  *  Licensed under the Apache License, Version 2.0 (the "License");
  62.  *      you may not use this file except in compliance with the License.
  63.  *  
  64.  *  You may obtain a copy of the License at
  65.  *      http://www.apache.org/licenses/LICENSE-2.0
  66.  *  
  67.  *  Unless required by applicable law or agreed to in writing, software
  68.  *      distributed under the License is distributed on an "AS IS" BASIS,
  69.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  70.  *  
  71.  *  See the License for the specific language governing permissions and
  72.  *      limitations under the License.
  73.  */

  74. /**
  75.  * <i>KernelDensityL1Bound</i> demonstrates the Computation of the Probabilistic Bounds for the L<sub>1</sub>
  76.  * Errors of Kernel Density Estimation using Variants of the 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 KernelDensityL1Bound {

  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 KernelDensityEstimationL1 KernelDensityL1Function (
  104.         final int iNumVariate)
  105.         throws Exception
  106.     {
  107.         return new KernelDensityEstimationL1 (
  108.             new FlatUnivariate (0.),
  109.             1.,
  110.             iNumVariate,
  111.             new FlatUnivariate (-2.)
  112.         );
  113.     }

  114.     private static final void MartingaleDifferencesRun (
  115.         final Binary bsg,
  116.         final int iNumSample,
  117.         final int iNumSet)
  118.         throws Exception
  119.     {
  120.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  121.         for (int j = 0; j < iNumSet; ++j) {
  122.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  123.                 bsg,
  124.                 iNumSample
  125.             );

  126.             EfronSteinMetrics esam = new EfronSteinMetrics (
  127.                 KernelDensityL1Function (iNumSample),
  128.                 aSSAM
  129.             );

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

  131.             strDump += FormatUtil.FormatDouble (esam.martingaleVarianceUpperBound(), 1, 3, 1.);
  132.         }

  133.         System.out.println (strDump + " |");
  134.     }

  135.     private static final void GhostVariateVarianceRun (
  136.         final Binary bsg,
  137.         final int iNumSample,
  138.         final int iNumSet)
  139.         throws Exception
  140.     {
  141.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  142.         for (int j = 0; j < iNumSet; ++j) {
  143.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  144.                 bsg,
  145.                 iNumSample
  146.             );

  147.             EfronSteinMetrics esam = new EfronSteinMetrics (
  148.                 KernelDensityL1Function (iNumSample),
  149.                 aSSAM
  150.             );

  151.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  152.                 bsg,
  153.                 iNumSample
  154.             );

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

  156.             strDump += FormatUtil.FormatDouble (esam.ghostVarianceUpperBound (aSSAMGhost), 1, 3, 1.);
  157.         }

  158.         System.out.println (strDump + " |");
  159.     }

  160.     private static final void EfronSteinSteeleRun (
  161.         final Binary bsg,
  162.         final int iNumSample,
  163.         final int iNumSet)
  164.         throws Exception
  165.     {
  166.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  167.         for (int j = 0; j < iNumSet; ++j) {
  168.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  169.                 bsg,
  170.                 iNumSample
  171.             );

  172.             EfronSteinMetrics esam = new EfronSteinMetrics (
  173.                 KernelDensityL1Function (iNumSample),
  174.                 aSSAM
  175.             );

  176.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  177.                 bsg,
  178.                 iNumSample
  179.             );

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

  181.             strDump += FormatUtil.FormatDouble (esam.efronSteinSteeleBound (aSSAMGhost), 1, 3, 1.);
  182.         }

  183.         System.out.println (strDump + " |");
  184.     }

  185.     private static final void PivotDifferencesRun (
  186.         final Binary bsg,
  187.         final int iNumSample,
  188.         final int iNumSet)
  189.         throws Exception
  190.     {
  191.         String strDump = "\t| " + FormatUtil.FormatDouble (iNumSample, 2, 0, 1.) + " => ";

  192.         for (int j = 0; j < iNumSet; ++j) {
  193.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  194.                 bsg,
  195.                 iNumSample
  196.             );

  197.             EfronSteinMetrics esam = new EfronSteinMetrics (
  198.                 KernelDensityL1Function (iNumSample),
  199.                 aSSAM
  200.             );

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

  202.             strDump += FormatUtil.FormatDouble (esam.pivotVarianceUpperBound (new FlatMultivariateRandom (0.)), 1, 3, 1.);
  203.         }

  204.         System.out.println (strDump + " |");
  205.     }

  206.     public static final void main (
  207.         final String[] astrArgs)
  208.         throws Exception
  209.     {
  210.         EnvManager.InitEnv ("");

  211.         int iNumSet = 5;

  212.         int[] aiSampleSize = new int[] {
  213.             3, 10, 25, 50
  214.         };

  215.         Binary bin = new Binary (0.7);

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

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

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

  219.         for (int iSampleSize : aiSampleSize)
  220.             MartingaleDifferencesRun (
  221.                 bin,
  222.                 iSampleSize,
  223.                 iNumSet
  224.             );

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

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

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

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

  229.         for (int iSampleSize : aiSampleSize)
  230.             GhostVariateVarianceRun (
  231.                 bin,
  232.                 iSampleSize,
  233.                 iNumSet
  234.             );

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

  236.         aiSampleSize = new int[] {
  237.             3, 10, 25, 50, 75, 99
  238.         };

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

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

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

  242.         for (int iSampleSize : aiSampleSize)
  243.             EfronSteinSteeleRun (
  244.                 bin,
  245.                 iSampleSize,
  246.                 iNumSet
  247.             );

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

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

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

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

  252.         for (int iSampleSize : aiSampleSize)
  253.             PivotDifferencesRun (
  254.                 bin,
  255.                 iSampleSize,
  256.                 iNumSet
  257.             );

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

  259.         EnvManager.TerminateEnv();
  260.     }
  261. }