It is possible to use Turing.jl to perform Bayesian parameter estimation on models defined in SequentialSamplingModels.jl. Below, we show you how to estimate the parameters for the Linear Ballistic Accumulator (LBA) and to use it to estimate effects.
Note that you can easily swap the LBA model from this example for other SSM models simply by changing the names of the parameters.
The first step is to load the required packages. You will need to install each package in your local environment in order to run the code locally. We will also set a random number generator so that the results are reproducible.
using Turing
using SequentialSamplingModels
using Random
using LinearAlgebra
using StatsPlots
using Random
Random.seed!(45461)
We will use the LBA distribution to simulate data (100 trials) with fixed parameters (those we want to recover only from the data using Bayesian modeling).
# Generate some data with known parameters
dist = LBA(ν=[3.0, 2.0], A = .8, k = .2, τ = .3)
data = rand(dist, 100)
The rand()
function will sample random draws from the distribution, and store that into a named tuple of 2 vectors (one for choice
and one for rt
). The individual vectors can be accessed by their names using data.choice
and data.rt
.
The code snippet below defines a model in Turing. The model function accepts a tuple containing
a vector of choices and a vector of reaction times. The sampling statements define the prior distributions for each parameter. The non-decision time parameter min_rt
. The last sampling statement defines the likelihood of the data given the sampled parameter values.
# Specify LBA model
@model function model_lba(data; min_rt = minimum(data.rt))
# Priors
ν ~ MvNormal(zeros(2), I * 2)
A ~ truncated(Normal(.8, .4), 0.0, Inf)
k ~ truncated(Normal(.2, .2), 0.0, Inf)
τ ~ Uniform(0.0, min_rt)
# Likelihood
data ~ LBA(;ν, A, k, τ )
end
Finally, we perform parameter estimation with sample()
, which takes the model, and details about the sampling algorithm:
model(data)
: the Turing model with data passedNUTS(1000, .65)
: a sampler object for the No U-Turn Sampler for 1000 warmup samples.MCMCThreads()
: instructs Turing to run each chain on a separate threadn_iterations
: the number of iterations performed after warmupn_chains
: the number of chains
# Estimate parameters
chain = sample(model_lba(data), NUTS(1000, .85), MCMCThreads(), 1000, 4)
We can compute a description of the posterior distributions.
# Summarize posteriors
summarystats(chain)
As you can see, based on the mean values of the posterior distributions, the original parameters (ν=[3.0, 2.0], A = .8, k = .2, τ = .3
) are successfully recovered from the data (the accuracy would increase with more data).
It is important to verify that the chains converged. We see that the chains converged according to
plot(chain)