CumulativeSeriesEstimator.java

  1. package org.drip.specialfunction.digamma;

  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>CumulativeSeriesEstimator</i> implements the Cumulative Series Based Digamma Estimation. The References
  76.  * are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Abramowitz, M., and I. A. Stegun (2007): Handbook of Mathematics Functions <b>Dover Book on
  82.  *              Mathematics</b>
  83.  *      </li>
  84.  *      <li>
  85.  *          Blagouchine, I. V. (2018): Three Notes on Ser's and Hasse's Representations for the
  86.  *              Zeta-Functions https://arxiv.org/abs/1606.02044 <b>arXiv</b>
  87.  *      </li>
  88.  *      <li>
  89.  *          Mezo, I., and M. E. Hoffman (2017): Zeros of the Digamma Function and its Barnes G-function
  90.  *              Analogue <i>Integral Transforms and Special Functions</i> <b>28 (28)</b> 846-858
  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): Digamma Function https://en.wikipedia.org/wiki/Digamma_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/digamma/README.md">Estimation Techniques for Digamma Function</a></li>
  107.  *  </ul>
  108.  *
  109.  * @author Lakshmi Krishnamurthy
  110.  */

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

  114.     /**
  115.      * Compute the Abramowitz-Stegun (2007) Cumulative Series of Digamma Estimator
  116.      *
  117.      * @param termCount Number of Terms in the Estimation
  118.      *
  119.      * @return The Abramowitz-Stegun (2007) Cumulative Series of Digamma Estimator
  120.      */

  121.     public static final CumulativeSeriesEstimator AbramowitzStegun2007 (
  122.         final int termCount)
  123.     {
  124.         try
  125.         {
  126.             return new CumulativeSeriesEstimator (
  127.                 org.drip.specialfunction.digamma.CumulativeSeries.AbramowitzStegun2007 (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))
  136.                     {
  137.                         throw new java.lang.Exception
  138.                             ("CumulativeSeriesEstimator::AbramowitzStegun2007::evaluate => Invalid Inputs");
  139.                     }

  140.                     return cumulativeSeries().evaluate (z - 1.) -
  141.                         org.drip.specialfunction.gamma.Definitions.EULER_MASCHERONI;
  142.                 }
  143.             };
  144.         }
  145.         catch (java.lang.Exception e)
  146.         {
  147.             e.printStackTrace();
  148.         }

  149.         return null;
  150.     }

  151.     /**
  152.      * Compute the Harmonic Cumulative Series of Digamma Estimator
  153.      *
  154.      * @return The Harmonic Cumulative Series of Digamma Estimator
  155.      */

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

  174.                     double harmonicEstimate = 0.;

  175.                     for (int i = 1; i < (int) z; ++i)
  176.                     {
  177.                         harmonicEstimate = harmonicEstimate + (1. / i);
  178.                     }

  179.                     return harmonicEstimate - org.drip.specialfunction.gamma.Definitions.EULER_MASCHERONI;
  180.                 }
  181.             };
  182.         }
  183.         catch (java.lang.Exception e)
  184.         {
  185.             e.printStackTrace();
  186.         }

  187.         return null;
  188.     }

  189.     /**
  190.      * Compute the Half-Integer Cumulative Series of Digamma Estimator
  191.      *
  192.      * @return The Half-Integer Cumulative Series of Digamma Estimator
  193.      */

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

  212.                     double halfIntegerEstimate = 0.;

  213.                     for (int i = 1; i <= (int) z; ++i)
  214.                     {
  215.                         halfIntegerEstimate = halfIntegerEstimate + (1. / (-0.5 + i));
  216.                     }

  217.                     return halfIntegerEstimate - org.drip.specialfunction.gamma.Definitions.EULER_MASCHERONI
  218.                         - java.lang.Math.log (4.);
  219.                 }
  220.             };
  221.         }
  222.         catch (java.lang.Exception e)
  223.         {
  224.             e.printStackTrace();
  225.         }

  226.         return null;
  227.     }

  228.     /**
  229.      * Compute the Saddle-Point Cumulative Series of Digamma Estimator
  230.      *
  231.      * @param logGammaEstimator The Log Gamma Estimator
  232.      * @param saddlePointFunction The Saddle Point Generation Function
  233.      * @param saddlePointCount The Saddle Point Count
  234.      *
  235.      * @return The Saddle-Point Cumulative Series of Digamma Estimator
  236.      */

  237.     public static final CumulativeSeriesEstimator MezoHoffman2017 (
  238.         final org.drip.function.definition.R1ToR1 logGammaEstimator,
  239.         final org.drip.function.definition.R1ToR1 saddlePointFunction,
  240.         final int saddlePointCount)
  241.     {
  242.         if (null == logGammaEstimator)
  243.         {
  244.             return null;
  245.         }

  246.         try
  247.         {
  248.             return new CumulativeSeriesEstimator (
  249.                 org.drip.specialfunction.digamma.CumulativeSeries.MezoHoffman2017 (
  250.                     saddlePointFunction,
  251.                     saddlePointCount
  252.                 ),
  253.                 null
  254.             )
  255.             {
  256.                 @Override public double evaluate (
  257.                     final double z)
  258.                     throws java.lang.Exception
  259.                 {
  260.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  261.                     {
  262.                         throw new java.lang.Exception
  263.                             ("CumulativeSeriesEstimator::MezoHoffman2017::evaluate => Invalid Inputs");
  264.                     }

  265.                     return -1. * java.lang.Math.exp (
  266.                         logGammaEstimator.evaluate (z) +
  267.                         cumulativeSeries().evaluate (z) +
  268.                         2. * org.drip.specialfunction.gamma.Definitions.EULER_MASCHERONI * z
  269.                     );
  270.                 }
  271.             };
  272.         }
  273.         catch (java.lang.Exception e)
  274.         {
  275.             e.printStackTrace();
  276.         }

  277.         return null;
  278.     }

  279.     /**
  280.      * Compute the Gauss Cumulative Series of Digamma Estimator
  281.      *
  282.      * @param termCount Term Count
  283.      *
  284.      * @return The Gauss Cumulative Series of Digamma Estimator
  285.      */

  286.     public static final CumulativeSeriesEstimator Gauss (
  287.         final int termCount)
  288.     {
  289.         try
  290.         {
  291.             return new CumulativeSeriesEstimator (
  292.                 org.drip.specialfunction.digamma.CumulativeSeries.Gauss (termCount),
  293.                 null
  294.             )
  295.             {
  296.                 @Override public double evaluate (
  297.                     final double z)
  298.                     throws java.lang.Exception
  299.                 {
  300.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z) || 0. > z || 1. < z)
  301.                     {
  302.                         throw new java.lang.Exception
  303.                             ("CumulativeSeriesEstimator::Gauss::evaluate => Invalid Inputs");
  304.                     }

  305.                     return 2. * cumulativeSeries().evaluate (z) -
  306.                         org.drip.specialfunction.gamma.Definitions.EULER_MASCHERONI -
  307.                         java.lang.Math.log (2. * termCount) -
  308.                         0.5 * java.lang.Math.PI / java.lang.Math.tan (java.lang.Math.PI * z);
  309.                 }
  310.             };
  311.         }
  312.         catch (java.lang.Exception e)
  313.         {
  314.             e.printStackTrace();
  315.         }

  316.         return null;
  317.     }

  318.     /**
  319.      * Compute the Asymptotic Cumulative Series of Digamma Estimator
  320.      *
  321.      * @return The Asymptotic Cumulative Series of Digamma Estimator
  322.      */

  323.     public static final CumulativeSeriesEstimator Asymptotic()
  324.     {
  325.         try
  326.         {
  327.             return new CumulativeSeriesEstimator (
  328.                 org.drip.specialfunction.digamma.CumulativeSeries.Asymptotic(),
  329.                 null
  330.             )
  331.             {
  332.                 @Override public double evaluate (
  333.                     final double z)
  334.                     throws java.lang.Exception
  335.                 {
  336.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z) || 0. > z)
  337.                     {
  338.                         throw new java.lang.Exception
  339.                             ("CumulativeSeriesEstimator::Asymptotic::evaluate => Invalid Inputs");
  340.                     }

  341.                     return java.lang.Math.log (z) - 0.5 / z + cumulativeSeries().evaluate (z);
  342.                 }
  343.             };
  344.         }
  345.         catch (java.lang.Exception e)
  346.         {
  347.             e.printStackTrace();
  348.         }

  349.         return null;
  350.     }

  351.     /**
  352.      * Compute the Exponential Asymptotic Cumulative Series of Digamma Estimator
  353.      *
  354.      * @return The Exponential Asymptotic Cumulative Series of Digamma Estimator
  355.      */

  356.     public static final CumulativeSeriesEstimator ExponentialAsymptote()
  357.     {
  358.         try
  359.         {
  360.             return new CumulativeSeriesEstimator (
  361.                 org.drip.specialfunction.digamma.CumulativeSeries.ExponentialAsymptote(),
  362.                 null
  363.             )
  364.             {
  365.                 @Override public double evaluate (
  366.                     final double z)
  367.                     throws java.lang.Exception
  368.                 {
  369.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z) || 0. > z)
  370.                     {
  371.                         throw new java.lang.Exception
  372.                             ("CumulativeSeriesEstimator::ExponentialAsymptote::evaluate => Invalid Inputs");
  373.                     }

  374.                     return cumulativeSeries().evaluate (z);
  375.                 }
  376.             };
  377.         }
  378.         catch (java.lang.Exception e)
  379.         {
  380.             e.printStackTrace();
  381.         }

  382.         return null;
  383.     }

  384.     /**
  385.      * Compute the Exponential Asymptotic Cumulative Series of Digamma + 0.5 Estimator
  386.      *
  387.      * @return The Exponential Asymptotic Cumulative Series of Digamma + 0.5 Estimator
  388.      */

  389.     public static final CumulativeSeriesEstimator ExponentialAsymptoteHalfShifted()
  390.     {
  391.         try
  392.         {
  393.             return new CumulativeSeriesEstimator (
  394.                 org.drip.specialfunction.digamma.CumulativeSeries.ExponentialAsymptoteHalfShifted(),
  395.                 null
  396.             )
  397.             {
  398.                 @Override public double evaluate (
  399.                     final double z)
  400.                     throws java.lang.Exception
  401.                 {
  402.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  403.                     {
  404.                         throw new java.lang.Exception
  405.                             ("CumulativeSeriesEstimator::ExponentialAsymptoteHalfShifted::evaluate => Invalid Inputs");
  406.                     }

  407.                     return z - 0.5 + cumulativeSeries().evaluate (z - 0.5);
  408.                 }
  409.             };
  410.         }
  411.         catch (java.lang.Exception e)
  412.         {
  413.             e.printStackTrace();
  414.         }

  415.         return null;
  416.     }

  417.     /**
  418.      * Compute the Taylor-Riemann Zeta Cumulative Series of Digamma Estimator
  419.      *
  420.      * @param riemannZetaEstimator The Riemann-Zeta Estimator
  421.      * @param termCount Term Count
  422.      *
  423.      * @return The Taylor-Riemann Zeta Cumulative Series of Digamma Estimator
  424.      */

  425.     public static final CumulativeSeriesEstimator TaylorRiemannZeta (
  426.         final org.drip.function.definition.R1ToR1 riemannZetaEstimator,
  427.         final int termCount)
  428.     {
  429.         try
  430.         {
  431.             return new CumulativeSeriesEstimator (
  432.                 org.drip.specialfunction.digamma.CumulativeSeries.TaylorRiemannZeta (
  433.                     riemannZetaEstimator,
  434.                     termCount
  435.                 ),
  436.                 null
  437.             )
  438.             {
  439.                 @Override public double evaluate (
  440.                     final double z)
  441.                     throws java.lang.Exception
  442.                 {
  443.                     if (!org.drip.numerical.common.NumberUtil.IsValid (z) || 0. > z)
  444.                     {
  445.                         throw new java.lang.Exception
  446.                             ("CumulativeSeriesEstimator::TaylorRiemannZeta::evaluate => Invalid Inputs");
  447.                     }

  448.                     return -1. * cumulativeSeries().evaluate (z - 1.) -
  449.                         org.drip.specialfunction.gamma.Definitions.EULER_MASCHERONI;
  450.                 }
  451.             };
  452.         }
  453.         catch (java.lang.Exception e)
  454.         {
  455.             e.printStackTrace();
  456.         }

  457.         return null;
  458.     }

  459.     protected CumulativeSeriesEstimator (
  460.         final org.drip.numerical.estimation.R1ToR1Series cumulativeSeries,
  461.         final org.drip.numerical.differentiation.DerivativeControl dc)
  462.         throws java.lang.Exception
  463.     {
  464.         super (dc);

  465.         _cumulativeSeries = cumulativeSeries;
  466.     }

  467.     /**
  468.      * Retrieve the Underlying Cumulative Series
  469.      *
  470.      * @return The Underlying Cumulative Series
  471.      */

  472.     public org.drip.numerical.estimation.R1ToR1Series cumulativeSeries()
  473.     {
  474.         return _cumulativeSeries;
  475.     }
  476. }