tests/testthat/test-model_ddm.R

context("DDM - shifted Drift Diffusion Model with outliers")

# Reference implementation of the mixture. The decision component is delegated
# to brms::dwiener(), which is what the migration is *not* about; what is
# written out longhand here is the mixture around it and the 1/K on the outlier
# component.
ref_dens <- function(y, drift, boundary, bias, ndt, response, poutlier) {
  d_out <- 2 * stats::dnorm(y, 0, 0.2) / 2
  d_dec <- if (y - ndt > 0) {
    brms::dwiener(y, alpha = boundary, tau = ndt, beta = bias, delta = drift,
                  resp = response)
  } else {
    0
  }
  poutlier * d_out + (1 - poutlier) * d_dec
}

make_prep <- function(y, dec, drift, boundary, bias, ndt, poutlier,
                      sigmadrift = 0, sigmabias = 0, sigmandt = 0,
                      n_draws = 10, family = cogmod_ddm()) {
  structure(
    list(
      data = list(Y = y, dec = dec),
      family = family,
      dpars = list(
        mu = rep(drift, n_draws),
        boundary = rep(boundary, n_draws),
        bias = rep(bias, n_draws),
        sigmadrift = rep(sigmadrift, n_draws),
        sigmabias = rep(sigmabias, n_draws),
        sigmandt = rep(sigmandt, n_draws),
        ndt = rep(ndt, n_draws),
        poutlier = rep(poutlier, n_draws)
      )
    ),
    class = "brmsprep"
  )
}


# dcogmod_ddm -------------------------------------------------------------

test_that("dcogmod_ddm matches the mixture density", {
  skip_if_not_installed("RWiener") # brms::dwiener() calls it
  pars <- list(drift = 0.5, boundary = 1, bias = 0.4, ndt = 0.2)
  for (poutlier in c(0, 0.02, 0.4)) {
    for (response in 0:1) {
      for (y in c(0.05, 0.15, 0.25, 0.6, 1.5, 4)) {
        expect_equal(
          do.call(dcogmod_ddm, c(list(x = y), pars,
                                 list(response = response,
                                      poutlier = poutlier))),
          do.call(ref_dens, c(list(y = y), pars,
                              list(response = response,
                                   poutlier = poutlier))),
          tolerance = 1e-10,
          info = sprintf("y = %.2f, response = %d, poutlier = %.2f",
                         y, response, poutlier)
        )
      }
    }
  }
})


test_that("the density sums to one over responses and integrates to one", {
  # Including the three between-trial variability parameters one at a time and
  # all together, since each takes a different branch of the density.
  grid <- list(
    c(drift = 0.5, boundary = 1, bias = 0.5, ndt = 0.2, sv = 0, sw = 0, st = 0),
    c(drift = -1.5, boundary = 1.6, bias = 0.35, ndt = 0.25, sv = 0, sw = 0,
      st = 0),
    c(drift = 0.8, boundary = 1, bias = 0.6, ndt = 0.2, sv = 0.8, sw = 0,
      st = 0),
    c(drift = 0.8, boundary = 1, bias = 0.5, ndt = 0.2, sv = 0, sw = 0.5,
      st = 0),
    c(drift = 0.8, boundary = 1, bias = 0.5, ndt = 0.2, sv = 0, sw = 0,
      st = 0.08),
    c(drift = 0.3, boundary = 1.2, bias = 0.45, ndt = 0.15, sv = 0.6, sw = 0.4,
      st = 0.06),
    c(drift = 0, boundary = 1, bias = 0.5, ndt = 0, sv = 0, sw = 0, st = 0)
  )
  for (g in grid) {
    for (poutlier in c(0, 0.03, 0.4)) {
      total <- sum(vapply(0:1, function(k) {
        stats::integrate(
          function(t) {
            dcogmod_ddm(t, g[["drift"]], g[["boundary"]], g[["bias"]],
                        g[["ndt"]], response = k, sigmadrift = g[["sv"]],
                        sigmabias = g[["sw"]], sigmandt = g[["st"]],
                        poutlier = poutlier)
          },
          lower = 0, upper = Inf, subdivisions = 4000
        )$value
      }, numeric(1)))
      expect_equal(total, 1, tolerance = 1e-4,
                   info = sprintf("sv=%.1f sw=%.1f st=%.2f poutlier=%.2f",
                                  g[["sv"]], g[["sw"]], g[["st"]], poutlier))
    }
  }
})


test_that("responses faster than ndt keep positive density (the whole point)", {
  y <- c(0.001, 0.05, 0.1, 0.19)
  for (poutlier in c(0.02, 0.4)) {
    for (response in 0:1) {
      d <- dcogmod_ddm(y, drift = 0.5, boundary = 1, bias = 0.5, ndt = 0.2,
                       response = response, poutlier = poutlier)
      expect_true(all(d > 0))
      # exactly the outlier component, thinned by the two response options
      expect_equal(d, poutlier * 0.5 * 2 * stats::dnorm(y, 0, 0.2),
                   tolerance = 1e-12)
    }
  }
})


test_that("poutlier = 0 recovers the plain shifted diffusion", {
  y <- c(0.05, 0.15, 0.2)
  expect_equal(dcogmod_ddm(y, drift = 0.5, ndt = 0.2, response = 1,
                           poutlier = 0),
               rep(0, length(y)))
  expect_equal(dcogmod_ddm(y, drift = 0.5, ndt = 0.2, response = 1,
                           poutlier = 0, log = TRUE),
               rep(-Inf, length(y)))
})


test_that("the tau0 offset is exactly that: an offset", {
  skip_if_not_installed("RWiener") # brms::dwiener() calls it
  # brms::dwiener() refuses a zero non-decision time, but the density depends on
  # the time and the non-decision time only through their difference, so the
  # decision component is evaluated at (t + tau0, tau0). This checks that the
  # shift really does cancel: the mixture density at ndt must equal the plain
  # Wiener density with that same ndt.
  y <- c(0.21, 0.3, 0.8, 3)
  for (k in 0:1) {
    expect_equal(
      dcogmod_ddm(y, drift = 0.7, boundary = 1, bias = 0.4, ndt = 0.2,
                  response = k, poutlier = 0, log = TRUE),
      brms::dwiener(y, alpha = 1, tau = 0.2, beta = 0.4, delta = 0.7,
                    resp = k, log = TRUE),
      tolerance = 1e-12
    )
  }
})


test_that("dcogmod_ddm is vectorized over every argument", {
  n <- 7
  d <- dcogmod_ddm(seq(0.3, 1.2, length.out = n),
                   drift = seq(-1, 1, length.out = n),
                   boundary = seq(0.8, 1.6, length.out = n),
                   bias = seq(0.3, 0.7, length.out = n),
                   ndt = seq(0.05, 0.25, length.out = n),
                   response = rep(0:1, length.out = n),
                   poutlier = seq(0, 0.1, length.out = n))
  expect_length(d, n)
  expect_true(all(is.finite(d) & d >= 0))
})


