diff --git a/.Rbuildignore b/.Rbuildignore index ca5eeb3..65eee3c 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -14,3 +14,5 @@ ^README\.qmd$ ^LICENSE\.md$ ^\.positai$ +^README\.Rmd$ +^README\.html$ diff --git a/NAMESPACE b/NAMESPACE index 1e5287d..5abc8e7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,7 +6,9 @@ S3method(print,cv_density_lp) S3method(print,density_lp) S3method(print,density_lp_ppp) S3method(print,domain_lp) +S3method(summary,cv_density_lp) S3method(summary,density_lp) +S3method(summary,density_lp_ppp) export(cv_density_lp) export(cv_density_lp_ppp) export(density_lp) diff --git a/R/cv.R b/R/cv.R index 5f4c7d3..bc7f360 100644 --- a/R/cv.R +++ b/R/cv.R @@ -37,6 +37,13 @@ new_cv_density_lp <- function(m_hat, h_hat, scores, grids, call) { #' - `$scores`: matrix `|m_grid| x |h_grid|` of CV scores, #' - `$grids`: list containing `$m` and `$h` grids used. #' @seealso [density_lp()], [cv_density_lp_ppp()] +#' @examples +#' set.seed(1) +#' X <- matrix(runif(30 * 2), ncol = 2) +#' dom <- domain_Rd(2) +#' cv <- cv_density_lp(X, h_grid = c(0.2, 0.4), m_grid = 0:1, domain = dom, N_quad = 100L) +#' print(cv) +#' #' @export cv_density_lp <- function(X, h_grid, m_grid = 0:3, domain, N_quad = 500L) { check_X(X) @@ -99,7 +106,7 @@ cv_density_lp <- function(X, h_grid, m_grid = 0:3, domain, N_quad = 500L) { warning(sprintf( paste0( "%d/%d LOO evaluations failed for the selected parameters", - " (m = %d, h = %g).", + " (m = %d, h = %g): V(h) empty or Gram matrix singular.", " The CV score is unreliable: consider a larger h or a coarser grid." ), n_fail[idx[1L], idx[2L]], @@ -111,7 +118,7 @@ cv_density_lp <- function(X, h_grid, m_grid = 0:3, domain, N_quad = 500L) { warning(sprintf( paste0( "%d/%d LOO evaluations failed for the selected parameters", - " (m = %d, h = %g).", + " (m = %d, h = %g): V(h) empty or Gram matrix singular.", " Results should be interpreted with caution." ), n_fail[idx[1L], idx[2L]], @@ -143,6 +150,12 @@ cv_density_lp <- function(X, h_grid, m_grid = 0:3, domain, N_quad = 500L) { #' @param N_quad Number of quadrature points per evaluation (default 500). #' @return An S3 object of class `"cv_density_lp"` (see [cv_density_lp()]). #' @seealso [cv_density_lp()], [density_lp_ppp()] +#' @examples +#' set.seed(1) +#' pp <- spatstat.geom::ppp(runif(30), runif(30), window = spatstat.geom::owin()) +#' cv <- cv_density_lp_ppp(pp, h_grid = c(0.2, 0.4), m_grid = 0:1, N_quad = 100L) +#' print(cv) +#' #' @importFrom spatstat.geom Window #' @export cv_density_lp_ppp <- function(pp, h_grid, m_grid = 0:3, N_quad = 500L) { diff --git a/R/density_lp.R b/R/density_lp.R index b7a83c5..3f864e5 100644 --- a/R/density_lp.R +++ b/R/density_lp.R @@ -52,6 +52,17 @@ new_density_lp <- function( #' #' @return An S3 object of class `"density_lp"`. #' +#' @examples +#' set.seed(1) +#' X <- matrix(runif(50 * 2), ncol = 2) +#' t_grid <- as.matrix(expand.grid( +#' x = seq(0.1, 0.9, length.out = 5), +#' y = seq(0.1, 0.9, length.out = 5) +#' )) +#' dom <- domain_Rd(2) +#' fit <- density_lp(X, t_grid = t_grid, h = 0.3, m = 1L, domain = dom, N_quad = 100L) +#' print(fit) +#' #' @export density_lp <- function(X, t_grid, h, m = 0L, domain, N_quad = 500L) { check_X(X) @@ -106,9 +117,9 @@ density_lp <- function(X, t_grid, h, m = 0L, domain, N_quad = 500L) { t_grid = t_grid, X = X, h = h, - m = m, + m = as.integer(m), domain = domain, - N_quad = N_quad, + N_quad = as.integer(N_quad), n_fail = n_fail, call = match.call() ) diff --git a/R/density_lp_ppp.R b/R/density_lp_ppp.R index 3378f65..8c3f914 100644 --- a/R/density_lp_ppp.R +++ b/R/density_lp_ppp.R @@ -25,13 +25,19 @@ new_density_lp_ppp <- function(im_obj, params, stats, call) { #' #' @param pp A `ppp` object (spatstat.geom). #' @param h Bandwidth (scalar > 0). -#' @param m Polynomial degree (integer >= 0, default 1). +#' @param m Polynomial degree (integer >= 0, default 0). #' @param N_quad Number of quadrature points per target point (default 500). #' @param nx Number of pixel columns (default 128). #' @param ny Number of pixel rows (default 128). #' @return An S3 object of class `"density_lp_ppp"`, which also inherits from #' `spatstat.geom::im`. Pixels outside `Window(pp)` are `NA`. #' @seealso [density_lp()], [domain_from_owin()] +#' @examples +#' set.seed(1) +#' pp <- spatstat.geom::ppp(runif(50), runif(50), window = spatstat.geom::owin()) +#' fit <- density_lp_ppp(pp, h = 0.3, m = 1L, N_quad = 100L, nx = 20L, ny = 20L) +#' print(fit) +#' #' @importFrom spatstat.geom Window inside.owin im unitname #' @export density_lp_ppp <- function(pp, h, m = 0L, N_quad = 500L, nx = 128L, ny = 128L) { diff --git a/R/domain.R b/R/domain.R index 2152365..61a8a5f 100644 --- a/R/domain.R +++ b/R/domain.R @@ -24,6 +24,9 @@ print.domain_lp <- function(x, ...) { #' #' @param d Space dimension (integer >= 1). #' @return An `"domain_lp"` object. +#' @examples +#' dom <- domain_Rd(2) +#' print(dom) #' @export domain_Rd <- function(d) { stopifnot(is.numeric(d), length(d) == 1L, is.finite(d), d >= 1, d == floor(d)) @@ -44,6 +47,10 @@ domain_Rd <- function(d) { #' coordinate axis), returning a logical vector of length `n`. The dimension #' `d` is inferred from `length(formals(is_in_domain))`. #' @return An `"domain_lp"` object. +#' @examples +#' is_in <- function(x, y) x^2 + y^2 <= 1 +#' dom <- domain_from_indicator(is_in) +#' print(dom) #' @export domain_from_indicator <- function(is_in_domain) { stopifnot(is.function(is_in_domain)) @@ -66,6 +73,10 @@ domain_from_indicator <- function(is_in_domain) { #' #' @param win An `owin` object (spatstat.geom). #' @return An `"domain_lp"` object (d = 2). +#' @examples +#' win <- spatstat.geom::owin(c(0, 1), c(0, 1)) +#' dom <- domain_from_owin(win) +#' print(dom) #' @export domain_from_owin <- function(win) { stopifnot(inherits(win, "owin")) diff --git a/R/domain_sector.R b/R/domain_sector.R index ff372d0..9af3a58 100644 --- a/R/domain_sector.R +++ b/R/domain_sector.R @@ -4,6 +4,9 @@ #' #' @param k Sector exponent (scalar > 0). #' @return An `"domain_lp"` object (d = 2). +#' @examples +#' dom <- domain_sector(k = 2) +#' print(dom) #' @export domain_sector <- function(k) { stopifnot(is.numeric(k), length(k) == 1L, is.finite(k), k > 0) diff --git a/R/methods.R b/R/methods.R index 3aaef5d..0973a55 100644 --- a/R/methods.R +++ b/R/methods.R @@ -139,15 +139,75 @@ plot.density_lp <- function( invisible(x) } +#' Summary of a density_lp_ppp object +#' @param object A `"density_lp_ppp"` object. +#' @param ... Ignored. +#' @return `object` invisibly. +#' @export +summary.density_lp_ppp <- function(object, ...) { + cat("Local Polynomial Density Estimation Summary (spatstat)\n") + cat("-------------------------------------------------------\n") + cat(sprintf( + "Call : %s\n", + paste(deparse(object$call), collapse = "\n") + )) + cat(sprintf("Domain : %s\n", object$params$domain$label)) + cat(sprintf("Observations : n = %d\n", object$stats$n_obs)) + cat(sprintf( + "Grid : %d x %d pixels\n", + length(object$xcol), + length(object$yrow) + )) + cat("\nParameters:\n") + cat(sprintf(" Bandwidth (h) : %g\n", object$params$h)) + cat(sprintf(" Degree (m) : %d\n", object$params$m)) + cat(sprintf(" Quadrature : %d MC points\n", object$params$N_quad)) + if (!is.null(object$stats$n_fail) && object$stats$n_fail > 0L) { + cat(sprintf(" Note : %d failure(s)\n", object$stats$n_fail)) + } + cat("\nResults (estimate):\n") + print(summary(as.vector(object$v), na.rm = TRUE)) + invisible(object) +} + # S3 methods for "cv_density_lp" ------------------------------------------------ +#' Summary of a cv_density_lp object +#' @param object A `"cv_density_lp"` object. +#' @param ... Ignored. +#' @return `object` invisibly. +#' @export +summary.cv_density_lp <- function(object, ...) { + row_idx <- which(object$grids$m == object$m_hat) + col_idx <- which(object$grids$h == object$h_hat) + score <- object$scores[row_idx, col_idx] + cat("LOO Cross-Validation Summary -- local polynomial density\n") + cat("---------------------------------------------------------\n") + cat(sprintf( + "Call : %s\n", + paste(deparse(object$call), collapse = "\n") + )) + cat(sprintf("m grid : %s\n", paste(object$grids$m, collapse = ", "))) + cat(sprintf( + "h grid : %s\n", + paste(round(object$grids$h, 4L), collapse = ", ") + )) + cat("\nCV score matrix (lower is better):\n") + print(round(object$scores, 4L)) + cat(sprintf("\nSelected : m = %d, h = %.6g\n", object$m_hat, object$h_hat)) + cat(sprintf("CV score : %.5g\n", score)) + invisible(object) +} + #' Print a cv_density_lp object #' @param x A \code{"cv_density_lp"} object. #' @param ... Ignored. #' @return \code{x} invisibly. #' @export print.cv_density_lp <- function(x, ...) { - score <- x$scores[paste0("m=", x$m_hat), as.character(round(x$h_hat, 6L))] + row_idx <- which(x$grids$m == x$m_hat) + col_idx <- which(x$grids$h == x$h_hat) + score <- x$scores[row_idx, col_idx] cat("LOO cross-validation -- local polynomial density\n") cat(sprintf(" Selected m : %d\n", x$m_hat)) cat(sprintf(" Selected h : %.6g\n", x$h_hat)) diff --git a/R/rfactory.R b/R/rfactory.R index a66e094..7095bc2 100644 --- a/R/rfactory.R +++ b/R/rfactory.R @@ -75,14 +75,27 @@ rfactory <- function(f, is_in_domain = NULL, box, M = NULL, N_pilot = 2000L) { } stopifnot(is.numeric(M), length(M) == 1L, is.finite(M), M > 0) - function(n) { + function(n, max_iter = 10000L) { n <- as.integer(n) stopifnot(n >= 1L) batch <- max(1000L, n * 10L) out <- matrix(NA_real_, n, d) filled <- 0L + iter <- 0L while (filled < n) { + iter <- iter + 1L + if (iter > max_iter) { + stop( + sprintf( + "rfactory: collected only %d/%d points after %d iterations. ", + filled, + n, + max_iter + ), + "Acceptance rate may be too low: check `box`, `f`, and `is_in_domain`." + ) + } cands <- .sample_box(batch) if (!is.null(is_in_domain)) { cands <- cands[.call_fn(is_in_domain, cands), , drop = FALSE] diff --git a/man/cv_density_lp.Rd b/man/cv_density_lp.Rd index c2a6b89..4e76e5c 100644 --- a/man/cv_density_lp.Rd +++ b/man/cv_density_lp.Rd @@ -35,6 +35,14 @@ using the algebraic LOO correction \frac{n\,\hat{f}_\gamma(X_i) - h^{-d}\|H_0^{(X_i)}\|^2}{n-1},} where \eqn{\|H_0^{(X_i)}\|^2 = (B_{X_i}^{-1})_{11}} is the self-influence scalar returned as a byproduct of the C++ pipeline. +} +\examples{ +set.seed(1) +X <- matrix(runif(30 * 2), ncol = 2) +dom <- domain_Rd(2) +cv <- cv_density_lp(X, h_grid = c(0.2, 0.4), m_grid = 0:1, domain = dom, N_quad = 100L) +print(cv) + } \seealso{ \code{\link[=density_lp]{density_lp()}}, \code{\link[=cv_density_lp_ppp]{cv_density_lp_ppp()}} diff --git a/man/cv_density_lp_ppp.Rd b/man/cv_density_lp_ppp.Rd index 1b0c88f..14654e4 100644 --- a/man/cv_density_lp_ppp.Rd +++ b/man/cv_density_lp_ppp.Rd @@ -21,6 +21,13 @@ An S3 object of class \code{"cv_density_lp"} (see \code{\link[=cv_density_lp]{cv \description{ Convenience wrapper around \code{\link[=cv_density_lp]{cv_density_lp()}} for \code{ppp} objects. The domain is derived automatically from \code{Window(pp)}. +} +\examples{ +set.seed(1) +pp <- spatstat.geom::ppp(runif(30), runif(30), window = spatstat.geom::owin()) +cv <- cv_density_lp_ppp(pp, h_grid = c(0.2, 0.4), m_grid = 0:1, N_quad = 100L) +print(cv) + } \seealso{ \code{\link[=cv_density_lp]{cv_density_lp()}}, \code{\link[=density_lp_ppp]{density_lp_ppp()}} diff --git a/man/density_lp.Rd b/man/density_lp.Rd index bd4e03e..1abc14d 100644 --- a/man/density_lp.Rd +++ b/man/density_lp.Rd @@ -28,3 +28,15 @@ An S3 object of class \code{"density_lp"}. \description{ Local polynomial density estimation on a grid of target points } +\examples{ +set.seed(1) +X <- matrix(runif(50 * 2), ncol = 2) +t_grid <- as.matrix(expand.grid( + x = seq(0.1, 0.9, length.out = 5), + y = seq(0.1, 0.9, length.out = 5) +)) +dom <- domain_Rd(2) +fit <- density_lp(X, t_grid = t_grid, h = 0.3, m = 1L, domain = dom, N_quad = 100L) +print(fit) + +} diff --git a/man/density_lp_ppp.Rd b/man/density_lp_ppp.Rd index 17e87c6..904fcf3 100644 --- a/man/density_lp_ppp.Rd +++ b/man/density_lp_ppp.Rd @@ -11,7 +11,7 @@ density_lp_ppp(pp, h, m = 0L, N_quad = 500L, nx = 128L, ny = 128L) \item{h}{Bandwidth (scalar > 0).} -\item{m}{Polynomial degree (integer >= 0, default 1).} +\item{m}{Polynomial degree (integer >= 0, default 0).} \item{N_quad}{Number of quadrature points per target point (default 500).} @@ -28,6 +28,13 @@ Convenience wrapper around \code{\link[=density_lp]{density_lp()}} for \code{ppp The evaluation grid and the domain are derived automatically from \code{Window(pp)}. The result is a \code{spatstat.geom::im} pixel image, fully compatible with the spatstat plotting and analysis ecosystem. +} +\examples{ +set.seed(1) +pp <- spatstat.geom::ppp(runif(50), runif(50), window = spatstat.geom::owin()) +fit <- density_lp_ppp(pp, h = 0.3, m = 1L, N_quad = 100L, nx = 20L, ny = 20L) +print(fit) + } \seealso{ \code{\link[=density_lp]{density_lp()}}, \code{\link[=domain_from_owin]{domain_from_owin()}} diff --git a/man/domain_Rd.Rd b/man/domain_Rd.Rd index 5520bb0..e73e7da 100644 --- a/man/domain_Rd.Rd +++ b/man/domain_Rd.Rd @@ -15,3 +15,7 @@ An \code{"domain_lp"} object. \description{ Domain R^d (no constraint: neighbourhood = full \eqn{[-h, h]^d}) } +\examples{ +dom <- domain_Rd(2) +print(dom) +} diff --git a/man/domain_from_indicator.Rd b/man/domain_from_indicator.Rd index a18da3c..70dd5fa 100644 --- a/man/domain_from_indicator.Rd +++ b/man/domain_from_indicator.Rd @@ -17,3 +17,8 @@ An \code{"domain_lp"} object. \description{ Domain defined by an indicator function } +\examples{ +is_in <- function(x, y) x^2 + y^2 <= 1 +dom <- domain_from_indicator(is_in) +print(dom) +} diff --git a/man/domain_from_owin.Rd b/man/domain_from_owin.Rd index 453b3a1..e214874 100644 --- a/man/domain_from_owin.Rd +++ b/man/domain_from_owin.Rd @@ -18,3 +18,8 @@ sampler uses \code{\link[spatstat.random:runifpoint]{spatstat.random::runifpoint \eqn{V(h) = \mathcal{D} \cap [-h, h]^2}, giving exact area and unbiased Monte Carlo weights. } +\examples{ +win <- spatstat.geom::owin(c(0, 1), c(0, 1)) +dom <- domain_from_owin(win) +print(dom) +} diff --git a/man/domain_sector.Rd b/man/domain_sector.Rd index 0025e44..be45f9c 100644 --- a/man/domain_sector.Rd +++ b/man/domain_sector.Rd @@ -15,3 +15,7 @@ An \code{"domain_lp"} object (d = 2). \description{ Polynomial sector \eqn{D_k = \{(x, y) : x \in [0, 1],\, 0 \le y \le x^k\}} } +\examples{ +dom <- domain_sector(k = 2) +print(dom) +} diff --git a/man/summary.cv_density_lp.Rd b/man/summary.cv_density_lp.Rd new file mode 100644 index 0000000..9d2fc87 --- /dev/null +++ b/man/summary.cv_density_lp.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/methods.R +\name{summary.cv_density_lp} +\alias{summary.cv_density_lp} +\title{Summary of a cv_density_lp object} +\usage{ +\method{summary}{cv_density_lp}(object, ...) +} +\arguments{ +\item{object}{A \code{"cv_density_lp"} object.} + +\item{...}{Ignored.} +} +\value{ +\code{object} invisibly. +} +\description{ +Summary of a cv_density_lp object +} diff --git a/man/summary.density_lp_ppp.Rd b/man/summary.density_lp_ppp.Rd new file mode 100644 index 0000000..0d91efd --- /dev/null +++ b/man/summary.density_lp_ppp.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/methods.R +\name{summary.density_lp_ppp} +\alias{summary.density_lp_ppp} +\title{Summary of a density_lp_ppp object} +\usage{ +\method{summary}{density_lp_ppp}(object, ...) +} +\arguments{ +\item{object}{A \code{"density_lp_ppp"} object.} + +\item{...}{Ignored.} +} +\value{ +\code{object} invisibly. +} +\description{ +Summary of a density_lp_ppp object +} diff --git a/src/gram_matrix.cpp b/src/gram_matrix.cpp index 32cc88c..f2cac14 100644 --- a/src/gram_matrix.cpp +++ b/src/gram_matrix.cpp @@ -168,7 +168,9 @@ arma::vec cv_lp_fixed_h_m_cpp(const arma::mat& X, for (int i = 0; i < n; i++) { arma::mat U_quad = Rcpp::as(U_quad_list[i]); - if (U_quad.n_cols < 2) { + // Return -inf (not an error) so the loop continues for other observations. + // The R side counts !is.finite(log_loo) as n_fail and warns accordingly. + if (U_quad.n_cols < 2) { // empty neighbourhood V(h) log_loo(i) = -arma::datum::inf; continue; } @@ -179,7 +181,7 @@ arma::vec cv_lp_fixed_h_m_cpp(const arma::mat& X, // 2. Cholesky arma::mat L_upper; - if (!arma::chol(L_upper, B)) { + if (!arma::chol(L_upper, B)) { // singular Gram matrix log_loo(i) = -arma::datum::inf; continue; } diff --git a/tests/testthat/test-cv.R b/tests/testthat/test-cv.R index 0ec0832..9b29110 100644 --- a/tests/testthat/test-cv.R +++ b/tests/testthat/test-cv.R @@ -55,7 +55,9 @@ test_that("cv_density_lp: h_hat is in h_grid", { test_that("cv_density_lp: selected score is the minimum finite score", { cv <- make_square_cv() - best <- cv$scores[paste0("m=", cv$m_hat), as.character(round(cv$h_hat, 6L))] + row_idx <- which(cv$grids$m == cv$m_hat) + col_idx <- which(cv$grids$h == cv$h_hat) + best <- cv$scores[row_idx, col_idx] expect_equal(best, min(cv$scores, na.rm = TRUE)) }) @@ -134,3 +136,44 @@ test_that("cv_density_lp_ppp: m_hat and h_hat are in the grids", { test_that("cv_density_lp_ppp: error if input is not a ppp", { expect_error(cv_density_lp_ppp(matrix(1, 10, 2), h_grid = 0.3), "ppp") }) + +# ── Integer storage ──────────────────────────────────────────────────────────── + +test_that("cv_density_lp: m_grid stored as integer in grids$m", { + cv <- make_square_cv(m_grid = c(0, 1)) # passed as double + expect_type(cv$grids$m, "integer") +}) + +# ── print CV score not NA ────────────────────────────────────────────────────── + +test_that("print.cv_density_lp: CV score is not NA", { + cv <- make_square_cv() + row_idx <- which(cv$grids$m == cv$m_hat) + col_idx <- which(cv$grids$h == cv$h_hat) + score <- cv$scores[row_idx, col_idx] + expect_false(is.na(score)) + expect_true(is.finite(score)) +}) + +# ── summary.cv_density_lp ───────────────────────────────────────────────────── + +test_that("summary.cv_density_lp: produces output and returns invisibly", { + cv <- make_square_cv() + expect_output(summary(cv), "Selected") + expect_output(summary(cv), "CV score matrix") + expect_identical(withVisible(summary(cv))$visible, FALSE) +}) + +# ── Warning on high failure rate ─────────────────────────────────────────────── + +test_that("cv_density_lp: warns when >25% LOO evaluations fail", { + set_seed() + n <- 30L + X <- matrix(runif(n * 2L), n, 2L) + dom <- domain_Rd(2L) + # h very small -> most V(h) neighbourhoods empty -> high failure rate + expect_warning( + cv_density_lp(X, h_grid = c(0.01), m_grid = 0L, domain = dom, N_quad = 50L), + "failed" + ) +}) diff --git a/tests/testthat/test-density_lp.R b/tests/testthat/test-density_lp.R index f0798ff..79dec00 100644 --- a/tests/testthat/test-density_lp.R +++ b/tests/testthat/test-density_lp.R @@ -218,6 +218,56 @@ test_that("density_lp: NA and warning when a grid point has empty V(h)", { expect_false(is.na(res$estimate[1L])) }) +# ── Integer storage of params ────────────────────────────────────────────────── + +test_that("density_lp: params$m and params$N_quad are stored as integers", { + set.seed(1L) + X <- matrix(runif(50L * 2L), 50L, 2L) + res <- density_lp( + X, + matrix(0.5, 1L, 2L), + h = 0.3, + m = 1, # passed as double + domain = domain_Rd(2L), + N_quad = 200 # passed as double + ) + expect_type(res$params$m, "integer") + expect_type(res$params$N_quad, "integer") +}) + +# ── summary.density_lp ──────────────────────────────────────────────────────── + +test_that("summary.density_lp: produces output and returns invisibly", { + set.seed(20L) + X <- matrix(runif(100L * 2L), 100L, 2L) + res <- density_lp( + X, + matrix(c(0.3, 0.5, 0.7, 0.5), nrow = 2L), + h = 0.3, + m = 1L, + domain = domain_Rd(2L), + N_quad = 200L + ) + expect_output(summary(res), "Bandwidth") + expect_identical(withVisible(summary(res))$visible, FALSE) +}) + +# ── Multiple failures ───────────────────────────────────────────────────────── + +test_that("density_lp: n_fail correctly counts multiple failed grid points", { + X <- matrix(runif(50L * 2L, 0.6, 1), 50L, 2L) + dom <- domain_from_indicator(function(x, y) { + x >= 0.5 & x <= 1 & y >= 0.5 & y <= 1 + }) + # Both grid points have empty V(h): observations are in [0.6,1]^2 but t is at corner [0,0.1] + t_bad <- matrix(c(0.05, 0.05, 0.08, 0.08), nrow = 2L, byrow = TRUE) + expect_warning( + res <- density_lp(X, t_bad, h = 0.04, m = 0L, domain = dom, N_quad = 100L) + ) + expect_equal(res$stats$n_fail, 2L) + expect_true(all(is.na(res$estimate))) +}) + test_that("density_lp: m=1 integrates approximately to 1 on interior grid", { set.seed(101L) n <- 1000L diff --git a/tests/testthat/test-rfactory.R b/tests/testthat/test-rfactory.R index 26590ba..6b6dfae 100644 --- a/tests/testthat/test-rfactory.R +++ b/tests/testthat/test-rfactory.R @@ -79,3 +79,14 @@ test_that("rfactory: error if all pilot values are zero", { "zero" ) }) + +test_that("rfactory: max_iter guard triggers when acceptance rate is zero", { + # Supply M to skip the pilot phase, then is_in_domain rejects everything + r <- rfactory( + function(x, y) 1, + function(x, y) rep(FALSE, length(x)), + box_disc, + M = 1.0 + ) + expect_error(r(10L, max_iter = 5L), "iterations") +})