R1Combinatorial.java

  1. package org.drip.spaces.metric;

  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>R1Combinatorial</i> implements the Normed, Bounded/Unbounded Combinatorial l<sub>p</sub> R<sup>d</sup>
  80.  * Spaces. The Reference we've used is:
  81.  *
  82.  * <br><br>
  83.  *  <ul>
  84.  *      <li>
  85.  *          Carl, B., and I. Stephani (1990): <i>Entropy, Compactness, and the Approximation of Operators</i>
  86.  *              <b>Cambridge University Press</b> Cambridge UK
  87.  *      </li>
  88.  *  </ul>
  89.  *
  90.  * <br><br>
  91.  *  <ul>
  92.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  93.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  94.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/README.md">R<sup>1</sup> and R<sup>d</sup> Vector/Tensor Spaces (Validated and/or Normed), and Function Classes</a></li>
  95.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/metric/README.md">Hilbert/Banach Normed Metric Spaces</a></li>
  96.  *  </ul>
  97.  * <br><br>
  98.  *
  99.  * @author Lakshmi Krishnamurthy
  100.  */

  101. public class R1Combinatorial extends org.drip.spaces.tensor.R1CombinatorialVector implements
  102.     org.drip.spaces.metric.R1Normed {
  103.     private int _iPNorm = -1;
  104.     private org.drip.measure.continuous.R1Univariate _distR1 = null;

  105.     /**
  106.      * Construct the Standard l^p R^1 Combinatorial Space Instance
  107.      *
  108.      * @param lsElementSpace The List Space of Elements
  109.      * @param distR1 The R^1 Borel Sigma Measure
  110.      * @param iPNorm The p-norm of the Space
  111.      *
  112.      * @return The Standard l^p R^1 Combinatorial Space Instance
  113.      */

  114.     public static final R1Combinatorial Standard (
  115.         final java.util.List<java.lang.Double> lsElementSpace,
  116.         final org.drip.measure.continuous.R1Univariate distR1,
  117.         final int iPNorm)
  118.     {
  119.         try {
  120.             return new R1Combinatorial (lsElementSpace, distR1, iPNorm);
  121.         } catch (java.lang.Exception e) {
  122.             e.printStackTrace();
  123.         }

  124.         return null;
  125.     }

  126.     /**
  127.      * Construct the Supremum (i.e., l^Infinity) R^1 Combinatorial Space Instance
  128.      *
  129.      * @param lsElementSpace The List Space of Elements
  130.      * @param distR1 The R^1 Borel Sigma Measure
  131.      *
  132.      * @return The Supremum (i.e., l^Infinity) R^1 Combinatorial Space Instance
  133.      */

  134.     public static final R1Combinatorial Supremum (
  135.         final java.util.List<java.lang.Double> lsElementSpace,
  136.         final org.drip.measure.continuous.R1Univariate distR1)
  137.     {
  138.         try {
  139.             return new R1Combinatorial (lsElementSpace, distR1, java.lang.Integer.MAX_VALUE);
  140.         } catch (java.lang.Exception e) {
  141.             e.printStackTrace();
  142.         }

  143.         return null;
  144.     }

  145.     /**
  146.      * R1Combinatorial Space Constructor
  147.      *
  148.      * @param lsElementSpace The List Space of Elements
  149.      * @param distR1 The R^1 Borel Sigma Measure
  150.      * @param iPNorm The p-norm of the Space
  151.      *
  152.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  153.      */

  154.     public R1Combinatorial (
  155.         final java.util.List<java.lang.Double> lsElementSpace,
  156.         final org.drip.measure.continuous.R1Univariate distR1,
  157.         final int iPNorm)
  158.         throws java.lang.Exception
  159.     {
  160.         super (lsElementSpace);

  161.         if (0 > (_iPNorm = iPNorm))
  162.             throw new java.lang.Exception ("R1Combinatorial Constructor: Invalid p-norm");

  163.         _distR1 = distR1;
  164.     }

  165.     @Override public int pNorm()
  166.     {
  167.         return _iPNorm;
  168.     }

  169.     @Override public org.drip.measure.continuous.R1Univariate borelSigmaMeasure()
  170.     {
  171.         return _distR1;
  172.     }

  173.     @Override public double sampleMetricNorm (
  174.         final double dblX)
  175.         throws java.lang.Exception
  176.     {
  177.         if (!validateInstance (dblX))
  178.             throw new java.lang.Exception ("R1Combinatorial::sampleMetricNorm => Invalid Inputs");

  179.         return java.lang.Math.abs (dblX);
  180.     }

  181.     @Override public double populationMode()
  182.         throws java.lang.Exception
  183.     {
  184.         if (null == _distR1)
  185.             throw new java.lang.Exception ("R1Combinatorial::populationMode => Invalid Inputs");

  186.         double dblMode = java.lang.Double.NaN;
  187.         double dblModeProbability = java.lang.Double.NaN;

  188.         for (double dblElement : elementSpace()) {
  189.             if (!org.drip.numerical.common.NumberUtil.IsValid (dblMode))
  190.                 dblModeProbability = _distR1.density (dblMode = dblElement);
  191.             else {
  192.                 double dblElementProbability = _distR1.density (dblElement);

  193.                 if (dblElementProbability > dblModeProbability) {
  194.                     dblMode = dblElement;
  195.                     dblModeProbability = dblElementProbability;
  196.                 }
  197.             }
  198.         }

  199.         return dblMode;
  200.     }

  201.     @Override public double populationMetricNorm()
  202.         throws java.lang.Exception
  203.     {
  204.         if (null == _distR1)
  205.             throw new java.lang.Exception ("R1Combinatorial::populationMetricNorm => Invalid Inputs");

  206.         double dblNorm = 0.;
  207.         double dblNormalizer = 0.;

  208.         for (double dblElement : elementSpace()) {
  209.             double dblElementProbability = _distR1.density (dblElement);

  210.             dblNormalizer += dblElementProbability;

  211.             dblNorm += sampleMetricNorm (dblElement) * dblElementProbability;
  212.         }

  213.         return dblNorm / dblNormalizer;
  214.     }

  215.     @Override public double borelMeasureSpaceExpectation (
  216.         final org.drip.function.definition.R1ToR1 funcR1ToR1)
  217.         throws java.lang.Exception
  218.     {
  219.         if (null == funcR1ToR1 || null == _distR1)
  220.             throw new java.lang.Exception
  221.                 ("R1Combinatorial::borelMeasureSpaceExpectation => Invalid Inputs");

  222.         double dblNormalizer = 0.;
  223.         double dblBorelMeasureSpaceExpectation = 0.;

  224.         for (double dblElement : elementSpace()) {
  225.             double dblElementProbability = _distR1.density (dblElement);

  226.             dblNormalizer += dblElementProbability;

  227.             dblBorelMeasureSpaceExpectation += funcR1ToR1.evaluate (dblElement) * dblElementProbability;
  228.         }

  229.         return dblBorelMeasureSpaceExpectation / dblNormalizer;
  230.     }
  231. }