test_that("dcogmod_ddm returns 0 density for invalid parameters", {
  args <- list(x = 0.5, drift = 0.5, boundary = 1, bias = 0.5, ndt = 0.2,
               response = 1)
  for (bad in list(list(boundary = 0), list(bias = 0), list(bias = 1),
                   list(sigmadrift = -1), list(sigmabias = 1),
                   list(ndt = -0.1), list(poutlier = 1.5),
                   list(response = 2))) {
    expect_warning(d <- do.call(dcogmod_ddm, modifyList(args, bad)),
                   info = names(bad))
    expect_equal(d, 0)
  }
})


test_that("the variability parameters are legitimately zero", {
  # That is the classic 4-parameter DDM, so their lower bounds are closed -
  # unlike `boundary` and `bias`, which are not.
  expect_silent(d <- dcogmod_ddm(0.5, drift = 0.5, response = 1,
                                 sigmadrift = 0, sigmabias = 0, sigmandt = 0))
  expect_true(is.finite(d) && d > 0)
  expect_silent(rcogmod_ddm(10, drift = 0.5, sigmadrift = 0, sigmabias = 0,
                            sigmandt = 0))
})


test_that("the st0 quadrature resolves a range that reaches fast decision times", {
  # rtdists issue #28, reproduced here before the fix: when the st0 range
  # covers decision times from about zero up to `t`, the integrand over the
  # non-decision time holds the whole early peak of the first-passage density
  # inside a sliver of the range, and a fixed 25-node rule on the plain time
  # scale missed it by up to 3% on the sweep below. The rule now runs over log
  # decision time from where the density is dead, and these are the cases that
  # broke the old one, checked against the same rule at 400 nodes (which agrees
  # with 800 and 1600 nodes to 1e-12).
  ldens <- function(t, w, st0, sw, response = 0, sv = 0, nodes = 25) {
    n <- max(length(t), length(w), length(st0), length(sw), length(response))
    pars <- list(
      t = rep_len(t, n), drift = rep_len(0.5, n), boundary = rep_len(0.5, n),
      bias = rep_len(w, n), response = rep_len(response, n),
      sigmadrift = rep_len(sv, n), sigmabias = rep_len(sw, n),
      sigmandt = rep_len(st0, n)
    )
    cogmod:::.ddm_ldens_var(pars, nodes = nodes)
  }
  relerr <- function(x, ref) abs(exp(x - ref) - 1)

  # the issue's own examples: start point near the responding boundary, and
  # the st0 range reaching down to zero decision time (the old rule was out by
  # 5.5e-4, 5.7e-3, 6e-6, 5.5e-4 and 6e-7 on these, in order)
  hard <- list(
    list(t = 0.16, w = 0.3, st0 = 0.16, sw = 0),
    list(t = 0.16, w = 0.3, st0 = 0.16, sw = 0.5),
    list(t = 0.16, w = 0.3, st0 = 0.16, sw = 0, response = 1),
    list(t = 0.16, w = 0.3, st0 = 0.16, sw = 0, sv = 0.3),
    list(t = 0.025, w = 0.3, st0 = 0.1, sw = 0)
  )
  for (h in hard) {
    expect_lt(relerr(do.call(ldens, h), do.call(ldens, c(h, nodes = 400))),
              1e-9)
  }

  # the sweep: 120 cells, half of them with the range reaching zero
  g <- expand.grid(w = c(0.1, 0.2, 0.3, 0.5), st0 = c(0.05, 0.1, 0.2),
                   sw = c(0, 0.5), t = c(0.02, 0.05, 0.1, 0.2, 0.5))
  e <- relerr(ldens(g$t, g$w, g$st0, g$sw), ldens(g$t, g$w, g$st0, g$sw, nodes = 400))
  expect_lt(max(e), 1e-8)

  # and through the public density, with the range placed by `ndt`
  d <- dcogmod_ddm(0.46, drift = 0.5, boundary = 0.5, bias = 0.3, ndt = 0.3,
                   response = 0, sigmabias = 0.5, sigmandt = 0.16, log = TRUE)
  expect_equal(d, ldens(0.16, 0.3, 0.16, 0.5, nodes = 400), tolerance = 1e-9)
})


# rcogmod_ddm -------------------------------------------------------------

test_that("rcogmod_ddm returns rt and response", {
  set.seed(1)
  sim <- rcogmod_ddm(500, drift = 0.5, boundary = 1, bias = 0.5, ndt = 0.2)
  expect_true(is.data.frame(sim))
  expect_named(sim, c("rt", "response"))
  expect_equal(nrow(sim), 500)
  expect_true(all(sim$response %in% c(0, 1)))
  expect_true(all(is.finite(sim$rt)))
  expect_true(all(sim$rt > 0.2))
  # a positive drift pushes towards the boundary coded 1
  expect_gt(mean(sim$response == 1), 0.5)
})


test_that("pcogmod_ddm integrates dcogmod_ddm", {
  # The CDF is what rcogmod_ddm() inverts to draw from, so an error here would
  # land straight in the draws. Checked against the package's own density rather
  # than against rtdists, which is the less accurate of the two: at drift -4,
  # boundary 0.6, bias 0.25 it deviates from this integral by up to 5e-4 where
  # pcogmod_ddm() is at rounding error. See ?rcogmod_ddm.
  grid <- covering_grid(
    drift = c(-3, -1, 0, 1, 3),
    boundary = c(0.6, 1.2, 2.0),
    bias = c(0.05, 0.3, 0.5, 0.7, 0.95),
    q = c(0.35, 0.6, 1.2, 3),
    response = c(0L, 1L)
  )
  for (i in seq_len(nrow(grid))) {
    g <- grid[i, ]
    integrated <- stats::integrate(
      function(t) dcogmod_ddm(t, drift = g$drift, boundary = g$boundary,
                              bias = g$bias, ndt = 0.2, response = g$response,
                              poutlier = 0),
      0, g$q, rel.tol = 1e-11, subdivisions = 2000
    )$value
    expect_equal(
      pcogmod_ddm(g$q, drift = g$drift, boundary = g$boundary, bias = g$bias,
                  ndt = 0.2, response = g$response, poutlier = 0),
      integrated,
      tolerance = 1e-8,
      info = sprintf("drift %.1f boundary %.1f bias %.2f q %.2f resp %d",
                     g$drift, g$boundary, g$bias, g$q, g$response)
    )
  }
})


test_that("pcogmod_ddm behaves like a CDF", {
  q <- seq(0.05, 6, length.out = 200)
  p <- pcogmod_ddm(q, drift = -1.5, boundary = 1.1, bias = 0.45, ndt = 0.2,
                   poutlier = 0.05)
  expect_false(is.unsorted(p))
  expect_true(all(p >= 0 & p <= 1))
  # The two defective CDFs partition the marginal one.
  expect_equal(pcogmod_ddm(1.5, response = 0) + pcogmod_ddm(1.5, response = 1),
               pcogmod_ddm(1.5))
  expect_equal(pcogmod_ddm(0.8, lower.tail = FALSE), 1 - pcogmod_ddm(0.8))
  expect_equal(pcogmod_ddm(0.8, log.p = TRUE), log(pcogmod_ddm(0.8)))
  # Nothing arrives before the non-decision time without an outlier process.
  expect_equal(pcogmod_ddm(0.1, ndt = 0.2, poutlier = 0), 0)
  expect_equal(pcogmod_ddm(500), 1)
  expect_length(pcogmod_ddm(c(0.3, 0.5, 0.9), drift = c(-1, 0, 1)), 3)
  # The between-trial variability parameters would each need a quadrature layer,
  # so they are not arguments at all rather than being silently ignored.
  expect_error(pcogmod_ddm(1, sigmadrift = 0.5))
})


