diff --git a/include/kde1d/kde1d.hpp b/include/kde1d/kde1d.hpp index b384887..256e673 100644 --- a/include/kde1d/kde1d.hpp +++ b/include/kde1d/kde1d.hpp @@ -541,16 +541,18 @@ inline Kde1d::Kde1d(const interp::InterpolationGrid& grid, //! Fits the kernel density estimate to data. //! -//! Observations that are `NaN` are dropped. Weights are rescaled to average -//! one, so only their relative sizes matter. Calling this again re-selects the -//! bandwidth unless one was supplied at construction. +//! Observations that are `NaN` are dropped, as are those whose weight is `NaN` +//! or zero. Weights are rescaled to average one, so only their relative sizes +//! matter. Calling this again re-selects the bandwidth unless one was supplied +//! at construction. //! //! @param x vector of observations. //! @param weights vector of weights for each observation (optional). //! @throws std::invalid_argument if `x` is empty, if `weights` is given with a -//! different length, if the variable is discrete and any observation is not -//! an integer, or if any observation lies outside `[xmin, xmax]` -- for a -//! zero-inflated variable, any nonzero observation. +//! different length, if any weight is infinite or negative, if the drop +//! markers above leave no observation standing, if the variable is discrete +//! and any observation is not an integer, or if any observation lies outside +//! `[xmin, xmax]` -- for a zero-inflated variable, any nonzero observation. //! @throws std::runtime_error if the discrete masses cannot be normalized. inline void Kde1d::fit(const Eigen::VectorXd& x, const Eigen::VectorXd& weights) @@ -563,6 +565,14 @@ Kde1d::fit(const Eigen::VectorXd& x, const Eigen::VectorXd& weights) Eigen::VectorXd xx = x; Eigen::VectorXd w = weights; tools::remove_nans(xx, w); + // A NaN observation, a NaN weight and a zero weight are each a drop marker, + // so a caller can legitimately pass any of them -- but when they drop every + // row, `w.mean()` is a division by zero and the grid below is built from an + // empty sample, which terminates the process instead of raising. + if (xx.size() == 0) { + throw std::invalid_argument( + "x and weights must leave at least one observation standing"); + } if (w.size() > 0) { w /= w.mean(); @@ -1684,6 +1694,17 @@ Kde1d::check_inputs(const Eigen::VectorXd& x, if ((weights.size() > 0) && (weights.size() != x.size())) throw std::invalid_argument("x and weights must have the same size"); + + if (weights.size() > 0) { + // Neither of these is a missing-value marker and both survive the + // rescaling below: an infinite weight normalizes every other one to zero, + // and a negative one yields a density that is not the weighted one. + if (weights.array().isInf().any()) + throw std::invalid_argument("weights must not be infinite"); + // `NaN < 0` is false, so this reads only the entries that are not markers. + if ((weights.array() < 0.0).any()) + throw std::invalid_argument("weights must be nonnegative"); + } } inline void diff --git a/test/test.cpp b/test/test.cpp index ae51beb..0de5b95 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -280,6 +280,45 @@ TEST_CASE("two-sided exploding endpoints retain the bulk fit", CHECK(fit.get_edf() == Approx(bulk.get_edf()).epsilon(1e-12)); } +TEST_CASE("inputs that leave nothing to fit are refused", "[kde1d]") +{ + const Eigen::VectorXd x = Eigen::VectorXd::LinSpaced(20, -2.0, 2.0); + const Eigen::VectorXd ones = Eigen::VectorXd::Ones(20); + Kde1d fit(NAN, NAN, "continuous"); + + // `remove_nans` treats a NaN observation, a NaN weight and a zero weight as + // drop markers, so each of these drops every row. `fit` then rescaled by a + // zero sum and interpolated an empty grid, terminating the process. + CHECK_THROWS(fit.fit(x, Eigen::VectorXd::Constant(20, NAN))); + CHECK_THROWS(fit.fit(x, Eigen::VectorXd::Zero(20))); + CHECK_THROWS(fit.fit(Eigen::VectorXd::Constant(20, NAN), ones)); + CHECK_THROWS(fit.fit(Eigen::VectorXd::Constant(20, NAN))); + + // Neither of these is a marker: an infinite weight normalizes the rest to + // zero, and a negative one gave a density that was not the weighted one. + Eigen::VectorXd infinite = ones; + infinite(7) = std::numeric_limits::infinity(); + CHECK_THROWS(fit.fit(x, infinite)); + + Eigen::VectorXd negative = ones; + negative(11) = -1.0; + CHECK_THROWS(fit.fit(x, negative)); + + // Partial markers stay legal: they drop their own row and nothing else, and + // NaN and zero must agree, since `remove_nans` treats them the same. + Eigen::VectorXd as_nan = ones; + Eigen::VectorXd as_zero = ones; + for (Eigen::Index i = 1; i < ones.size(); i += 2) { + as_nan(i) = NAN; + as_zero(i) = 0.0; + } + Kde1d dropped(NAN, NAN, "continuous"); + Kde1d zeroed(NAN, NAN, "continuous"); + CHECK_NOTHROW(dropped.fit(x, as_nan)); + CHECK_NOTHROW(zeroed.fit(x, as_zero)); + CHECK(dropped.get_values().isApprox(zeroed.get_values(), 1e-12)); +} + TEST_CASE("boundary experts support weights and manual bandwidths", "[boundary-expert]") {