tests/testthat/test-ccp-solver.R

testthat::test_that("semi-relaxed initializer is log-domain column softmax", {
  causalOT:::torch_check()
  C <- torch::torch_tensor(
    matrix(
      c(0.0, 2.0, 1.0, 3.0, 1.0, 4.0, 2.0, 0.5, 1.5, 1.0, 3.0, 0.0),
      nrow = 4,
      byrow = TRUE
    ),
    dtype = torch::torch_double()
  )
  b <- torch::torch_tensor(c(0.2, 0.5, 0.3), dtype = torch::torch_double())
  lambda <- 10.0

  w <- causalOT:::cot_init_semirelaxed(C, b, lambda)
  eta <- -C / lambda
  expected <- (eta -
    eta$logsumexp(1)$view(c(1L, -1L)) +
    log(b)$view(c(1L, -1L)))$logsumexp(2)$exp()

  testthat::expect_true(all(as.logical((w >= 0)$to(device = "cpu"))))
  testthat::expect_equal(w$sum()$item(), 1.0, tolerance = 1e-10)
  testthat::expect_equal(as.numeric(w), as.numeric(expected), tolerance = 1e-10)
})

testthat::test_that("ccp solver runs fixed lambda primal tensorized COT", {
  testthat::skip_on_cran()
  causalOT:::torch_check()
  set.seed(2141)
  n <- 15
  m <- 13
  d <- 2
  x <- matrix(stats::rnorm(n * d), n, d)
  y <- matrix(stats::rnorm(m * d), m, d)

  opt <- cotOptions(
    lambda = 100.0,
    solver = "mirror",
    niter = 3L,
    ot_niter = 500L,
    ot_tol = 1e-8,
    tol = 1e-10
  )
  cot <- causalOT:::COT$new(source = x, target = y, options = opt)

  testthat::expect_silent(cot$solve())
  testthat::expect_equal(sum(cot$weights), 1.0, tolerance = 1e-6)
  testthat::expect_true(all(cot$weights >= 0))

  ots <- cot$.__enclos_env__$private$optimizer$.__enclos_env__$private$ot_objects
  ot <- ots[[ls(ots)[[1L]]]]
  diag <- causalOT:::cot_ccp_diagnostics(ot)

  testthat::expect_true(is.finite(diag$objective))
  testthat::expect_true(diag$objective >= -1e-7)
  testthat::expect_lt(diag$row_marginal_max_error, 1e-5)
  testthat::expect_lt(diag$column_marginal_max_error, 1e-5)
  testthat::expect_equal(
    cot$.__enclos_env__$private$optimizer$.__enclos_env__$private$ccp_kkt_resid,
    diag$relative_fw_gap,
    # the divergence is a small difference of large terms, so the relative
    # gap only matches up to cancellation error
    tolerance = 1e-2
  )
})

testthat::test_that("ccp option rejects unsupported first-slice configurations", {
  causalOT:::torch_check()
  testthat::expect_error(
    suppressWarnings(
      cotOptions(lambda = 100.0, opt.direction = "dual", solver = "mirror")
    ),
    "opt.direction"
  )
  testthat::expect_error(
    cotOptions(
      lambda = c(100.0, 10.0),
      solver = "mirror"
    ),
    "single finite positive lambda"
  )
  testthat::expect_error(
    cotOptions(
      lambda = 100.0,
      solver = "mirror",
      balance.formula = "~X1"
    ),
    "balance constraints"
  )
})