test_that("pcogmod_ddm stays a CDF with the start point against a boundary", {
  # rtdists's pdiffusion() was non-monotone for a start point near either
  # boundary until rtdists/rtdists@7dab07c: its PDE solver initialised the
  # grid with one boundary condition and interpolated against another. This
  # is a series, not a solver, so that mechanism is absent - but the grids
  # above stop at bias 0.05 and 0.95, and the series' term count and time
  # floor both depend on the start point, so the edges get asked directly.
  q <- seq(0.2, 4, by = 0.005)
  for (w in c(0.01, 0.02, 0.98, 0.99)) {
    info <- sprintf("bias %.2f", w)
    ps <- lapply(0:1, function(k) {
      pcogmod_ddm(q, drift = 0.5, boundary = 1, bias = w, ndt = 0.2,
                  response = k, poutlier = 0)
    })
    for (k in 0:1) {
      p <- ps[[k + 1]]
      pk <- pcogmod_ddm(Inf, drift = 0.5, boundary = 1, bias = w, ndt = 0.2,
                        response = k, poutlier = 0)
      expect_true(all(is.finite(p)), info = info)
      expect_false(is.unsorted(p), info = info)
      expect_true(all(p >= 0 & p <= pk), info = info)
    }
    # The two defective CDFs partition the marginal one, here as everywhere.
    expect_equal(ps[[1]] + ps[[2]],
                 pcogmod_ddm(q, drift = 0.5, boundary = 1, bias = w, ndt = 0.2,
                             poutlier = 0),
                 info = info)
    # And each is still the integral of its density, minority response
    # included - which is where the mass is smallest and a series is most
    # likely to be short a term.
    for (k in 0:1) {
      for (qq in c(0.35, 0.8, 2)) {
        integrated <- stats::integrate(
          function(t) dcogmod_ddm(t, drift = 0.5, boundary = 1, bias = w,
                                  ndt = 0.2, response = k, poutlier = 0),
          0, qq, rel.tol = 1e-11, subdivisions = 2000
        )$value
        expect_equal(
          pcogmod_ddm(qq, drift = 0.5, boundary = 1, bias = w, ndt = 0.2,
                      response = k, poutlier = 0),
          integrated, tolerance = 1e-8,
          info = sprintf("%s response %d q %.2f", info, k, qq)
        )
      }
    }
  }
})


test_that("pcogmod_ddm is the choice probability at infinite time", {
  # The series can only approach the choice probability - every term is
  # exp(-R t) - so infinite time is taken analytically rather than from it, and
  # used to be left at the pre-allocated zero instead.
  g <- list(drift = -4, boundary = 0.6, bias = 0.25, ndt = 0.2, poutlier = 0)
  for (resp in c(0L, 1L)) {
    expect_equal(
      do.call(pcogmod_ddm, c(list(Inf, response = resp), g)),
      do.call(pcogmod_ddm, c(list(1e6, response = resp), g)),
      info = sprintf("response %d", resp)
    )
  }
  expect_equal(do.call(pcogmod_ddm, c(list(Inf), g)), 1)
  expect_equal(do.call(pcogmod_ddm, c(list(Inf, lower.tail = FALSE), g)), 0)
  # An infinite time among finite ones must not disturb them, and -Inf is still
  # before any response.
  expect_equal(
    do.call(pcogmod_ddm, c(list(c(0.5, Inf, -Inf), response = 0L), g)),
    c(do.call(pcogmod_ddm, c(list(0.5, response = 0L), g)),
      do.call(pcogmod_ddm, c(list(Inf, response = 0L), g)), 0)
  )
})


test_that("pcogmod_ddm agrees with the sampler it is inverted from", {
  skip_on_cran()
  set.seed(4)
  for (g in list(c(-2, 1.2, 0.5), c(1.5, 0.8, 0.35), c(0, 1.5, 0.6))) {
    sim <- rcogmod_ddm(60000, drift = g[1], boundary = g[2], bias = g[3],
                       ndt = 0.2, poutlier = 0)
    probs <- c(0.1, 0.25, 0.5, 0.75, 0.9)
    qq <- unname(stats::quantile(sim$rt, probs))
    expect_equal(
      pcogmod_ddm(qq, drift = g[1], boundary = g[2], bias = g[3], ndt = 0.2,
                  poutlier = 0),
      probs,
      tolerance = 0.01,
      info = sprintf("drift %.1f boundary %.1f bias %.2f", g[1], g[2], g[3])
    )
  }
})


test_that("rcogmod_ddm reproduces its own density", {
  set.seed(42)
  n <- 40000
  for (g in list(
    list(v = 0.5, a = 1, w = 0.5, ndt = 0.2, sv = 0, sw = 0, st = 0),
    list(v = 0.3, a = 1.2, w = 0.45, ndt = 0.15, sv = 0.6, sw = 0.4, st = 0.06)
  )) {
    for (poutlier in c(0, 0.05)) {
      sim <- rcogmod_ddm(n, drift = g$v, boundary = g$a, bias = g$w,
                         ndt = g$ndt, sigmadrift = g$sv, sigmabias = g$sw,
                         sigmandt = g$st, poutlier = poutlier)
      dens <- function(t, k) {
        dcogmod_ddm(t, drift = g$v, boundary = g$a, bias = g$w, ndt = g$ndt,
                    response = k, sigmadrift = g$sv, sigmabias = g$sw,
                    sigmandt = g$st, poutlier = poutlier)
      }
      for (k in 0:1) {
        integrated <- stats::integrate(function(t) dens(t, k), 0, Inf,
                                       subdivisions = 4000)$value
        expect_equal(mean(sim$response == k), integrated, tolerance = 0.015,
                     info = sprintf("sv %.1f, response %d, poutlier %.2f",
                                    g$sv, k, poutlier))
      }
      for (pr in c(0.25, 0.5, 0.9)) {
        qq <- unname(stats::quantile(sim$rt, pr))
        cdf <- sum(vapply(0:1, function(k) {
          stats::integrate(function(t) dens(t, k), 0, qq,
                           subdivisions = 4000)$value
        }, numeric(1)))
        expect_equal(cdf, pr, tolerance = 0.015,
                     info = sprintf("sv %.1f, p %.2f, poutlier %.2f",
                                    g$sv, pr, poutlier))
      }
    }
  }
})


test_that("ndt is a genuine shift, not a fraction of anything observed", {
  # The case the old tau * minrt parameterization could not express: a
  # non-decision time well above what any sample minimum would allow.
  set.seed(9)
  sim <- rcogmod_ddm(20000, drift = 0.5, boundary = 1, bias = 0.5, ndt = 0.9,
                     poutlier = 0)
  expect_gt(min(sim$rt), 0.9)
})


test_that("rcogmod_ddm produces responses below ndt only via outliers", {
  set.seed(2)
  expect_true(all(rcogmod_ddm(2000, drift = 0.5, ndt = 0.4,
                              poutlier = 0)$rt > 0.4))
  sim <- rcogmod_ddm(20000, drift = 0.5, ndt = 0.4, poutlier = 0.3)
  expect_true(any(sim$rt < 0.4))
  fast <- sim$response[sim$rt < 0.02]
  expect_setequal(unique(fast), c(0, 1))
  expect_equal(mean(fast == 0), 0.5, tolerance = 0.1)
})


