R1Multivariate.java

  1. package org.drip.measure.continuous;

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

  77. /**
  78.  * <i>R1Multivariate</i> contains the Generalized R<sup>1</sup> Multivariate Distributions.
  79.  *
  80.  *  <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  84.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/README.md">R<sup>d</sup> Continuous/Discrete Probability Measures</a></li>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/continuous/README.md">R<sup>1</sup> and R<sup>d</sup> Continuous Random Measure</a></li>
  86.  *  </ul>
  87.  *
  88.  * @author Lakshmi Krishnamurthy
  89.  */

  90. public abstract class R1Multivariate {
  91.     private org.drip.measure.continuous.MultivariateMeta _meta = null;

  92.     protected R1Multivariate (
  93.         final org.drip.measure.continuous.MultivariateMeta meta)
  94.         throws java.lang.Exception
  95.     {
  96.         if (null == (_meta = meta))
  97.             throw new java.lang.Exception ("R1Multivariate Constructor => Invalid Inputs");
  98.     }

  99.     /**
  100.      * Retrieve the Multivariate Meta Instance
  101.      *
  102.      * @return The Multivariate Meta Instance
  103.      */

  104.     public org.drip.measure.continuous.MultivariateMeta meta()
  105.     {
  106.         return _meta;
  107.     }

  108.     /**
  109.      * Retrieve the Left Edge Bounding Multivariate
  110.      *
  111.      * @return The Left Edge Bounding Multivariate
  112.      */

  113.     public double[] leftEdge()
  114.     {
  115.         int iNumVariate = _meta.numVariable();

  116.         double[] adblLeftEdge = new double[iNumVariate];

  117.         for (int i = 0; i < iNumVariate; ++i)
  118.             adblLeftEdge[i] = java.lang.Double.MIN_NORMAL;

  119.         return adblLeftEdge;
  120.     }

  121.     /**
  122.      * Retrieve the Right Edge Bounding Multivariate
  123.      *
  124.      * @return The Right Edge Bounding Multivariate
  125.      */

  126.     public double[] rightEdge()
  127.     {
  128.         int iNumVariate = _meta.numVariable();

  129.         double[] adblRightEdge = new double[iNumVariate];

  130.         for (int i = 0; i < iNumVariate; ++i)
  131.             adblRightEdge[i] = java.lang.Double.MAX_VALUE;

  132.         return adblRightEdge;
  133.     }

  134.     /**
  135.      * Compute the Density under the Distribution at the given Multivariate
  136.      *
  137.      * @param adblVariate The Multivariate at which the Density needs to be computed
  138.      *
  139.      * @return The Density
  140.      *
  141.      * @throws java.lang.Exception Thrown if the Density cannot be computed
  142.      */

  143.     public abstract double density (
  144.         final double[] adblVariate)
  145.         throws java.lang.Exception;

  146.     /**
  147.      * Convert the Multivariate Density into an RdToR1 Functions Instance
  148.      *
  149.      * @return The Multivariate Density converted into an RdToR1 Functions Instance
  150.      */

  151.     public org.drip.function.definition.RdToR1 densityRdToR1()
  152.     {
  153.         return new org.drip.function.definition.RdToR1 (null) {
  154.             @Override public int dimension()
  155.             {
  156.                 return _meta.numVariable();
  157.             }

  158.             @Override public double evaluate (
  159.                 final double[] adblVariate)
  160.                 throws java.lang.Exception
  161.             {
  162.                 return density (adblVariate);
  163.             }
  164.         };
  165.     }

  166.     /**
  167.      * Compute the Cumulative under the Distribution to the given Variate Values
  168.      *
  169.      * @param adblVariate Array of Variate Values to which the Cumulative is to be computed
  170.      *
  171.      * @return The Cumulative
  172.      *
  173.      * @throws java.lang.Exception Thrown if the Cumulative cannot be computed
  174.      */

  175.     public double cumulative (
  176.         final double[] adblVariate)
  177.         throws java.lang.Exception
  178.     {
  179.         return densityRdToR1().integrate (leftEdge(), adblVariate);
  180.     }

  181.     /**
  182.      * Compute the Incremental under the Distribution between the 2 Multivariate Instances
  183.      *
  184.      * @param adblVariateLeft Left Multivariate Instance to which the Cumulative is to be computed
  185.      * @param adblVariateRight Right Multivariate Instance to which the Cumulative is to be computed
  186.      *
  187.      * @return The Incremental
  188.      *
  189.      * @throws java.lang.Exception Thrown if the Incremental cannot be computed
  190.      */

  191.     public double incremental (
  192.         final double[] adblVariateLeft,
  193.         final double[] adblVariateRight)
  194.         throws java.lang.Exception
  195.     {
  196.         return densityRdToR1().integrate (adblVariateLeft, adblVariateRight);
  197.     }

  198.     /**
  199.      * Compute the Expectation of the Specified R^d To R^1 Function Instance
  200.      *
  201.      * @param funcRdToR1 The R^d To R^1 Function Instance
  202.      *
  203.      * @return The Expectation of the Specified R^d To R^1 Function Instance
  204.      *
  205.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  206.      */

  207.     public double expectation (
  208.         final org.drip.function.definition.RdToR1 funcRdToR1)
  209.         throws java.lang.Exception
  210.     {
  211.         if (null == funcRdToR1)
  212.             throw new java.lang.Exception ("R1Multivariate::expectation => Invalid Inputs");

  213.         return new org.drip.function.definition.RdToR1 (null) {
  214.             @Override public int dimension()
  215.             {
  216.                 return _meta.numVariable();
  217.             }

  218.             @Override public double evaluate (
  219.                 final double[] adblVariate)
  220.                 throws java.lang.Exception
  221.             {
  222.                 return density (adblVariate) * funcRdToR1.evaluate (adblVariate);
  223.             }
  224.         }.integrate (leftEdge(), rightEdge());
  225.     }

  226.     /**
  227.      * Compute the Mean of the Distribution
  228.      *
  229.      * @return The Mean of the Distribution
  230.      */

  231.     public double[] mean()
  232.     {
  233.         int iNumVariate = _meta.numVariable();

  234.         double[] adblMean = new double[iNumVariate];

  235.         for (int i = 0; i < iNumVariate; ++i) {
  236.             final int iVariate = i;

  237.             try {
  238.                 adblMean[i] = expectation (new org.drip.function.definition.RdToR1 (null) {
  239.                     @Override public int dimension()
  240.                     {
  241.                         return _meta.numVariable();
  242.                     }

  243.                     @Override public double evaluate (
  244.                         final double[] adblVariate)
  245.                         throws java.lang.Exception
  246.                     {
  247.                         return density (adblVariate) * adblVariate[iVariate];
  248.                     }
  249.                 });
  250.             } catch (java.lang.Exception e) {
  251.                 e.printStackTrace();

  252.                 return null;
  253.             }
  254.         }

  255.         return adblMean;
  256.     }

  257.     /**
  258.      * Compute the Variance of the Distribution
  259.      *
  260.      * @return The Variance of the Distribution
  261.      */

  262.     public double[] variance()
  263.     {
  264.         final double[] adblMean = mean();

  265.         if (null == adblMean) return null;

  266.         final int iNumVariate = adblMean.length;
  267.         double[] adblVariance = new double[iNumVariate];

  268.         for (int i = 0; i < iNumVariate; ++i) {
  269.             final int iVariate = i;

  270.             try {
  271.                 adblVariance[i] = expectation (new org.drip.function.definition.RdToR1 (null) {
  272.                     @Override public int dimension()
  273.                     {
  274.                         return _meta.numVariable();
  275.                     }

  276.                     @Override public double evaluate (
  277.                         final double[] adblVariate)
  278.                         throws java.lang.Exception
  279.                     {
  280.                         double dblSecondMoment = 0.;

  281.                         for (int i = 0; i < iNumVariate; ++i) {
  282.                             double dblOffset = adblVariate[iVariate] - adblMean[iVariate];
  283.                             dblSecondMoment = dblSecondMoment + dblOffset * dblOffset;
  284.                         }

  285.                         return density (adblVariate) * dblSecondMoment;
  286.                     }
  287.                 });
  288.             } catch (java.lang.Exception e) {
  289.                 e.printStackTrace();

  290.                 return null;
  291.             }
  292.         }

  293.         return adblVariance;
  294.     }
  295. }