Digamma.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.numbers.gamma;

  18. /**
  19.  * <a href="http://en.wikipedia.org/wiki/Digamma_function">Digamma function</a>.
  20.  * <p>
  21.  * It is defined as the logarithmic derivative of the \( \Gamma \)
  22.  * ({@link Gamma}) function:
  23.  * \( \frac{d}{dx}(\ln \Gamma(x)) = \frac{\Gamma^\prime(x)}{\Gamma(x)} \).
  24.  * </p>
  25.  *
  26.  * @see Gamma
  27.  */
  28. public final class Digamma {
  29.     /** <a href="http://en.wikipedia.org/wiki/Euler-Mascheroni_constant">Euler-Mascheroni constant</a>. */
  30.     private static final double GAMMA = 0.577215664901532860606512090082;

  31.     /** C limit. */
  32.     private static final double C_LIMIT = 49;
  33.     /** S limit. */
  34.     private static final double S_LIMIT = 1e-5;
  35.     /** Fraction. */
  36.     private static final double F_M1_12 = -1d / 12;
  37.     /** Fraction. */
  38.     private static final double F_1_120 = 1d / 120;
  39.     /** Fraction. */
  40.     private static final double F_M1_252 = -1d / 252;

  41.     /** Private constructor. */
  42.     private Digamma() {
  43.         // intentionally empty.
  44.     }

  45.     /**
  46.      * Computes the digamma function.
  47.      *
  48.      * This is an independently written implementation of the algorithm described in
  49.      * <a href="http://www.uv.es/~bernardo/1976AppStatist.pdf">Jose Bernardo,
  50.      * Algorithm AS 103: Psi (Digamma) Function, Applied Statistics, 1976</a>.
  51.      * A <a href="https://en.wikipedia.org/wiki/Digamma_function#Reflection_formula">
  52.      * reflection formula</a> is incorporated to improve performance on negative values.
  53.      *
  54.      * Some of the constants have been changed to increase accuracy at the moderate
  55.      * expense of run-time.  The result should be accurate to within {@code 1e-8}.
  56.      * relative tolerance for {@code 0 < x < 1e-5}  and within {@code 1e-8} absolute
  57.      * tolerance otherwise.
  58.      *
  59.      * @param x Argument.
  60.      * @return digamma(x) to within {@code 1e-8} relative or absolute error whichever
  61.      * is larger.
  62.      */
  63.     public static double value(double x) {
  64.         if (!Double.isFinite(x)) {
  65.             return x;
  66.         }

  67.         double digamma = 0;
  68.         if (x < 0) {
  69.             // Use reflection formula to fall back into positive values.
  70.             digamma -= Math.PI / Math.tan(Math.PI * x);
  71.             x = 1 - x;
  72.         }

  73.         if (x > 0 && x <= S_LIMIT) {
  74.             // Use method 5 from Bernardo AS103, accurate to O(x).
  75.             return digamma - GAMMA - 1 / x;
  76.         }

  77.         while (x < C_LIMIT) {
  78.             digamma -= 1 / x;
  79.             x += 1;
  80.         }

  81.         // Use method 4, accurate to O(1/x^8)
  82.         final double inv = 1 / (x * x);
  83.         //            1       1        1         1
  84.         // log(x) -  --- - ------ + ------- - -------
  85.         //           2 x   12 x^2   120 x^4   252 x^6
  86.         digamma += Math.log(x) - 0.5 / x + inv * (F_M1_12 + inv * (F_1_120 + F_M1_252 * inv));

  87.         return digamma;
  88.     }
  89. }