Skip to content

Fix NaN gradients in Hurdle distributions for continuous components#8057

Merged
ricardoV94 merged 3 commits into
pymc-devs:mainfrom
pirzada-ahmadfaraz:fix/hurdle-dlogp-nan
Jan 17, 2026
Merged

Fix NaN gradients in Hurdle distributions for continuous components#8057
ricardoV94 merged 3 commits into
pymc-devs:mainfrom
pirzada-ahmadfaraz:fix/hurdle-dlogp-nan

Conversation

@pirzada-ahmadfaraz
Copy link
Copy Markdown
Contributor

Summary

Fixes the dlogp function returning NaN for HurdleGamma and HurdleLogNormal when observed data contains zeros.

Problem

The gradient computation (dlogp) was failing because pt.where evaluates both branches before selecting one. When computing logp(dist, 0) for continuous distributions like Gamma, this produces -inf, which results in NaN gradients - even though that branch is not selected for zero values.

Solution

Replace zero values with a safe dummy value (1.0) before computing the logp of the continuous distribution:

safe_value = pt.switch(pt.eq(value, 0), 1.0, value)
hurdle_logp = pt.where(
    pt.eq(value, 0),
    pt.log(1 - psi),
    pt.log(psi) + logp(dist, safe_value),  # Use safe_value instead of value
)

Since pt.where still selects the correct branch based on the condition, the final logp result is unchanged, but the gradient computation is now well-defined.

Testing

Added regression tests for both HurdleGamma and HurdleLogNormal that verify dlogp does not return NaN values.

Fixes #8053

The dlogp function was returning NaN for HurdleGamma and HurdleLogNormal
when observed data contained zeros. This was caused by pt.where evaluating
both branches - even when selecting log(1-psi) for zero values, the
logp(dist, 0) computation produced -inf for continuous distributions,
resulting in NaN gradients.

The fix replaces zero values with a safe dummy value (1.0) before computing
the logp of the continuous distribution. Since pt.where still selects the
correct branch based on the condition, the final logp result is unchanged,
but the gradient computation is now well-defined.

Fixes pymc-devs#8053
@ricardoV94
Copy link
Copy Markdown
Member

This is also fixed by pymc-devs/pytensor#1850 but this implementation is more robust

@ricardoV94
Copy link
Copy Markdown
Member

Pre-commit is failing

Comment thread tests/distributions/test_mixture.py Outdated

check_logp(HurdleLogNormal, Rplus, {"psi": Unit, "mu": R, "sigma": Rplusbig}, logp_fn)

def test_hurdle_gamma_dlogp_no_nan(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you pytest.mark.parametrize the test on HurdleGamma | HurdleLogNormal ? The logic is nearly the same

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.44%. Comparing base (c68c56e) to head (993fc16).
⚠️ Report is 54 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8057      +/-   ##
==========================================
+ Coverage   91.42%   91.44%   +0.01%     
==========================================
  Files         117      117              
  Lines       19154    19155       +1     
==========================================
+ Hits        17512    17516       +4     
+ Misses       1642     1639       -3     
Files with missing lines Coverage Δ
pymc/distributions/mixture.py 96.45% <100.00%> (+1.19%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pirzada-ahmadfaraz
Copy link
Copy Markdown
Contributor Author

@ricardoV94 ping

@ricardoV94 ricardoV94 merged commit 1f4c83a into pymc-devs:main Jan 17, 2026
42 checks passed
@welcome
Copy link
Copy Markdown

welcome Bot commented Jan 17, 2026

Congratulations Banner]
Congrats on merging your first pull request! 🎉 We here at PyMC are proud of you! 💖 Thank you so much for your contribution 🎁

@ricardoV94
Copy link
Copy Markdown
Member

Thanks @pirzada-ahmadfaraz

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dlogp fails for HurdleGamma observation model

2 participants