L1LossLearner.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>L1LossLearner</i> implements the Learner Class that holds the Space of Normed R<sup>x</sup> To Normed
  80.  * R<sup>1</sup> Learning Functions that employs L<sub>1</sub> Empirical Loss Routine. Class-Specific
  81.  * Asymptotic Sample, Covering Number based Upper Probability Bounds and other Parameters are also
  82.  * maintained.
  83.  *  
  84.  * <br><br>
  85.  * The References are:
  86.  *  
  87.  * <br><br>
  88.  * <ul>
  89.  *  <li>
  90.  *      Alon, N., S. Ben-David, N. Cesa Bianchi, and D. Haussler (1997): Scale-sensitive Dimensions, Uniform
  91.  *          Convergence, and Learnability <i>Journal of Association of Computational Machinery</i> <b>44
  92.  *          (4)</b> 615-631
  93.  *  </li>
  94.  *  <li>
  95.  *      Anthony, M., and P. L. Bartlett (1999): <i>Artificial Neural Network Learning - Theoretical
  96.  *          Foundations</i> <b>Cambridge University Press</b> Cambridge, UK
  97.  *  </li>
  98.  *  <li>
  99.  *      Kearns, M. J., R. E. Schapire, and L. M. Sellie (1994): <i>Towards Efficient Agnostic Learning</i>
  100.  *          Machine Learning <b>17 (2)</b> 115-141
  101.  *  </li>
  102.  *  <li>
  103.  *      Lee, W. S., P. L. Bartlett, and R. C. Williamson (1998): The Importance of Convexity in Learning with
  104.  *          Squared Loss <i>IEEE Transactions on Information Theory</i> <b>44</b> 1974-1980
  105.  *  </li>
  106.  *  <li>
  107.  *      Vapnik, V. N. (1998): <i>Statistical learning Theory</i> <b>Wiley</b> New York
  108.  *  </li>
  109.  * </ul>
  110.  *
  111.  *  <br><br>
  112.  *  <ul>
  113.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  114.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning</a></li>
  115.  *      <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>
  116.  *      <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>
  117.  *  </ul>
  118.  *
  119.  * @author Lakshmi Krishnamurthy
  120.  */

  121. public class L1LossLearner extends org.drip.learning.rxtor1.GeneralizedLearner {
  122.     private org.drip.learning.bound.MeasureConcentrationExpectationBound _cleb = null;

  123.     /**
  124.      * L1LossLearner Constructor
  125.      *
  126.      * @param funcClassRxToR1 R^x To R^1 Function Class
  127.      * @param cdpb The Covering Number based Deviation Upper Probability Bound Generator
  128.      * @param regularizerFunc The Regularizer Function
  129.      * @param cleb The Concentration of Measure based Loss Expectation Upper Bound Evaluator
  130.      *
  131.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  132.      */

  133.     public L1LossLearner (
  134.         final org.drip.spaces.functionclass.NormedRxToNormedR1Finite funcClassRxToR1,
  135.         final org.drip.learning.bound.CoveringNumberLossBound cdpb,
  136.         final org.drip.learning.regularization.RegularizationFunction regularizerFunc,
  137.         final org.drip.learning.bound.MeasureConcentrationExpectationBound cleb)
  138.         throws java.lang.Exception
  139.     {
  140.         super (funcClassRxToR1, cdpb, regularizerFunc);

  141.         if (null == (_cleb = cleb)) throw new java.lang.Exception ("L1LossLearner ctr: Invalid Inputs");
  142.     }

  143.     /**
  144.      * Retrieve the Concentration of Measure based Loss Expectation Upper Bound Evaluator Instance
  145.      *
  146.      * @return The Concentration of Measure based Loss Expectation Upper Bound Evaluator Instance
  147.      */

  148.     public org.drip.learning.bound.MeasureConcentrationExpectationBound concentrationLossBoundEvaluator()
  149.     {
  150.         return _cleb;
  151.     }

  152.     @Override public double lossSampleCoveringNumber (
  153.         final org.drip.spaces.instance.GeneralizedValidatedVector gvvi,
  154.         final double dblEpsilon,
  155.         final boolean bSupremum)
  156.         throws java.lang.Exception
  157.     {
  158.         org.drip.spaces.functionclass.NormedRxToNormedR1Finite funcClassRxToR1 = functionClass();

  159.         return bSupremum ? funcClassRxToR1.sampleSupremumCoveringNumber (gvvi, dblEpsilon) :
  160.             funcClassRxToR1.sampleCoveringNumber (gvvi, dblEpsilon);
  161.     }

  162.     @Override public double empiricalLoss (
  163.         final org.drip.function.definition.R1ToR1 funcLearnerR1ToR1,
  164.         final org.drip.spaces.instance.GeneralizedValidatedVector gvviX,
  165.         final org.drip.spaces.instance.GeneralizedValidatedVector gvviY)
  166.         throws java.lang.Exception
  167.     {
  168.         if (null == funcLearnerR1ToR1 || null == gvviX || !(gvviX instanceof
  169.             org.drip.spaces.instance.ValidatedR1) || null == gvviY || !(gvviY instanceof
  170.                 org.drip.spaces.instance.ValidatedR1))
  171.             throw new java.lang.Exception ("L1LossLearner::empiricalLoss => Invalid Inputs");

  172.         double[] adblX = ((org.drip.spaces.instance.ValidatedR1) gvviX).instance();

  173.         double[] adblY = ((org.drip.spaces.instance.ValidatedR1) gvviY).instance();

  174.         double dblEmpiricalLoss = 0.;
  175.         int iNumSample = adblX.length;

  176.         if (iNumSample != adblY.length)
  177.             throw new java.lang.Exception ("L1LossLearner::empiricalLoss => Invalid Inputs");

  178.         for (int i = 0; i < iNumSample; ++i)
  179.             dblEmpiricalLoss += java.lang.Math.abs (funcLearnerR1ToR1.evaluate (adblX[i]) - adblY[i]);

  180.         return dblEmpiricalLoss;
  181.     }

  182.     @Override public double empiricalLoss (
  183.         final org.drip.function.definition.RdToR1 funcLearnerRdToR1,
  184.         final org.drip.spaces.instance.GeneralizedValidatedVector gvviX,
  185.         final org.drip.spaces.instance.GeneralizedValidatedVector gvviY)
  186.         throws java.lang.Exception
  187.     {
  188.         if (null == funcLearnerRdToR1 || null == gvviX || !(gvviX instanceof
  189.             org.drip.spaces.instance.ValidatedRd) || null == gvviY || !(gvviY instanceof
  190.                 org.drip.spaces.instance.ValidatedR1))
  191.             throw new java.lang.Exception ("L1LossLearner::empiricalLoss => Invalid Inputs");

  192.         double[][] aadblX = ((org.drip.spaces.instance.ValidatedRd) gvviX).instance();

  193.         double[] adblY = ((org.drip.spaces.instance.ValidatedR1) gvviY).instance();

  194.         double dblEmpiricalLoss = 0.;
  195.         int iNumSample = aadblX.length;

  196.         if (iNumSample != adblY.length)
  197.             throw new java.lang.Exception ("L1LossLearner::empiricalLoss => Invalid Inputs");

  198.         for (int i = 0; i < iNumSample; ++i)
  199.             dblEmpiricalLoss += java.lang.Math.abs (funcLearnerRdToR1.evaluate (aadblX[i]) - adblY[i]);

  200.         return dblEmpiricalLoss;
  201.     }

  202.     @Override public double empiricalRisk (
  203.         final org.drip.measure.continuous.R1R1 distR1R1,
  204.         final org.drip.function.definition.R1ToR1 funcLearnerR1ToR1,
  205.         final org.drip.spaces.instance.GeneralizedValidatedVector gvviX,
  206.         final org.drip.spaces.instance.GeneralizedValidatedVector gvviY)
  207.         throws java.lang.Exception
  208.     {
  209.         if (null == distR1R1 || null == funcLearnerR1ToR1 || null == gvviX || !(gvviX instanceof
  210.             org.drip.spaces.instance.ValidatedR1) || null == gvviY || !(gvviY instanceof
  211.                 org.drip.spaces.instance.ValidatedR1))
  212.             throw new java.lang.Exception ("L1LossLearner::empiricalRisk => Invalid Inputs");

  213.         double[] adblX = ((org.drip.spaces.instance.ValidatedR1) gvviX).instance();

  214.         double[] adblY = ((org.drip.spaces.instance.ValidatedR1) gvviY).instance();

  215.         double dblNormalizer = 0.;
  216.         double dblEmpiricalLoss = 0.;
  217.         int iNumSample = adblX.length;

  218.         if (iNumSample != adblY.length)
  219.             throw new java.lang.Exception ("L1LossLearner::empiricalRisk => Invalid Inputs");

  220.         for (int i = 0; i < iNumSample; ++i) {
  221.             double dblDensity = distR1R1.density (adblX[i], adblY[i]);

  222.             dblNormalizer += dblDensity;

  223.             dblEmpiricalLoss += dblDensity * java.lang.Math.abs (funcLearnerR1ToR1.evaluate (adblX[i]) -
  224.                 adblY[i]);
  225.         }

  226.         return dblEmpiricalLoss / dblNormalizer;
  227.     }

  228.     @Override public double empiricalRisk (
  229.         final org.drip.measure.continuous.RdR1 distRdR1,
  230.         final org.drip.function.definition.RdToR1 funcLearnerRdToR1,
  231.         final org.drip.spaces.instance.GeneralizedValidatedVector gvviX,
  232.         final org.drip.spaces.instance.GeneralizedValidatedVector gvviY)
  233.         throws java.lang.Exception
  234.     {
  235.         if (null == distRdR1 || null == funcLearnerRdToR1 || null == gvviX || !(gvviX instanceof
  236.             org.drip.spaces.instance.ValidatedRd) || null == gvviY || !(gvviY instanceof
  237.                 org.drip.spaces.instance.ValidatedR1))
  238.             throw new java.lang.Exception ("L1LossLearner::empiricalRisk => Invalid Inputs");

  239.         double[][] aadblX = ((org.drip.spaces.instance.ValidatedRd) gvviX).instance();

  240.         double[] adblY = ((org.drip.spaces.instance.ValidatedR1) gvviY).instance();

  241.         double dblNormalizer = 0.;
  242.         double dblEmpiricalLoss = 0.;
  243.         int iNumSample = aadblX.length;

  244.         if (iNumSample != adblY.length)
  245.             throw new java.lang.Exception ("L1LossLearner::empiricalRisk => Invalid Inputs");

  246.         for (int i = 0; i < iNumSample; ++i) {
  247.             double dblDensity = distRdR1.density (aadblX[i], adblY[i]);

  248.             dblNormalizer += dblDensity;

  249.             dblEmpiricalLoss += dblDensity * java.lang.Math.abs (funcLearnerRdToR1.evaluate (aadblX[i]) -
  250.                 adblY[i]);
  251.         }

  252.         return dblEmpiricalLoss / dblNormalizer;
  253.     }
  254. }