## ---- brier_score ---------------------------------------------------- test_that("Brier score is zero for perfect predictions", { outcome <- c(0, 1, 0, 1) confidence <- c(0, 1, 0, 1) expect_equal(brier_score(outcome, confidence), 0) }) test_that("Brier score is one for maximally wrong predictions", { outcome <- c(0, 1, 0, 1) confidence <- c(1, 0, 1, 0) expect_equal(brier_score(outcome, confidence), 1) }) test_that("Brier score ignores incomplete cases", { outcome <- c(0, 1, NA, 1) confidence <- c(0, 1, 0.5, 1) expect_equal(brier_score(outcome, confidence), 0) }) ## ---- ece and calibration_curve ------------------------------------- test_that("ECE is zero for perfectly calibrated predictions", { # within each bin, predicted probability equals empirical frequency set.seed(10) confidence <- rep(c(0.05, 0.95), each = 100) outcome <- c(rbinom(100, 1, 0.05), rbinom(100, 1, 0.95)) val <- ece(outcome, confidence, n_bins = 10) expect_lt(val, 0.05) # near zero up to sampling noise }) test_that("ECE is large for badly miscalibrated predictions", { # model claims high confidence but is wrong half the time confidence <- rep(0.95, 100) outcome <- rep(c(0, 1), 50) # only 50% actually positive val <- ece(outcome, confidence, n_bins = 10) expect_gt(val, 0.4) }) test_that("calibration_curve omits empty bins rather than treating them as zero", { confidence <- c(0.1, 0.1, 0.9, 0.9) outcome <- c(0, 0, 1, 1) cc <- calibration_curve(outcome, confidence, n_bins = 10) expect_true(all(cc$n > 0)) expect_lte(nrow(cc), 10) }) ## ---- input validation ---------------------------------------------- test_that("calibration rejects probabilities outside [0, 1]", { expect_error(ai_calibration(c(0, 1, 1), c(-0.1, 0.5, 1.1)), "\\[0, 1\\]") }) test_that("calibration rejects non-binary outcomes", { expect_error(ai_calibration(c(0, 1, 2), c(0.1, 0.5, 0.9)), "binary") }) test_that("calibration rejects mismatched lengths", { expect_error(ai_calibration(c(0, 1), c(0.5)), "same length") }) test_that("calibration rejects n_bins < 2", { expect_error(ai_calibration(c(0, 1), c(0.2, 0.8), n_bins = 1), "n_bins") }) test_that("ai_calibration returns ece, brier, and curve", { set.seed(11) outcome <- rbinom(100, 1, 0.4) confidence <- pmin(pmax(outcome * 0.5 + runif(100, 0, 0.5), 0), 1) out <- ai_calibration(outcome, confidence) expect_s3_class(out, "aiEvalR_calibration") expect_true(all(c("ece", "brier", "curve") %in% names(out))) }) ## ---- empirical_output_interval ------------------------------------- test_that("empirical_output_interval brackets the median", { set.seed(12) samples <- rnorm(1000, mean = 5, sd = 1) out <- empirical_output_interval(samples, conf = 0.90) expect_lt(out$lower, out$median) expect_lt(out$median, out$upper) expect_equal(out$median, median(samples), tolerance = 0.1) })