test_that("every DDM draw lands on its target quantile of the CDF", {
  # Each draw is the inversion of the defective CDF at a uniform, so pushing it
  # back through `.ddm_cdf_lower()` must return that uniform. This is exact,
  # per draw, where the distributional tests above are statistical: it is what
  # caught the sampler returning a 30 s response for a 3 ms one when Newton
  # declared convergence on a step size alone, about once in 10,000 draws at
  # a short boundary and an extreme start point.
  #
  # The sampler consumes two runif(n) in this order: the response, then the
  # quantile. The response check makes this test fail loudly, rather than pass
  # vacuously, if that ever changes.
  n <- 20000
  for (g in list(c(v = 1, a = 0.5, w = 0.1), c(v = -3, a = 0.5, w = 0.1),
                 c(v = 0.5, a = 1, w = 0.5), c(v = 4, a = 3, w = 0.9),
                 c(v = 0, a = 2, w = 0.3))) {
    v <- rep(g[["v"]], n); a <- rep(g[["a"]], n); w <- rep(g[["w"]], n)
    set.seed(35)
    u <- stats::runif(n)
    q <- stats::runif(n)
    set.seed(35)
    sim <- .ddm_fpt_rng(n, v, a, w)
    lower <- sim$response == 0
    expect_identical(lower, u < .ddm_plower(v, a, w))
    expect_true(all(is.finite(sim$rt) & sim$rt > 0))
    vf <- ifelse(lower, v, -v)
    wf <- ifelse(lower, w, 1 - w)
    pf <- .ddm_plower(vf, a, wf)
    surv <- pf - .ddm_cdf_lower(sim$rt, vf, a, wf)
    expect_lt(max(abs(surv - (1 - q) * pf) / pf), 1e-9,
              label = sprintf("worst quantile error at v %.1f a %.1f w %.1f",
                              g[["v"]], g[["a"]], g[["w"]]))
  }
})


test_that("posterior_predict_cogmod_ddm takes a vector of observations", {
  # A hand-built brmsprep, shaped as brms::posterior_predict() has it by the
  # time the family method is called: every dpar already a draws x observations
  # matrix (or a scalar, for one fixed in the formula). That is all
  # brms::get_dpar() needs, and it keeps a model fit out of this test.
  set.seed(11)
  ndraws <- 20; nobs <- 7
  prep <- structure(list(
    dpars = list(
      mu = matrix(rnorm(ndraws * nobs, 1, 0.5), ndraws, nobs),
      boundary = matrix(runif(ndraws * nobs, 0.8, 2), ndraws, nobs),
      bias = matrix(runif(ndraws * nobs, 0.3, 0.7), ndraws, nobs),
      sigmadrift = 0, sigmabias = 0, sigmandt = 0,
      # distinct per observation, so the layout of the stacked output shows
      ndt = matrix(rep(seq(0.1, 0.7, by = 0.1), each = ndraws), ndraws, nobs),
      poutlier = matrix(0.02, ndraws, nobs)
    ),
    family = cogmod_ddm(), nobs = nobs, ndraws = ndraws
  ), class = "brmsprep")

  one <- posterior_predict_cogmod_ddm(3, prep)
  expect_equal(dim(one), c(ndraws, 2))
  expect_true(all(one[, 1] > 0.3))          # ndt of observation 3

  idx <- c(2, 5, 7)
  many <- posterior_predict_cogmod_ddm(idx, prep)
  expect_equal(dim(many), c(ndraws * length(idx), 2))
  expect_true(all(many[, 2] %in% c(0, 1)))
  # draws for i[1] first, then i[2], ...: each block sits above its own ndt
  blocks <- split(many[, 1], rep(idx, each = ndraws))
  for (k in idx) expect_gt(min(blocks[[as.character(k)]]), k / 10)
  # the outlier component is off by default and on when asked
  expect_true(all(many[, 1] > 0.2))
  set.seed(12)
  with_out <- posterior_predict_cogmod_ddm(rep(idx, 40), prep,
                                           predict_outliers = TRUE)
  expect_true(any(with_out[, 1] < 0.2))
})


test_that("rcogmod_ddm errors on invalid parameters", {
  expect_error(rcogmod_ddm(10, boundary = 0), "boundary")
  expect_error(rcogmod_ddm(10, bias = 0), "bias")
  expect_error(rcogmod_ddm(10, sigmadrift = -1), "sigmadrift")
  expect_error(rcogmod_ddm(10, ndt = -1), "ndt")
  expect_error(rcogmod_ddm(10, poutlier = 2), "poutlier")
})


# brms methods ------------------------------------------------------------

test_that("log_lik_cogmod_ddm matches dcogmod_ddm", {
  prep <- make_prep(y = c(0.5, 0.15), dec = c(1, 0), drift = 0.5, boundary = 1,
                    bias = 0.4, ndt = 0.2, poutlier = 0.03)
  for (i in 1:2) {
    expect_equal(
      log_lik_cogmod_ddm(i, prep),
      rep(dcogmod_ddm(prep$data$Y[i], 0.5, 1, 0.4, 0.2,
                      response = prep$data$dec[i], poutlier = 0.03,
                      log = TRUE), 10)
    )
  }
})


test_that("log_lik_cogmod_ddm uses the observed choice", {
  p0 <- make_prep(y = 0.5, dec = 0, drift = 1.5, boundary = 1, bias = 0.7,
                  ndt = 0.2, poutlier = 0)
  p1 <- make_prep(y = 0.5, dec = 1, drift = 1.5, boundary = 1, bias = 0.7,
                  ndt = 0.2, poutlier = 0)
  expect_false(isTRUE(all.equal(log_lik_cogmod_ddm(1, p0),
                                log_lik_cogmod_ddm(1, p1))))
})


test_that("log_lik_cogmod_ddm errors when dec is missing", {
  prep <- make_prep(y = 0.5, dec = 1, drift = 0.5, boundary = 1, bias = 0.5,
                    ndt = 0.2, poutlier = 0)
  prep$data$dec <- NULL
  expect_error(log_lik_cogmod_ddm(1, prep), "dec")
})


test_that("posterior_predict_cogmod_ddm excludes outliers by default", {
  set.seed(3)
  prep <- make_prep(y = 0.5, dec = 1, drift = 0.5, boundary = 1, bias = 0.5,
                    ndt = 0.4, poutlier = 0.9, n_draws = 500)
  out <- posterior_predict_cogmod_ddm(1, prep)
  expect_true(is.matrix(out))
  expect_equal(dim(out), c(500L, 2L))
  expect_true(all(out[, 1] > 0.4))
  expect_true(all(out[, 2] %in% c(0, 1)))

  with_out <- posterior_predict_cogmod_ddm(1, prep, predict_outliers = TRUE)
  expect_true(any(with_out[, 1] < 0.4))
})


