GammaInequalityLemma.java

  1. package org.drip.specialfunction.property;

  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>GammaInequalityLemma</i> contains the Verifiable Inequality Lemmas of the Gamma Function. The
  76.  * References 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/property/README.md">Special Function Property Lemma Verifiers</a></li>
  107.  *  </ul>
  108.  *
  109.  * @author Lakshmi Krishnamurthy
  110.  */

  111. public class GammaInequalityLemma
  112. {

  113.     /**
  114.      * Construct the Asymptotic Upper Approximate
  115.      *
  116.      * @param alpha Alpha
  117.      *
  118.      * @return The Asymptotic Upper Approximate
  119.      */

  120.     public static final org.drip.function.definition.R1ToR1Property AsymptoticUpperApproximate (
  121.         final double alpha)
  122.     {
  123.         if (!org.drip.numerical.common.NumberUtil.IsValid (alpha))
  124.         {
  125.             return null;
  126.         }

  127.         try
  128.         {
  129.             return new org.drip.function.definition.R1ToR1Property (
  130.                 org.drip.function.definition.R1ToR1Property.GTE,
  131.                 new org.drip.function.definition.R1ToR1 (null)
  132.                 {
  133.                     @Override public double evaluate (
  134.                         final double s)
  135.                         throws java.lang.Exception
  136.                     {
  137.                         if (!org.drip.numerical.common.NumberUtil.IsValid (s))
  138.                         {
  139.                             throw new java.lang.Exception
  140.                                 ("GammaInequalityLemma::AsymptoticUpperApproximate::evaluate => Invalid Inputs");
  141.                         }

  142.                         return org.drip.specialfunction.loggamma.InfiniteSumEstimator.Weierstrass (1638400).evaluate
  143.                             (s + alpha);
  144.                     }
  145.                 },
  146.                 new org.drip.function.definition.R1ToR1 (null)
  147.                 {
  148.                     @Override public double evaluate (
  149.                         final double s)
  150.                         throws java.lang.Exception
  151.                     {
  152.                         if (!org.drip.numerical.common.NumberUtil.IsValid (s))
  153.                         {
  154.                             throw new java.lang.Exception
  155.                                 ("GammaInequalityLemma::AsymptoticUpperApproximate::evaluate => Invalid Inputs");
  156.                         }

  157.                         return alpha * java.lang.Math.log (s)  +
  158.                             org.drip.specialfunction.loggamma.InfiniteSumEstimator.Weierstrass (1638400).evaluate (s);
  159.                     }
  160.                 },
  161.                 org.drip.function.definition.R1ToR1Property.MISMATCH_TOLERANCE
  162.             );
  163.         }
  164.         catch (java.lang.Exception e)
  165.         {
  166.             e.printStackTrace();
  167.         }

  168.         return null;
  169.     }

  170.     /**
  171.      * Generate the Exponentially Convex Inequality Verifier
  172.      *
  173.      * @param z1 z1
  174.      * @param z2 z2
  175.      *
  176.      * @return The Exponentially Convex Inequality Verifier
  177.      */

  178.     public static final org.drip.function.definition.R1ToR1Property ExponentiallyConvex (
  179.         final double z1,
  180.         final double z2)
  181.     {
  182.         if (!org.drip.numerical.common.NumberUtil.IsValid (z1) ||
  183.             !org.drip.numerical.common.NumberUtil.IsValid (z2))
  184.         {
  185.             return null;
  186.         }

  187.         try
  188.         {
  189.             return new org.drip.function.definition.R1ToR1Property (
  190.                 org.drip.function.definition.R1ToR1Property.LTE,
  191.                 new org.drip.function.definition.R1ToR1 (null)
  192.                 {
  193.                     @Override public double evaluate (
  194.                         final double t)
  195.                         throws java.lang.Exception
  196.                     {
  197.                         if (!org.drip.numerical.common.NumberUtil.IsValid (t) || 0. > t || 1. < t)
  198.                         {
  199.                             throw new java.lang.Exception
  200.                                 ("GammaInequalityLemma::ExponentiallyConvex::evaluate => Invalid Inputs");
  201.                         }

  202.                         return org.drip.specialfunction.loggamma.InfiniteSumEstimator.Weierstrass (1638400).evaluate
  203.                             (t * z1 + (1. - t) * z2);
  204.                     }
  205.                 },
  206.                 new org.drip.function.definition.R1ToR1 (null)
  207.                 {
  208.                     @Override public double evaluate (
  209.                         final double t)
  210.                         throws java.lang.Exception
  211.                     {
  212.                         if (!org.drip.numerical.common.NumberUtil.IsValid (t) || 0. > t || 1. < t)
  213.                         {
  214.                             throw new java.lang.Exception
  215.                                 ("GammaInequalityLemma::ExponentiallyConvex::evaluate => Invalid Inputs");
  216.                         }

  217.                         org.drip.specialfunction.loggamma.InfiniteSumEstimator weierStrass =
  218.                             org.drip.specialfunction.loggamma.InfiniteSumEstimator.Weierstrass (1638400);

  219.                         return t * weierStrass.evaluate (z1) + (1. - t) * weierStrass.evaluate (z2);
  220.                     }
  221.                 },
  222.                 org.drip.function.definition.R1ToR1Property.MISMATCH_TOLERANCE
  223.             );
  224.         }
  225.         catch (java.lang.Exception e)
  226.         {
  227.             e.printStackTrace();
  228.         }

  229.         return null;
  230.     }

  231.     /**
  232.      * Generate the Spaced Point Convex Inequality Verifier
  233.      *
  234.      * @param y y
  235.      *
  236.      * @return The Spaced Point Convex Inequality Verifier
  237.      */

  238.     public static final org.drip.function.definition.R1ToR1Property SpacedPointConvex (
  239.         final double y)
  240.     {
  241.         if (!org.drip.numerical.common.NumberUtil.IsValid (y))
  242.         {
  243.             return null;
  244.         }

  245.         final org.drip.specialfunction.loggamma.InfiniteSumEstimator weierStrass =
  246.             org.drip.specialfunction.loggamma.InfiniteSumEstimator.Weierstrass (1638400);

  247.         try
  248.         {
  249.             final double logGammaY = weierStrass.evaluate (y);

  250.             return new org.drip.function.definition.R1ToR1Property (
  251.                 org.drip.function.definition.R1ToR1Property.GT,
  252.                 new org.drip.function.definition.R1ToR1 (null)
  253.                 {
  254.                     @Override public double evaluate (
  255.                         final double x)
  256.                         throws java.lang.Exception
  257.                     {
  258.                         if (!org.drip.numerical.common.NumberUtil.IsValid (x) || x >= y)
  259.                         {
  260.                             throw new java.lang.Exception
  261.                                 ("GammaInequalityLemma::SpacedPointConvex::evaluate => Invalid Inputs");
  262.                         }

  263.                         return (logGammaY - weierStrass.evaluate (x)) / (y - x);
  264.                     }
  265.                 },
  266.                 new org.drip.function.definition.R1ToR1 (null)
  267.                 {
  268.                     @Override public double evaluate (
  269.                         final double x)
  270.                         throws java.lang.Exception
  271.                     {
  272.                         if (!org.drip.numerical.common.NumberUtil.IsValid (x))
  273.                         {
  274.                             throw new java.lang.Exception
  275.                                 ("GammaInequalityLemma::SpacedPointConvex::evaluate => Invalid Inputs");
  276.                         }

  277.                         org.drip.specialfunction.gamma.EulerIntegralSecondKind eulerIntegralSecondKind =
  278.                             new org.drip.specialfunction.gamma.EulerIntegralSecondKind (null);

  279.                         return eulerIntegralSecondKind.derivative (
  280.                             x,
  281.                             1
  282.                         ) - weierStrass.evaluate (x);
  283.                     }
  284.                 },
  285.                 org.drip.function.definition.R1ToR1Property.MISMATCH_TOLERANCE
  286.             );
  287.         }
  288.         catch (java.lang.Exception e)
  289.         {
  290.             e.printStackTrace();
  291.         }

  292.         return null;
  293.     }

  294.     /**
  295.      * Generate the Logarithmically Convex Inequality Verifier
  296.      *
  297.      * @return The Logarithmically Convex Inequality Verifier
  298.      */

  299.     public static final org.drip.function.definition.R1ToR1Property LogarithmicConvex()
  300.     {
  301.         final org.drip.specialfunction.loggamma.InfiniteSumEstimator weierStrass =
  302.             org.drip.specialfunction.loggamma.InfiniteSumEstimator.Weierstrass (1638400);

  303.         try
  304.         {
  305.             return new org.drip.function.definition.R1ToR1Property (
  306.                 org.drip.function.definition.R1ToR1Property.GT,
  307.                 new org.drip.function.definition.R1ToR1 (null)
  308.                 {
  309.                     @Override public double evaluate (
  310.                         final double z)
  311.                         throws java.lang.Exception
  312.                     {
  313.                         if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  314.                         {
  315.                             throw new java.lang.Exception
  316.                                 ("GammaInequalityLemma::LogarithmicConvex::evaluate => Invalid Inputs");
  317.                         }

  318.                         return java.lang.Math.log (
  319.                             new org.drip.specialfunction.gamma.EulerIntegralSecondKind (null).derivative (
  320.                                 z,
  321.                                 2
  322.                             )
  323.                         ) + weierStrass.evaluate (z);
  324.                     }
  325.                 },
  326.                 new org.drip.function.definition.R1ToR1 (null)
  327.                 {
  328.                     @Override public double evaluate (
  329.                         final double z)
  330.                         throws java.lang.Exception
  331.                     {
  332.                         if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  333.                         {
  334.                             throw new java.lang.Exception
  335.                                 ("GammaInequalityLemma::LogarithmicConvex::evaluate => Invalid Inputs");
  336.                         }

  337.                         return java.lang.Math.log (
  338.                             new org.drip.specialfunction.gamma.EulerIntegralSecondKind (null).derivative (
  339.                                 z,
  340.                                 1
  341.                             )
  342.                         );
  343.                     }
  344.                 },
  345.                 org.drip.function.definition.R1ToR1Property.MISMATCH_TOLERANCE
  346.             );
  347.         }
  348.         catch (java.lang.Exception e)
  349.         {
  350.             e.printStackTrace();
  351.         }

  352.         return null;
  353.     }

  354.     /**
  355.      * Generate the Gautschi Left Inequality Verifier
  356.      *
  357.      * @param s s
  358.      *
  359.      * @return The Gautschi Left Inequality Verifier
  360.      */

  361.     public static final org.drip.function.definition.R1ToR1Property GautschiLeft (
  362.         final double s)
  363.     {
  364.         if (!org.drip.numerical.common.NumberUtil.IsValid (s) || 0. >= s || 1. <= s)
  365.         {
  366.             return null;
  367.         }

  368.         final org.drip.specialfunction.loggamma.InfiniteSumEstimator weierStrass =
  369.             org.drip.specialfunction.loggamma.InfiniteSumEstimator.Weierstrass (1638400);

  370.         try
  371.         {
  372.             return new org.drip.function.definition.R1ToR1Property (
  373.                 org.drip.function.definition.R1ToR1Property.LT,
  374.                 new org.drip.function.definition.R1ToR1 (null)
  375.                 {
  376.                     @Override public double evaluate (
  377.                         final double z)
  378.                         throws java.lang.Exception
  379.                     {
  380.                         if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  381.                         {
  382.                             throw new java.lang.Exception
  383.                                 ("GammaInequalityLemma::GautschiLeft::evaluate => Invalid Inputs");
  384.                         }

  385.                         return (1. - s) * java.lang.Math.log (z);
  386.                     }
  387.                 },
  388.                 new org.drip.function.definition.R1ToR1 (null)
  389.                 {
  390.                     @Override public double evaluate (
  391.                         final double z)
  392.                         throws java.lang.Exception
  393.                     {
  394.                         if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  395.                         {
  396.                             throw new java.lang.Exception
  397.                                 ("GammaInequalityLemma::GautschiLeft::evaluate => Invalid Inputs");
  398.                         }

  399.                         return weierStrass.evaluate (z + 1) - weierStrass.evaluate (z + s);
  400.                     }
  401.                 },
  402.                 org.drip.function.definition.R1ToR1Property.MISMATCH_TOLERANCE
  403.             );
  404.         }
  405.         catch (java.lang.Exception e)
  406.         {
  407.             e.printStackTrace();
  408.         }

  409.         return null;
  410.     }

  411.     /**
  412.      * Generate the Gautschi Right Inequality Verifier
  413.      *
  414.      * @param s s
  415.      *
  416.      * @return The Gautschi Right Inequality Verifier
  417.      */

  418.     public static final org.drip.function.definition.R1ToR1Property GautschiRight (
  419.         final double s)
  420.     {
  421.         if (!org.drip.numerical.common.NumberUtil.IsValid (s) || 0. >= s || 1. <= s)
  422.         {
  423.             return null;
  424.         }

  425.         final org.drip.specialfunction.loggamma.InfiniteSumEstimator weierStrass =
  426.             org.drip.specialfunction.loggamma.InfiniteSumEstimator.Weierstrass (1638400);

  427.         try
  428.         {
  429.             return new org.drip.function.definition.R1ToR1Property (
  430.                 org.drip.function.definition.R1ToR1Property.LT,
  431.                 new org.drip.function.definition.R1ToR1 (null)
  432.                 {
  433.                     @Override public double evaluate (
  434.                         final double z)
  435.                         throws java.lang.Exception
  436.                     {
  437.                         if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  438.                         {
  439.                             throw new java.lang.Exception
  440.                                 ("GammaInequalityLemma::GautschiRight::evaluate => Invalid Inputs");
  441.                         }

  442.                         return weierStrass.evaluate (z + 1) - weierStrass.evaluate (z + s);
  443.                     }
  444.                 },
  445.                 new org.drip.function.definition.R1ToR1 (null)
  446.                 {
  447.                     @Override public double evaluate (
  448.                         final double z)
  449.                         throws java.lang.Exception
  450.                     {
  451.                         if (!org.drip.numerical.common.NumberUtil.IsValid (z))
  452.                         {
  453.                             throw new java.lang.Exception
  454.                                 ("GammaInequalityLemma::GautschiRight::evaluate => Invalid Inputs");
  455.                         }

  456.                         return (1. - s) * java.lang.Math.log (z + 1.);
  457.                     }
  458.                 },
  459.                 org.drip.function.definition.R1ToR1Property.MISMATCH_TOLERANCE
  460.             );
  461.         }
  462.         catch (java.lang.Exception e)
  463.         {
  464.             e.printStackTrace();
  465.         }

  466.         return null;
  467.     }

  468.     /**
  469.      * Generate the Jensen Multi-Point Interpolant Convexity Verification
  470.      *
  471.      * @param multiPoint2D Multi-Point 2D
  472.      *
  473.      * @return Jensen Multi-Point Interpolant Convexity Verification
  474.      */

  475.     public static final org.drip.function.definition.R1PropertyVerification JensenMultiPointInterpolant (
  476.         final org.drip.numerical.common.Array2D multiPoint2D)
  477.     {
  478.         if (null == multiPoint2D)
  479.         {
  480.             return null;
  481.         }

  482.         final org.drip.specialfunction.loggamma.InfiniteSumEstimator weierStrass =
  483.             org.drip.specialfunction.loggamma.InfiniteSumEstimator.Weierstrass (1638400);

  484.         double[] xArray = multiPoint2D.x();

  485.         double[] aArray = multiPoint2D.y();

  486.         double interpolantDenominator = 0.;
  487.         double interpolantNumerator = 0.;
  488.         int count = aArray.length;
  489.         double rValue = 0.;

  490.         for (int index = 0; index < count; ++index)
  491.         {
  492.             interpolantNumerator = interpolantNumerator + aArray[index] * xArray[index];
  493.             interpolantDenominator = interpolantDenominator + aArray[index];
  494.         }

  495.         double interpolantDenominatorInverse = 1. / interpolantDenominator;

  496.         try
  497.         {
  498.             double lValue = weierStrass.evaluate (interpolantNumerator* interpolantDenominatorInverse);

  499.             for (int index = 0; index < count; ++index)
  500.             {
  501.                 rValue = rValue + aArray[index] * weierStrass.evaluate (xArray[index]);
  502.             }

  503.             return new org.drip.function.definition.R1PropertyVerification (
  504.                 lValue,
  505.                 rValue = rValue * interpolantDenominatorInverse,
  506.                 lValue <= rValue
  507.             );
  508.         }
  509.         catch (java.lang.Exception e)
  510.         {
  511.             e.printStackTrace();
  512.         }

  513.         return null;
  514.     }
  515. }