Repository navigation
fix: refuse fit inputs that leave no observation standing - #40
Merged
Merged
Conversation
`remove_nans` treats three things as drop markers: a `NaN` observation, a `NaN` weight, and a zero weight. `fit` then rescales by `w.mean()` and builds the interpolation grid from what is left. When every row carries a marker nothing is left, so the rescaling divides by zero and the grid is built from an empty sample — which terminates the process rather than raising. Reachable from `fit(x, weights)` with all-`NaN` or all-zero weights, and from `fit(x)` alone with all-`NaN` observations. Two weight values are not markers and were passing through: an infinite weight normalizes every other one to zero, and a negative weight yields a density that is not the weighted one, silently. So `check_inputs` gains the two weight checks — it is reached with weights only from `fit`, the evaluation paths calling it with observations alone — and `fit` checks for survivors immediately after `remove_nans`, which is where the answer already is. Partial markers stay legal and are unaffected: a `NaN` weight and a zero weight still drop their own row and agree with each other, which the new test pins alongside the refusals. Found while auditing the Python bindings in vinecopulib/pyvinecopulib#326, where the crash surfaced as a segfault in `Kde1d.fit(x, weights)`.
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.
Why
Kde1d::fitterminates the process — aSIGSEGV, not an exception — on threeinputs a caller can reach directly:
tools::remove_nansdocuments three drop markers — aNaNobservation, aNaNweight, and a zero weight.fitthen doesw /= w.mean()and builds theinterpolation grid from the survivors. When every row carries a marker there are
none: the rescaling divides by zero and the grid is built from an empty sample.
Two weight values are not markers at all and were passing straight through:
What changed
check_inputsgains the infinite and negative weight checks. It is reachedwith a non-empty
weightsonly fromfit— thepdf/cdf/quantilepathscall it with observations alone — so evaluation is unaffected.
fitchecks for survivors immediately afterremove_nans, which is wherethe answer already is, rather than re-deriving it.
@throwslist onfitand its "observations that are NaN are dropped"note now say that a
NaNor zero weight drops its row too.Partial markers stay legal and are untouched. A
NaNweight and a zeroweight each drop their own row and nothing else, and they agree with each
other — the existing "works with weights" section relies on the first and the
existing boundary-expert test on the second. The new test pins that agreement
next to the refusals, so a future tightening cannot quietly turn a drop into an
error.
Testing
kde1d-unit-test: 578 assertions in 32 cases, all passing.kde1d-invariant-test: 721 assertions in 5 cases, all passing.No numerical expectation moved; the diff only adds refusals on inputs that
previously crashed or silently mis-weighted.
clang-formatreports the same 129 pre-existing violations before and afterthis change (a formatter-version skew in the repo), so these additions add
none.
Context
Found while auditing the Python bindings in
vinecopulib/pyvinecopulib#326, where this surfaced as a segfault from
pyvinecopulib.core.Kde1d.fit(x, weights)and fromTorchKde1d.fit. That PRcarries a binding-level guard in the meantime, since a crash takes the
interpreter down; it can be dropped once this lands and the submodule pin
moves.