Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions include/kde1d/kde1d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>::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]")
{
Expand Down
Loading