org.knowceans.util
Class Gamma

java.lang.Object
  extended by org.knowceans.util.Gamma

public class Gamma
extends java.lang.Object

Gamma represents the Gamma function and its derivatives

Author:
heinrich

Field Summary
static double GAMMA
          Euler- Mascheroni constant
 
Constructor Summary
Gamma()
           
 
Method Summary
static double digamma(double x)
           Computes the digamma function of x.
static double digamma0(double x)
          Nonrecursive version, truncated Taylor series of Psi(x) = d/dx Gamma(x).
static double diPochhammer(double x, int n)
          Pochhammer digamma function
static int factorial(int n)
          faculty of an integer.
static long fak(int i)
          Recursive implementation of the factorial.
static double fdelta(double[] x)
           
static double fdelta(int[] x)
           
static double fdelta(int[] x, double alpha)
          "standard" fdelta function
static double fdelta(int K, double x)
           
static double fdeltaRatio(int[] nk, double[] alphak, int[] nkless, double[] tempk)
          compute the ratio of the dirichlet partition functions
static double fgamma(double x)
          gamma function
static double invdigamma(double y)
          coarse approximation of the inverse of the digamma function (after Eqs.
static double ldelta(double[] x)
          "Dirichlet delta function" is the partition function of the Dirichlet distribution and the k-dimensional generalisation of the beta function.
static double ldelta(int[] x)
           
static double ldelta(int[] x, double alpha)
          log Delta function with a symmetric concentration parameter alpha that is added to every element in the vector.
static double ldelta(int[] x, double[] alpha)
          log Delta function with a concentration parameter alpha that is added to every element in the vector.
static double ldelta(int K, double x)
          Symmetric version of the log Dirichlet delta function
static double lgamma(double x)
          Returns the natural logarithm of the gamma function Γ(x).
static double lgamma0(double x)
          truncated Taylor series of log Gamma(x).
static double trigamma(double x)
           Computes the trigamma function of x.
static double trigamma0(double x)
          Non-recursive version, truncated Taylor series of d/dx Psi(x) = d^2/dx^2 Gamma(x).
static double triPochhammer(double x, int n)
          Pochhammer trigamma function.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GAMMA

public static final double GAMMA
Euler- Mascheroni constant

Since:
2.0
See Also:
Constant Field Values
Constructor Detail

Gamma

public Gamma()
Method Detail

lgamma

public static double lgamma(double x)
Returns the natural logarithm of the gamma function Γ(x). The implementation of this method is based on: gregh: this implementation sets 0 argument to a very small double (1e-15) in order to cope with degenerate Dirichlet distributions

Parameters:
x - the value.
Returns:
log(Γ(x))

lgamma0

public static double lgamma0(double x)
truncated Taylor series of log Gamma(x). From lda-c

Parameters:
x -
Returns:

fgamma

public static double fgamma(double x)
gamma function

Parameters:
x -
Returns:

factorial

public static int factorial(int n)
faculty of an integer.

Parameters:
n -
Returns:

ldelta

public static double ldelta(double[] x)
"Dirichlet delta function" is the partition function of the Dirichlet distribution and the k-dimensional generalisation of the beta function. fdelta(a) = prod_k fgamma(a_k) / fgamma( sum_k a_k ) = int_(sum x = 1) prod_k x_k^(a_k-1) dx. See G. Heinrich: Parameter estimation for text analysis (http://www.arbylon.net/publications/text-est.pdf)

Parameters:
x -
Returns:

fdelta

public static double fdelta(double[] x)

ldelta

public static double ldelta(int[] x)

fdelta

public static double fdelta(int[] x)

ldelta

public static double ldelta(int[] x,
                            double alpha)
log Delta function with a symmetric concentration parameter alpha that is added to every element in the vector.

Parameters:
x -
alpha -
Returns:

ldelta

public static double ldelta(int[] x,
                            double[] alpha)
log Delta function with a concentration parameter alpha that is added to every element in the vector.

Parameters:
x -
alpha -
Returns:

fdelta

public static double fdelta(int[] x,
                            double alpha)
"standard" fdelta function

Parameters:
x -
alpha -
Returns:

ldelta

public static double ldelta(int K,
                            double x)
Symmetric version of the log Dirichlet delta function

Parameters:
K -
x -
Returns:

fdelta

public static double fdelta(int K,
                            double x)

fdeltaRatio

public static double fdeltaRatio(int[] nk,
                                 double[] alphak,
                                 int[] nkless,
                                 double[] tempk)
compute the ratio of the dirichlet partition functions

delta(n(k) + alpha(k)) / delta(n(k) - nless(k) + alpha(k))

Parameters:
nk - [K] numerator counts / integer part
alphak - [K] numerator hyperparameter / fractional part
nkless - [K] count vector that denominator is less than numerator
temp - [K] buffer used for computing
Returns:

digamma

public static double digamma(double x)

Computes the digamma function of x.

This is an independently written implementation of the algorithm described in Jose Bernardo, Algorithm AS 103: Psi (Digamma) Function, Applied Statistics, 1976.

Some of the constants have been changed to increase accuracy at the moderate expense of run-time. The result should be accurate to within 10^-8 absolute tolerance for x >= 10^-5 and within 10^-8 relative tolerance for x > 0.

Performance for large negative values of x will be quite expensive (proportional to |x|). Accuracy for negative values of x should be about 10^-8 absolute for results less than 10^5 and 10^-8 relative for results larger than that.

gregh: this implementation sets 0 argument to a very small double (1e-15) in order to cope with degenerate Dirichlet distributions

Parameters:
x - the argument
Returns:
digamma(x) to within 10-8 relative or absolute error whichever is smaller
Since:
2.0
See Also:
Digamma at wikipedia , Bernardo's original article

digamma0

public static double digamma0(double x)
Nonrecursive version, truncated Taylor series of Psi(x) = d/dx Gamma(x). From lda-c

Parameters:
x -
Returns:

invdigamma

public static double invdigamma(double y)
coarse approximation of the inverse of the digamma function (after Eqs. 132-135 in Minka (2003), Estimating a Dirichlet distribution)


trigamma

public static double trigamma(double x)

Computes the trigamma function of x. This function is derived by taking the derivative of the implementation of digamma.

Parameters:
x - the argument
Returns:
trigamma(x) to within 10-8 relative or absolute error whichever is smaller
Since:
2.0
See Also:
Trigamma at wikipedia , Gamma1#digamma(double)

trigamma0

public static double trigamma0(double x)
Non-recursive version, truncated Taylor series of d/dx Psi(x) = d^2/dx^2 Gamma(x). From lda-c

Parameters:
x -
Returns:

fak

public static long fak(int i)
Recursive implementation of the factorial.

Parameters:
i -
Returns:

diPochhammer

public static double diPochhammer(double x,
                                  int n)
Pochhammer digamma function

Parameters:
x -
n -
Returns:

triPochhammer

public static double triPochhammer(double x,
                                   int n)
Pochhammer trigamma function.

Parameters:
x -
n -
Returns: