Minimal implementations of Active Inference demonstrating how an agent achieves goal-directed behavior by minimizing Variational Free Energy (VFE).
Active Inference is a theoretical framework from neuroscience that unifies perception and action under a single principle: minimizing surprise (or free energy). An agent maintains beliefs about the world and acts to make its observations match its predictions.
| File | Description | Target |
|---|---|---|
sim_1d.py |
1D point mass simulation | x = 10 |
sim_2d.py |
2D point mass with animation | (x, y) = (10, 7) |
| File | Description | Target |
|---|---|---|
sim_1d_prob.py |
1D with belief uncertainty (σ) | x = 10 |
sim_2d_prob.py |
2D with uncertainty ellipses + animation | (x, y) = (10, 7) |
| File | Description | Target |
|---|---|---|
sim_1d_efe.py |
1D with VFE for perception + EFE for action | x = 10 |
In the other simulations, VFE is used for both perception and action. The EFE simulation uses two different objectives: VFE updates beliefs to match observations, while EFE selects actions by predicting future outcomes. The agent predicts outcomes using an internal physics model with assumed friction b_model=0.5, which becomes incorrect when true friction changes to b_true=5.0 at step 500.
All simulations model a point mass that must reach a target position while adapting to environmental changes:
- Agent: Maintains beliefs about position and generates control actions
- Environment: Physics world with mass, friction, and stiffness
- Challenge: At step 500, friction increases 10x to test robustness
Requires Python 3.12+ and uv.
uv sync# Deterministic simulations
uv run python sim_1d.py
uv run python sim_2d.py
# Probabilistic simulations (with uncertainty tracking)
uv run python sim_1d_prob.py
uv run python sim_2d_prob.py
# Expected Free Energy simulation
uv run python sim_1d_efe.pyBelief is a point estimate
Belief is a Gaussian distribution
The agent updates both belief mean
In variational inference,
| Symbol | Name | Description | In code |
|---|---|---|---|
| Recognition model | Agent's belief about state | ||
| Prior | Where the agent expects to be | ||
| Likelihood | Observation model |
The letter
The accuracy term is derived from the negative log-likelihood of a Gaussian distribution.
Step 1: Gaussian PDF
The probability density of observing
Step 2: Take negative log
Step 3: Drop constants
Since
Why
| Symbol | Name | Type | Meaning |
|---|---|---|---|
| Belief uncertainty | Dynamic (updated) | Agent's confidence about its position estimate | |
| Observation noise | Fixed (parameter) | Agent's model of sensor noise |
The agent's belief is
The combined variance arises from integrating out the uncertain position.
Precision
Precision is the inverse of variance:
The effective precision combines both sources of uncertainty:
In Active Inference, prediction errors are precision-weighted — errors matter more when the agent is confident (high precision), and less when uncertain (low precision).
Two competing terms:
| Term | Effect on |
|---|---|
| Large error + small |
|
| Small |
This creates adaptive uncertainty:
The complexity term is the KL divergence between the agent's belief and the prior (goal state).
Derivation of KL divergence for Gaussians
For
Step 1: Definition of KL divergence
Step 2: Compute
(since
Step 3: Compute
Step 4: Subtract
Three terms:
| Term | Meaning |
|---|---|
| Ratio of uncertainties — penalizes if belief is more uncertain than prior | |
| Distance from goal — penalizes beliefs far from target | |
| Uncertainty cost — penalizes high belief uncertainty |
Intuition
The complexity term pulls the agent's belief toward the goal state:
-
$\mu \to \mu_{target}$ — "I should be at the target" -
$\sigma \to \sigma_{prior}$ — "My uncertainty should match my prior expectation"
Combined with the accuracy term, the agent balances:
- Accuracy: Match observations (sensory evidence)
- Complexity: Stay close to goals (prior preferences)
While VFE is used for perception (state estimation), Expected Free Energy (EFE) is used for action selection (planning). EFE evaluates future actions by predicting their consequences.
Two components:
| Term | Meaning |
|---|---|
| Pragmatic value | Expected distance from preferred outcomes (goal-seeking) |
| Epistemic value | Expected uncertainty reduction (information-seeking) |
Implementation in sim_1d_efe.py:
The agent maintains:
-
Beliefs: Position (
$\mu$ ) and velocity ($\mu_v$ ) estimates -
Internal model: Predicts next state given action (with assumed friction
$b_{model}$ ) - VFE: Updates beliefs to match observations
- EFE: Selects actions that minimize expected distance from target
Deriving the implementation from the general definition:
The implementation is a simplification of the general EFE formula:
Pragmatic value:
| General | Implementation |
|---|---|
-
$o_{preferred} = x_{target}$ (goal position) -
$o \approx \mu_{next}$ (predicted observation = predicted position) - Point estimate replaces expectation:
$\mathbb{E}_{q}[\cdot] \to$ evaluate at$\mu$ - Add precision weight
$\pi_{target}$
Epistemic value:
| General | Implementation |
|---|---|
True epistemic value measures expected information gain (how much will I learn?). The implementation replaces this with a velocity settling term — a prior preference that velocity should be zero at the goal.
Simplifications:
- Point estimates: No expectation over distributions, just evaluate at belief mean
- No information gain: Epistemic term replaced by velocity regularization
- Deterministic predictions:
predict_next_statereturns a single state, not a distribution
The implementation captures the core idea (select actions leading to preferred outcomes) but omits the full probabilistic treatment.
Model mismatch demonstration:
The simulation demonstrates what happens when the agent's internal model differs from reality:
- Agent assumes friction
$b_{model} = 0.5$ - After step 500, true friction increases to
$b_{true} = 5.0$ - The agent cannot fully compensate because it underestimates the required force
This illustrates a key aspect of Active Inference: agents act based on their generative model of the world, and model errors lead to suboptimal behavior.
- Friston, K. (2010). The free-energy principle: a unified brain theory? Nature Reviews Neuroscience, 11(2), 127-138. [Nature] [PubMed]
- Friston, K., Rigoli, F., Ognibene, D., Mathys, C., Fitzgerald, T., & Pezzulo, G. (2015). Active inference and epistemic value. Cognitive Neuroscience, 6(4), 187-214. [Taylor & Francis] [PubMed]
- Friston, K., FitzGerald, T., Rigoli, F., Schwartenbeck, P., & Pezzulo, G. (2017). Active Inference: A Process Theory. Neural Computation, 29(1), 1-49. [MIT Press] [PubMed]