test_that("the family flag drives predictions when no argument is given", {
  set.seed(4)
  prep <- make_prep(y = 0.5, dec = 1, drift = 0.5, boundary = 1, bias = 0.5,
                    ndt = 0.4, poutlier = 0.9, n_draws = 500,
                    family = cogmod_ddm(predict_outliers = TRUE))
  expect_true(any(posterior_predict_cogmod_ddm(1, prep)[, 1] < 0.4))
  expect_true(all(
    posterior_predict_cogmod_ddm(1, prep, predict_outliers = FALSE)[, 1] > 0.4
  ))
})


test_that("posterior_epred_cogmod_ddm returns the mean RT of the diffusion", {
  set.seed(5)
  prep <- make_prep(y = 0.5, dec = 1, drift = 0.5, boundary = 1, bias = 0.4,
                    ndt = 0.2, poutlier = 0.05, n_draws = 4)
  ep <- posterior_epred_cogmod_ddm(prep)
  sim <- rcogmod_ddm(200000, drift = 0.5, boundary = 1, bias = 0.4, ndt = 0.2,
                     poutlier = 0)
  expect_equal(unname(ep[1]), mean(sim$rt), tolerance = 0.01)

  # ... and the outlier component only when asked for, which pulls it towards
  # the half Normal's own mean
  ep_out <- posterior_epred_cogmod_ddm(prep, predict_outliers = TRUE)
  expect_equal(unname(ep_out[1]),
               0.95 * unname(ep[1]) + 0.05 * cogmod:::.mcontam(),
               tolerance = 1e-10)
})


test_that("posterior_epred_cogmod_ddm warns when the closed form is not exact", {
  prep <- make_prep(y = 0.5, dec = 1, drift = 0.5, boundary = 1, bias = 0.5,
                    ndt = 0.2, poutlier = 0, sigmadrift = 0.5)
  expect_warning(posterior_epred_cogmod_ddm(prep), "4-parameter")
})


test_that("posterior_epred_cogmod_ddm handles a drift at zero", {
  # The closed form has a removable singularity there.
  prep <- make_prep(y = 0.5, dec = 1, drift = 0, boundary = 1, bias = 0.4,
                    ndt = 0.2, poutlier = 0)
  ep <- posterior_epred_cogmod_ddm(prep)
  expect_true(all(is.finite(ep)))
  z <- 0.4 * 1
  expect_equal(unname(ep[1]), 0.2 + z * (1 - z), tolerance = 1e-8)
})


# family ------------------------------------------------------------------

test_that("cogmod_ddm() builds a valid brms custom family", {
  fam <- cogmod_ddm()
  expect_s3_class(fam, "customfamily")
  # Order matters: brms passes the dpars to cogmod_ddm_lpdf in exactly this one.
  expect_equal(fam$dpars, c("mu", "boundary", "bias", "sigmadrift",
                            "sigmabias", "sigmandt", "ndt", "poutlier"))
  expect_equal(unname(cogmod:::.family_links(fam)),
               c("identity", "softplus", "logit", "softplus", "logit", "log",
                 "log", "logit"))
  expect_equal(fam$vars, "dec[n]")
  expect_false(fam$predict_outliers)
  # the old parameterization is gone, sigmatau with it
  expect_false(any(c("tau", "minrt", "sigmatau") %in% fam$dpars))
})


test_that("cogmod_ddm is one of the outlier-mixture families", {
  expect_true("cogmod_ddm" %in% cogmod:::.OUTLIER_FAMILIES)
  expect_true("cogmod_ddm" %in% cogmod:::.cogmod_families())
  expect_length(cogmod:::.cogmod_families(),
                length(unique(cogmod:::.cogmod_families())))
})


test_that("predict_outliers survives the validation brm() performs", {
  fam <- brms:::validate_family(cogmod_ddm(predict_outliers = TRUE))
  expect_true(fam$predict_outliers)
})


# Stan code ---------------------------------------------------------------

test_that("stanvars carry the likelihood with the outlier component", {
  code <- cogmod_ddm_stanvars()[[1]]$scode
  expect_true(grepl("real cogmod_ddm_lpdf", code))
  expect_true(grepl("int dec", code))
  expect_true(grepl("log_mix\\(poutlier", code))
  expect_true(grepl("half Normal with scale 0.2", code))
  expect_true(grepl("real cogmod_ddm_decision_lpdf", code))
  # the 4- and 5-parameter forms are the package's own series (.WFPT_STAN),
  # and only the 7-parameter form is Stan's
  expect_true(grepl("real cogmod_wfpt_lpdf", code, fixed = TRUE))
  expect_true(grepl("return cogmod_wfpt_lpdf(t | boundary, w, v, sigmadrift);",
                    code, fixed = TRUE))
  expect_false(grepl("wiener_lpdf(y | boundary, tau0, w, v)", code, fixed = TRUE))
  expect_false(grepl("wiener_lpdf(y | boundary, tau0, w, v, sigmadrift)", code,
                     fixed = TRUE))
  # ...and that one carries the package's tolerance, not Stan's
  # default: it is generated from .DDM_WIENER_PRECISION, so check that number
  expect_true(grepl(
    sprintf("wiener_lpdf\\(y \\| boundary, tau0, w, v, sigmadrift, sw, sigmandt, %s\\)",
            formatC(cogmod:::.DDM_WIENER_PRECISION, format = "g", digits = 17,
                    width = 1)),
    code
  ))
  # the old parameterization is gone
  expect_false(grepl("real tau,", code))
  expect_false(grepl("real minrt", code))
  expect_false(grepl("sigmatau", code))
})


test_that("the Stan offset is generated from the R one", {
  # They have to be the same number, so only one of them is written down.
  code <- cogmod_ddm_stanvars()[[1]]$scode
  expect_true(grepl(sprintf("real tau0 = %s;",
                            formatC(cogmod:::.DDM_TAU0, format = "g",
                                    digits = 17, width = 1)),
                    code, fixed = TRUE))
})


