KullbackLieblerDivergence.java

  1. package org.drip.sample.gammadistribution;

  2. import org.drip.function.definition.R1ToR1;
  3. import org.drip.function.definition.R2ToR1;
  4. import org.drip.measure.gamma.R1ShapeScaleComposite;
  5. import org.drip.measure.gamma.R1ShapeScaleDistribution;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.service.env.EnvManager;
  8. import org.drip.specialfunction.digamma.CumulativeSeriesEstimator;
  9. import org.drip.specialfunction.gamma.EulerIntegralSecondKind;
  10. import org.drip.specialfunction.incompletegamma.LowerEulerIntegral;

  11. /*
  12.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  13.  */

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

  83. /**
  84.  * <i>KullbackLieblerDivergence</i> demonstrates the Kullback-Liebler Divergence between a Pair of
  85.  *  R<sup>1</sup> Gamma Distributions using the Shape/Scale Parameterization. The References are:
  86.  *
  87.  * <br><br>
  88.  *  <ul>
  89.  *      <li>
  90.  *          Devroye, L. (1986): <i>Non-Uniform Random Variate Generation</i> <b>Springer-Verlag</b> New York
  91.  *      </li>
  92.  *      <li>
  93.  *          Gamma Distribution (2019): Gamma Distribution
  94.  *              https://en.wikipedia.org/wiki/Chi-squared_distribution
  95.  *      </li>
  96.  *      <li>
  97.  *          Louzada, F., P. L. Ramos, and E. Ramos (2019): A Note on Bias of Closed-Form Estimators for the
  98.  *              Gamma Distribution Derived From Likelihood Equations <i>The American Statistician</i> <b>73
  99.  *              (2)</b> 195-199
  100.  *      </li>
  101.  *      <li>
  102.  *          Minka, T. (2002): Estimating a Gamma distribution https://tminka.github.io/papers/minka-gamma.pdf
  103.  *      </li>
  104.  *      <li>
  105.  *          Ye, Z. S., and N. Chen (2017): Closed-Form Estimators for the Gamma Distribution Derived from
  106.  *              Likelihood Equations <i>The American Statistician</i> <b>71 (2)</b> 177-181
  107.  *      </li>
  108.  *  </ul>
  109.  *
  110.  *  <br><br>
  111.  *  <ul>
  112.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  113.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  114.  *      <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>
  115.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/dynamics/README.md">R<sup>1</sup> Gamma Distribution Implementation/Properties</a></li>
  116.  *  </ul>
  117.  *
  118.  * @author Lakshmi Krishnamurthy
  119.  */

  120. public class KullbackLieblerDivergence
  121. {

  122.     private static final R2ToR1 LowerIncompleteGamma()
  123.         throws Exception
  124.     {
  125.         return new R2ToR1()
  126.         {
  127.             @Override public double evaluate (
  128.                 final double s,
  129.                 final double t)
  130.                 throws Exception
  131.             {
  132.                 return new LowerEulerIntegral (
  133.                     null,
  134.                     t
  135.                 ).evaluate (
  136.                     s
  137.                 );
  138.             }
  139.         };
  140.     }

  141.     public static final void main (
  142.         final String[] argumentArray)
  143.         throws Exception
  144.     {
  145.         EnvManager.InitEnv (
  146.             ""
  147.         );

  148.         double k0 = 2.0;
  149.         double theta0 = 1.0;
  150.         int digammaTermCount = 1000;
  151.         double[] thetaArray =
  152.         {
  153.              1.0,
  154.              1.5,
  155.              2.0,
  156.              2.5,
  157.              3.0,
  158.         };
  159.         double[] kArray =
  160.         {
  161.              // 1,
  162.              1.0,
  163.              1.5,
  164.              2.0,
  165.              2.5,
  166.              3.0,
  167.              3.5,
  168.              4.0,
  169.         };

  170.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (
  171.             null
  172.         );

  173.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

  174.         R1ToR1 digammaEstimator = CumulativeSeriesEstimator.AbramowitzStegun2007 (
  175.             digammaTermCount
  176.         );

  177.         R1ShapeScaleDistribution gammaDistribution0 = R1ShapeScaleDistribution.Standard (
  178.             k0,
  179.             theta0,
  180.             gammaEstimator,
  181.             digammaEstimator,
  182.             lowerIncompleteGammaEstimator
  183.         );

  184.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  185.         System.out.println ("\t|                                        GAMMA FUNCTION KULLBACK-LIEBLER DIVERGENCE ESTIMATE                                      ||");

  186.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  187.         System.out.println ("\t|        L -> R:                                                                                                                  ||");

  188.         System.out.println ("\t|                - Shape, Scale                                                                                                   ||");

  189.         System.out.println ("\t|                - Values for different s                                                                                         ||");

  190.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  191.         for (double theta : thetaArray)
  192.         {
  193.             for (double k : kArray)
  194.             {
  195.                 R1ShapeScaleDistribution gammaDistribution = R1ShapeScaleDistribution.Standard (
  196.                     k,
  197.                     theta,
  198.                     gammaEstimator,
  199.                     digammaEstimator,
  200.                     lowerIncompleteGammaEstimator
  201.                 );

  202.                 System.out.println (
  203.                     "\t| [" + FormatUtil.FormatDouble (k, 1, 1, 1., false) + ", " +
  204.                     FormatUtil.FormatDouble (theta, 1, 1, 1., false) +
  205.                     "] =>" + " " + FormatUtil.FormatDouble (
  206.                         R1ShapeScaleComposite.KullbackLieblerDivergence (
  207.                             gammaDistribution0,
  208.                             gammaDistribution
  209.                         ), 1, 6, 1., false
  210.                     ) + " ||"
  211.                 );
  212.             }
  213.         }

  214.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  215.         EnvManager.TerminateEnv();
  216.     }
  217. }