InfiniteSumEstimator.java

  1. package org.drip.specialfunction.loggamma;

  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>InfiniteSumEstimator</i> estimates Log Gamma using the Infinite Series Infinite Sum. The References
  76.  * are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Blagouchine, I. V. (2014): Re-discovery of Malmsten's Integrals, their Evaluation by Contour
  82.  *              Integration Methods, and some Related Results <i>Ramanujan Journal</i> <b>35 (1)</b> 21-110
  83.  *      </li>
  84.  *      <li>
  85.  *          Borwein, J. M., and R. M. Corless (2017): Gamma Function and the Factorial in the Monthly
  86.  *              https://arxiv.org/abs/1703.05349 <b>arXiv</b>
  87.  *      </li>
  88.  *      <li>
  89.  *          Davis, P. J. (1959): Leonhard Euler's Integral: A Historical Profile of the Gamma Function
  90.  *              <i>American Mathematical Monthly</i> <b>66 (10)</b> 849-869
  91.  *      </li>
  92.  *      <li>
  93.  *          Whitaker, E. T., and G. N. Watson (1996): <i>A Course on Modern Analysis</i> <b>Cambridge
  94.  *              University Press</b> New York
  95.  *      </li>
  96.  *      <li>
  97.  *          Wikipedia (2019): Gamma Function https://en.wikipedia.org/wiki/Gamma_function
  98.  *      </li>
  99.  *  </ul>
  100.  *
  101.  *  <br><br>
  102.  *  <ul>
  103.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  104.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FunctionAnalysisLibrary.md">Function Analysis Library</a></li>
  105.  *      <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>
  106.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/specialfunction/loggamma/README.md">Analytic/Series/Integral Log Gamma Estimators</a></li>
  107.  *  </ul>
  108.  *
  109.  * @author Lakshmi Krishnamurthy
  110.  */

  111. public abstract class InfiniteSumEstimator extends org.drip.numerical.estimation.R1ToR1Estimator
  112. {
  113.     private org.drip.numerical.estimation.R1ToR1Series _infiniteSumSeries = null;

  114.     /**
  115.      * Compute the Euler Infinite Sum Series of Log Gamma Estimator
  116.      *
  117.      * @param termCount Number of Terms in the Estimation
  118.      *
  119.      * @return The Euler Infinite Sum Series of Log Gamma Estimator
  120.      */

  121.     public static final InfiniteSumEstimator Euler (
  122.         final int termCount)
  123.     {
  124.         try
  125.         {
  126.             return new InfiniteSumEstimator (
  127.                 org.drip.specialfunction.loggamma.InfiniteSumSeries.Euler (termCount),
  128.                 null
  129.             )
  130.             {
  131.                 @Override public double evaluate (
  132.                     final double z)
  133.                     throws java.lang.Exception
  134.                 {
  135.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z) || z <= 0.)
  136.                     {
  137.                         throw new java.lang.Exception
  138.                             ("InfiniteSumEstimator::Euler::evaluate => Invalid Inputs");
  139.                     }

  140.                     return infiniteSumSeries().evaluate (z) - java.lang.Math.log (z);
  141.                 }
  142.             };
  143.         }
  144.         catch (java.lang.Exception e)
  145.         {
  146.             e.printStackTrace();
  147.         }

  148.         return null;
  149.     }

  150.     /**
  151.      * Compute the Weierstrass Infinite Sum Series of Log Gamma Estimator
  152.      *
  153.      * @param termCount Number of Terms in the Estimation
  154.      *
  155.      * @return The Weierstrass Infinite Sum Series of Log Gamma Estimator
  156.      */

  157.     public static final InfiniteSumEstimator Weierstrass (
  158.         final int termCount)
  159.     {
  160.         try
  161.         {
  162.             return new InfiniteSumEstimator (
  163.                 org.drip.specialfunction.loggamma.InfiniteSumSeries.Weierstrass (termCount),
  164.                 null
  165.             )
  166.             {
  167.                 @Override public double evaluate (
  168.                     final double z)
  169.                     throws java.lang.Exception
  170.                 {
  171.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z) || z <= 0.)
  172.                     {
  173.                         throw new java.lang.Exception
  174.                             ("InfiniteSumEstimator::Weierstrass::evaluate => Invalid Inputs");
  175.                     }

  176.                     return infiniteSumSeries().evaluate (z) - java.lang.Math.log (z) -
  177.                         z * org.drip.specialfunction.gamma.Definitions.EULER_MASCHERONI;
  178.                 }
  179.             };
  180.         }
  181.         catch (java.lang.Exception e)
  182.         {
  183.             e.printStackTrace();
  184.         }

  185.         return null;
  186.     }

  187.     /**
  188.      * Compute the Fourier Infinite Sum Series of Log Gamma Estimator
  189.      *
  190.      * @param termCount Number of Terms in the Estimation
  191.      *
  192.      * @return The Fourier Infinite Sum Series of Log Gamma Estimator
  193.      */

  194.     public static final InfiniteSumEstimator Fourier (
  195.         final int termCount)
  196.     {
  197.         try
  198.         {
  199.             return new InfiniteSumEstimator (
  200.                 org.drip.specialfunction.loggamma.InfiniteSumSeries.Fourier (termCount),
  201.                 null
  202.             )
  203.             {
  204.                 @Override public double evaluate (
  205.                     final double z)
  206.                     throws java.lang.Exception
  207.                 {
  208.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z) || 0. >= z || 1. <= z)
  209.                     {
  210.                         throw new java.lang.Exception
  211.                             ("InfiniteSumEstimator::Fourier::evaluate => Invalid Inputs");
  212.                     }

  213.                     return (0.5 - z) * (org.drip.specialfunction.gamma.Definitions.EULER_MASCHERONI +
  214.                         java.lang.Math.log (2.)) + (1. - z) * java.lang.Math.log (java.lang.Math.PI) -
  215.                         0.5 * java.lang.Math.log (java.lang.Math.sin (java.lang.Math.PI * z)) +
  216.                         infiniteSumSeries().evaluate (z) / java.lang.Math.PI;
  217.                 }
  218.             };
  219.         }
  220.         catch (java.lang.Exception e)
  221.         {
  222.             e.printStackTrace();
  223.         }

  224.         return null;
  225.     }

  226.     /**
  227.      * Compute the Blagouchine (2015) Infinite Sum Series of Log Gamma Estimator
  228.      *
  229.      * @param termCount Number of Terms in the Estimation
  230.      *
  231.      * @return The Blagouchine (2015) Infinite Sum Series of Log Gamma Estimator
  232.      */

  233.     public static final InfiniteSumEstimator Blagouchine2015 (
  234.         final int termCount)
  235.     {
  236.         try
  237.         {
  238.             return new InfiniteSumEstimator (
  239.                 org.drip.specialfunction.loggamma.InfiniteSumSeries.Blagouchine2015 (termCount),
  240.                 null
  241.             )
  242.             {
  243.                 @Override public double evaluate (
  244.                     final double z)
  245.                     throws java.lang.Exception
  246.                 {
  247.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z) || 0. >= z || 1. <= z || 0.5 == z)
  248.                     {
  249.                         throw new java.lang.Exception
  250.                             ("InfiniteSumEstimator::Blagouchine2015::evaluate => Invalid Inputs");
  251.                     }

  252.                     return (0.5 - z) * java.lang.Math.log (2. * java.lang.Math.PI) +
  253.                         0.5 * (
  254.                             java.lang.Math.log (java.lang.Math.PI) -
  255.                             java.lang.Math.log (java.lang.Math.sin (java.lang.Math.PI * z))
  256.                         ) +
  257.                         infiniteSumSeries().evaluate (z) / java.lang.Math.PI +
  258.                         org.drip.numerical.integration.NewtonCotesQuadratureGenerator.GaussLaguerreLeftDefinite (
  259.                             0.,
  260.                             100
  261.                         ).integrate (
  262.                             new org.drip.function.definition.R1ToR1 (null)
  263.                             {
  264.                                 @Override public double evaluate (
  265.                                     final double x)
  266.                                     throws java.lang.Exception
  267.                                 {
  268.                                     return 0. == x || java.lang.Double.isInfinite (x) ? 0. :
  269.                                         java.lang.Math.exp (-1. * termCount * x) *
  270.                                         java.lang.Math.log (x) / (
  271.                                             java.lang.Math.cosh (x) -
  272.                                             java.lang.Math.cos (2. * java.lang.Math.PI * z)
  273.                                         );
  274.                                 }
  275.                             }
  276.                         ) * java.lang.Math.sin (2. * java.lang.Math.PI * z) / (2. * java.lang.Math.PI);
  277.                 }
  278.             };
  279.         }
  280.         catch (java.lang.Exception e)
  281.         {
  282.             e.printStackTrace();
  283.         }

  284.         return null;
  285.     }

  286.     /**
  287.      * InfiniteSum Constructor
  288.      *
  289.      * @param infiniteSumSeries R<sup>1</sup> To R<sup>1</sup> Infinite Sum Series
  290.      * @param dc Differential Control
  291.      *
  292.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  293.      */

  294.     protected InfiniteSumEstimator (
  295.         final org.drip.numerical.estimation.R1ToR1Series infiniteSumSeries,
  296.         final org.drip.numerical.differentiation.DerivativeControl dc)
  297.         throws java.lang.Exception
  298.     {
  299.         super (dc);

  300.         _infiniteSumSeries = infiniteSumSeries;
  301.     }

  302.     /**
  303.      * Retrieve the Underlying Infinite Sum Series
  304.      *
  305.      * @return The Underlying Infinite Sum Series
  306.      */

  307.     public org.drip.numerical.estimation.R1ToR1Series infiniteSumSeries()
  308.     {
  309.         return _infiniteSumSeries;
  310.     }

  311.     @Override public org.drip.numerical.estimation.R1Estimate seriesEstimateNative (
  312.         final double x)
  313.     {
  314.         return null == _infiniteSumSeries ? seriesEstimate (
  315.             x,
  316.             null,
  317.             null
  318.         ) : seriesEstimate (
  319.             x,
  320.             _infiniteSumSeries.termWeightMap(),
  321.             _infiniteSumSeries
  322.         );
  323.     }

  324.     @Override public org.drip.function.definition.PoleResidue poleResidue (
  325.         final double x)
  326.     {
  327.         if (!org.drip.numerical.common.NumberUtil.IsValid (x))
  328.         {
  329.             return null;
  330.         }

  331.         int n = (int) x;

  332.         if (0 != (x - n) || x >= 0.)
  333.         {
  334.             return org.drip.function.definition.PoleResidue.NotAPole (x);
  335.         }

  336.         n = -n;

  337.         try
  338.         {
  339.             return new org.drip.function.definition.PoleResidue (
  340.                 x,
  341.                 (1 == n % 2 ? -1. : 1.) /
  342.                     new org.drip.specialfunction.gamma.NemesAnalytic (null).evaluate (n + 1.)
  343.             );
  344.         }
  345.         catch (java.lang.Exception e)
  346.         {
  347.             e.printStackTrace();
  348.         }

  349.         return null;
  350.     }
  351. }