diff --git a/include/stochtree/partition_tracker.h b/include/stochtree/partition_tracker.h index f25c875c..09aea435 100644 --- a/include/stochtree/partition_tracker.h +++ b/include/stochtree/partition_tracker.h @@ -31,6 +31,7 @@ #include #include +#include #include namespace StochTree { @@ -495,10 +496,16 @@ class FeaturePresortRoot { } std::iota(feature_sort_indices_.begin(), feature_sort_indices_.end(), 0); - // Define a custom comparator to be used with stable_sort: - // For every two indices l and r store as elements of `data_sort_indices_`, - // compare them for sorting purposes by indexing the covariate's raw data with both l and r - auto comp_op = [&](size_t const &l, size_t const &r) { return std::less{}(covariates(l, feature_index_), covariates(r, feature_index_)); }; + // Ordinary floating-point less-than is not a strict weak ordering with NaNs. + // Sort NaNs last, consistent with numeric splits routing them right, while + // stable_sort preserves the input order of equal values (including NaNs). + auto comp_op = [&](size_t const &l, size_t const &r) { + const double left_value = covariates(l, feature_index_); + const double right_value = covariates(r, feature_index_); + if (std::isnan(left_value)) return false; + if (std::isnan(right_value)) return true; + return left_value < right_value; + }; std::stable_sort(feature_sort_indices_.begin(), feature_sort_indices_.end(), comp_op); } diff --git a/test/cpp/test_sorted_partition_tracker.cpp b/test/cpp/test_sorted_partition_tracker.cpp index 99dbbe5d..8151e46c 100644 --- a/test/cpp/test_sorted_partition_tracker.cpp +++ b/test/cpp/test_sorted_partition_tracker.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include @@ -98,3 +100,51 @@ TEST(SortedNodeSampleTracker, BasicOperations) { expected_result = {3,0,7,4,5}; ASSERT_EQ(sorted_node_sampler_tracker.NodeIndices(4, 1), expected_result); } + +// Exercise the production presort through its public partition view. +TEST(FeaturePresortRoot, MissingValuesSortLastAcrossPermutations) { + const double nan = std::numeric_limits::quiet_NaN(); + const std::vector values{-1.0, 0.0, 1.0, nan, nan, 1.0}; + std::vector permutation{0, 1, 2, 3, 4, 5}; + do { + Eigen::MatrixXd covariates(6, 1); + for (int i = 0; i < 6; ++i) covariates(i, 0) = values[permutation[i]]; + // Construct expected order independently; preserve ties and NaN order. + std::vector expected; + for (double value : {-1.0, 0.0, 1.0}) { + for (int i = 0; i < 6; ++i) { + if (covariates(i, 0) == value) expected.push_back(i); + } + } + for (int i = 0; i < 6; ++i) { + if (std::isnan(covariates(i, 0))) expected.push_back(i); + } + StochTree::FeaturePresortRoot root(covariates, 0, StochTree::FeatureType::kNumeric); + StochTree::FeaturePresortPartition partition(&root, covariates, 0, StochTree::FeatureType::kNumeric); + ASSERT_EQ(partition.NodeIndices(0), expected); + // The scored sorted prefix must agree with numeric routing (NaNs go right). + partition.SplitFeatureNumeric(covariates, 0, 0, 0.0); + ASSERT_EQ(partition.NodeIndices(1), (std::vector{expected[0], expected[1]})); + ASSERT_EQ(partition.NodeIndices(2), (std::vector{expected[2], expected[3], expected[4], expected[5]})); + } while (std::next_permutation(permutation.begin(), permutation.end())); +} + +TEST(FeaturePresortRoot, NonMissingOrderAndStableTiesArePreserved) { + const double inf = std::numeric_limits::infinity(); + Eigen::MatrixXd covariates(8, 1); + covariates << 2.0, -0.0, inf, -2.0, 2.0, -inf, 0.0, -2.0; + StochTree::FeaturePresortRoot root(covariates, 0, StochTree::FeatureType::kNumeric); + StochTree::FeaturePresortPartition partition(&root, covariates, 0, StochTree::FeatureType::kNumeric); + EXPECT_EQ(partition.NodeIndices(0), (std::vector{5, 3, 7, 1, 6, 0, 4, 2})); +} + +TEST(FeaturePresortRoot, EmptySingletonAndAllMissingFeatures) { + for (int n : {0, 1, 5}) { + Eigen::MatrixXd covariates = Eigen::MatrixXd::Constant(n, 1, std::numeric_limits::quiet_NaN()); + StochTree::FeaturePresortRoot root(covariates, 0, StochTree::FeatureType::kNumeric); + StochTree::FeaturePresortPartition partition(&root, covariates, 0, StochTree::FeatureType::kNumeric); + std::vector expected(n); + std::iota(expected.begin(), expected.end(), 0); + EXPECT_EQ(partition.NodeIndices(0), expected); + } +}