tools/audit-numerics.R

# Independent algebra/invariance audit; not a coverage simulation.
# Usage: Rscript tools/audit-numerics.R output.csv [cases]
args <- commandArgs(trailingOnly = TRUE)
stopifnot(length(args) >= 1L)
library(IJSE)
b <- if (length(args) >= 2L) as.integer(args[2L]) else 80L
set.seed(73031)
records <- list()
for (case in seq_len(b)) {
  s <- sample(c(2L, 3L, 7L, 19L, 60L), 1)
  n <- sample(c(2L, 3L, 8L, 17L, 45L), 1)
  p <- sample(c(1L, 2L, 5L), 1)
  d <- matrix(rnorm(s * p), s, p)
  colnames(d) <- paste0("p", seq_len(p))
  ll <- matrix(rnorm(s * n), s, n)
  j <- sample.int(n - 1L, 1) + 1L
  ids <- sample(rep(seq_len(j), length.out = n))
  # Cross-products and explicit unit sums, independently of the implementation.
  cd <- sweep(d, 2, colMeans(d), "-")
  cl <- sweep(ll, 2, colMeans(ll), "-")
  c_obs <- crossprod(cd, cl) / (s - 1)
  a <- matrix(rnorm(2 * p), 2, p)
  perm <- sample.int(n)
  for (clustered in c(FALSE, TRUE)) {
    group <- if (clustered) ids else NULL
    c_units <- if (clustered) {
      matrix(vapply(unique(ids), function(k) {
        rowSums(c_obs[, ids == k, drop = FALSE])
      }, numeric(p)), nrow = p)
    } else c_obs
    k <- ncol(c_units)
    centered <- sweep(c_units, 1, rowMeans(c_units), "-")
    target <- k / (k - 1) * tcrossprod(centered)
    ref <- ijse_draws(d, ll, group, diagnostics = "none")
    errors <- c(reference = max(abs(unname(vcov(ref)) - target)))
    for (block in c(1L, 7L)) {
      stream <- ijse_draws(d, function(ii) ll[, ii, drop = FALSE], group,
        n_obs = n, block_size = block, diagnostics = "none")
      errors[paste0("block", block)] <- max(abs(vcov(stream) - vcov(ref)))
    }
    shifted <- ijse_draws(sweep(d, 2, seq_len(p) * 1e4, "+"), ll,
                           group, diagnostics = "none")
    constants <- ijse_draws(d, sweep(ll, 2, seq_len(n) * 1e4, "+"),
                            group, diagnostics = "none")
    reordered <- ijse_draws(d, ll[, perm, drop = FALSE],
      if (clustered) group[perm] else NULL, diagnostics = "none")
    linear <- ijse_draws(d, ll, group,
      transform = function(x) x %*% t(a), diagnostics = "none")
    errors["translation"] <- max(abs(vcov(shifted) - vcov(ref)))
    errors["likelihood_constant"] <- max(abs(vcov(constants) - vcov(ref)))
    errors["permutation"] <- max(abs(vcov(reordered) - vcov(ref)))
    errors["linear_transform"] <- max(abs(unname(vcov(linear)) - a %*% target %*% t(a)))
    scales <- rep(max(1, max(abs(target))), length(errors))
    scales[length(errors)] <- max(1, max(abs(a %*% target %*% t(a))))
    relative <- errors / scales
    stopifnot(all(is.finite(relative)), max(relative) < 1e-7)
    records[[length(records) + 1L]] <- data.frame(case = case, draws = s,
      observations = n, quantities = p, clustered = clustered, units = k,
      check = names(errors), absolute_error = unname(errors),
      scaled_error = unname(relative))
  }
}
out <- do.call(rbind, records)
write.csv(out, args[1L], row.names = FALSE)
cat(b, "configurations;", nrow(out), "numerical comparisons passed.\n")
cat("Maximum scaled discrepancy:", max(out$scaled_error), "\n")

Try the IJSE package in your browser

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

IJSE documentation built on Oct. 22, 2026, 5:18 p.m.