stan-dev / stan-dev/math

Numerical issues in neg_binomial_lccdf if beta is very large

Open
#2,031 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

Description

When the second argument to neg_binomial_lccdf is very large, the function can return -Inf even if the answer is finite.

Example

We take advantage of the fact the neg_binomial_lccdf(0|a, b) == log(1 - exp(neg_binomial_lpmf(0| a, b))
The formula then simplifies to neg_binomial_lccdf(0|a, b) == log1m( (b / (b + 1)) ^ a). Now for large b b/(b +1) is numerically exactly 1, but we can further rewrite as neg_binomial_lccdf(0|a, b) == log1m_exp(-a * log1p(1/b)).

So consider the following Stan program:

transformed data {
  real alpha = 1;
  real beta = 1e18;
  print(neg_binomial_lccdf(0 | alpha, beta));
  print(log1m_exp(-alpha * log1p(1/beta)));
}

Which outputs

-inf
-41.4465

Expected Output

-41.4465
-41.4465

Note that the two versions start to diverge only at around beta > 1e14, so this is unlikely to be super important in practice.

Current Version:

v3.3.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the neg_binomial_lccdf implementation and reproduce the Stan program from the issue with beta set to 1e18. The work is done when the function returns the finite value shown in the expected output, with coverage for this large-beta case.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.