A fixed trial budget has to be set in advance, and set for the least precise participant you can imagine, so everyone else is over-tested. If inference costs a fraction of a millisecond you can instead refit after every trial and stop each participant when their own estimate is precise enough.
You can try this without writing anything: the live task runs a similar loop in your browser.
-
State the target
A target is a statement about precision, not about trial count. Either a maximum posterior SD on some parameters, or a minimum confidence in a construct.
import grintools as gt # p01 from the example data in "Analyse data you already have" counts = [[29, 2, 3, 6], [1, 32, 0, 2], [2, 3, 36, 4], [2, 6, 3, 29]] fit, con = gt.infer(counts) # stop when every correlation is known to within an SD of 0.35 decision = gt.stop_on_precision(fit, sd_max=0.35, params=["rho"]) decision.stop # True / False print(decision.summary()) # stop = False (combine='all') # sd:rho_1 0.582 vs 0.350 [not met] decision.checks # one dict per target: value, threshold, met, reachable decision.blocked_by # targets the data may never be able to meetlibrary(grin) # p01 from the example data in "Analyse data you already have" counts <- matrix(c(29, 2, 3, 6, 1, 32, 0, 2, 2, 3, 36, 4, 2, 6, 3, 29), nrow = 4, byrow = TRUE) fit <- grin_infer(counts) # stop when every correlation is known to within an SD of 0.35 decision <- grin_stop_on_precision(fit$result, sd_max = 0.35, params = c("rho")) decision$stop # TRUE / FALSE decision$checks # one entry per target: value, threshold, met, reachable decision$blocked_by # targets the data may never be able to meet// In a browser task, the same rule is three lines: refit, take the largest // posterior SD you care about, compare to your target. const out = await model.predict({ counts, trials }); const rhoSD = out.std.slice(8, 12); const stop = Math.max(...rhoSD) <= 0.35;The summary names the worst of the four correlations, the one furthest from the target. Here that is
rho_1. The rule is met only when that one is within the target too. -
Put it in the trial loop
GRIN is fast enough to check after every trial. Wait until each stimulus has been shown at least 3 times, because the fit needs a few observations of every stimulus before it means anything.
import numpy as np, grintools as gt counts = np.zeros((4, 4), dtype=int) MIN_PER_STIMULUS, MAX_TRIALS = 3, 400 for n in range(1, MAX_TRIALS + 1): s = present_random_stimulus() # your experiment r = collect_response() # your experiment counts[s, r] += 1 if counts.sum(axis=1).min() < MIN_PER_STIMULUS: continue # too early to ask fit, con = gt.infer(counts) if gt.stop_on_precision(fit, sd_max=0.35, params=["rho"]).stop: print(f"target reached after {n} trials") breaklibrary(grin) counts <- matrix(0L, 4, 4) MIN_PER_STIMULUS <- 3; MAX_TRIALS <- 400 for (n in seq_len(MAX_TRIALS)) { s <- present_random_stimulus() # your experiment r <- collect_response() # your experiment counts[s, r] <- counts[s, r] + 1L if (min(rowSums(counts)) < MIN_PER_STIMULUS) next # too early to ask fit <- grin_infer(counts) d <- grin_stop_on_precision(fit$result, sd_max = 0.35, params = c("rho")) if (isTRUE(d$stop)) { message("target reached after ", n, " trials") break } }import { loadModelCached } from "./assets/js/grin-model.js"; const model = await loadModelCached("./assets/models/cm"); const counts = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]; const MIN_PER_STIMULUS = 3, MAX_TRIALS = 400; for (let n = 1; n <= MAX_TRIALS; n++) { const s = presentRandomStimulus(); // your experiment const r = await collectResponse(); // your experiment counts[s][r] += 1; const trials = counts.map((row) => row.reduce((a, b) => a + b, 0)); if (Math.min(...trials) < MIN_PER_STIMULUS) continue; // too early to ask const out = await model.predict({ counts, trials }); const rhoSD = out.std.slice(8, 12); if (Math.max(...rhoSD) <= 0.35) break; // every correlation within 0.35 }The JavaScript runs inside a
<script type="module">on a page served from the GRINwebfolder, which holds the model and the ONNX runtime it loads. -
Always set a ceiling
Some participants will not reach any given target, because the design does not contain the information. This is most likely for the correlations, and at high accuracy. Without a maximum you are running an experiment with no defined end.
The example ceiling of 400 trials is 100 per stimulus. With a target as tight as 0.35 on the correlations, expect many participants to reach the ceiling before the target. Report the ceiling and how many participants hit it. A saving computed over a cohort where the slowest were quietly dropped is not a saving.
Two caveats worth knowing
Targets on correlations inherit a calibration issue. The
correlation intervals run narrower than nominal, so a target on
rho is met sooner than it should be. Either pass
calibrated=True / calibrated = TRUE, or set the
target a little tighter than you actually want.
Stopping on a construct probability is different from stopping on precision. A probability near one half can mean the data are uninformative, and more trials will fix that. It can also mean that the true answer sits near a boundary, and more trials will not fix that. A precision target is the safer default.
Next: steer stimulus levels into the informative range, or go back to all tutorials.