Skip to content

Approximations

Helper functions for settings without a conjugate prior, such as the negative binomial dispersion parameter.

Approximation helpers for settings without a conjugate prior.

No conjugate prior exists for the negative binomial dispersion parameter r. Bradlow, Hardie and Fader (2002) obtain closed-form posterior inference for the negative binomial distribution by expanding ratios of gamma functions into polynomials. This module implements the truncated polynomial expansion of the gamma function ratio that makes their approach work, as a helper rather than a conjugate model.

negbin_gamma_ratio_approximation(*, z, k, m=50)

Approximate the gamma function ratio gamma(z) / gamma(z + k).

No conjugate prior exists for the negative binomial dispersion parameter r. Bradlow, Hardie and Fader (2002) obtain closed-form posterior inference for the negative binomial distribution by expanding ratios of gamma functions into polynomials. This helper implements the truncated polynomial expansion of the second of those ratios. It is an approximation helper, not a conjugate update.

What is exact:

  • The expansion starts from the identity gamma(z) / gamma(z + k) = 1 / (z * (z + 1) * ... * (z + k - 1)) which holds exactly for positive integer k.
  • In the full approach, the other gamma function ratio that appears, gamma(r + x) / gamma(r), is exactly the rising factorial r * (r + 1) * ... * (r + x - 1) and therefore exactly a degree x polynomial in r for integer counts x. No approximation is needed for that part.

What is approximate:

  • Writing y = z + k, each denominator factor is rewritten as 1 / (y - j) = (1 / y) / (1 - j / y) and the geometric series 1 / (1 - j / y) = sum_i (j / y) ** i is truncated. Keeping terms through order m of each series, multiplying the truncated polynomials together, and keeping terms through order m of the product gives a polynomial in 1 / y with coefficients U_0 = 1, U_1, ..., U_m. The returned approximation is y ** (-k) times that polynomial.

  • The truncation error vanishes as m grows and decreases as y grows relative to k. The approximation is reliable when z = y - k is not small compared to k; it can remain poor when z is small and k is large, no matter how large m is. Validate the error for your own parameter values.

Parameters:

Name Type Description Default
z NUMERIC

positive argument of the numerator gamma function. In the negative binomial application this is n * r + alpha_1 where n is the number of observations, r is the dispersion parameter, and alpha_1 is the first parameter of the beta prime prior on the rate.

required
k int

positive integer number of denominator factors. In the negative binomial application this is C1 - 1 where C1 = x_total + alpha_2 + 1 with x_total the sum of the observed counts and alpha_2 the second parameter of the beta prime prior on the rate.

required
m int

highest order of the expansion kept. Coefficients U_0 through U_m are retained, so larger values are more accurate and more costly. Defaults to 50.

50

Returns:

Type Description
NUMERIC

Approximate value of gamma(z) / gamma(z + k). A float for

NUMERIC

scalar z and a numpy array for array z.

Raises:

Type Description
ValueError

if z is not positive, or if k or m is not a positive integer. Integer valued floats are accepted for k and m.

TypeError

if k or m is not a number.

Example
import numpy as np

from conjugate.approximations import negbin_gamma_ratio_approximation

# counts x = [0, 1, 1, 2, 3, 5] with a beta prime prior on the
# rate with alpha_1 = alpha_2 = 2
n, x_total, alpha_1, alpha_2 = 6, 12, 2.0, 2.0
r = np.array([2.0, 5.0])
z = n * r + alpha_1
k = x_total + alpha_2

ratio = negbin_gamma_ratio_approximation(z=z, k=k, m=50)
References

Bradlow, E. T., Hardie, B. G. S., and Fader, P. S. (2002). "Bayesian Inference for the Negative Binomial Distribution via Polynomial Expansions." Journal of Computational and Graphical Statistics, 11(1), 189-201. doi:10.1198/106186002317375677

