Entropy 2025, 27, 62 14 of 33
init_aif function, including the policy length and which parts of the information gain to
use, as well as the policy precision γ.
 
# Infer policies
infer_policies !( aif )
 
Finally, sample_action! then samples the next action from the agent. This is performed by
marginalising the policy probabilities to obtain the probabilities for the action on the next
time step, and then softmax transforming it with the α action precision parameter.
 
# Sample Action
sample_action !( aif )
 
These functions can be combined by users in various ways, depending on their purpose.
Often, however, users will want to combine them in a single function that implements
the full action–perception loop that receives an observation and returns an action. This is
implemented with the ActionModels sister package for behavioural modelling.
3.2. Simulation withActionModels
ActionModels is a library for implementing, simulating and fitting various behavioural
models to data. Here, we show how to use it in conjunction with ActiveInference to
make the simulation of AIF models easy and in a fully generalised framework that is
compatible with other types of cognitive and behavioural models as well. ActiveInference
provides a full "action model"—a full model of the action-generating process in an agent—
for using AIF called action_pomdp!. In this case, all this information is contained in the
AIF object. action_pomdp! then takes the AIF object and a single-time-step observation
as arguments, and then runs state inference, parameter learning and policy inference,
and returns probability distributions over the possible actions of the agent.
 
observation = [1] # observation with one modality
# Run the action model for a single observation
action_distributions = action_pomdp !( aif :: AIF , observation )
 
This can conveniently be used in conjunction with an ActionModels agent, a more abstract
structure that is used for running behavioural models in general, and which is used when
fitting models to data. We therefore begin with initialising an agent that contains the
AIF object:
 
# Initialize ActionModels Agent with active inference agent as a substruct .
agent = init_agent (
action_model = action_pomdp !, # The active inference action model
substruct = aif , # The AIF object
)
 
The agent object can be used with a set of standard functions. single_input! provides
the agent with an observation, updates it is beliefs and returns a sampled action; for non-
action-dependent observations, give_inputs! provides a series of observations across time
steps and returns actions for each. These can be easily used in an agent-based simulation to
have AIF agents evolve and act over time.