testthat::test_that("solver option routes to the expected optimizer class", {
  causalOT:::torch_check()
  set.seed(619)
  x <- matrix(stats::rnorm(20), 10, 2)
  y <- matrix(stats::rnorm(18), 9, 2)

  opt_ccp <- cotOptions(
    lambda = 100.0,
    solver = "mirror",
    cost.online = "tensorized"
  )
  cot_ccp <- causalOT:::COT$new(source = x, target = y, options = opt_ccp)
  testthat::expect_s3_class(
    cot_ccp$.__enclos_env__$private$optimizer,
    "cotCCPTrain"
  )

  opt_lbfgs <- cotOptions(
    lambda = 100.0,
    solver = "lbfgs",
    cost.online = "tensorized"
  )
  cot_lbfgs <- causalOT:::COT$new(source = x, target = y, options = opt_lbfgs)
  testthat::expect_s3_class(
    cot_lbfgs$.__enclos_env__$private$optimizer,
    "OTProblem"
  )
  testthat::expect_false(inherits(
    cot_lbfgs$.__enclos_env__$private$optimizer,
    "cotCCPTrain"
  ))
})

testthat::test_that("ccp solve carries updated weights across iterations", {
  testthat::skip_on_cran()
  causalOT:::torch_check()
  set.seed(991)
  n <- 15
  m <- 13
  d <- 2
  x <- matrix(stats::rnorm(n * d), n, d)
  y <- matrix(stats::rnorm(m * d), m, d)

  run_ccp <- function(niter) {
    opt <- cotOptions(
      lambda = 100.0,
      solver = "mirror",
      polish = "none",
      cost.online = "tensorized",
      niter = niter,
      ot_niter = 100L,
      ot_tol = 1e-6,
      tol = 1e-12
    )
    cot <- causalOT:::COT$new(source = x, target = y, options = opt)
    cot$solve()
    cot$weights
  }

  w1 <- run_ccp(1L)
  w2 <- run_ccp(2L)

  testthat::expect_equal(sum(w1), 1.0, tolerance = 1e-6)
  testthat::expect_equal(sum(w2), 1.0, tolerance = 1e-6)
  testthat::expect_gt(max(abs(w1 - w2)), 1e-8)
})

testthat::test_that("ccp default fast path tries one tau per iteration", {
  testthat::skip_on_cran()
  causalOT:::torch_check()
  set.seed(416)
  x <- matrix(stats::rnorm(24), 12, 2)
  y <- matrix(stats::rnorm(22), 11, 2)

  opt <- cotOptions(
    lambda = 100.0,
    solver = "mirror",
    cost.online = "tensorized",
    niter = 3L,
    ot_niter = 50L,
    ot_tol = 1e-5,
    tol = 0
  )
  cot <- causalOT:::COT$new(source = x, target = y, options = opt)
  cot$solve()

  priv <- cot$.__enclos_env__$private$optimizer$.__enclos_env__$private
  testthat::expect_false(priv$ccp_backtracking)
  testthat::expect_equal(priv$ccp_candidate_count, priv$ccp_iter)
})

testthat::test_that("ccp sparse KKT diagnostic matches post-hoc diagnostic", {
  testthat::skip_on_cran()
  causalOT:::torch_check()
  set.seed(417)
  x <- matrix(stats::rnorm(24), 12, 2)
  y <- matrix(stats::rnorm(22), 11, 2)

  opt <- cotOptions(
    lambda = 100.0,
    solver = "mirror",
    cost.online = "tensorized",
    niter = 4L,
    ot_niter = 50L,
    ot_tol = 1e-5,
    tol = 0
  )
  cot <- causalOT:::COT$new(source = x, target = y, options = opt)
  cot$.__enclos_env__$private$optimizer$.__enclos_env__$private$ccp_diagnostics_every <- 100L
  cot$solve()

  priv <- cot$.__enclos_env__$private$optimizer$.__enclos_env__$private
  ot <- priv$ot_objects[[ls(priv$ot_objects)[[1L]]]]
  diag <- causalOT:::cot_ccp_diagnostics(ot)
  testthat::expect_equal(
    priv$ccp_kkt_resid,
    diag$relative_fw_gap,
    # the divergence is a small difference of large terms, so the relative
    # gap only matches up to cancellation error
    tolerance = 1e-2
  )
})

