Repository navigation
Evaluate OrderedLogistic probabilities in log space - #8657
Open
saifmodan2006 wants to merge 1 commit into
Open
saifmodan2006 wants to merge 1 commit into
saifmodan2006 wants to merge 1 commit into
Conversation
Interior categories lost all precision when both neighbouring CDFs rounded to 0 or 1, so their probability came out as exactly 0 and their logp as -inf. compute_p now follows the OrderedProbit pattern and works in log space: log(sigmoid(z)) is -softplus(-z), and an interior log probability is log_cdf(hi) + log_ccdf(lo) + log(1 - exp(lo - hi)), which stays accurate in both tails. Fixes pymc-devs#8655 Tests: TestOrderedLogistic, 9 passed, including new test_logp_precise_in_tails
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8657 +/- ##
==========================================
+ Coverage 92.21% 92.24% +0.03%
==========================================
Files 129 130 +1
Lines 21567 21744 +177
==========================================
+ Hits 19887 20057 +170
- Misses 1680 1687 +7
🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
OrderedLogisticcomputed its category probabilities by subtractingsigmoid CDFs in probability space. When the cutpoints sit far into
either tail, both neighbouring CDFs round to 0 or 1, so an interior
category comes out with probability exactly 0 and logp of
-inf.For example, with
eta = 8.1123 * -5andcutpoints = 8.1123 * [0.3887, 1.0153], the middle category logp was-inf, while a stable log-space evaluation gives-43.721. The bugwas confirmed under normal compilation with all of
eta,cutpointsand the value as symbolic inputs, so it is not a constant-folding
artefact.
compute_pnow follows theOrderedProbitpattern and works in logspace:
log(sigmoid(z))is-softplus(-z), and an interior logprobability is
log_cdf(hi) + log_ccdf(lo) + log(1 - exp(lo - hi)),which stays accurate in both tails. Probabilities are then
exp(log_p), so the{name}_probsdeterministic keeps accuratevalues. On ordinary inputs the probabilities match the previous
formula to within 3e-16.
No new documentation was needed: the change is internal to
compute_pand follows the existingOrderedProbitpattern.AI assistance: the fix and the regression test were drafted with AI
assistance; all code was verified by running the tests below.
Related Issue
Checklist
Type of change
Tests run
tests/distributions/test_discrete.py::TestOrderedLogistic::test_logp_precise_in_tails: checks the middle-category logp against a stable SciPy reference in both tails (upper tail-43.720970551922655, lower tail mirror image) and checks the gradients stay finite. It fails on main withassert array(-inf) == -43.72and passes with this fix.tests/distributions/test_discrete.py::TestOrderedLogistic: 9 passed.tests/distributions/test_discrete.py::TestOrderedProbit: 9 passed.ruff checkandruff format --checkpass on both changed files.