Skip to content

Fix/monotonic drop first - #1002

Merged
Matthias Schmidtblaicher (MatthiasSchmidtblaicherQC) merged 7 commits into
mainfrom
fix/monotonic-drop-first
May 5, 2026
Merged

Fix/monotonic drop first#1002
Matthias Schmidtblaicher (MatthiasSchmidtblaicherQC) merged 7 commits into
mainfrom
fix/monotonic-drop-first

Conversation

@MatthiasSchmidtblaicherQC

@MatthiasSchmidtblaicherQC Matthias Schmidtblaicher (MatthiasSchmidtblaicherQC) commented May 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Summary

  • Fix ValueError: Factor 'C(x)-' not found in feature_names when using monotonic_constraints with drop_first=True on categorical factors.
  • Add pickle backward compatibility for monotonic_constraints.

Root cause

_resolve_monotonic_constraints_from_model_spec reads factor names via str(ScopedFactor), which appends a - suffix when the factor is rank-reduced (drop_first=True). Feature names never carry this suffix (e.g. "C(education)[1.0]" not "C(education)-[1.0]"), so the downstream lookup in _build_monotonic_constraints fails.

Fix: strip trailing -/+ from the factor name before passing it on.

Test plan

  • test_monotonic_constraints_categorical_drop_first
  • Full formula test suite (87 tests)

Checklist

  • Added a CHANGELOG.rst entry

Formulaic appends a '-' (or '+') suffix to factor names in the model spec
when drop_first=True to signal rank reduction. The constraint resolution
code passed this suffixed name (e.g. "C(age)-") to _build_monotonic_constraints,
which failed to find it in feature_names_ (which use "C(age)[1]" etc.).

Strip the suffix with rstrip("-+") before the lookup.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Models pickled before this attribute existed will get None as default
when unpickled, avoiding AttributeError.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a ValueError encountered when using monotonic_constraints with categorical factors under drop_first=True by normalizing Formulaic factor names during constraint resolution, and it restores pickle backward compatibility for models saved before the monotonic_constraints attribute existed.

Changes:

  • Strip Formulaic’s trailing -/+ suffixes from factor names before matching them to generated feature names for monotonic constraint construction.
  • Add a missing default for monotonic_constraints in __setstate__ to support unpickling older models.
  • Add a regression test covering C(...) with drop_first=True and monotonic constraints; document the fix in the changelog.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
tests/glm/test_formula.py Adds regression test for categorical C(...) with drop_first=True under monotonic_constraints.
src/glum/_glm.py Ensures older pickles load by defaulting missing monotonic_constraints in __setstate__.
src/glum/_formula.py Normalizes factor names via suffix stripping before monotonic constraint lookup.
CHANGELOG.rst Adds 3.4.1 unreleased notes describing the bug fix and pickle compatibility.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@MatthiasSchmidtblaicherQC
Matthias Schmidtblaicher (MatthiasSchmidtblaicherQC) deleted the fix/monotonic-drop-first branch May 5, 2026 07:13
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.

3 participants