RdDecisionFunction.java

  1. package org.drip.learning.svm;

  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>RdDecisionFunction</i> exposes the R<sup>d</sup> Decision-Function Based SVM Functionality for
  80.  * Classification and Regression.
  81.  *
  82.  * <br><br>
  83.  *  The References are:
  84.  * <br><br>
  85.  * <ul>
  86.  *  <li>
  87.  *      Shawe-Taylor, J., P. L. Bartlett, R. C. Williamson, and M. Anthony (1996): A Framework for Structural
  88.  *          Risk Minimization, in: <i>Proceedings of the 9th Annual Conference on Computational Learning
  89.  *          Theory</i> <b>ACM</b> New York 68-76
  90.  *  </li>
  91.  *  <li>
  92.  *      Vapnik, V., and A. Chervonenkis (1974): <i>Theory of Pattern Recognition (in Russian)</i>
  93.  *          <b>Nauka</b> Moscow USSR
  94.  *  </li>
  95.  *  <li>
  96.  *      Vapnik, V. (1995): <i>The Nature of Statistical Learning</i> <b>Springer-Verlag</b> New York
  97.  *  </li>
  98.  * </ul>
  99.  *
  100.  *  <br><br>
  101.  *  <ul>
  102.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  103.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning</a></li>
  104.  *      <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>
  105.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/learning/svm">Kernel SVM Decision Function Operator</a></li>
  106.  *  </ul>
  107.  *
  108.  * @author Lakshmi Krishnamurthy
  109.  */

  110. public abstract class RdDecisionFunction extends org.drip.function.definition.RdToR1 {
  111.     private double _dblB = java.lang.Double.NaN;
  112.     private double[] _adblInverseMarginWeight = null;
  113.     private org.drip.spaces.metric.RdNormed _rdInverseMargin = null;
  114.     private org.drip.spaces.tensor.RdGeneralizedVector _rdPredictor = null;

  115.     /**
  116.      * RdDecisionFunction Constructor
  117.      *
  118.      * @param rdPredictor The R^d Metric Input Predictor Space
  119.      * @param rdInverseMargin The Inverse Margin Weights R^d Space
  120.      * @param adblInverseMarginWeight Array of Inverse Margin Weights
  121.      * @param dblB The Kernel Offset
  122.      *
  123.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  124.      */

  125.     public RdDecisionFunction (
  126.         final org.drip.spaces.tensor.RdGeneralizedVector rdPredictor,
  127.         final org.drip.spaces.metric.RdNormed rdInverseMargin,
  128.         final double[] adblInverseMarginWeight,
  129.         final double dblB)
  130.         throws java.lang.Exception
  131.     {
  132.         super (null);

  133.         if (null == (_rdPredictor = rdPredictor) || null == (_rdInverseMargin = rdInverseMargin) || null ==
  134.             (_adblInverseMarginWeight = adblInverseMarginWeight) || !org.drip.numerical.common.NumberUtil.IsValid
  135.                 (_dblB = dblB))
  136.             throw new java.lang.Exception ("RdDecisionFunction ctr: Invalid Inputs");

  137.         int iNumMarginWeight = _adblInverseMarginWeight.length;

  138.         if (0 == iNumMarginWeight || iNumMarginWeight != _rdPredictor.dimension())
  139.             throw new java.lang.Exception ("RdDecisionFunction ctr: Invalid Inputs");
  140.     }

  141.     /**
  142.      * Retrieve the Input Predictor Metric Vector Space
  143.      *
  144.      * @return The Input Predictor Metric Vector Space
  145.      */

  146.     public org.drip.spaces.tensor.RdGeneralizedVector predictorSpace()
  147.     {
  148.         return _rdPredictor;
  149.     }

  150.     /**
  151.      * Retrieve the Inverse Margin Weight Metric Vector Space
  152.      *
  153.      * @return The Inverse Margin Weight Metric Vector Space
  154.      */

  155.     public org.drip.spaces.metric.RdNormed inverseMarginSpace()
  156.     {
  157.         return _rdInverseMargin;
  158.     }

  159.     /**
  160.      * Retrieve the Decision Kernel Weights
  161.      *
  162.      * @return The Decision Kernel Weights
  163.      */

  164.     public double[] inverseMarginWeights()
  165.     {
  166.         return _adblInverseMarginWeight;
  167.     }

  168.     /**
  169.      * Retrieve the Offset
  170.      *
  171.      * @return The Offset
  172.      */

  173.     public double offset()
  174.     {
  175.         return _dblB;
  176.     }

  177.     /**
  178.      * Classify the Specified Multi-dimensional Point
  179.      *
  180.      * @param adblX The Multi-dimensional Input Point
  181.      *
  182.      * @return +1/-1 Boolean Space Output Equivalents
  183.      *
  184.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  185.      */

  186.     public short classify (
  187.         final double[] adblX)
  188.         throws java.lang.Exception
  189.     {
  190.         return evaluate (adblX) > 0. ? org.drip.spaces.tensor.BinaryBooleanVector.BBV_UP :
  191.             org.drip.spaces.tensor.BinaryBooleanVector.BBV_DOWN;
  192.     }

  193.     /**
  194.      * Regress on the Specified Multi-dimensional Point
  195.      *
  196.      * @param adblX The Multi-dimensional Input Point
  197.      *
  198.      * @return The Regression Output
  199.      *
  200.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  201.      */

  202.     public double regress (
  203.         final double[] adblX)
  204.         throws java.lang.Exception
  205.     {
  206.         return evaluate (adblX);
  207.     }

  208.     /**
  209.      * Compute the Entropy Number Upper Bounds Instance for the Specified Inputs
  210.      *
  211.      * @param dsoFactorizer The Factorizing Diagonal Scaling Operator
  212.      * @param dblFeatureSpaceMaureyConstant The Feature Space Maurey Constant
  213.      *
  214.      * @return The Entropy Number Upper Bounds Instance
  215.      */

  216.     public org.drip.learning.svm.DecisionFunctionOperatorBounds entropyNumberUpperBounds (
  217.         final org.drip.learning.kernel.DiagonalScalingOperator dsoFactorizer,
  218.         final double dblFeatureSpaceMaureyConstant)
  219.     {
  220.         try {
  221.             return new org.drip.learning.svm.DecisionFunctionOperatorBounds (dsoFactorizer,
  222.                 inverseMarginSpace().populationMetricNorm(), dblFeatureSpaceMaureyConstant,
  223.                     predictorSpace().dimension());
  224.         } catch (java.lang.Exception e) {
  225.             e.printStackTrace();
  226.         }

  227.         return null;
  228.     }

  229.     /**
  230.      * Compute the Decision Function's Asymptotic Exponent for the Entropy Number
  231.      *
  232.      * @param dsoFactorizer The Factorizing Diagonal Scaling Operator
  233.      *
  234.      * @return The Decision Function's Asymptotic Exponent for the Entropy Number
  235.      *
  236.      * @throws java.lang.Exception Thrown if the Asymptotoc Exponent cannot be computed
  237.      */

  238.     public double logEntropyNumberAsymptote (
  239.         final org.drip.learning.kernel.DiagonalScalingOperator dsoFactorizer)
  240.         throws java.lang.Exception
  241.     {
  242.         if (null == dsoFactorizer)
  243.             throw new java.lang.Exception
  244.                 ("RdDecisionFunction::logEntropyNumberAsymptote => Invalid Inputs");

  245.         org.drip.learning.bound.DiagonalOperatorCoveringBound docb = dsoFactorizer.entropyNumberAsymptote();

  246.         if (null == docb)
  247.             throw new java.lang.Exception
  248.                 ("RdDecisionFunction::logEntropyNumberAsymptote => Cannot get Diagonal Operator Covering Bounds");

  249.         return org.drip.learning.bound.DiagonalOperatorCoveringBound.BASE_DIAGONAL_ENTROPY_ASYMPTOTE_EXPONENT
  250.             == docb.entropyNumberAsymptoteType() ? -1. * docb.entropyNumberAsymptoteExponent() - 0.5 :
  251.                 -1. * docb.entropyNumberAsymptoteExponent();
  252.     }

  253.     /**
  254.      * Optimize the Hyper-plane for the Purposes of Regression
  255.      *
  256.      * @param adblEmpirical The Empirical Observation Array
  257.      * @param dblMargin The Optimization Margin
  258.      * @param dblInverseWidthNormConstraint The Inverse Width Norm Constraint
  259.      *
  260.      * @return TRUE - The Hyper-plane has been successfully Optimized for Regression
  261.      */

  262.     public abstract boolean optimizeRegressionHyperplane (
  263.         final double[] adblEmpirical,
  264.         final double dblMargin,
  265.         final double dblInverseWidthNormConstraint
  266.     );

  267.     /**
  268.      * Optimize the Hyper-plane for the Purposes of Classification
  269.      *
  270.      * @param asEmpirical The Empirical Observation Array
  271.      * @param dblMargin The Optimization Margin
  272.      * @param dblInverseWidthNormConstraint The Inverse Width Norm Constraint
  273.      *
  274.      * @return TRUE - The Hyper-plane has been successfully Optimized for Classification
  275.      */

  276.     public abstract boolean optimizeClassificationHyperplane (
  277.         final short[] asEmpirical,
  278.         final double dblMargin,
  279.         final double dblInverseWidthNormConstraint
  280.     );
  281. }