test_that("Stan cogmod_ddm_lpdf matches dcogmod_ddm", {
  skip_on_cran()
  skip_if_not_installed("cmdstanr")

  lpdf <- stan_fun("cogmod_ddm")
  grid <- covering_grid(
    Y = c(0.02, 0.25, 0.5, 1.2, 4),
    mu = c(-1.5, 0, 0.8),
    boundary = c(0.6, 1.2, 2),
    bias = c(0.05, 0.3, 0.5, 0.7, 0.95),
    sigmadrift = c(0, 0.8),
    sigmabias = c(0, 0.5),
    sigmandt = c(0, 0.08),
    ndt = c(0, 0.15, 0.4),
    poutlier = c(0, 0.001, 0.4),
    dec = 0:1,
    # the three Stan branches - 4-, 5- and 7-parameter - all have to be swept,
    # and the 7-parameter one is where the quadrature lives
    always = function(g) {
      g$boundary == 1.2 & g$bias == 0.5 & g$ndt == 0.15 & g$poutlier == 0.001
    }
  )
  # The regime of rtdists issue #28 - the st0 range reaching down to fast
  # decision times, the start point near the responding boundary - where the R
  # side used to be out by up to 3%. Every cell is in the 7-parameter branch.
  hard <- expand.grid(
    Y = 0.1 + c(0.025, 0.16, 0.2), mu = 0.5, boundary = 0.5, bias = c(0.1, 0.3),
    sigmadrift = c(0, 0.3), sigmabias = c(0, 0.5), sigmandt = c(0.16, 0.2),
    ndt = 0.1, poutlier = 0.01, dec = 0:1
  )
  for (i in seq_len(nrow(grid) + nrow(hard))) {
    g <- if (i <= nrow(grid)) grid[i, ] else hard[i - nrow(grid), ]
    stan <- lpdf(g$Y, g$mu, g$boundary, g$bias, g$sigmadrift, g$sigmabias,
                 g$sigmandt, g$ndt, g$poutlier, as.integer(g$dec))
    r <- dcogmod_ddm(g$Y, g$mu, g$boundary, g$bias, g$ndt, response = g$dec,
                     sigmadrift = g$sigmadrift, sigmabias = g$sigmabias,
                     sigmandt = g$sigmandt, poutlier = g$poutlier, log = TRUE)
    if (is.infinite(r)) {
      expect_equal(stan, r)
    } else {
      # Relative, not absolute. The floor is looser than the closed-form
      # families manage because the 7-parameter branch is adaptive quadrature on
      # the Stan side and fixed-node Gauss-Legendre on the R side: the two
      # integrate the same function by different rules. The R side agrees with
      # itself at 400 nodes to 1e-9 on every cell here (see the quadrature test
      # above), and the worst cells sit at 2e-6, which is Stan's own stopping
      # tolerance - so this is as tight as the comparison can be made.
      expect_lt(abs(stan - r) / max(1, abs(r)), 1e-5)
    }
  }

  # invalid arguments are rejected on both sides
  expect_equal(lpdf(0.5, 0.5, 0, 0.5, 0, 0, 0, 0.2, 0.02, 1L), -Inf)
  expect_equal(lpdf(0.5, 0.5, 1, 0, 0, 0, 0, 0.2, 0.02, 1L), -Inf)
  expect_equal(lpdf(0.5, 0.5, 1, 1, 0, 0, 0, 0.2, 0.02, 1L), -Inf)
  expect_equal(lpdf(0.5, 0.5, 1, 0.5, -1, 0, 0, 0.2, 0.02, 1L), -Inf)
  expect_equal(lpdf(0.5, 0.5, 1, 0.5, 0, 1, 0, 0.2, 0.02, 1L), -Inf)
  expect_equal(lpdf(0.5, 0.5, 1, 0.5, 0, 0, -1, 0.2, 0.02, 1L), -Inf)
  expect_equal(lpdf(0.5, 0.5, 1, 0.5, 0, 0, 0, -0.1, 0.02, 1L), -Inf)
  expect_equal(lpdf(0.5, 0.5, 1, 0.5, 0, 0, 0, 0.2, 1.5, 1L), -Inf)
  expect_equal(lpdf(0.5, 0.5, 1, 0.5, 0, 0, 0, 0.2, 0.02, 2L), -Inf)
  expect_equal(lpdf(0, 0.5, 1, 0.5, 0, 0, 0, 0.2, 0.02, 1L), -Inf)
})


test_that("the 4- and 5-parameter Stan density is the R series, to 1e-10", {
  skip_on_cran()
  skip_if_not_installed("cmdstanr")

  # Both sides run the same series (.ddm_lfpt0() and .WFPT_STAN), so the
  # comparison can be far tighter than the 7-parameter one above. The grid
  # reaches both tails - rescaled times from 1e-3 to 8000 - and start points
  # 1e-4 from either boundary, where the two sums arranged against
  # cancellation do their work. `poutlier = 0` leaves the decision density
  # alone.
  lpdf <- stan_fun("cogmod_ddm")
  g <- expand.grid(Y = c(0.03, 0.3, 1, 3, 20), boundary = c(0.05, 0.5, 1.5, 4),
                   bias = c(1e-4, 0.05, 0.5, 0.95, 1 - 1e-4), mu = c(-6, 0, 2.5),
                   sigmadrift = c(0, 0.4, 3), dec = 0:1)
  worst <- 0
  for (i in seq_len(nrow(g))) {
    x <- g[i, ]
    stan <- lpdf(x$Y, x$mu, x$boundary, x$bias, x$sigmadrift, 0, 0, 0.01, 0,
                 as.integer(x$dec))
    r <- dcogmod_ddm(x$Y, x$mu, x$boundary, x$bias, 0.01, response = x$dec,
                     sigmadrift = x$sigmadrift, poutlier = 0, log = TRUE)
    expect_true(is.finite(stan) && is.finite(r))
    worst <- max(worst, abs(stan - r) / max(1, abs(r)))
  }
  expect_lt(worst, 1e-10)
})


test_that("the series switch is where Navarro & Fuss's term counts cross", {
  ks <- cogmod:::.ddm_fpt_ks
  kl <- cogmod:::.ddm_fpt_kl
  us <- cogmod:::.DDM_FPT_SWITCH
  expect_equal(ks(us), kl(us), tolerance = 1e-12)
  # The small-time count rises with u and the large-time one falls, so each is
  # at its largest at the switch, and the lengths .WFPT_STAN writes out for
  # them - m = 3 each way and K = 6 - have to cover that.
  u <- exp(seq(log(1e-6), log(us), length.out = 200))
  expect_true(all(diff(ks(u)) >= 0))
  u <- exp(seq(log(us), log(1e6), length.out = 200))
  expect_true(all(diff(kl(u)) <= 0))
  expect_lte(max(2, ceiling(ks(us) / 2)), 3)
  expect_lte(max(2, ceiling(kl(us))), 6)
  # and the Stan code switches where the R code does
  expect_true(grepl(sprintf("if (u < %s)", formatC(us, format = "g", digits = 17,
                                                   width = 1)),
                    cogmod:::.WFPT_STAN, fixed = TRUE))
})


test_that("the series keeps its precision with the start point next to either boundary", {
  # Reference values from the small-time series at 80 digits (mpmath), at a
  # rescaled time where neither e1 nor the k = 1 term is negligible. "near" is
  # the start point next to the boundary that is hit, "far" next to the other.
  # The two leading small-time terms cancel on one side when written out
  # (6e-9 here, far side) and on the other when paired through expm1() (5e-8,
  # near side), so .ddm_lfpt0() picks by side; this holds both to 1e-12.
  f <- cogmod:::.ddm_lfpt0
  expect_equal(f(0.1, 1e-6, 1 - 1e-6), -11.28057161245287393, tolerance = 1e-13)
  expect_equal(f(0.1, 1e-9, 1 - 1e-9), -18.18832689143001087, tolerance = 5e-14)
  expect_equal(f(0.1, 1 - 1e-6, 1e-6), -13.39019969377375054, tolerance = 1e-13)
  expect_equal(f(0.1, 1 - 1e-9, 1e-9), -20.29795497276385043, tolerance = 5e-14)
  # and the Stan code makes the same choice
  expect_true(grepl("w < 0.5 ? -expm1(x) - w * (1 + e1) : wl + (wl - 2) * e1",
                    cogmod:::.WFPT_STAN, fixed = TRUE))
})


# priors and inits --------------------------------------------------------

