fn

polygamma

Tensor
polygamma(n: int, x: Tensor)
source

Polygamma function ψ(n)(x)\psi^{(n)}(x).

The n-th derivative of the digamma function. Polygamma functions appear in the derivatives of log-partition functions of the Gamma / Beta / Dirichlet families and are needed for evaluating Fisher information matrices for these distributions analytically.

Parameters

nint
Non-negative integer order. Only n{0,1,2,3}n \in \{0, 1, 2, 3\} are supported; n = 0 recovers lucid.digamma, n = 1 is the trigamma function. Higher orders raise NotImplementedError.
xTensor
Real argument; any floating-point dtype.

Returns

Tensor

ψ(n)(x)\psi^{(n)}(x) element-wise, same shape and dtype as x.

Notes

Definition (for n >= 1):

ψ(n)(x)=dn+1dxn+1logΓ(x)=(1)n+1n!k=01(x+k)n+1.\psi^{(n)}(x) = \frac{d^{n+1}}{dx^{n+1}} \log \Gamma(x) = (-1)^{n+1}\, n!\, \sum_{k=0}^\infty \frac{1}{(x + k)^{n+1}}.

Implementation: shift x upward by K=6K = 6 using the recurrence

ψ(n)(x)=ψ(n)(x+1)+(1)n+1n!xn+1,\psi^{(n)}(x) = \psi^{(n)}(x + 1) + \frac{(-1)^{n+1} n!}{x^{n+1}},

accumulate the per-step corrections, then evaluate the Bernoulli asymptotic series at the shifted argument where it is well-conditioned. Accuracy is roughly seven decimal digits across the positive-real regime.

Raises ValueError for n < 0 and NotImplementedError for n >= 4.

Examples

>>> import lucid
>>> from lucid.special import polygamma
>>> polygamma(1, lucid.tensor([1.0, 2.0, 5.0]))
Tensor([1.6449, 0.6449, 0.2213])