Apply gini(negatives=...) option; guard degenerate cases - #289
Merged
Conversation
MaxGhenis
commented
Apr 17, 2026
MaxGhenis
left a comment
Collaborator
Author
There was a problem hiding this comment.
Self-review. Verified:
xis now used throughout rather thanself(the original bug). Both weighted/unweighted branches collapsed into one weighted implementation usingnp.average-style cumulative trapezoidal area.negatives="zero"usesnp.where(x<0, 0.0, x)(was buggy in-place which could have ducked the mutated-x bug).negatives="shift"subtractsnp.amin(x)only when min < 0. UnknownnegativesraisesValueError.- Degenerate cases: empty → NaN; all-zero total → 0.0 (not NaN/RuntimeWarning); negatives with
negatives=None→ warning that result may leave [0,1]. - Uses
kind="mergesort"for stable sort on ties. - Test covers warn path,
zeroclamp (closed-form = 2/3),shiftbounded in [0,1], all-zero = 0, invalid arg raises. - CI green Py 3.9-3.13.
Both branches of MicroSeries.gini sorted self directly rather than the local x that was mutated by the negatives option, so negatives='zero' and negatives='shift' were silently ignored: s = MicroSeries([-5, 0, 10], weights=[1,1,1]) s.gini(negatives='zero') # returned 2.0 (unchanged) s.gini(negatives='shift') # returned 2.0 (unchanged) Fix: use the mutated x throughout, collapse the two branches into a single weighted implementation, warn when called with negatives in the data and negatives=None (the Lorenz formula can return values outside [0, 1]), short-circuit all-zero and empty series to 0 (previously raised RuntimeWarning and returned NaN), and raise on an unknown negatives option.
MaxGhenis
force-pushed
the
fix/gini-negatives
branch
from
April 17, 2026 16:18
86f3984 to
fa33129
Compare
This was referenced Aug 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Both branches of
MicroSeries.ginisortedselfdirectly rather than the localxthat was mutated by thenegativesoption, sonegatives='zero'andnegatives='shift'were silently ignored. The raw formula with negatives can also return values outside[0, 1], and an all-zero series hit aZeroDivisionError/RuntimeWarning.Reproduction
Fix
xthroughoutnegatives=NonenegativesoptionTest plan
test_gini_negatives_option_appliedcoversnegatives=None(warns),'zero','shift', all-zero, and unknown option raising.