Skip to content

beta_binomial_lpmf is unstable for large shape arguments #2508

Description

@martinmodrak

Description

As the shape arguments for the beta part get large (roughly > 1e14) the beta_binomial_lpmf function starts to return problematic values.

Example

beta_binomial_lpmf(500, 1000,  0.5 * 1e10,  0.5 * 1e10)
// -3.67992
beta_binomial_lpmf(500, 1000,  0.5 * 1e13,  0.5 * 1e13)
// -3.6802
beta_binomial_lpmf(500, 1000,  0.5 * 1e19,  0.5 * 1e19)
// 689.467

Expected Output

The values should converge towards binomial_lpmf(500, 1000, 0.5) = -3.67992.

Current Version:

v4.1.0

Proposed solution

It is possible that a similar trick to the one used in lbeta would be needed - compute separately a stirling approximation to the gamma functions involved, simplify analytically and then add corrections via lgamma_stirling_diff.

I'll try to start a PR with some tests demonstrating the problematic behaviour

Activity

  1. wds15 commented on Nov 19, 2024

    @wds15
    Contributor

    The log cdf functions of the beta binomial are also unstable. The below function is a direct call to the respective Stan math function:

    image

  2. avehtari commented on Oct 8, 2026

    @avehtari
    Member

    This is partially addressed by #3434 (not yet merged)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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