testthat::test_that("ccp fast path does not mutate the fixed y self potential", {
  testthat::skip_on_cran()
  causalOT:::torch_check()
  set.seed(418)
  x <- matrix(stats::rnorm(24), 12, 2)
  y <- matrix(stats::rnorm(22), 11, 2)

  opt <- cotOptions(
    lambda = 100.0,
    solver = "mirror",
    cost.online = "tensorized",
    niter = 2L,
    ot_niter = 50L,
    ot_tol = 1e-5,
    tol = 0
  )
  cot <- causalOT:::COT$new(source = x, target = y, options = opt)
  priv <- cot$.__enclos_env__$private$optimizer$.__enclos_env__$private
  ot <- priv$ot_objects[[ls(priv$ot_objects)[[1L]]]]
  ot$sinkhorn_cot(niter = 50L, tol = 1e-5)
  g_yy_before <- as.numeric(ot$potentials$g_yy$to(device = "cpu"))
  cot$solve()

  testthat::expect_equal(
    as.numeric(ot$potentials$g_yy$to(device = "cpu")),
    g_yy_before,
    tolerance = 1e-12
  )
})

testthat::test_that("dual solve writes solved weights back to COT object", {
  testthat::skip_on_cran()
  causalOT:::torch_check()
  set.seed(971)
  n <- 15
  m <- 13
  d <- 2
  x <- matrix(stats::rnorm(n * d), n, d)
  y <- matrix(stats::rnorm(m * d), m, d)

  opt <- suppressWarnings(cotOptions(
    lambda = 100.0,
    opt.direction = "dual",
    cost.online = "tensorized",
    niter = 5L,
    ot_niter = 50L,
    ot_tol = 1e-5,
    tol = 1e-5
  ))
  cot <- causalOT:::COT$new(source = x, target = y, options = opt)
  start_weights <- cot$weights

  testthat::expect_silent(cot$solve())
  direct_weights <- as.numeric(
    cot$.__enclos_env__$private$optimizer$weights$to(device = "cpu")
  )

  testthat::expect_equal(cot$weights, direct_weights, tolerance = 1e-7)
  testthat::expect_equal(sum(cot$weights), 1.0, tolerance = 1e-7)
  testthat::expect_gt(max(abs(cot$weights - start_weights)), 1e-6)
})

testthat::test_that("converged scale floors the relative check near zero", {
  # a loss near zero needs an impossibly small change without the floor
  testthat::expect_false(causalOT:::converged(1e-5 + 1e-9, 1e-5, 1e-6))
  testthat::expect_true(
    causalOT:::converged(1e-5 + 1e-9, 1e-5, 1e-6, scale = 1)
  )
  # away from zero the floor does not loosen the relative check
  testthat::expect_false(causalOT:::converged(10.1, 10, 1e-6, scale = 1))
})

testthat::test_that("lbfgs uses the optimality test for a single divergence", {
  testthat::skip_on_cran()
  causalOT:::torch_check()
  set.seed(2141)
  n <- 20
  d <- 2
  y <- matrix(stats::rnorm(n * d), n, d)
  # the source contains the target, so the optimal divergence is near zero
  x <- rbind(y, matrix(stats::rnorm(n * d), n, d))

  opt <- cotOptions(
    lambda = 10,
    solver = "lbfgs",
    line_search_fn = "strong_wolfe",
    polish = "auto",
    niter = 200L,
    ot_niter = 500L,
    ot_tol = 1e-6,
    tol = 1e-6
  )
  cot <- causalOT:::COT$new(source = x, target = y, options = opt)
  priv <- cot$.__enclos_env__$private$optimizer$.__enclos_env__$private
  cot$solve()

  testthat::expect_true(priv$optimality_check_eligible())
  testthat::expect_true(isTRUE(as.logical(utils::tail(
    unlist(priv$converged_run),
    1L
  ))))
  testthat::expect_lt(
    as.numeric(utils::tail(unlist(priv$iterations_run), 1L)),
    200
  )
})

Try the causalOT package in your browser

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

causalOT documentation built on Nov. 24, 2026, 1:06 a.m.