Skip to content

Robust ratio-study breakdown binning + explicit bins option#366

Open
drussellmrichie wants to merge 1 commit into
larsiusprime:masterfrom
drussellmrichie:fix/ratio-study-binning
Open

Robust ratio-study breakdown binning + explicit bins option#366
drussellmrichie wants to merge 1 commit into
larsiusprime:masterfrom
drussellmrichie:fix/ratio-study-binning

Conversation

@drussellmrichie

Copy link
Copy Markdown
Contributor

Extracts breakdown bin-edge computation into a pure helper (_compute_breakdown_edges) and fixes two issues:

  1. Robustness: the inline builder used np.quantile (returns NaN if the column has any NaN) and kept an edge whenever it was not in bins (no monotonicity guarantee). A heavily-skewed column (many rows sharing a value) collapsed the quantile edges to duplicates, so pd.cut raised "bins must increase monotonically" and crashed the entire ratio study. Now uses np.nanquantile, keeps only strictly-increasing edges, and returns no bins for a degenerate (all-NaN/constant) column so the breakdown is skipped instead of crashing.

  2. New bins breakdown option: explicit value edges with optional bin_labels. quantiles give equal-count bins (degenerate on heavily-skewed columns) and slice_size formats labels as integers (unusable for 0–1 columns); explicit bins cover those cases.

Adds unit tests for the helper.

Extract bin-edge computation into a pure helper (_compute_breakdown_edges)
and fix two issues it now handles:

1. Robustness: the inline builder used np.quantile (returns NaN if the column
   has any NaN) and kept an edge whenever it was "not in bins" (no
   monotonicity guarantee). A heavily-skewed column (many rows sharing a
   value) collapsed the quantile edges to duplicates, so pd.cut raised
   "bins must increase monotonically" and crashed the whole ratio study. Now
   uses np.nanquantile, keeps only strictly-increasing edges, and returns no
   bins for a degenerate (all-NaN/constant) column so the breakdown is skipped.

2. New "bins" breakdown option: explicit value edges with optional
   "bin_labels". quantiles give equal-count bins (degenerate on skewed
   columns) and slice_size formats labels as integers (unusable for 0-1
   columns); explicit bins cover those cases.

Adds unit tests for the helper.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution.
Please sign our CLA at the following link:
Click here to sign the CLA.

No action is required from you in this PR thread. Once you have signed the CLA externally, a maintainer will verify your signature and record it here on your behalf by commenting:


I affirm that this contributor has signed the CLA


Russell Richie seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

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