tests/testthat/test-variance-bounds.R

test_that("generic Cauchy bounds match analytic formula", {
  V0_bar <- 0.01
  V1_bar <- 0.04
  
  b <- variance_bounds(
    N0 = 100, N1 = 120,
    V0_bar = V0_bar,
    V1_bar = V1_bar,
    cov_sign = "unrestricted",
    overlap_mode = "generic"
  )
  
  v_lower_expected <- (sqrt(V0_bar) - sqrt(V1_bar))^2
  v_upper_expected <- (sqrt(V0_bar) + sqrt(V1_bar))^2
  
  expect_equal(b$var_lower, v_lower_expected, tolerance = 1e-12)
  expect_equal(b$var_upper, v_upper_expected, tolerance = 1e-12)
})

test_that("nonnegative covariance restriction sharpens upper bound", {
  V0_bar <- 0.01
  V1_bar <- 0.04
  
  b_unres <- variance_bounds(
    N0 = 100, N1 = 120,
    V0_bar = V0_bar,
    V1_bar = V1_bar,
    cov_sign = "unrestricted",
    overlap_mode = "generic"
  )
  
  b_nonneg <- variance_bounds(
    N0 = 100, N1 = 120,
    V0_bar = V0_bar,
    V1_bar = V1_bar,
    cov_sign = "nonnegative",
    overlap_mode = "generic"
  )
  
  # Lower bound is the same; upper bound is smaller with nonnegative cov
  expect_equal(b_unres$var_lower, b_nonneg$var_lower, tolerance = 1e-12)
  expect_lt(b_nonneg$var_upper, b_unres$var_upper)
})

test_that("overlap-aware bounds are no looser than generic bounds", {
  V0_bar <- 0.01
  V1_bar <- 0.04
  
  N0 <- 100
  N1 <- 120
  nS <- 80
  
  sigma2S <- 0.02  # pooled stayer variance
  
  b_generic <- variance_bounds(
    N0 = N0, N1 = N1,
    V0_bar = V0_bar,
    V1_bar = V1_bar,
    cov_sign = "unrestricted",
    overlap_mode = "generic"
  )
  
  b_overlap <- variance_bounds(
    N0 = N0, N1 = N1,
    V0_bar = V0_bar,
    V1_bar = V1_bar,
    nS = nS,
    sigma2S = sigma2S,
    cov_sign = "unrestricted",
    overlap_mode = "overlap_aware"
  )
  
  # Overlap-aware bounds should be at least as tight as generic
  expect_gte(b_overlap$var_lower, b_generic$var_lower - 1e-12)
  expect_lte(b_overlap$var_upper, b_generic$var_upper + 1e-12)
})

Try the wavecov package in your browser

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

wavecov documentation built on Nov. 3, 2026, 5:07 p.m.