test_that("cogmod_priors fills ndt and poutlier for cogmod_ddm", {
  set.seed(6)
  sim <- rcogmod_ddm(150, drift = 0.5, ndt = 0.2, poutlier = 0.03)
  d <- data.frame(RT = sim$rt, Error = sim$response,
                  Condition = rep(c("a", "b"), length.out = 150),
                  id = rep(1:10, length.out = 150))

  modelled <- brms::bf(RT | dec(Error) ~ 1, boundary ~ 1, bias ~ 1,
                       sigmadrift = 0, sigmabias = 0, sigmandt = 0, ndt ~ 1,
                       poutlier ~ 1, family = cogmod_ddm())
  p <- cogmod_priors(modelled, d)
  expect_true(any(p$dpar == "ndt" & p$class == "Intercept" &
                    p$prior == "normal(-1.2, 0.5)"))
  expect_true(any(p$dpar == "poutlier" & p$class == "Intercept" &
                    p$prior == "normal(-6, 1)"))

  omitted <- brms::bf(RT | dec(Error) ~ 1, family = cogmod_ddm())
  p2 <- cogmod_priors(omitted, d)
  expect_false(any(grepl("uniform", p2$prior)))
  expect_true(any(p2$class == "ndt" & p2$prior == "lognormal(-1.2, 0.5)"))
  expect_true(any(p2$class == "poutlier" & p2$prior == "exponential(280)"))

  mixed <- brms::bf(RT | dec(Error) ~ Condition + (1 | id), ndt ~ Condition,
                    sigmadrift = 0, sigmabias = 0, sigmandt = 0,
                    family = cogmod_ddm())
  p3 <- cogmod_priors(mixed, d)
  expect_s3_class(p3, "brmsprior")
  code <- brms::make_stancode(mixed, data = d, prior = p3,
                              stanvars = cogmod_stanvars(mixed))
  expect_false(grepl("uniform\\(", code))
  expect_true(grepl("cogmod_ddm_lpdf", code))
})


test_that("cogmod_priors fences off the three variability parameters", {
  # Each has a floor at zero that its link reaches only at minus infinity, and
  # the likelihood stops changing well before then - the same improper-posterior
  # failure as cogmod_lba1()'s sigmabias. brms leaves all three flat.
  set.seed(11)
  sim <- rcogmod_ddm(200, drift = 0.5, ndt = 0.2, poutlier = 0.02)
  d <- data.frame(RT = sim$rt, Error = sim$response,
                  Condition = rep(c("a", "b"), length.out = 200))
  f <- brms::bf(RT | dec(Error) ~ Condition, sigmadrift ~ Condition,
                sigmabias ~ Condition, sigmandt ~ Condition, ndt ~ 1,
                family = cogmod_ddm())

  raw <- brms::get_prior(f, data = d, family = cogmod_ddm())
  flat <- raw$dpar %in% c("sigmadrift", "sigmabias", "sigmandt")
  expect_true(all(raw$prior[flat] == ""))

  p <- cogmod_priors(f, d)
  pick <- function(dp, cls) {
    p$prior[p$dpar == dp & p$class == cls & nzchar(p$prior)]
  }
  expect_equal(pick("sigmadrift", "Intercept"), "normal(0, 1)")
  expect_equal(pick("sigmabias", "Intercept"), "normal(-2, 1)")
  expect_equal(pick("sigmandt", "Intercept"), "normal(-3, 1)")
  for (dp in c("sigmadrift", "sigmabias", "sigmandt")) {
    expect_equal(pick(dp, "b"), "normal(0, 0.5)", label = paste(dp, "slope"))
  }

  # omitted, they live on their natural scales, which differ from one another
  f2 <- brms::bf(RT | dec(Error) ~ 1, family = cogmod_ddm())
  p2 <- cogmod_priors(f2, d)
  expect_equal(p2$prior[p2$class == "sigmadrift"], "lognormal(-1, 0.75)")
  expect_equal(p2$prior[p2$class == "sigmabias"], "beta(1, 5)")
  expect_equal(p2$prior[p2$class == "sigmandt"], "lognormal(-3, 1)")

  for (form in list(f, f2)) {
    expect_silent(
      brms::make_stancode(form, data = d, family = cogmod_ddm(),
                          prior = cogmod_priors(form, d),
                          stanvars = cogmod_stanvars(form))
    )
  }
})


test_that("cogmod_priors gives boundary and bias proper priors", {
  # Both are identified, but brms leaves `boundary` flat - an improper prior on
  # a softplus link, which brms then refuses to sample from - and the slopes
  # of `bias` flat beside its logistic(0, 1) intercept.
  set.seed(12)
  sim <- rcogmod_ddm(200, drift = 0.5, ndt = 0.2, poutlier = 0.02)
  d <- data.frame(RT = sim$rt, Error = sim$response,
                  Condition = rep(c("a", "b"), length.out = 200),
                  id = rep(1:10, length.out = 200))
  f <- brms::bf(RT | dec(Error) ~ 1, boundary ~ Condition + (1 | id),
                bias ~ Condition + (1 | id), ndt ~ 1, sigmadrift = 0,
                sigmabias = 0, sigmandt = 0, family = cogmod_ddm())
  raw <- brms::get_prior(f, data = d, family = cogmod_ddm())
  expect_true(all(!nzchar(raw$prior[raw$dpar == "boundary" & raw$class != "sd"])))
  expect_true(all(!nzchar(raw$prior[raw$dpar == "bias" & raw$class == "b"])))

  p <- cogmod_priors(f, d)
  pick <- function(dp, cls, grp = "") {
    p$prior[p$dpar == dp & p$class == cls & p$group == grp & !nzchar(p$coef) &
              nzchar(p$prior)]
  }
  expect_equal(pick("boundary", "Intercept"), "normal(1, 1)")
  expect_equal(pick("boundary", "b"), "normal(0, 0.5)")
  # brms's own intercept, untouched
  expect_equal(pick("bias", "Intercept"), "logistic(0, 1)")
  expect_equal(pick("bias", "b"), "normal(0, 0.5)")
  # and both group-level SDs get what every other dpar the family names gets
  expect_equal(pick("boundary", "sd", "id"), "exponential(1)")
  expect_equal(pick("bias", "sd", "id"), "exponential(1)")

  # Under `0 + Intercept` the intercept is a `b` coefficient: it gets the
  # intercept's prior, not the blanket slope one it would otherwise inherit.
  g <- brms::bf(RT | dec(Error) ~ 1, boundary ~ 0 + Intercept + Condition,
                bias ~ 0 + Intercept + Condition, ndt ~ 1, sigmadrift = 0,
                sigmabias = 0, sigmandt = 0, family = cogmod_ddm())
  q <- cogmod_priors(g, d)
  coef_prior <- function(dp) {
    q$prior[q$dpar == dp & q$class == "b" & q$coef == "Intercept"]
  }
  expect_equal(coef_prior("boundary"), "normal(1, 1)")
  expect_equal(coef_prior("bias"), "logistic(0, 1)")

  # omitted, `boundary` takes its natural-scale row and `bias` keeps brms's
  # beta(1, 1), which is already proper
  h <- brms::bf(RT | dec(Error) ~ 1, sigmadrift = 0, sigmabias = 0,
                sigmandt = 0, family = cogmod_ddm())
  r <- cogmod_priors(h, d)
  expect_equal(r$prior[r$class == "boundary"], "lognormal(0.27, 0.5)")
  expect_equal(r$prior[r$class == "bias"], "beta(1, 1)")

  for (form in list(f, g, h)) {
    expect_silent(
      brms::make_stancode(form, data = d, family = cogmod_ddm(),
                          prior = cogmod_priors(form, d),
                          stanvars = cogmod_stanvars(form))
    )
  }
  # and with nothing flat left, brms will sample from the priors alone
  expect_no_error(
    brms::make_stancode(f, data = d, family = cogmod_ddm(), prior = p,
                        stanvars = cogmod_stanvars(f), sample_prior = "only")
  )
})


