Skip to content

Truncated distributions return NaN/inf when the truncation range lies far in the tail #607

Open

Description

@dweindl

If both truncation limits lie far enough in a tail that the untruncated CDF gives the same value at both (0 in the lower tail, 1.0 in the upper tail), the normalizer 1 / (F(trunc_high) - F(trunc_low)) divides by zero. This affects pdf, cdf, ppf and sample within the truncation range:

from petab.v1.distributions import Normal
d = Normal(0, 1, trunc=(-40, -38))  # RuntimeWarning: divide by zero
d.cdf([-41, -39, -37])  # [0, nan, 1]
d.pdf(-39)              # nan
d.ppf(0.5)              # -inf
d.sample(3)             # [-inf, -inf, -inf]

This is an edge case, but it can happen in petab.v2, because Parameter.prior_dist truncates priors at the parameter bounds, and those bounds can lie far in the tail of a narrow prior. Possible fixes: do the calculations in log space (logcdf/logsf), use the survival function for the upper tail, or at least raise an informative error in the constructor when the truncation range has (numerically) zero probability mass.

🤖 Generated with Claude Code

Activity

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