Skip to content

Evaluate OrderedLogistic probabilities in log space - #8657

Open
saifmodan2006 wants to merge 1 commit into
pymc-devs:mainfrom
saifmodan2006:fix/ordered-logistic-logp-8655
Open

saifmodan2006 wants to merge 1 commit into
pymc-devs:mainfrom
saifmodan2006:fix/ordered-logistic-logp-8655

Conversation

@saifmodan2006

Copy link
Copy Markdown
Contributor

Description

OrderedLogistic computed its category probabilities by subtracting
sigmoid 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 * -5 and
cutpoints = 8.1123 * [0.3887, 1.0153], the middle category logp was
-inf, while a stable log-space evaluation gives -43.721. The bug
was confirmed under normal compilation with all of eta, cutpoints
and the value as symbolic inputs, so it is not a constant-folding
artefact.

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. Probabilities are then
exp(log_p), so the {name}_probs deterministic keeps accurate
values. On ordinary inputs the probabilities match the previous
formula to within 3e-16.

No new documentation was needed: the change is internal to
compute_p and follows the existing OrderedProbit pattern.

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

  • Checked that the pre-commit linting/style checks pass
  • Included tests that prove the fix is effective or that the new feature works
  • Added necessary documentation (docstrings and/or example notebooks)
  • If you are a pro: each commit corresponds to a relevant logical change

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

Tests run

  • New regression test 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 with assert array(-inf) == -43.72 and passes with this fix.
  • tests/distributions/test_discrete.py::TestOrderedLogistic: 9 passed.
  • tests/distributions/test_discrete.py::TestOrderedProbit: 9 passed.
  • ruff check and ruff format --check pass on both changed files.

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
@read-the-docs-community

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.24%. Comparing base (bc3904b) to head (4b36f38).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            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     
Files with missing lines Coverage Δ
pymc/distributions/discrete.py 99.43% <100.00%> (+<0.01%) ⬆️

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OrderedLogistic loses precision in interior-category tail log probabilities

1 participant