Skip to content

C port followup: accuracy and domain quirks in the special-function approximations #535

Backlog

Description

@nspope

These behaviours of the hypergeometric and polygamma approximations were kept as-is when the numerical code moved from numba to C (#534), and are worth a look. Reproducers call the low-level _tsdate module, which exposes each scalar function under its C name without the tsd_ prefix.

The reproducers that call internal functions of _tsdate (e.g. _tsdate.moments) run at commit d8a95ac; later commits in #534 removed those exports and test the functions in lib/tests/tests.c instead.

valid_hyp2f1 depends on the last bit of z for large negative z

tsd_valid_hyp2f1 (lib/approx.c) rejects z when z / (z - 1) >= 1. For z < 0 that ratio should always be below one, but once z - 1 rounds to z it equals 1, so for z between -2**54 and -2**53 the result alternates with the parity of the mantissa:

import _tsdate
big = 2.0**53
[_tsdate.valid_hyp2f1(1.0, 2.0, 3.0, z) for z in (-(big - 2), -big, -(big + 2), -(big + 4), -(big + 6))]
# [1.0, 0.0, 1.0, 0.0, 1.0]

When it returns false, the two-node moments (tsd_moments) and the EP update are skipped as invalid.

valid_hyp1f1 accepts b == a, but hyp1f1_laplace rejects it

tsd_valid_hyp1f1 (lib/approx.c) allows b >= a, while tsd_hyp1f1_laplace (lib/hypergeo.c) requires b > a and returns TSD_ERR_INVALID_HYPERGEO. So the validity check passes and the evaluation then fails:

_tsdate.valid_hyp1f1(1.0, 1.0, 1.0)              # 1.0
_tsdate.hyp1f1_laplace(1.0, 1.0, 1.0)            # LibraryError: Parameters outside the domain ...
_tsdate.leafward_moments(1.0, 1.0, 1.0, -1.0, 0.5)  # same error

This needs a mutation count of -1, so it is not reachable from dating, but the two checks disagree.

trigamma drops the constant term for small x

For 0 < x <= 1e-4, tsd_trigamma (lib/hypergeo.c) returns 1 / x**2 and leaves out the pi**2 / 6 term of the expansion, a relative error of up to about 1.6e-8 at x = 1e-4 (1.6e-10 at x = 1e-5 compared with scipy.special.polygamma(1, x)).

digamma and trigamma lose accuracy near negative integers

The reflection formula used for x <= 0 in tsd_digamma / tsd_trigamma loses precision near the poles. Compared with SciPy, the relative error is about 1e-15 at x = -3.5 but 8e-10 (digamma) and 1.7e-9 (trigamma) at x = -2.0000001.

betaln cancellation for very unequal arguments

tsd_betaln (lib/hypergeo.c) computes lgamma(p) + lgamma(q) - lgamma(p + q), which cancels badly when one argument is huge: _tsdate.betaln(1e-10, 1e10) has relative error about 1.1e-7 against scipy.special.betaln.

gammainc_der is inaccurate near its branch switch

tsd_gammainc_der (lib/hypergeo.c, the derivative of the regularised lower incomplete gamma with respect to p) switches from a series to a continued fraction when x > 1 and x >= p, and to a Gaussian approximation when p > 1000. The continued fraction is inaccurate near its switch, and there is a small jump at p = 1000:

import numpy as np
_tsdate.gammainc_der(1.0, 1.0)                        # -0.43173
_tsdate.gammainc_der(1.0, np.nextafter(1.0, 2))       # -0.42256  (2% jump)
_tsdate.gammainc_der(2.0, np.nextafter(2.0, 0))       # -0.29400
_tsdate.gammainc_der(2.0, 2.0)                        # -0.29585  (0.6% jump)
_tsdate.gammainc_der(1000.0, 1000.0)                  # -0.0126167140
_tsdate.gammainc_der(np.nextafter(1000.0, 2000), 1000.0)  # -0.0126170643 (rel. 2.8e-5)

The series values agree with a finite difference of scipy.special.gammainc (-0.431730 at (1, 1), -0.294005 at (2, 2)); the continued-fraction values are off. gammainc_der is used by approximate_gamma_iqr (rescaling of posteriors).

hyperu_laplace is inaccurate for a < 1

tsd_hyperu_laplace (lib/hypergeo.c) uses a Stirling-type approximation that degrades for small a:

  • _tsdate.hyperu_laplace(0.5, 1.0, 1.0)[0] is -0.1407, while log(scipy.special.hyperu(0.5, 1, 1)) is -0.1510.
  • As a -> 0 the derivative with respect to x can have the wrong sign: _tsdate.hyperu_laplace(0.01, 10.0, 10.0) gives +0.049, while the numerical derivative of log(hyperu) is -0.0036 (likewise for a = 1e-4, 1e-6).
  • _tsdate.hyperu_laplace(1.0, 1.0, 1e-300) returns (inf, -inf) although U(1, 1, x) is finite there.

The EP updates call it with a >= 1, so the first two points may not matter in practice.

hyp1f1_laplace is inaccurate for small a and overflows for large x

tsd_hyp1f1_laplace (lib/hypergeo.c):

  • _tsdate.hyp1f1_laplace(1e-3, 1.0, 1.0) returns -0.327, while log(scipy.special.hyp1f1(1e-3, 1, 1)) is 0.0013.
  • _tsdate.hyp1f1_laplace(1.0, 2.0, 1e15) returns -inf; the true value, log((exp(x) - 1) / x), is about 1e15.

hyp2f1_laplace is inaccurate near z = 1 and jumps at the unit-argument switch

tsd_hyp2f1_laplace (lib/hypergeo.c) switches to a separate unit-argument formula when isclose(z, 1) (relative tolerance 1e-5), and the two branches do not agree at the switch:

_tsdate.hyp2f1_laplace(0.3, 0.4, 1.5, 0.99998999)  # 0.153707  (Laplace branch)
_tsdate.hyp2f1_laplace(0.3, 0.4, 1.5, 0.99999)     # 0.166524  (unit-argument branch)

The jump is larger for small shapes (up to about -0.92 in log space), the approximation is inaccurate for small shapes near z = 1, and for tiny z it has absolute noise around 1e-14. The unit-argument helper also carries a TODO that its limits do not converge when c - a - b is close to zero.

hyp2f1_laplace can divide by c - a

With c == a, tsd_hyp2f1_laplace divides by c - a whenever the saddle point y falls inside (0, 1). With exact inputs this is caught earlier (y lands on the boundary and the domain error is returned), but for extreme random draws y can round into (0, 1) and the function then fails with TSD_ERR_ZERO_DIVISION ("Floating-point division by zero") rather than a domain error or a NaN.

Activity

  1. self-assigned this
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions