Skip to content


Repository files navigation

A tutorial on Bayesian models of perception

This repository contains Matlab code associated with the paper:

Vincent (2015) A tutorial on Bayesian models of Perception, Journal of Mathematical Psychology. do:10.1016/

If you find the paper and/or code useful, the best way to thank me is by citing me :)

This code is published under the MIT licence, see LICENSE.txt.

If... then...

  • You can't get the code to work, then feel free to email and I'll try my best to respond to clarify things.

  • You want to use the code for learning alongside the paper, then feel free to clone or fork this repository, or download a .zip of the code.

  • If you find a bug, then:

    • create an Issue, or...
    • create a pull request through GitHub with fixed code :)
  • If you find the paper useful in your academic work, or adapt any of the code for your own work, please to share the love and cite the paper.


  • ~ the root directory contains the main Matlab scripts and functions for producing the figures, see the section below.
  • figs/ output of the code saved in .fig and .pdf format.
  • funcs/ contains the majority of the Matlab functions and JAGS model files. You don't need to run any of these files as they are called by the main scripts in the root directory.
  • output/ data generated by the code is saved here.

Reproducing the figures

  • generate_common_data.m generates and saves a set of common data which can be used with all of the models. There is no need to run this as example data is already included in the files commondata_model*.mat.
  • model1runme.m By changing the input variable {'gridApprox', 'mcmcJAGS', 'mcmcCustom'} this file will run the 3 inference steps using the different respective methods:
    • gridApprox will conduct parameter estimation via grid approximation.
    • mcmcCustom will conduct parameter estimation with an implementation of the Metropolis-Hastings sampling algorithm (see mhAlgorithm.m).
    • mcmcJAGS will use the JAGS inference software to conduct parameter estimation using MCMC methods.
  • model1MCMCse.m runs a large simulation to study the variability in MCMC-derived estimates of an observers internal variance. Warning: this function can take days to run even on a fast multi-core machine with lots of RAM.
  • model1MCMCconvergence
  • model2runme.m
  • model2psychometric
  • model3runme.m

Options when running the code

  • If you have a computer with multiple CPU cores, then you can run the MCMC chains in parallel by setting mcmcparams.doparallel = 1 in the file /funcs/define_mcmcparams.m.
  • You can elect to make inferences based upon the common dataset generated by the file /generate_common_data.m by setting DATAMODE='load'. Otherwise, new simulated observer behaviour can be generated by DATAMODE='generate'.


  • This code was generated and tested with Matlab 2014a.
  • The Matlab Statistics Toolbox is required for Stage 3 of Model 1, when using grid approximation.
  • Models using JAGS to make inferences require:


Vincent, B. T. (2015) A tutorial on Bayesian models of Perception, Journal of Mathematical Psychology.







No packages published