Skip to content

[BUG] Fix incorrect UBRE score: bitwise NOT (~) used instead of logical NOT#572

Open
tarun-227 wants to merge 1 commit intodswah:mainfrom
tarun-227:fix/ubre-bitwise-not-bug
Open

[BUG] Fix incorrect UBRE score: bitwise NOT (~) used instead of logical NOT#572
tarun-227 wants to merge 1 commit intodswah:mainfrom
tarun-227:fix/ubre-bitwise-not-bug

Conversation

@tarun-227
Copy link
Copy Markdown

@tarun-227 tarun-227 commented Apr 1, 2026

Summary

The UBRE (Un-Biased Risk Estimator) formula in _estimate_GCV_UBRE uses ~add_scale (bitwise NOT) where not add_scale (logical NOT) was intended.

In Python, ~True evaluates to -2 and ~False to -1, not 0 and 1 as expected:

>>> ~True   # -2, not 0
>>> ~False  # -1, not 1

This causes the UBRE score to be inflated by 2 * scale for every model with known scale (Poisson, Binomial). For Poisson models (scale=1), the reported UBRE is off by +2.0:

# Before (buggy):  UBRE = dev/n + 2*scale + 2*γ*edof*scale/n
# After (correct):  UBRE = dev/n + 2*γ*edof*scale/n
# Reference: Wood 2006, section 4.5.1, pg 177-182

Impact: While model selection via gridsearch is unaffected (constant offset doesn't change ranking), the UBRE values stored in statistics_["UBRE"] are incorrect. This matters for users who inspect or compare UBRE scores directly.

Fix: Replace ~add_scale with not add_scale on line 1223.

Test plan

  • Added test_UBRE_formula_matches_wood — verifies UBRE against the analytical formula from Wood 2006 for both add_scale=True and add_scale=False
  • All 163 tests pass
  • Pre-formatted with ruff format

The UBRE formula used `~add_scale` (bitwise NOT) instead of
`not add_scale` (logical NOT). In Python, `~True` evaluates to `-2`
and `~False` to `-1`, rather than the intended `0` and `1`.

This caused every UBRE score to be inflated by `2 * scale`, producing
incorrect values in `statistics_["UBRE"]` for models with known scale
(Poisson, Binomial). Model selection via gridsearch was unaffected
since the offset is constant, but the reported scores were wrong.

Reference: Wood 2006, section 4.5.1, pg 177-182.
@tarun-227 tarun-227 force-pushed the fix/ubre-bitwise-not-bug branch from cc801d9 to 1d5010a Compare April 1, 2026 08:23
@tarun-227
Copy link
Copy Markdown
Author

The test (3.12, windows-latest) failure is unrelated to this PR — it's a _tkinter.TclError: Can't find a usable init.tcl in test_gen_imgs.py::test_expectiles, caused by a broken Tcl/Tk installation on the GitHub Actions runner. The same failure appears on other open PRs (#570, #571). All 162 actual tests pass.

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.

1 participant