R1ShapeScaleComposite.java

  1. package org.drip.measure.gamma;

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

  74. /**
  75.  * <i>R1ShapeScaleComposite</i> implements the Scale-Scale Composite Measures. The References are:
  76.  *
  77.  * <br><br>
  78.  *  <ul>
  79.  *      <li>
  80.  *          Devroye, L. (1986): <i>Non-Uniform Random Variate Generation</i> <b>Springer-Verlag</b> New York
  81.  *      </li>
  82.  *      <li>
  83.  *          Gamma Distribution (2019): Gamma Distribution
  84.  *              https://en.wikipedia.org/wiki/Chi-squared_distribution
  85.  *      </li>
  86.  *      <li>
  87.  *          Louzada, F., P. L. Ramos, and E. Ramos (2019): A Note on Bias of Closed-Form Estimators for the
  88.  *              Gamma Distribution Derived From Likelihood Equations <i>The American Statistician</i> <b>73
  89.  *              (2)</b> 195-199
  90.  *      </li>
  91.  *      <li>
  92.  *          Minka, T. (2002): Estimating a Gamma distribution https://tminka.github.io/papers/minka-gamma.pdf
  93.  *      </li>
  94.  *      <li>
  95.  *          Ye, Z. S., and N. Chen (2017): Closed-Form Estimators for the Gamma Distribution Derived from
  96.  *              Likelihood Equations <i>The American Statistician</i> <b>71 (2)</b> 177-181
  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/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  104.  *      <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>
  105.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/gamma/README.md">R<sup>1</sup> Gamma Distribution Implementation/Properties</a></li>
  106.  *  </ul>
  107.  *
  108.  * @author Lakshmi Krishnamurthy
  109.  */

  110. public class R1ShapeScaleComposite
  111. {

  112.     /**
  113.      * Generate a Random Number that follows the F Distribution
  114.      *
  115.      * @param gammaDistribution1 Gamma Distribution #1
  116.      * @param gammaDistribution2 Gamma Distribution #2
  117.      *
  118.      * @return Random Number that follows the F Distribution
  119.      *
  120.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  121.      */

  122.     public static final double RandomF (
  123.         final org.drip.measure.gamma.R1ShapeScaleDiscrete gammaDistribution1,
  124.         final org.drip.measure.gamma.R1ShapeScaleDiscrete gammaDistribution2)
  125.         throws java.lang.Exception
  126.     {
  127.         if (null == gammaDistribution1 ||
  128.             null == gammaDistribution2)
  129.         {
  130.             throw new java.lang.Exception (
  131.                 "R1ShapeScaleComposite::RandomF => Invalid Inputs"
  132.             );
  133.         }

  134.         org.drip.measure.gamma.ShapeScaleParameters shapeScaleParameters1 =
  135.             gammaDistribution1.shapeScaleParameters();

  136.         org.drip.measure.gamma.ShapeScaleParameters shapeScaleParameters2 =
  137.             gammaDistribution2.shapeScaleParameters();

  138.         return gammaDistribution1.random() / (
  139.             shapeScaleParameters1.shape() * shapeScaleParameters1.scale()
  140.         ) / (gammaDistribution2.random() / (
  141.             shapeScaleParameters2.shape() * shapeScaleParameters2.scale()
  142.         ));
  143.     }

  144.     /**
  145.      * Generate a Random Number that follows the Beta Prime Distribution
  146.      *
  147.      * @param gammaDistribution1 Gamma Distribution #1
  148.      * @param gammaDistribution2 Gamma Distribution #2
  149.      *
  150.      * @return Random Number that follows the Beta Prime Distribution
  151.      *
  152.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  153.      */

  154.     public static final double RandomBetaPrime (
  155.         final org.drip.measure.gamma.R1ShapeScaleDiscrete gammaDistribution1,
  156.         final org.drip.measure.gamma.R1ShapeScaleDiscrete gammaDistribution2)
  157.         throws java.lang.Exception
  158.     {
  159.         if (null == gammaDistribution1 ||
  160.             null == gammaDistribution2)
  161.         {
  162.             throw new java.lang.Exception (
  163.                 "R1ShapeScaleComposite::RandomBetaPrime => Invalid Inputs"
  164.             );
  165.         }

  166.         return gammaDistribution1.random() / gammaDistribution2.random();
  167.     }

  168.     /**
  169.      * Generate a Random Number that follows the Beta Distribution
  170.      *
  171.      * @param gammaDistribution1 Gamma Distribution #1
  172.      * @param gammaDistribution2 Gamma Distribution #2
  173.      *
  174.      * @return Random Number that follows the Beta Distribution
  175.      *
  176.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  177.      */

  178.     public static final double RandomBeta (
  179.         final org.drip.measure.gamma.R1ShapeScaleDiscrete gammaDistribution1,
  180.         final org.drip.measure.gamma.R1ShapeScaleDiscrete gammaDistribution2)
  181.         throws java.lang.Exception
  182.     {
  183.         if (null == gammaDistribution1 ||
  184.             null == gammaDistribution2)
  185.         {
  186.             throw new java.lang.Exception (
  187.                 "R1ShapeScaleComposite::RandomBeta => Invalid Inputs"
  188.             );
  189.         }

  190.         double scale = gammaDistribution1.shapeScaleParameters().scale();

  191.         if (scale != gammaDistribution2.shapeScaleParameters().scale())
  192.         {
  193.             throw new java.lang.Exception (
  194.                 "R1ShapeScaleComposite::RandomBeta => Invalid Inputs"
  195.             );
  196.         }

  197.         double gammaDistribution1Random = gammaDistribution1.random();

  198.         return gammaDistribution1Random / (gammaDistribution1Random + gammaDistribution2.random());
  199.     }

  200.     /**
  201.      * Generate a Random Vector that follows the Dirichlet Distribution
  202.      *
  203.      * @param gammaDistributionArray Gamma Distribution Array
  204.      *
  205.      * @return Random Vector that follows the Dirichlet Distribution
  206.      */

  207.     public static final double[] RandomDirichletVector (
  208.         final org.drip.measure.gamma.R1ShapeScaleDiscrete[] gammaDistributionArray)
  209.     {
  210.         if (null == gammaDistributionArray)
  211.         {
  212.             return null;
  213.         }

  214.         double dirichletSum = 0.;
  215.         int dirichletVectorCount = gammaDistributionArray.length;
  216.         double[] randomDirichletVector = new double[dirichletVectorCount];

  217.         if (0 == dirichletVectorCount)
  218.         {
  219.             return null;
  220.         }

  221.         for (int dirichletVectorIndex = 0;
  222.             dirichletVectorIndex < dirichletVectorCount;
  223.             ++dirichletVectorIndex)
  224.         {
  225.             if (null == gammaDistributionArray[dirichletVectorIndex] ||
  226.                 1 != gammaDistributionArray[dirichletVectorIndex].shapeScaleParameters().scale())
  227.             {
  228.                 return null;
  229.             }

  230.             try
  231.             {
  232.                 dirichletSum = dirichletSum + (
  233.                     randomDirichletVector[dirichletVectorIndex] =
  234.                     gammaDistributionArray[dirichletVectorIndex].random()
  235.                 );
  236.             }
  237.             catch (java.lang.Exception e)
  238.             {
  239.                 e.printStackTrace();

  240.                 return null;
  241.             }
  242.         }

  243.         for (int dirichletVectorIndex = 0;
  244.             dirichletVectorIndex < dirichletVectorCount;
  245.             ++dirichletVectorIndex)
  246.         {
  247.             randomDirichletVector[dirichletVectorIndex] = randomDirichletVector[dirichletVectorIndex] /
  248.                 dirichletSum;
  249.         }

  250.         return randomDirichletVector;
  251.     }

  252.     /**
  253.      * Compute the Kullback-Liebler Divergence for the Gamma Distribution Pair
  254.      *
  255.      * @param gammaDistribution1 Gamma Distribution #1
  256.      * @param gammaDistribution2 Gamma Distribution #2
  257.      *
  258.      * @return The Kullback-Liebler Divergence for the Gamma Distribution Pair
  259.      *
  260.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  261.      */

  262.     public static final double KullbackLieblerDivergence (
  263.         final org.drip.measure.gamma.R1ShapeScaleDistribution gammaDistribution1,
  264.         final org.drip.measure.gamma.R1ShapeScaleDistribution gammaDistribution2)
  265.         throws java.lang.Exception
  266.     {
  267.         if (null == gammaDistribution1 ||
  268.             null == gammaDistribution2)
  269.         {
  270.             throw new java.lang.Exception (
  271.                 "R1ShapeScaleComposite::KullbackLieblerDivergence => Invalid Inputs"
  272.             );
  273.         }

  274.         org.drip.measure.gamma.ShapeScaleParameters shapeScaleParameters1 =
  275.             gammaDistribution1.shapeScaleParameters();

  276.         org.drip.measure.gamma.ShapeScaleParameters shapeScaleParameters2 =
  277.             gammaDistribution2.shapeScaleParameters();

  278.         double scale1 = shapeScaleParameters1.scale();

  279.         double scale2 = shapeScaleParameters2.scale();

  280.         double shape1 = shapeScaleParameters1.shape();

  281.         double shape2 = shapeScaleParameters2.shape();

  282.         org.drip.function.definition.R1ToR1 gammaEstimator = gammaDistribution1.gammaEstimator();

  283.         return (shape1 - shape2) * gammaDistribution1.digammaEstimator().evaluate (
  284.             shape1
  285.         ) - gammaEstimator.evaluate (
  286.             shape1
  287.         ) + gammaEstimator.evaluate (
  288.             shape2
  289.         ) + shape2 * (
  290.             java.lang.Math.log (
  291.                 scale2
  292.             ) - java.lang.Math.log (
  293.                 scale1
  294.             )
  295.         ) + shape1 * (scale1 - scale2) / scale1;
  296.     }
  297. }