Fitting MCMC models with Approximate Bayesian Computation

Lucas Castillo

University of Warwick

C. Stella Qian

University of Warwick

Adam N. Sanborn

University of Warwick

The challenge

We want to come up with a posterior over models or parameters…

\[ p(\theta|D) \propto \data{rn-color=#F2D541}{\class{rn-fragment fragment}{p(D|\theta)}} P(\theta) \] but we do not have a likelihood function.

Approximate Bayesian Computation

\[ p(\theta|D) \approx \frac 1 N \sum_{i=1}^N \delta (\mathcal M(\theta), y_{obs})\ \text{for large}\ N \]

\(\delta(a,b): 1\ \text{if}\ a = b\ \text{else}\ 0\)

Example

Q: I rolled \(N\) 6-sided dice and got an 11. How many dice did I roll?

Computation-Accuracy Trade-offs

Tolerance

In the previous example, I simulated 1,000,000 rolls.

Only 29,371 (\(2.9\%\)) equalled 11 (throw away \(97\%\) of the data).

We can instead keep \(11 \pm \epsilon\).

For \(\epsilon = 1\), we keep \(8.76\%\) of data instead.

Computation-Accuracy Trade-offs

Summary statistics

  • For high-dimensional data it is difficult to be identical or very similar (e.g. random generation data is 150-dimensional).
  • Instead use a function \(S(\cdot)\) that reduces the dimensionality of the data
  • Evaluate similarity in summary space.
  • Ideally, the choice of summary statistics should be sufficient (very difficult in practice): \[ p(\theta|S) = p(\theta|S, y_{obs}) \]

Plan

  1. Show you some summary statistics of your data
  • How close were you to true randomness?
  1. We fit the models to the data
  • Which model describes your performance best?
  1. Pointers to parameter estimation and other simulation-based approaches.

Experiment

Castillo et al. (2026) asked participants to say heights of people in the UK at random.

People are pretty bad at producing random sequences

Instead, they show patterns that can be used to identify which MCMC sampler best describes their item production

Instructions

Your task is to say the height of one of the people in the picture, chosen at random, every time you are prompted to do so. To do this, imagine:

  1. Shuffling the papers in the bag
  2. Choosing a random paper from the bag and reading the height.
  3. Returning the paper to the bag
  4. Repeating the process

Random Generation Summary Statistics

Repetitions

Adjacencies

Turning Points

Distances

Shape

RNG

gzip

Your Results

Most random

[1] "armand"

From Summaries to Model Inference

  1. Calculate summary statistics from the simulated data and observed data

We’ve computed these beforehand!

  • In the files you can find:
    • summaries from participants: data/obs_summaries.rds
      • Cleaned: removed items 4SD away (e.g. “1680”) in response2
    • summaries from models: simultaions/sim_summaries.rds
  • End product is a data frame for participants and models

Removed values:

 [1]   16   19   16   19 1791 1661   19   61   19   16   14   18   14 1687 1598
[16]  183

Summary dataframes

obs_summaries[1:2,]
# A tibble: 2 × 9
  id_number pseudonym      R      A   TPF     D       S    RNG  gzip
      <dbl> <chr>      <dbl>  <dbl> <dbl> <dbl>   <dbl>  <dbl> <dbl>
1        96 alligator 0      0.129  0.565  10.1 0.00416 0.108   1.61
2      1234 🦒        0.0205 0.0769 0.632  17.3 0.00309 0.0228  1.34
sim_summaries[1:2,]
  models    sigma epsilon  L nChains     alpha         R      A        TPF
1     MH 35.02590     0.1 33       6 0.9661909 0.1409396 0.1875 0.41216216
2 RECHMC 35.08824     0.1  3       6 0.8357762 0.7986577 1.0000 0.02702703
         D             S        RNG      gzip
1 4.507812  0.0002089995 0.08906698 1.2266667
2 1.000000 -0.0006306943 0.78358612 0.5066667

Inference

  1. Select a subset of simulations where the distance between simulations and observed data is smaller than a tolerance \(\epsilon\).
  1. Getting the mean and SD of the simulated summaries (\(M_s\), \(SD_s\))
  2. Standardizing observed and simulated summaries with those values (\((S_{obs} - M_s) / SD_s\))
  3. For each person, calculate Euclidean distance between each simulated row and observed summaries
  4. For that person, look at only the closest \(10\%\) simulations.
  5. What’s the proportion of models in that subset?

We use the abc package to do that for us.

library(abc)
fit <- abc(
  target= obs_summaries[obs_summaries$pseudonym == "lks", 3:9],
  param = sim_summaries$models,
  sumstat = sim_summaries[, 7:13],
  tol = .05,
  method = "rejection"
)
table(fit$unadj.values) / sum(table(fit$unadj.values))

   HMC    MC3  MCHMC  MCREC RECHMC RECMC3 
0.0116 0.5212 0.0482 0.0212 0.0032 0.3946 

Practice

Find out which model best fits your data Use 4.abc.R.

Inference

Do this for every participant then combine the likelihoods

df <- data.frame()
for (i in unique(obs_summaries$id_number)){
  fit <- abc(
    target= obs_summaries[obs_summaries$id_number == i, 3:9],
    param = sim_summaries$models,
    sumstat = sim_summaries[, 7:13],
    tol = .05, method = "rejection"
  )
  result <- cbind(
    data.frame(
      id_number=i,
      pseudonym=obs_summaries$pseudonym[obs_summaries$id_number == i]
    ),
    data.frame(table(fit$unadj.values) / sum(table(fit$unadj.values)))
  )
  df <- rbind(df, result)
}

df %>%
  group_by(Var1) %>%
  summarise(LL = sum(log(Freq))) %>%
  mutate(BF = exp(LL - min(LL)))
# A tibble: 8 × 3
  Var1       LL      BF
  <fct>   <dbl>   <dbl>
1 HMC    -47.0  5.92e 6
2 MC3    -25.0  2.24e16
3 MCHMC  -31.9  2.29e13
4 MCREC  -44.9  4.93e 7
5 RECHMC -62.6  1   e 0
6 RECMC3 -28.1  9.50e14
7 MH      -8.72 2.59e23
8 RECMH  -12.7  4.62e21

Inference

Other simulation-based inference

Parameter Estimation

library(abc)
fit <- abc(
  target= obs_summaries[
    obs_summaries$pseudonym == "lks", 3:9
  ],
  param = sim_summaries[
    sim_summaries$models == "MC3",
    c("sigma", "nChains")
  ],
  sumstat = sim_summaries[
    sim_summaries$models == "MC3", 7:13
  ],
  tol = .1,
  method = "rejection"
)

Other Simulation-based Approaches

Do Approximate Bayesian Computation but select \(\theta\) with MCMC (use distance as ‘goodness’)

sampler_mh(..., custom_density = \(x){})

Select models via machine-learning classification (Pudlo et al., 2016)

Use a neural network to produce summary statistics (Valentin et al., 2024)

Use a neural network to classify data (e.g. Bayesflow)

A good review in Cranmer et al. (2020)

Future Directions

Discussion

Discussion Questions

  • How do you see yourself using this?
    • What features would be useful to add for this?

Further Reading

References

Bramley, N. R., Dayan, P., Griffiths, T. L., & Lagnado, D. A. (2017). Formalizing Neurath’s ship: Approximate algorithms for online causal learning. Psychological Review, 124(3), 301–338. https://doi.org/10.1037/rev0000061
Bruckner, R., Nassar, M. R., Li, S.-C., & Eppinger, B. (2025). Differences in learning across the lifespan emerge via resource-rational computations. Psychological Review. https://doi.org/10.1037/rev0000526
Castillo, L., León-Villagrá, P., Chater, N., & Sanborn, A. N. (2024). Explaining the flaws in human random generation as local sampling with momentum. PLOS Computational Biology, 20(1), 1–24. https://doi.org/10.1371/journal.pcbi.1011739
Castillo, L., León-Villagrá, P., Falbén, J. K., Chater, N., & Sanborn, A. N. (2026). Random generation is what comes to mind in naturalistic settings. Cognition, 274, 106554. https://doi.org/10.1016/j.cognition.2026.106554
Castillo, L., Li, Y.-X., & Sanborn, A. N. (2025). The samplr package: A tool for modeling human cognition with sampling algorithms. https://doi.org/10.31234/osf.io/ax8hm_v2
Cranmer, K., Brehmer, J., & Louppe, G. (2020). The frontier of simulation-based inference. Proceedings of the National Academy of Sciences, 117(48), 30055–30062. https://doi.org/10.1073/pnas.1912789117
Dasgupta, I., Schulz, E., & Gershman, S. J. (2017). Where do hypotheses come from? Cognitive Psychology, 96, 1–25. https://doi.org/10.1016/j.cogpsych.2017.05.001
Davis, Z. J., & Rehder, B. (2020). A process model of causal reasoning. Cognitive Science, 44(5), e12839.
Gershman, S. J., Vul, E., & Tenenbaum, J. B. (2012). Multistability and Perceptual Inference. Neural Computation, 24(1), 1–24. https://doi.org/10.1162/NECO_a_00226
Pudlo, P., Marin, J.-M., Estoup, A., Cornuet, J.-M., Gautier, M., & Robert, C. P. (2016). Reliable ABC model choice via random forests. Bioinformatics, 32(6), 859–866. https://doi.org/10.1093/bioinformatics/btv684
Sisson, S. A., Fan, Y., & Beaumont, M. A. (Eds.). (2019). Handbook of approximate Bayesian computation. CRC Press, Taylor & Francis Group.
Spicer, J., Zhu, J.-Q., Chater, N., & Sanborn, A. N. (2022). Perceptual and Cognitive Judgments Show Both Anchoring and Repulsion. Psychological Science, 33(9), 1395–1407. https://doi.org/10.1177/09567976221089599
Spicer, J., Zhu, J.-Q., Chater, N., & Sanborn, A. N. (2024). How do people predict a random walk? Lessons for models of human cognition. Psychological Review, 131(5), 1069–1113. https://doi.org/10.1037/rev0000493
Turner, B. M., & Van Zandt, T. (2012). A tutorial on approximate Bayesian computation. Journal of Mathematical Psychology, 56(2), 69–85. https://doi.org/10.1016/j.jmp.2012.02.005
Valentin, S., Castillo, L., Sanborn, A. N., & Lucas, C. G. (2024). Distinguishing Between Process Models of Causal Learning. Proceedings of the Annual Meeting of the Cognitive Science Society, 46. https://escholarship.org/uc/item/2p43j2cw
Zhu, J.-Q., León-Villagrá, P., Chater, N., & Sanborn, A. N. (2022). Understanding the structure of cognitive noise. PLoS Computational Biology, 18(8), e1010312. https://doi.org/10.1371/journal.pcbi.1010312
Zhu, J.-Q., Sundh, J., Spicer, J., Chater, N., & Sanborn, A. N. (2024). The autocorrelated Bayesian sampler: A rational process for probability judgments, estimates, confidence intervals, choices, confidence judgments, and response times. Psychological Review, 131(2), 456–493. https://doi.org/10.1037/rev0000427