CombinatorialEstimate.java

  1. package org.drip.specialfunction.beta;

  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>CombinatorialEstimate</i> implements the Combinatorial Function Estimate using Beta-based Schemes. The
  76.  * References are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Abramowitz, M., and I. A. Stegun (2007): <i>Handbook of Mathematics Functions</i> <b>Dover Book
  82.  *              on Mathematics</b>
  83.  *      </li>
  84.  *      <li>
  85.  *          Davis, P. J. (1959): Leonhard Euler's Integral: A Historical Profile of the Gamma Function
  86.  *              <i>American Mathematical Monthly</i> <b>66 (10)</b> 849-869
  87.  *      </li>
  88.  *      <li>
  89.  *          Whitaker, E. T., and G. N. Watson (1996): <i>A Course on Modern Analysis</i> <b>Cambridge
  90.  *              University Press</b> New York
  91.  *      </li>
  92.  *      <li>
  93.  *          Wikipedia (2019): Beta Function https://en.wikipedia.org/wiki/Beta_function
  94.  *      </li>
  95.  *      <li>
  96.  *          Wikipedia (2019): Gamma Function https://en.wikipedia.org/wiki/Gamma_function
  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/FunctionAnalysisLibrary.md">Function Analysis Library</a></li>
  104.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/specialfunction/README.md">Special Function Implementation Analysis</a></li>
  105.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/specialfunction/beta/README.md">Estimation Techniques for Beta Function</a></li>
  106.  *  </ul>
  107.  *
  108.  * @author Lakshmi Krishnamurthy
  109.  */

  110. public class CombinatorialEstimate
  111. {

  112.     /**
  113.      * Estimate the Binomial Coefficient Using the Beta Function
  114.      *
  115.      * @param n n
  116.      * @param k k
  117.      * @param betaEstimator The Beta Function Estimator
  118.      *
  119.      * @return Binomial Coefficient Using the Beta Function
  120.      *
  121.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  122.      */

  123.     public static final double BetaBinomial (
  124.         final double n,
  125.         final double k,
  126.         final org.drip.function.definition.R2ToR1 betaEstimator)
  127.         throws java.lang.Exception
  128.     {
  129.         if (!org.drip.numerical.common.NumberUtil.IsValid (n) || 0. >= n ||
  130.             !org.drip.numerical.common.NumberUtil.IsValid (k) || 0. >= k ||
  131.             n < k ||
  132.             null == betaEstimator)
  133.         {
  134.             throw new java.lang.Exception ("CombinatorialEstimate::BetaBinomial => Invalid Inputs");
  135.         }

  136.         return 1. / (
  137.             (n + 1.) * betaEstimator.evaluate (
  138.                 n - k + 1.,
  139.                 k + 1.
  140.             )
  141.         );
  142.     }

  143.     /**
  144.      * Estimate the Binomial Coefficient Using a Continuous Interpolation Function
  145.      *
  146.      * @param n n
  147.      * @param k k
  148.      * @param gammaEstimator The Gamma Function Estimator
  149.      *
  150.      * @return Binomial Coefficient Using a Continuous Interpolation Function
  151.      *
  152.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  153.      */

  154.     public static final double GammaBinomial (
  155.         final double n,
  156.         final double k,
  157.         final org.drip.function.definition.R1ToR1 gammaEstimator)
  158.         throws java.lang.Exception
  159.     {
  160.         if (!org.drip.numerical.common.NumberUtil.IsValid (n) || 0. >= n ||
  161.             !org.drip.numerical.common.NumberUtil.IsValid (k) || 0. >= k ||
  162.             n < k ||
  163.             null == gammaEstimator)
  164.         {
  165.             throw new java.lang.Exception ("CombinatorialEstimate::GammaBinomial => Invalid Inputs");
  166.         }

  167.         double gammaBinomial = (1 == n % 2 ? -1. : 1.) * gammaEstimator.evaluate (n + 1.) *
  168.             java.lang.Math.sin (java.lang.Math.PI * k) / java.lang.Math.PI;

  169.         for (int i = 0; i <= n; ++i)
  170.         {
  171.             gammaBinomial = gammaBinomial / (k - i);
  172.         }

  173.         return gammaBinomial;
  174.     }

  175.     /**
  176.      * Compute the Cumulative Binomial Distribution Function for the specified n, k, and p
  177.      *
  178.      * @param n n
  179.      * @param k k
  180.      * @param p p
  181.      * @param incompleteRegularizedEstimator The Incomplete Regularized Beta Estimator
  182.      *
  183.      * @return The Cumulative Binomial Distribution Function Value
  184.      *
  185.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  186.      */

  187.     public static final double CumulativeBinomialDistribution (
  188.         final double n,
  189.         final double k,
  190.         final double p,
  191.         final org.drip.specialfunction.beta.IncompleteRegularizedEstimator incompleteRegularizedEstimator)
  192.         throws java.lang.Exception
  193.     {
  194.         if (!org.drip.numerical.common.NumberUtil.IsValid (n) || 0. >= n ||
  195.             !org.drip.numerical.common.NumberUtil.IsValid (k) || 0. >= k ||
  196.             n < k ||
  197.             !org.drip.numerical.common.NumberUtil.IsValid (p) || 0. > p || 1. < p ||
  198.             null == incompleteRegularizedEstimator)
  199.         {
  200.             throw new java.lang.Exception
  201.                 ("CombinatorialEstimate::CumulativeBinomialDistribution => Invalid Inputs");
  202.         }

  203.         return incompleteRegularizedEstimator.evaluate (
  204.             1. - p,
  205.             n - k,
  206.             k + 1.
  207.         );
  208.     }
  209. }