EmpiricalPenaltySupremumEstimator.java

  1. package org.drip.learning.rxtor1;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

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

  78. /**
  79.  * <i>EmpiricalPenaltySupremumEstimator</i> contains the Implementation of the Empirical Penalty Supremum
  80.  * Estimator dependent on Multivariate Random Variables where the Multivariate Function is a Linear
  81.  * Combination of Bounded Univariate Functions acting on each Random Variate.
  82.  *
  83.  *  <br><br>
  84.  *  <ul>
  85.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  86.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning</a></li>
  87.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/learning">Agnostic Learning Bounds under Empirical Loss Minimization Schemes</a></li>
  88.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/learning/rxtor1">Statistical Learning Empirical Loss Penalizer</a></li>
  89.  *  </ul>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class EmpiricalPenaltySupremumEstimator extends org.drip.sequence.functional.BoundedMultivariateRandom {

  94.     /**
  95.      * Supremum Penalty computed off of Empirical Loss
  96.      */

  97.     public static final int SUPREMUM_PENALTY_EMPIRICAL_LOSS = 1;

  98.     /**
  99.      * Supremum Penalty computed off of Structural Loss
  100.      */

  101.     public static final int SUPREMUM_PENALTY_STRUCTURAL_LOSS = 2;

  102.     /**
  103.      * Supremum Penalty computed off of Regularized Loss
  104.      */

  105.     public static final int SUPREMUM_PENALTY_REGULARIZED_LOSS = 4;

  106.     /**
  107.      * Supremum Penalty computed off of Empirical Risk
  108.      */

  109.     public static final int SUPREMUM_PENALTY_EMPIRICAL_RISK = 8;

  110.     /**
  111.      * Supremum Penalty computed off of Structural Risk
  112.      */

  113.     public static final int SUPREMUM_PENALTY_STRUCTURAL_RISK = 16;

  114.     /**
  115.      * Supremum Penalty computed off of Regularized Risk
  116.      */

  117.     public static final int SUPREMUM_PENALTY_REGULARIZED_RISK = 32;

  118.     private int _iSupremumPenaltyLossMode = -1;
  119.     private org.drip.measure.continuous.R1R1 _distR1R1 = null;
  120.     private org.drip.measure.continuous.RdR1 _distRdR1 = null;
  121.     private org.drip.spaces.rxtor1.NormedR1ToNormedR1[] _aR1ToR1 = null;
  122.     private org.drip.spaces.rxtor1.NormedRdToNormedR1[] _aRdToR1 = null;
  123.     private org.drip.spaces.instance.GeneralizedValidatedVector _gvviY = null;
  124.     private org.drip.learning.rxtor1.EmpiricalLearningMetricEstimator _elme = null;

  125.     /**
  126.      * EmpiricalPenaltySupremumEstimator Constructor
  127.      *
  128.      * @param iSupremumPenaltyLossMode Supremum Loss Penalty Mode
  129.      * @param elme The Empirical Learning Metric Estimator Instance
  130.      * @param gvviY The Validated Outcome Instance
  131.      * @param distR1R1 R^1 R^1 Multivariate Measure
  132.      * @param distRdR1 R^d R^1 Multivariate Measure
  133.      *
  134.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  135.      */

  136.     public EmpiricalPenaltySupremumEstimator (
  137.         final int iSupremumPenaltyLossMode,
  138.         final org.drip.learning.rxtor1.EmpiricalLearningMetricEstimator elme,
  139.         final org.drip.spaces.instance.GeneralizedValidatedVector gvviY,
  140.         final org.drip.measure.continuous.R1R1 distR1R1,
  141.         final org.drip.measure.continuous.RdR1 distRdR1)
  142.         throws java.lang.Exception
  143.     {
  144.         if (null == (_elme = elme))
  145.             throw new java.lang.Exception ("EmpiricalPenaltySupremumEstimator ctr: Invalid Inputs");

  146.         org.drip.spaces.rxtor1.NormedRxToNormedR1[] aRxToR1 = _elme.functionClass().functionSpaces();

  147.         if (null == aRxToR1)
  148.             throw new java.lang.Exception ("EmpiricalPenaltySupremumEstimator ctr: Invalid Inputs");

  149.         if (aRxToR1 instanceof org.drip.spaces.rxtor1.NormedR1ToNormedR1[])
  150.             _aR1ToR1 = (org.drip.spaces.rxtor1.NormedR1ToNormedR1[]) aRxToR1;
  151.         else
  152.             _aRdToR1 = (org.drip.spaces.rxtor1.NormedRdToNormedR1[]) aRxToR1;

  153.         _gvviY = gvviY;
  154.         _distR1R1 = distR1R1;
  155.         _distRdR1 = distRdR1;
  156.         int iNumRxToR1 = aRxToR1.length;
  157.         _iSupremumPenaltyLossMode = iSupremumPenaltyLossMode;

  158.         if (SUPREMUM_PENALTY_EMPIRICAL_LOSS == _iSupremumPenaltyLossMode && null == _gvviY)
  159.             throw new java.lang.Exception ("EmpiricalPenaltySupremumEstimator ctr: Invalid Inputs");

  160.         for (int i = 0; i < iNumRxToR1; ++i) {
  161.             if (null == aRxToR1[i])
  162.                 throw new java.lang.Exception ("EmpiricalPenaltySupremumEstimator ctr: Invalid Inputs");
  163.         }
  164.     }

  165.     /**
  166.      * The Supremum Penalty Loss Mode Flag
  167.      *
  168.      * @return The Supremum Penalty Loss Mode Flag
  169.      */

  170.     public int supremumPenaltyLossMode()
  171.     {
  172.         return _iSupremumPenaltyLossMode;
  173.     }

  174.     /**
  175.      * Retrieve the Empirical Learning Metric Estimator Instance
  176.      *
  177.      * @return The Empirical Learning Metric Estimator Instance
  178.      */

  179.     public org.drip.learning.rxtor1.EmpiricalLearningMetricEstimator elme()
  180.     {
  181.         return _elme;
  182.     }

  183.     /**
  184.      * Retrieve the Validated Outcome Instance
  185.      *
  186.      * @return The Validated Outcome Instance
  187.      */

  188.     public org.drip.spaces.instance.GeneralizedValidatedVector empiricalOutcomes()
  189.     {
  190.         return _gvviY;
  191.     }

  192.     /**
  193.      * Compute the Empirical Penalty Supremum for the specified R^1 Input Space
  194.      *
  195.      * @param gvviX The R^1 Input Space
  196.      *
  197.      * @return The Empirical Penalty Supremum for the specified R^1 Input Space
  198.      */

  199.     public org.drip.learning.rxtor1.EmpiricalPenaltySupremum supremumR1 (
  200.         final org.drip.spaces.instance.GeneralizedValidatedVector gvviX)
  201.     {
  202.         if (null == _aR1ToR1) return null;

  203.         int iSupremumIndex  = 0;
  204.         int iNumR1ToR1 = _aR1ToR1.length;
  205.         double dblSupremumPenaltyLoss = 0.;

  206.         for (int i = 0 ; i < iNumR1ToR1; ++i) {
  207.             org.drip.function.definition.R1ToR1 funcR1ToR1 = _aR1ToR1[i].function();

  208.             if (null == funcR1ToR1) return null;

  209.             double dblPenaltyLoss = 0.;

  210.             try {
  211.                 if (SUPREMUM_PENALTY_EMPIRICAL_LOSS == _iSupremumPenaltyLossMode)
  212.                     dblPenaltyLoss += _elme.empiricalLoss (funcR1ToR1, gvviX, _gvviY);
  213.                 else if (SUPREMUM_PENALTY_STRUCTURAL_LOSS == _iSupremumPenaltyLossMode)
  214.                     dblPenaltyLoss += _elme.structuralLoss (funcR1ToR1, gvviX);
  215.                 else if (SUPREMUM_PENALTY_REGULARIZED_LOSS == _iSupremumPenaltyLossMode)
  216.                     dblPenaltyLoss += _elme.regularizedLoss (funcR1ToR1, gvviX, _gvviY);
  217.                 else if (SUPREMUM_PENALTY_EMPIRICAL_RISK == _iSupremumPenaltyLossMode)
  218.                     dblPenaltyLoss += _elme.empiricalRisk (_distR1R1, funcR1ToR1, gvviX, _gvviY);
  219.                 else if (SUPREMUM_PENALTY_STRUCTURAL_RISK == _iSupremumPenaltyLossMode)
  220.                     dblPenaltyLoss += _elme.structuralRisk (_distR1R1, funcR1ToR1, gvviX, _gvviY);
  221.                 else if (SUPREMUM_PENALTY_REGULARIZED_RISK == _iSupremumPenaltyLossMode)
  222.                     dblPenaltyLoss += _elme.regularizedRisk (_distR1R1, funcR1ToR1, gvviX, _gvviY);
  223.             } catch (java.lang.Exception e) {
  224.                 e.printStackTrace();

  225.                 return null;
  226.             }

  227.             if (dblPenaltyLoss > dblSupremumPenaltyLoss) {
  228.                 iSupremumIndex = i;
  229.                 dblSupremumPenaltyLoss = dblPenaltyLoss;
  230.             }
  231.         }

  232.         try {
  233.             return new org.drip.learning.rxtor1.EmpiricalPenaltySupremum (iSupremumIndex,
  234.                 dblSupremumPenaltyLoss / gvviX.sampleSize());
  235.         } catch (java.lang.Exception e) {
  236.             e.printStackTrace();
  237.         }

  238.         return null;
  239.     }

  240.     /**
  241.      * Compute the Empirical Penalty Supremum for the specified R^d Input Space
  242.      *
  243.      * @param gvviX The R^d Input Space
  244.      *
  245.      * @return The Empirical Penalty Supremum for the specified R^d Input Space
  246.      */

  247.     public org.drip.learning.rxtor1.EmpiricalPenaltySupremum supremumRd (
  248.         final org.drip.spaces.instance.GeneralizedValidatedVector gvviX)
  249.     {
  250.         if (null == _aRdToR1) return null;

  251.         int iSupremumIndex  = 0;
  252.         int iNumRdToR1 = _aRdToR1.length;
  253.         double dblSupremumPenaltyLoss = 0.;

  254.         for (int i = 0 ; i < iNumRdToR1; ++i) {
  255.             org.drip.function.definition.RdToR1 funcRdToR1 = _aRdToR1[i].function();

  256.             if (null == funcRdToR1) return null;

  257.             double dblPenaltyLoss = 0.;

  258.             try {
  259.                 if (SUPREMUM_PENALTY_EMPIRICAL_LOSS == _iSupremumPenaltyLossMode)
  260.                     dblPenaltyLoss += _elme.empiricalLoss (funcRdToR1, gvviX, _gvviY);
  261.                 else if (SUPREMUM_PENALTY_STRUCTURAL_LOSS == _iSupremumPenaltyLossMode)
  262.                     dblPenaltyLoss += _elme.structuralLoss (funcRdToR1, gvviX);
  263.                 else if (SUPREMUM_PENALTY_REGULARIZED_LOSS == _iSupremumPenaltyLossMode)
  264.                     dblPenaltyLoss += _elme.regularizedLoss (funcRdToR1, gvviX, _gvviY);
  265.                 else if (SUPREMUM_PENALTY_EMPIRICAL_RISK == _iSupremumPenaltyLossMode)
  266.                     dblPenaltyLoss += _elme.empiricalRisk (_distRdR1, funcRdToR1, gvviX, _gvviY);
  267.                 else if (SUPREMUM_PENALTY_STRUCTURAL_RISK == _iSupremumPenaltyLossMode)
  268.                     dblPenaltyLoss += _elme.structuralRisk (_distRdR1, funcRdToR1, gvviX, _gvviY);
  269.                 else if (SUPREMUM_PENALTY_REGULARIZED_RISK == _iSupremumPenaltyLossMode)
  270.                     dblPenaltyLoss += _elme.regularizedRisk (_distRdR1, funcRdToR1, gvviX, _gvviY);
  271.             } catch (java.lang.Exception e) {
  272.                 e.printStackTrace();

  273.                 return null;
  274.             }

  275.             if (dblPenaltyLoss > dblSupremumPenaltyLoss) {
  276.                 iSupremumIndex = i;
  277.                 dblSupremumPenaltyLoss = dblPenaltyLoss;
  278.             }
  279.         }

  280.         try {
  281.             return new org.drip.learning.rxtor1.EmpiricalPenaltySupremum (iSupremumIndex,
  282.                 dblSupremumPenaltyLoss / gvviX.sampleSize());
  283.         } catch (java.lang.Exception e) {
  284.             e.printStackTrace();
  285.         }

  286.         return null;
  287.     }

  288.     /**
  289.      * Compute the Empirical Penalty Supremum for the specified R^1/R^d Input Space
  290.      *
  291.      * @param gvviX The R^1/R^d Input Space
  292.      *
  293.      * @return The Empirical Penalty Supremum for the specified R^1/R^d Input Space
  294.      */

  295.     public org.drip.learning.rxtor1.EmpiricalPenaltySupremum supremum (
  296.         final org.drip.spaces.instance.GeneralizedValidatedVector gvviX)
  297.     {
  298.         org.drip.learning.rxtor1.EmpiricalPenaltySupremum epsR1 = supremumR1 (gvviX);

  299.         return null == epsR1 ? supremumRd (gvviX) : epsR1;
  300.     }

  301.     /**
  302.      * Retrieve the Supremum R^1 To R^1 Function Instance for the specified Variate Sequence
  303.      *
  304.      * @param adblX The Predictor Instance
  305.      *
  306.      * @return The Supremum R^1 To R^1 Function Instance
  307.      */

  308.     public org.drip.function.definition.R1ToR1 supremumR1ToR1 (
  309.         final double[] adblX)
  310.     {
  311.         org.drip.learning.rxtor1.EmpiricalPenaltySupremum eps = null;

  312.         try {
  313.             eps = supremumR1 (new org.drip.spaces.instance.ValidatedR1
  314.                 (org.drip.spaces.tensor.R1ContinuousVector.Standard(), adblX));
  315.         } catch (java.lang.Exception e) {
  316.             e.printStackTrace();

  317.             return null;
  318.         }

  319.         return _aR1ToR1[eps.index()].function();
  320.     }

  321.     /**
  322.      * Retrieve the Supremum R^d To R^1 Function Instance for the specified Variate Sequence
  323.      *
  324.      * @param aadblX The Predictor Instance
  325.      *
  326.      * @return The Supremum R^d To R^1 Function Instance
  327.      */

  328.     public org.drip.function.definition.RdToR1 supremumRdToR1 (
  329.         final double[][] aadblX)
  330.     {
  331.         org.drip.learning.rxtor1.EmpiricalPenaltySupremum eps = null;

  332.         try {
  333.             eps = supremumRd (new org.drip.spaces.instance.ValidatedRd
  334.                 (org.drip.spaces.tensor.RdContinuousVector.Standard (aadblX.length), aadblX));
  335.         } catch (java.lang.Exception e) {
  336.             e.printStackTrace();

  337.             return null;
  338.         }

  339.         return _aRdToR1[eps.index()].function();
  340.     }

  341.     @Override public int dimension()
  342.     {
  343.         return -1;
  344.     }

  345.     @Override public double evaluate (
  346.         final double[] adblX)
  347.         throws java.lang.Exception
  348.     {
  349.         org.drip.learning.rxtor1.EmpiricalPenaltySupremum eps = supremumR1 (new
  350.             org.drip.spaces.instance.ValidatedR1 (org.drip.spaces.tensor.R1ContinuousVector.Standard(),
  351.                 adblX));

  352.         if (null == eps)
  353.             throw new java.lang.Exception ("EmpiricalPenaltySupremumEstimator::evaluate => Invalid Inputs");

  354.         return eps.value();
  355.     }

  356.     /**
  357.      * Retrieve the Worst-case Loss over the Multivariate Sequence
  358.      *
  359.      * @param aadblX The Multivariate Array
  360.      *
  361.      * @return The Worst-case Loss over the Multivariate Sequence
  362.      *
  363.      * @throws java.lang.Exception Thrown if the Worst-Case Loss cannot be computed
  364.      */

  365.     public double evaluate (
  366.         final double[][] aadblX)
  367.         throws java.lang.Exception
  368.     {
  369.         if (null == aadblX)
  370.             throw new java.lang.Exception ("EmpiricalPenaltySupremumEstimator::evaluate => Invalid Inputs");

  371.         org.drip.learning.rxtor1.EmpiricalPenaltySupremum eps = supremumRd (new
  372.             org.drip.spaces.instance.ValidatedRd (org.drip.spaces.tensor.RdContinuousVector.Standard
  373.                 (aadblX.length), aadblX));

  374.         if (null == eps)
  375.             throw new java.lang.Exception ("EmpiricalPenaltySupremumEstimator::evaluate => Invalid Inputs");

  376.         return eps.value();
  377.     }

  378.     @Override public double targetVariateVarianceBound (
  379.         final int iTargetVariateIndex)
  380.         throws java.lang.Exception
  381.     {
  382.         return 1. / (_gvviY.sampleSize() * _gvviY.sampleSize());
  383.     }
  384. }