Skip to content

Fix NaN ordering in GFR feature presorting - #427

Merged
andrewherren merged 3 commits into
StochasticTree:mainfrom
dwachsmuth:codex/fix-gfr-nan-sorting
Oct 5, 2026
Merged

andrewherren merged 3 commits into
StochasticTree:mainfrom
dwachsmuth:codex/fix-gfr-nan-sorting

Conversation

@dwachsmuth

Copy link
Copy Markdown
Contributor

Sorry, here's one more. All came from investigating the same crashes, and with these three fixes on my local fork, my project is now running successfully!

FeaturePresortRoot::ArgsortRoot() currently passes ordinary floating-point less-than to std::stable_sort. With unequal non-NaN values and NaNs, this does not provide a strict weak ordering: 0 and NaN compare equivalent, and NaN and 1 compare equivalent, but 0 and 1 do not. Presorted rows can consequently leave NaNs interleaved with nonmissing values or leave nonmissing values out of order. GFR relies on these sorted rows when scoring candidate splits, while numeric partitioning routes NaNs right because NaN <= threshold is false. A malformed sorted prefix can therefore disagree with actual routing.

This change sorts non-NaNs ascending and NaNs last, retaining stable order for equal values and for NaNs. It preserves the existing comparator behavior for inputs without NaNs, including infinities and signed-zero ties. It does not introduce a learned missing direction or a new missing-data modeling strategy. The change is independent of the MCMC extrema and acceptance fixes.

Three C++ regression tests cover all 720 index permutations of six observations with two NaNs and a finite tie, agreement between the sorted prefix and numeric partitioning, nonmissing order/stability, and empty/singleton/all-NaN inputs. The mixed-data test fails against the unmodified upstream header and passes with this change. The full C++ suite passes all 31 tests. A standalone six-observation C++ reproducer exercising the actual presort is included in the accompanying submission materials.

The issue was discovered while investigating native crashes during GFR on numeric predictors with missing values. In separate R-0.4.5 diagnostics, this comparator repair alone eliminated two previously reproduced crashes; patched AddressSanitizer checks also passed. The small regression test targets the ordering defect rather than promising a portable native crash reproducer. Missing-data fits can change even at the same seed.

@andrewherren
andrewherren merged commit 7f6defd into StochasticTree:main Oct 5, 2026
16 checks passed
andrewherren added a commit to MichaelChirico/stochtree that referenced this pull request Oct 5, 2026
Add Bug Fixes entries for StochasticTree#425, StochasticTree#426, StochasticTree#427, and StochasticTree#428, noting that they
change sampler output for a given seed, and add David Wachsmuth as a
contributor in DESCRIPTION and the package Rd.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.

2 participants