Source code in conjugate/approximations.py
def negbin_gamma_ratio_approximation(
    *,
    z: NUMERIC,
    k: int,
    m: int = 50,
) -> NUMERIC:
    """Approximate the gamma function ratio ``gamma(z) / gamma(z + k)``.

    No conjugate prior exists for the negative binomial dispersion
    parameter ``r``. Bradlow, Hardie and Fader (2002) obtain closed-form
    posterior inference for the negative binomial distribution by
    expanding ratios of gamma functions into polynomials. This helper
    implements the truncated polynomial expansion of the second of those
    ratios. It is an approximation helper, not a conjugate update.

    What is exact:

    - The expansion starts from the identity
      ``gamma(z) / gamma(z + k) = 1 / (z * (z + 1) * ... * (z + k - 1))``
      which holds exactly for positive integer ``k``.
    - In the full approach, the other gamma function ratio that appears,
      ``gamma(r + x) / gamma(r)``, is exactly the rising factorial
      ``r * (r + 1) * ... * (r + x - 1)`` and therefore exactly a degree
      ``x`` polynomial in ``r`` for integer counts ``x``. No
      approximation is needed for that part.

    What is approximate:

    - Writing ``y = z + k``, each denominator factor is rewritten as
      ``1 / (y - j) = (1 / y) / (1 - j / y)`` and the geometric series
      ``1 / (1 - j / y) = sum_i (j / y) ** i`` is truncated. Keeping
      terms through order ``m`` of each series, multiplying the
      truncated polynomials together, and keeping terms through order
      ``m`` of the product gives a polynomial in ``1 / y`` with
      coefficients ``U_0 = 1, U_1, ..., U_m``. The returned approximation
      is ``y ** (-k)`` times that polynomial.

    - The truncation error vanishes as ``m`` grows and decreases as
      ``y`` grows relative to ``k``. The approximation is reliable when
      ``z = y - k`` is not small compared to ``k``; it can remain poor
      when ``z`` is small and ``k`` is large, no matter how large ``m``
      is. Validate the error for your own parameter values.

    Args:
        z: positive argument of the numerator gamma function. In the
            negative binomial application this is ``n * r + alpha_1``
            where ``n`` is the number of observations, ``r`` is the
            dispersion parameter, and ``alpha_1`` is the first parameter
            of the beta prime prior on the rate.
        k: positive integer number of denominator factors. In the
            negative binomial application this is ``C1 - 1`` where
            ``C1 = x_total + alpha_2 + 1`` with ``x_total`` the sum of
            the observed counts and ``alpha_2`` the second parameter of
            the beta prime prior on the rate.
        m: highest order of the expansion kept. Coefficients ``U_0``
            through ``U_m`` are retained, so larger values are more
            accurate and more costly. Defaults to 50.

    Returns:
        Approximate value of ``gamma(z) / gamma(z + k)``. A float for
        scalar ``z`` and a numpy array for array ``z``.

    Raises:
        ValueError: if ``z`` is not positive, or if ``k`` or ``m`` is
            not a positive integer. Integer valued floats are accepted
            for ``k`` and ``m``.
        TypeError: if ``k`` or ``m`` is not a number.

    Example:
        ```python
        import numpy as np

        from conjugate.approximations import negbin_gamma_ratio_approximation

        # counts x = [0, 1, 1, 2, 3, 5] with a beta prime prior on the
        # rate with alpha_1 = alpha_2 = 2
        n, x_total, alpha_1, alpha_2 = 6, 12, 2.0, 2.0
        r = np.array([2.0, 5.0])
        z = n * r + alpha_1
        k = x_total + alpha_2

        ratio = negbin_gamma_ratio_approximation(z=z, k=k, m=50)
        ```

    References:
        Bradlow, E. T., Hardie, B. G. S., and Fader, P. S. (2002).
        "Bayesian Inference for the Negative Binomial Distribution via
        Polynomial Expansions." Journal of Computational and Graphical
        Statistics, 11(1), 189-201. doi:10.1198/106186002317375677
    """
    k = _validate_positive_integer(k, name="k")
    m = _validate_positive_integer(m, name="m")

    z_values = np.atleast_1d(np.asarray(z, dtype=float))
    if not np.all(z_values > 0) or not np.all(np.isfinite(z_values)):
        raise ValueError(f"z must be positive and finite, got {z}")

    results = []
    for z_value in z_values:
        y = z_value + k

        # each factor is y ** (-1) * sum_i (j / y) ** i * t ** i for
        # j = 1, ..., k and t = 1 / y; every entry is below 1 because
        # j < y, so no coefficient can overflow
        factors = (np.arange(1, k + 1)[:, None] / y) ** np.arange(m + 1) / y

        # multiply the truncated factors together, keeping terms through
        # order m of the product
        coefficients = factors[0]
        for factor in factors[1:]:
            coefficients = np.convolve(coefficients, factor)[: m + 1]

        results.append(coefficients.sum())

    results = np.asarray(results)
    if np.ndim(z) == 0:
        return float(results[0])

    return results

Comments