Skip to content

BUG: Raise TypeError for non-integer float observed data on discrete distributions#8285

Draft
kimjune01 wants to merge 4 commits into
pymc-devs:mainfrom
kimjune01:fix/discrete-float-observed-warning
Draft

BUG: Raise TypeError for non-integer float observed data on discrete distributions#8285
kimjune01 wants to merge 4 commits into
pymc-devs:mainfrom
kimjune01:fix/discrete-float-observed-warning

Conversation

@kimjune01
Copy link
Copy Markdown

Description

Model.make_obs_var silently truncates float observed data to integers when the distribution has an integer dtype (e.g. Binomial, Poisson). This can produce wrong posteriors with no indication that anything went wrong.

This PR adds a check in make_obs_var that raises TypeError when float observed data contains non-integer values that would lose information on cast. Float arrays with exact integer values (e.g. [0.0, 1.0]) are still accepted, since this is a common pattern with floatX data.

Handles ndarray, MaskedArray, and sparse matrices.

Closes #8282

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)

Type of change

  • Bug fix

…iscrete distributions

Fixes pymc-devs#8282. Discrete distributions (Binomial, Poisson, etc.) silently
cast float observed values to integers via .astype(), truncating data
without warning. Add a validation check in make_obs_var() that detects
when float-to-integer casting would alter values.

The check is narrowly scoped:
- Only fires for concrete data (not symbolic Variables)
- Only fires for float->integer dtype casts
- Allows float arrays with exact integer values (e.g. [0.0, 1.0])
- Handles masked arrays via .compressed()
- Handles scipy sparse matrices via .data attribute
@welcome
Copy link
Copy Markdown

welcome Bot commented May 9, 2026

Thank You Banner]
💖 Thanks for opening this pull request! 💖 The PyMC community really appreciates your time and effort to contribute to the project. Please make sure you have read our Contributing Guidelines and filled in our pull request template to the best of your ability.

@github-actions github-actions Bot added the bug label May 9, 2026
@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented May 9, 2026

@codecov
Copy link
Copy Markdown

codecov Bot commented May 9, 2026

Codecov Report

❌ Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.94%. Comparing base (9491f60) to head (10fcb80).

Files with missing lines Patch % Lines
pymc/model/core.py 92.30% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8285      +/-   ##
==========================================
- Coverage   91.94%   91.94%   -0.01%     
==========================================
  Files         125      125              
  Lines       20302    20314      +12     
==========================================
+ Hits        18667    18678      +11     
- Misses       1635     1636       +1     
Files with missing lines Coverage Δ
pymc/model/core.py 93.44% <92.30%> (-0.03%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

kimjune01 added 3 commits May 18, 2026 12:44
main refactored scipy.sparse to a lazy module (sparse = lazy_scipy_module("sparse"))
and removed 'import scipy.sparse as sps'. Merging main left our new
make_obs_var check referencing the now-undefined 'sps'. Switch to the
'sparse' alias to match the current convention.
@kimjune01 kimjune01 marked this pull request as draft May 20, 2026 21:39
@kimjune01 kimjune01 marked this pull request as ready for review May 21, 2026 02:12
@kimjune01 kimjune01 marked this pull request as draft May 22, 2026 13:28
@kimjune01
Copy link
Copy Markdown
Author

Cannot validate on current setup, so drafting. Please close or take it over.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Discrete distributions silently cast float observed values to integers

1 participant