test_that("k posterior target is correctly expressed on log scale", { time <- c(0.3, 0.7, 1.2, 2) status <- c(1, 1, 0, 1) common <- list(alp = 0.4, p1 = 0.45, p2 = 0.55, th1 = 0.8, th2 = 1.6, k2 = 2.1) current <- 1.2 candidate <- 1.7 implemented <- ambs:::.logpost_k1_ww(common$alp, common$p1, common$p2, common$th1, common$th2, candidate, common$k2, time, status, log(2), 1) - ambs:::.logpost_k1_ww(common$alp, common$p1, common$p2, common$th1, common$th2, current, common$k2, time, status, log(2), 1) expected <- ambs:::.loglik_ww(common$alp, common$p1, common$p2, common$th1, common$th2, candidate, common$k2, time, status) - ambs:::.loglik_ww(common$alp, common$p1, common$p2, common$th1, common$th2, current, common$k2, time, status) + dnorm(log(candidate), log(2), 1, log = TRUE) - dnorm(log(current), log(2), 1, log = TRUE) expect_equal(implemented, expected, tolerance = 1e-12) }) test_that("relative Gamma proposal has the stated moments and finite correction", { current <- c(1e-10, 0.5, 1e6) relative_variance <- 0.1 shape <- 1 / relative_variance scale <- relative_variance * current expect_equal(shape * scale, current) expect_equal(shape * scale^2, relative_variance * current^2) candidate <- current * c(0.8, 1.2, 0.9) correction <- dgamma(current, shape = shape, scale = relative_variance * candidate, log = TRUE) - dgamma(candidate, shape = shape, scale = relative_variance * current, log = TRUE) expect_true(all(is.finite(correction))) }) test_that("relabeling does not overwrite sampler acceptance rates", { set.seed(51) time <- c(0.2, 0.35, 0.5, 0.8, 1.1, 1.5, 2, 2.8, 3.5, 5) status <- c(1, 1, 0, 1, 1, 0, 1, 0, 1, 0) fit <- alpmixsurv(time, status, model = "ww", mcmc = list(nburn = 100, nsamp = 100, thin = 1)) expect_equal(fit$acceptance, fit$acceptance_raw) }) test_that("WW generalized-mean survival remains monotone for separated components", { time <- seq(8, 2200, length.out = 300) draw <- c(alpha = 2, p = 0.58, k1 = 0.0106, th1 = 10.5, k2 = 11.66, th2 = 0.058) survival <- ambs:::.curve_ww(draw, time, "s") expect_true(all(is.finite(survival))) expect_true(all(survival >= 0 & survival <= 1)) expect_true(all(diff(survival) <= 1e-12)) }) test_that("stable alpha-mixture agrees with direct arithmetic mixture", { log_s1 <- log(c(0.9, 0.5, 0.1)) log_s2 <- log(c(0.7, 0.3, 0.02)) log_h1 <- log(c(0.1, 0.2, 0.4)) log_h2 <- log(c(0.2, 0.3, 0.5)) out <- ambs:::.alpmix_log_quantities(1, 0.4, log_s1, log_s2, log_h1, log_h2) expected_survival <- 0.4 * exp(log_s1) + 0.6 * exp(log_s2) expected_hazard <- (0.4 * exp(log_s1 + log_h1) + 0.6 * exp(log_s2 + log_h2)) / expected_survival expect_equal(exp(out$log_surv), expected_survival, tolerance = 1e-12) expect_equal(exp(out$log_haz), expected_hazard, tolerance = 1e-12) })