BinaryClassifierSupremumBound.java

  1. package org.drip.sample.classifier;

  2. import org.drip.learning.rxtor1.*;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.sequence.functional.FlatMultivariateRandom;
  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) 2020 Lakshmi Krishnamurthy
  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 analytics/risk, transaction cost analytics,
  20.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  21.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  22.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  23.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  24.  *      and computational support.
  25.  *  
  26.  *      https://lakshmidrip.github.io/DROP/
  27.  *  
  28.  *  DROP is composed of three modules:
  29.  *  
  30.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  31.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  32.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  33.  *
  34.  *  DROP Product Core implements libraries for the following:
  35.  *  - Fixed Income Analytics
  36.  *  - Loan Analytics
  37.  *  - Transaction Cost Analytics
  38.  *
  39.  *  DROP Portfolio Core implements libraries for the following:
  40.  *  - Asset Allocation Analytics
  41.  *  - Asset Liability Management Analytics
  42.  *  - Capital Estimation Analytics
  43.  *  - Exposure Analytics
  44.  *  - Margin Analytics
  45.  *  - XVA Analytics
  46.  *
  47.  *  DROP Computational Core implements libraries for the following:
  48.  *  - Algorithm Support
  49.  *  - Computation Support
  50.  *  - Function Analysis
  51.  *  - Model Validation
  52.  *  - Numerical Analysis
  53.  *  - Numerical Optimizer
  54.  *  - Spline Builder
  55.  *  - Statistical Learning
  56.  *
  57.  *  Documentation for DROP is Spread Over:
  58.  *
  59.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  60.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  61.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  62.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  63.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  64.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  65.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  66.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  67.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  68.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  69.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  70.  *
  71.  *  Licensed under the Apache License, Version 2.0 (the "License");
  72.  *      you may not use this file except in compliance with the License.
  73.  *  
  74.  *  You may obtain a copy of the License at
  75.  *      http://www.apache.org/licenses/LICENSE-2.0
  76.  *  
  77.  *  Unless required by applicable law or agreed to in writing, software
  78.  *      distributed under the License is distributed on an "AS IS" BASIS,
  79.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  80.  *  
  81.  *  See the License for the specific language governing permissions and
  82.  *      limitations under the License.
  83.  */

  84. /**
  85.  * <i>BinaryClassifierSupremumBound</i> demonstrates the Computation of the Probabilistic Bounds for the
  86.  * Supremum among the Class of Binary Classifier Functions for an Empirical Sample from its Population Mean
  87.  * using Variants of the Efron-Stein Methodology.
  88.  *  
  89.  * <br><br>
  90.  *  <ul>
  91.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  92.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning</a></li>
  93.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  94.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/classifier/README.md">Binary Classifier Supremum Bounds Estimator</a></li>
  95.  *  </ul>
  96.  * <br><br>
  97.  *
  98.  * @author Lakshmi Krishnamurthy
  99.  */

  100. public class BinaryClassifierSupremumBound {

  101.     private static final double[] EmpiricalOutcome (
  102.         final int iNumOutcome)
  103.         throws Exception
  104.     {
  105.         double[] adblEmpiricalOutcome = new double[iNumOutcome];

  106.         for (int i = 0; i < iNumOutcome; ++i)
  107.             adblEmpiricalOutcome[i] = Math.random() + 0.5;

  108.         return adblEmpiricalOutcome;
  109.     }

  110.     private static final SingleSequenceAgnosticMetrics[] IIDDraw (
  111.         final UnivariateSequenceGenerator rsg,
  112.         final int iNumSample)
  113.         throws Exception
  114.     {
  115.         SingleSequenceAgnosticMetrics[] aSSAM = new SingleSequenceAgnosticMetrics[iNumSample];

  116.         for (int i = 0; i < iNumSample; ++i)
  117.             aSSAM[i] = rsg.sequence (iNumSample, null);

  118.         return aSSAM;
  119.     }

  120.     private static final EmpiricalPenaltySupremumEstimator EmpiricalLossSupremumFunction (
  121.         final double[] asEmpiricalOutcome)
  122.         throws Exception
  123.     {
  124.         // AbstractBinaryClassifier[] aClassifier = null;

  125.         return null;

  126.         /* return new EmpiricalLossSupremum (
  127.             new GeneralizedClassifierFunctionClass (
  128.                 aClassifier,
  129.                 new ExpectedSupremumLossAsymptote (
  130.                     0.01,
  131.                     -1.5
  132.                 )
  133.             ),
  134.             asEmpiricalOutcome
  135.         ); */
  136.     }

  137.     private static final void MartingaleDifferencesRun (
  138.         final Binary bsg,
  139.         final double[] adblEmpiricalOutcome,
  140.         final int iNumSet)
  141.         throws Exception
  142.     {
  143.         String strDump = "\t| " + FormatUtil.FormatDouble (adblEmpiricalOutcome.length, 2, 0, 1.) + " => ";

  144.         for (int j = 0; j < iNumSet; ++j) {
  145.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  146.                 bsg,
  147.                 adblEmpiricalOutcome.length
  148.             );

  149.             EmpiricalPenaltySupremumMetrics eslm = new EmpiricalPenaltySupremumMetrics (
  150.                 EmpiricalLossSupremumFunction (
  151.                     adblEmpiricalOutcome
  152.                 ),
  153.                 aSSAM,
  154.                 null
  155.             );

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

  157.             strDump += FormatUtil.FormatDouble (eslm.martingaleVarianceUpperBound(), 1, 3, 1.);
  158.         }

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

  161.     private static final void GhostVariateVarianceRun (
  162.         final Binary bsg,
  163.         final double[] adblEmpiricalOutcome,
  164.         final int iNumSet)
  165.         throws Exception
  166.     {
  167.         String strDump = "\t| " + FormatUtil.FormatDouble (adblEmpiricalOutcome.length, 2, 0, 1.) + " => ";

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

  173.             EmpiricalPenaltySupremumMetrics eslm = new EmpiricalPenaltySupremumMetrics (
  174.                 EmpiricalLossSupremumFunction (
  175.                     adblEmpiricalOutcome
  176.                 ),
  177.                 aSSAM,
  178.                 null
  179.             );

  180.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  181.                 bsg,
  182.                 adblEmpiricalOutcome.length
  183.             );

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

  185.             strDump += FormatUtil.FormatDouble (eslm.ghostVarianceUpperBound (aSSAMGhost), 1, 3, 1.);
  186.         }

  187.         System.out.println (strDump + " |");
  188.     }

  189.     private static final void EfronSteinSteeleRun (
  190.         final Binary bsg,
  191.         final double[] adblEmpiricalOutcome,
  192.         final int iNumSet)
  193.         throws Exception
  194.     {
  195.         String strDump = "\t| " + FormatUtil.FormatDouble (adblEmpiricalOutcome.length, 2, 0, 1.) + " => ";

  196.         for (int j = 0; j < iNumSet; ++j) {
  197.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  198.                 bsg,
  199.                 adblEmpiricalOutcome.length
  200.             );

  201.             EmpiricalPenaltySupremumMetrics eslm = new EmpiricalPenaltySupremumMetrics (
  202.                 EmpiricalLossSupremumFunction (
  203.                     adblEmpiricalOutcome
  204.                 ),
  205.                 aSSAM,
  206.                 null
  207.             );

  208.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  209.                 bsg,
  210.                 adblEmpiricalOutcome.length
  211.             );

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

  213.             strDump += FormatUtil.FormatDouble (eslm.efronSteinSteeleBound (aSSAMGhost), 1, 3, 1.);
  214.         }

  215.         System.out.println (strDump + " |");
  216.     }

  217.     private static final void PivotDifferencesRun (
  218.         final Binary bsg,
  219.         final double[] adblEmpiricalOutcome,
  220.         final int iNumSet)
  221.         throws Exception
  222.     {
  223.         String strDump = "\t| " + FormatUtil.FormatDouble (adblEmpiricalOutcome.length, 2, 0, 1.) + " => ";

  224.         for (int j = 0; j < iNumSet; ++j) {
  225.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  226.                 bsg,
  227.                 adblEmpiricalOutcome.length
  228.             );

  229.             EmpiricalPenaltySupremumMetrics eslm = new EmpiricalPenaltySupremumMetrics (
  230.                 EmpiricalLossSupremumFunction (
  231.                     adblEmpiricalOutcome
  232.                 ),
  233.                 aSSAM,
  234.                 null
  235.             );

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

  237.             strDump += FormatUtil.FormatDouble (eslm.pivotVarianceUpperBound (new FlatMultivariateRandom (0.)), 1, 3, 1.);
  238.         }

  239.         System.out.println (strDump + " |");
  240.     }

  241.     private static final void LugosiVarianceRun (
  242.         final Binary bsg,
  243.         final double[] adblEmpiricalOutcome,
  244.         final int iNumSet)
  245.         throws Exception
  246.     {
  247.         String strDump = "\t| " + FormatUtil.FormatDouble (adblEmpiricalOutcome.length, 2, 0, 1.) + " => ";

  248.         for (int j = 0; j < iNumSet; ++j) {
  249.             SingleSequenceAgnosticMetrics[] aSSAM = IIDDraw (
  250.                 bsg,
  251.                 adblEmpiricalOutcome.length
  252.             );

  253.             EmpiricalPenaltySupremumMetrics eslm = new EmpiricalPenaltySupremumMetrics (
  254.                 EmpiricalLossSupremumFunction (
  255.                     adblEmpiricalOutcome
  256.                 ),
  257.                 aSSAM,
  258.                 null
  259.             );

  260.             SingleSequenceAgnosticMetrics[] aSSAMGhost = IIDDraw (
  261.                 bsg,
  262.                 1
  263.             );

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

  265.             strDump += FormatUtil.FormatDouble (eslm.lugosiVarianceBound (aSSAMGhost[0].sequence()), 1, 3, 1.);
  266.         }

  267.         System.out.println (strDump + " |");
  268.     }

  269.     public static final void main (
  270.         final String[] astrArgs)
  271.         throws Exception
  272.     {
  273.         EnvManager.InitEnv ("");

  274.         int iNumSet = 5;

  275.         int[] aiSampleSize = new int[] {
  276.             3, 10, 25, 50
  277.         };

  278.         Binary bin = new Binary (0.7);

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

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

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

  282.         for (int iSampleSize : aiSampleSize)
  283.             MartingaleDifferencesRun (
  284.                 bin,
  285.                 EmpiricalOutcome (iSampleSize),
  286.                 iNumSet
  287.             );

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

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

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

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

  292.         for (int iSampleSize : aiSampleSize)
  293.             GhostVariateVarianceRun (
  294.                 bin,
  295.                 EmpiricalOutcome (iSampleSize),
  296.                 iNumSet
  297.             );

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

  299.         aiSampleSize = new int[] {
  300.             3, 10, 25, 50, 75, 99
  301.         };

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

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

  304.         for (int iSampleSize : aiSampleSize)
  305.             EfronSteinSteeleRun (
  306.                 bin,
  307.                 EmpiricalOutcome (iSampleSize),
  308.                 iNumSet
  309.             );

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

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

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

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

  314.         for (int iSampleSize : aiSampleSize)
  315.             PivotDifferencesRun (
  316.                 bin,
  317.                 EmpiricalOutcome (iSampleSize),
  318.                 iNumSet
  319.             );

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

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

  322.         System.out.println ("\t|       Lugosi Bounded Variance Upper Bound       |");

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

  324.         for (int iSampleSize : aiSampleSize)
  325.             LugosiVarianceRun (
  326.                 bin,
  327.                 EmpiricalOutcome (iSampleSize),
  328.                 iNumSet
  329.             );

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

  331.         EnvManager.TerminateEnv();
  332.     }
  333. }