Skip to content
fn

polygamma

→Tensor
polygamma(n: int, input: Tensor)

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. n = 0 recovers lucid.digamma, n = 1 is the trigamma function. Any order is accepted, though ψ(n)\psi^{(n)} grows like n!n! and overflows float32 for large n — see Notes.
inputTensor
Real argument; any floating-point dtype.

Returns

Tensor

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

Notes

Definition (for n >= 1):

ψ(n)(x)=dn+1dxn+1log⁡Γ(x)=(−1)n+1 n! ∑k=0∞1(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: for x >= 0 shift x upward by K=8K = 8 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. For x < 0 the n-th derivative of the reflection formula,

ψ(n)(x)=(−1)nψ(n)(1−x)−πdndxncot⁡(πx),\psi^{(n)}(x) = (-1)^n \psi^{(n)}(1 - x) - \pi \frac{d^n}{dx^n} \cot(\pi x),

maps the argument onto the right half. The non-positive integers (and -inf) are poles: n = 0 gives NaN there, except psi(+0) = -inf and psi(-0) = +inf; n >= 1 gives (−1)n+1∞(-1)^{n+1} \infty. The gradient is ψ(n+1)\psi^{(n+1)} everywhere, a pole's slope being the next order's pole value.

Raises ValueError for n < 0 and TypeError if n is not an integer (a float or a bool), as the reference does.

Accuracy was checked against an independent direct summation ψ(n)(x)=(−1)n+1n!∑k≥0(x+k)−(n+1)\psi^{(n)}(x) = (-1)^{n+1} n! \sum_{k \ge 0} (x+k)^{-(n+1)} for n=1…12n = 1 \dots 12 and x∈{0.3,1,2.5,7}x \in \{0.3, 1, 2.5, 7\}: the worst relative disagreement was 7e-11, which is the reference's own truncation floor rather than this routine's error, and is far below what float32 can represent anyway.

Note that ψ(n)(1)=(−1)n+1n! ζ(n+1)\psi^{(n)}(1) = (-1)^{n+1} n!\,\zeta(n+1), so the result itself exceeds the float32 range somewhere around n≈34n \approx 34; that is the dtype's limit, not the method's.

Examples

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

Used by 1

  • lucid._tensor._free_fn_methods