tests/testthat/test-unconditional-distribution-training-grid-normalization-contract.R

udist_training_grid_objective <- function(dat, bw, tree, accelerate) {
  old_options <- options(np.tree = tree,
                         np.macMseries.accelerate = accelerate,
                         np.messages = FALSE)
  on.exit(options(old_options), add = TRUE)
  evaluated <- np:::npudistbw.dbandwidth(
    dat = dat, bws = bw, bandwidth.compute = TRUE,
    nmulti = 1L, powell.remin = FALSE, bwsolver = "powell",
    eval.only = TRUE, do.full.integral = TRUE)
  as.numeric(evaluated$fval)
}

udist_literal_training_grid <- function(value, cdf_weight) {
  value <- as.matrix(value)
  n <- nrow(value)
  objective <- 0
  for (held_out in seq_len(n)) {
    donors <- setdiff(seq_len(n), held_out)
    for (evaluation in setdiff(seq_len(n), held_out)) {
      fitted <- mean(vapply(donors, function(donor) {
        product <- 1
        for (coordinate in seq_len(ncol(value)))
          product <- product * cdf_weight(
            value[evaluation, coordinate], value[donor, coordinate],
            coordinate)
        product
      }, numeric(1L)))
      indicator <- all(value[held_out, ] <= value[evaluation, ])
      objective <- objective + (as.double(indicator) - fitted)^2
    }
  }
  objective / (n * (n - 1))
}

test_that("unconditional continuous CDF-CV averages the admitted ordered pairs", {
  fixtures <- list(
    p1 = matrix(c(-2.1, -1.2, -0.3, 0.2, 0.9, 1.7, 2.8), ncol = 1L),
    p2 = cbind(seq(-1.8, 2.2, length.out = 11L),
               sin(seq_len(11L) * 0.67) + seq_len(11L) / 19))
  bandwidths <- list(p1 = 0.7, p2 = c(0.65, 0.55))

  for (fixture_name in names(fixtures)) {
    value <- fixtures[[fixture_name]]
    dat <- as.data.frame(value)
    names(dat) <- paste0("x", seq_len(ncol(dat)))
    bandwidth <- bandwidths[[fixture_name]]
    for (kernel_name in c("gaussian", "epanechnikov")) {
      kernel_cdf <- if (kernel_name == "gaussian") {
        function(evaluation, donor, coordinate) {
          stats::pnorm((evaluation - donor) / bandwidth[coordinate])
        }
      } else {
        function(evaluation, donor, coordinate) {
          u <- (evaluation - donor) / bandwidth[coordinate]
          edge <- sqrt(5)
          if (u <= -edge) return(0)
          if (u >= edge) return(1)
          0.5 + 3 / (4 * edge) * (u - u^3 / 15)
        }
      }
      expected <- udist_literal_training_grid(value, kernel_cdf)
      bw <- npudistbw(
        dat = dat, bws = bandwidth, bwmethod = "cv.cdf",
        bwtype = "fixed", bwscaling = FALSE,
        ckertype = kernel_name, ckerorder = 2L,
        bandwidth.compute = FALSE)
      for (tree in c(FALSE, TRUE))
        for (accelerate in c(FALSE, TRUE))
          expect_equal(
            udist_training_grid_objective(dat, bw, tree, accelerate),
            expected, tolerance = 1e-9)
    }
  }
})

test_that("beta CDF-CV uses the same empirical training-grid normalization", {
  value <- c(0.04, 0.12, 0.27, 0.43, 0.61, 0.78, 0.93)
  dat <- data.frame(x = value)
  bandwidth <- 0.19
  beta_cdf <- function(evaluation, donor, coordinate) {
    concentration <- 1 / bandwidth^2
    stats::pbeta(evaluation,
                 1 + donor * concentration,
                 1 + (1 - donor) * concentration)
  }
  expected <- udist_literal_training_grid(matrix(value, ncol = 1L), beta_cdf)
  bw <- npudistbw(
    dat = dat, bws = bandwidth, bwmethod = "cv.cdf",
    bwtype = "fixed", bwscaling = FALSE,
    ckertype = "beta", ckerorder = 2L,
    ckerbound = "fixed", ckerlb = 0, ckerub = 1,
    bandwidth.compute = FALSE)

  expect_equal(udist_training_grid_objective(dat, bw, FALSE, FALSE),
               expected, tolerance = 1e-9)
})

test_that("ordered-profile and dense CDF-CV share the empirical finalizer", {
  value <- ordered(c(1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4),
                   levels = 1:4)
  numeric_value <- as.numeric(value)
  dat <- data.frame(x = value)
  lambda <- 0.31
  ordered_cdf <- function(evaluation, donor, coordinate) {
    pmf <- function(z) ifelse(z == donor, 1 - lambda,
                             0.5 * (1 - lambda) * lambda^abs(z - donor))
    anchor <- 0.5 + 0.5 * pmf(donor)
    if (evaluation == donor) return(anchor)
    if (evaluation > donor)
      return(anchor + sum(pmf(seq.int(donor + 1L, evaluation))))
    anchor - sum(pmf(seq.int(evaluation + 1L, donor)))
  }
  expected <- udist_literal_training_grid(
    matrix(numeric_value, ncol = 1L), ordered_cdf)
  bw <- npudistbw(
    dat = dat, bws = lambda, bwmethod = "cv.cdf",
    bwtype = "fixed", bwscaling = FALSE,
    okertype = "wangvanryzin", bandwidth.compute = FALSE)

  dense <- udist_training_grid_objective(dat, bw, FALSE, FALSE)
  profile <- udist_training_grid_objective(dat, bw, TRUE, FALSE)
  expect_equal(dense, expected, tolerance = 1e-12)
  expect_equal(profile, expected, tolerance = 1e-12)
  expect_identical(profile, dense)
})

Try the np package in your browser

Any scripts or data that you put into this service are public.

np documentation built on Nov. 25, 2026, 1:06 a.m.