Entropy 2025, 27, 62 26 of 33
 
using Distributions
# Set priors
parameter_priors = Dict (
" alpha " => Truncated ( Normal (5,5), 0, 20)
)
 
Next, we instantiated the probabilistic model with data and parameter priors:
 
model = create_model (
agent , # ActionModels Agent containing the model
parameter_priors , # Dictionary with parameter priors
data ; # Dataframe containing data from all subjects
grouping_cols = [: SubjectID ], # Identifier column
input_cols = [" Location ", " Reward ", " Cue "], # Observation columns
action_cols = [" Action_Location ", " Action_Reward "] # Columns containing actions
)
 
Finally, we used the fit_model function to perform (parallelised) parameter estimation for
each of the agents:
 
results = fit_model (
model ; # Model object
parallelization = MCMCDistributed () # Parallelize over chains
n_iterations = 1000 , # Number of iterations
n_chains = 4 # Number of chains
)
 
The output contained the chains object with the resulting posterior samples:
 
# Take out chains from the results object and rename parameters
renamed_posterior_chains = rename_chains ( results . chains , model )
 
Summary Statistics
parameters mean std mcse ess_bulk ess_tail rhat
SubjectID:1.alpha 3.8785 0.2350 0.0034 4826.0114 2512.8061 1.0016
SubjectID:2.alpha 2.9718 0.1945 0.0029 4523.9650 2781.4532 1.0033
SubjectID:3.alpha 3.3598 0.2147 0.0031 4816.1661 3054.7500 1.0016
. . . . . . .
. . . . . . .
SubjectID:10.alpha 8.9233 0.9660 0.0170 3551.1005 2126.9604 1.0013
We could plot the posteriors and chains, which is often performed to diagnose whether the
sampling was successful (Figure 6):
 
using StatsPlots
# Plot chain traces for the first and the last subject
plot ( renamed_posterior_chains [:,1:1,:])
plot ( renamed_posterior_chains [:,10:10,:])
 
Further, we used plot_parameters from ActionModels to plot the posterior estimates
against their priors. Here, we performed this for one agent from each group, also high-
lighting the parameter value used to generate the behaviour (Figure 7). We saw that the
posterior was correctly centred around the generative value.