test_that("cogmod_inits covers the declared parameters", {
  set.seed(8)
  sim <- rcogmod_ddm(150, drift = 0.5, ndt = 0.2, poutlier = 0.03)
  d <- data.frame(RT = sim$rt, Error = sim$response,
                  Condition = rep(c("a", "b"), length.out = 150))
  f <- brms::bf(RT | dec(Error) ~ Condition, boundary ~ 1, bias ~ 1,
                sigmadrift ~ 1, sigmabias ~ 1, sigmandt ~ 1, ndt ~ 1,
                poutlier ~ 1, family = cogmod_ddm())
  vals <- cogmod_inits(f, d)(1)
  code <- suppressWarnings(
    brms::make_stancode(f, data = d, family = cogmod_ddm())
  )
  declared <- vapply(cogmod:::.stan_param_decls(code), `[[`, character(1),
                     "name")
  expect_setequal(names(vals), declared)
  expect_true(all(vapply(vals, function(v) all(is.finite(v)), logical(1))))

  v0 <- cogmod_inits(f, d, jitter = 0)(1)
  expect_lt(exp(v0$Intercept_ndt), 0.3)
  expect_equal(log1p(exp(v0$Intercept_boundary)), 1, tolerance = 1e-8)
  expect_equal(plogis(v0$Intercept_bias), 0.5, tolerance = 1e-8)
  # sigmandt is in seconds now, on a log link, so it starts at a plausible
  # range rather than at a fraction of a parameter that no longer exists
  expect_equal(exp(v0$Intercept_sigmandt), 0.05, tolerance = 1e-8)
})


# a real fit --------------------------------------------------------------

test_that("cogmod_ddm recovers ndt above the fastest observed response", {
  skip_if_not_slow()
  skip_on_cran()
  skip_if_not_installed("cmdstanr")

  set.seed(1234)
  n <- 2000
  sim <- rcogmod_ddm(n, drift = 0.8, boundary = 1.2, bias = 0.45, ndt = 0.25,
                     poutlier = 0.04)
  d <- data.frame(RT = sim$rt, Error = sim$response)
  expect_lt(min(d$RT), 0.25)

  f <- brms::bf(RT | dec(Error) ~ 1, boundary ~ 1, bias ~ 1,
                sigmadrift = 0, sigmabias = 0, sigmandt = 0, ndt ~ 1,
                poutlier ~ 1, family = cogmod_ddm())
  fit <- brms::brm(f, data = d, prior = cogmod_priors(f, d),
                   init = cogmod_inits(f, d), stanvars = cogmod_stanvars(f),
                   algorithm = "pathfinder", refresh = 0,
                   backend = "cmdstanr")

  s <- brms::posterior_summary(fit)[, "Estimate"]
  expect_equal(s[["b_Intercept"]], 0.8, tolerance = 0.25)
  expect_equal(log1p(exp(s[["b_boundary_Intercept"]])), 1.2, tolerance = 0.2)
  expect_equal(plogis(s[["b_bias_Intercept"]]), 0.45, tolerance = 0.08)
  expect_equal(exp(s[["b_ndt_Intercept"]]), 0.25, tolerance = 0.05)
  expect_equal(plogis(s[["b_poutlier_Intercept"]]), 0.04, tolerance = 0.04)
  expect_gt(exp(s[["b_ndt_Intercept"]]), min(d$RT) * 10)

  ll <- brms::log_lik(fit, ndraws = 10)
  expect_equal(dim(ll), c(10L, n))
  expect_true(all(is.finite(ll)))

  po <- p_outlier(fit)
  expect_equal(nrow(po), n)
  expect_true(all(po$p_outlier[po$rt < 0.1] > 0.99))
  expect_equal(mean(po$p_outlier), 0.04, tolerance = 0.04)

  expect_true(with_outliers(fit)$family$predict_outliers)
  expect_false(without_outliers(fit)$family$predict_outliers)
})


test_that("the defective tails of pcogmod_ddm add to the response probability", {
  # `lower.tail = FALSE` used to return `1 - P(RT <= q, choice = k)`, which is
  # `P(RT > q OR choice != k)` - at q = Inf it gave the probability of the
  # *other* response instead of zero. It is now the defective survival
  # `P(RT > q, choice = k)`, integrated on the same footing as the lower tail,
  # so the two add to that response's own mass. Same convention as
  # `pcogmod_rdm()`.
  pars <- list(drift = 0.8, boundary = 1, bias = 0.45, ndt = 0.2)
  for (poutlier in c(0, 0.1)) {
    pk <- vapply(0:1, function(k) {
      do.call(pcogmod_ddm, c(list(q = Inf, response = k), pars,
                             list(poutlier = poutlier)))
    }, numeric(1))
    expect_equal(sum(pk), 1, tolerance = 1e-10)

    for (k in 0:1) {
      # the survival of a response is exhausted at infinite time
      expect_equal(
        do.call(pcogmod_ddm, c(list(q = Inf, response = k,
                                    lower.tail = FALSE), pars,
                               list(poutlier = poutlier))),
        0, tolerance = 1e-10
      )
      for (q in c(0.3, 1, 5)) {
        lo <- do.call(pcogmod_ddm, c(list(q = q, response = k), pars,
                                     list(poutlier = poutlier)))
        hi <- do.call(pcogmod_ddm, c(list(q = q, response = k,
                                          lower.tail = FALSE), pars,
                                     list(poutlier = poutlier)))
        expect_equal(lo + hi, pk[k + 1], tolerance = 1e-12,
                     info = sprintf("k = %d, q = %.1f, poutlier = %.2f",
                                    k, q, poutlier))
      }
    }
  }

  # The marginal is untouched: there the attainable mass is one.
  for (q in c(0.4, 1, 3)) {
    lo <- do.call(pcogmod_ddm, c(list(q = q), pars, list(poutlier = 0.05)))
    hi <- do.call(pcogmod_ddm, c(list(q = q, lower.tail = FALSE), pars,
                                 list(poutlier = 0.05)))
    expect_equal(lo + hi, 1, tolerance = 1e-12)
  }

  # And the lower tail still integrates the defective density.
  for (k in 0:1) {
    for (q in c(0.4, 0.8, 2)) {
      expect_equal(
        do.call(pcogmod_ddm, c(list(q = q, response = k), pars,
                               list(poutlier = 0.05))),
        stats::integrate(function(z) {
          do.call(dcogmod_ddm, c(list(x = z, response = k), pars,
                                 list(poutlier = 0.05)))
        }, lower = 0, upper = q, rel.tol = 1e-10)$value,
        tolerance = 1e-8
      )
    }
  }
})

Try the cogmod package in your browser

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

cogmod documentation built on Nov. 28, 2026, 1:08 a.m.