RegularizerRdContinuousToR1Continuous.java

  1. package org.drip.learning.regularization;

  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>RegularizerRdContinuousToR1Continuous</i> computes the Structural Loss and Risk for the specified
  80.  * Normed R<sup>d</sup> Continuous To Normed R<sup>1</sup> Continuous Learning Function.
  81.  *  
  82.  * <br><br>
  83.  * <ul>
  84.  *  <li>
  85.  *      Alon, N., S. Ben-David, N. Cesa Bianchi, and D. Haussler (1997): Scale-sensitive Dimensions, Uniform
  86.  *          Convergence, and Learnability <i>Journal of Association of Computational Machinery</i> <b>44
  87.  *          (4)</b> 615-631
  88.  *  </li>
  89.  *  <li>
  90.  *      Anthony, M., and P. L. Bartlett (1999): <i>Artificial Neural Network Learning - Theoretical
  91.  *          Foundations</i> <b>Cambridge University Press</b> Cambridge, UK
  92.  *  </li>
  93.  *  <li>
  94.  *      Kearns, M. J., R. E. Schapire, and L. M. Sellie (1994): <i>Towards Efficient Agnostic Learning</i>
  95.  *          Machine Learning <b>17 (2)</b> 115-141
  96.  *  </li>
  97.  *  <li>
  98.  *      Lee, W. S., P. L. Bartlett, and R. C. Williamson (1998): The Importance of Convexity in Learning with
  99.  *          Squared Loss <i>IEEE Transactions on Information Theory</i> <b>44</b> 1974-1980
  100.  *  </li>
  101.  *  <li>
  102.  *      Vapnik, V. N. (1998): <i>Statistical learning Theory</i> <b>Wiley</b> New York
  103.  *  </li>
  104.  * </ul>
  105.  *
  106.  *  <br><br>
  107.  *  <ul>
  108.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  109.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning</a></li>
  110.  *      <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>
  111.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/learning/regularization">Statistical Learning Empirical Loss Regularizer</a></li>
  112.  *  </ul>
  113.  *
  114.  * @author Lakshmi Krishnamurthy
  115.  */

  116. public class RegularizerRdContinuousToR1Continuous extends
  117.     org.drip.spaces.rxtor1.NormedRdContinuousToR1Continuous implements
  118.         org.drip.learning.regularization.RegularizerRdToR1 {
  119.     private double _dblLambda = java.lang.Double.NaN;

  120.     /**
  121.      * RegularizerRdContinuousToR1Continuous Function Space Constructor
  122.      *
  123.      * @param funcRdToR1 The R^d To R^1 Function
  124.      * @param rdContinuousInput The Continuous R^d Input Metric Vector Space
  125.      * @param r1ContinuousOutput The Continuous R^1 Output Metric Vector Space
  126.      * @param dblLambda The Regularization Lambda
  127.      *
  128.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  129.      */

  130.     public RegularizerRdContinuousToR1Continuous (
  131.         final org.drip.function.definition.RdToR1 funcRdToR1,
  132.         final org.drip.spaces.metric.RdContinuousBanach rdContinuousInput,
  133.         final org.drip.spaces.metric.R1Continuous r1ContinuousOutput,
  134.         final double dblLambda)
  135.         throws java.lang.Exception
  136.     {
  137.         super (rdContinuousInput, r1ContinuousOutput, funcRdToR1);

  138.         if (!org.drip.numerical.common.NumberUtil.IsValid (_dblLambda = dblLambda) || 0 > _dblLambda)
  139.             throw new java.lang.Exception
  140.                 ("RegularizerRdContinuousToR1Continuous Constructor => Invalid Inputs");
  141.     }

  142.     @Override public double lambda()
  143.     {
  144.         return _dblLambda;
  145.     }

  146.     @Override public double structuralLoss (
  147.         final org.drip.function.definition.RdToR1 funcRdToR1,
  148.         final double[][] aadblX)
  149.         throws java.lang.Exception
  150.     {
  151.         if (null == funcRdToR1 || null == aadblX)
  152.             throw new java.lang.Exception
  153.                 ("RegularizerRdContinuousToR1Continuous::structuralLoss => Invalid Inputs");

  154.         double dblLoss = 0.;
  155.         int iNumSample = aadblX.length;

  156.         if (0 == iNumSample)
  157.             throw new java.lang.Exception
  158.                 ("RegularizerRdContinuousToR1Continuous::structuralLoss => Invalid Inputs");

  159.         int iPNorm = outputMetricVectorSpace().pNorm();

  160.         org.drip.function.definition.RdToR1 funcRegularizerRdToR1 = function();

  161.         if (java.lang.Integer.MAX_VALUE == iPNorm) {
  162.             double dblSupremum = 0.;

  163.             for (int i = 0; i < iNumSample; ++i) {
  164.                 double dblRegularizedValue = java.lang.Math.abs (funcRegularizerRdToR1.evaluate (aadblX[i]) *
  165.                     funcRdToR1.evaluate (aadblX[i]));

  166.                 if (dblSupremum < dblRegularizedValue) dblSupremum = dblRegularizedValue;
  167.             }

  168.             return dblSupremum;
  169.         }

  170.         for (int i = 0; i < iNumSample; ++i)
  171.             dblLoss += java.lang.Math.pow (java.lang.Math.abs (funcRegularizerRdToR1.evaluate (aadblX[i]) *
  172.                 funcRdToR1.evaluate (aadblX[i])), iPNorm);

  173.         return dblLoss / iPNorm;
  174.     }

  175.     @Override public double structuralRisk (
  176.         final org.drip.measure.continuous.RdR1 distRdR1,
  177.         final org.drip.function.definition.RdToR1 funcRdToR1,
  178.         final double[][] aadblX,
  179.         final double[] adblY)
  180.         throws java.lang.Exception
  181.     {
  182.         if (null == funcRdToR1 || null == aadblX || null == adblY)
  183.             throw new java.lang.Exception
  184.                 ("RegularizerRdContinuousToR1Continuous::structuralRisk => Invalid Inputs");

  185.         double dblLoss = 0.;
  186.         double dblNormalizer = 0.;
  187.         int iNumSample = aadblX.length;

  188.         if (0 == iNumSample || iNumSample != adblY.length)
  189.             throw new java.lang.Exception
  190.                 ("RegularizerRdContinuousToR1Continuous::structuralRisk => Invalid Inputs");

  191.         int iPNorm = outputMetricVectorSpace().pNorm();

  192.         org.drip.function.definition.RdToR1 funcRegularizerRdToR1 = function();

  193.         if (java.lang.Integer.MAX_VALUE == iPNorm) {
  194.             double dblSupremumDensity = 0.;
  195.             double dblSupremumNodeValue = 0.;

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

  198.                 if (dblDensity > dblSupremumDensity) {
  199.                     dblSupremumDensity = dblDensity;

  200.                     dblSupremumNodeValue = java.lang.Math.abs (funcRegularizerRdToR1.evaluate (aadblX[i]) *
  201.                         funcRdToR1.evaluate (aadblX[i]));
  202.                 }
  203.             }

  204.             return dblSupremumNodeValue;
  205.         }

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

  208.             dblNormalizer += dblDensity;

  209.             dblLoss += dblDensity * java.lang.Math.pow (java.lang.Math.abs (funcRegularizerRdToR1.evaluate
  210.                 (aadblX[i]) * funcRdToR1.evaluate (aadblX[i])), iPNorm);
  211.         }

  212.         return dblLoss / iPNorm / dblNormalizer;
  213.     }
  214. }