PyAIF
PyAIF is a Python package for constructing discrete active-inference agents with factorised hidden states. Version 0.1 provides categorical observations, single-step inference, deep temporal inference, policy evaluation, action selection, and categorical parameter learning.
Continuous-observation likelihoods are intentionally reserved for version 0.2.
Research applications and domain-specific likelihood construction belong in
the examples/ directory or in separate repositories.
Installation
PyAIF requires Python 3.9 or newer.
Install the published distribution:
python -m pip install pyaif-toolkit
The distribution is named pyaif-toolkit on PyPI, while the Python import
package remains PyAIF.
To install from a source checkout:
python -m pip install .
For development:
python -m pip install -e ".[dev]"
pytest
Quick start
The public API separates the state-transition model, observation model, and inference algorithm.
import numpy as np
from PyAIF import (
ActiveInfAgent,
CategoricalLikelihood,
GenerativeModel,
ShallowInference,
)
def object_array(*values):
result = np.empty(len(values), dtype=object)
for index, value in enumerate(values):
result[index] = np.asarray(value, dtype=float)
return result
# A[o, s]: categorical observation likelihood.
A = object_array(
np.array([
[0.95, 0.05],
[0.05, 0.95],
])
)
# B[s_next, s_previous, action]: controlled transitions.
B_factor = np.zeros((2, 2, 2))
B_factor[:, :, 0] = np.eye(2)
B_factor[:, :, 1] = np.fliplr(np.eye(2))
B = object_array(B_factor)
# D[s]: initial-state prior. C[o]: outcome preferences.
D = object_array(np.array([0.5, 0.5]))
C = object_array(np.zeros(2))
model = GenerativeModel(
B=B,
D=D,
controls_dim=[2],
controllable_factors=[0],
)
likelihood = CategoricalLikelihood(
A=A,
preferences=C,
modality_dependencies=[[0]],
)
agent = ActiveInfAgent(
model=model,
likelihood=likelihood,
inference=ShallowInference(),
action_selection="deterministic",
)
agent.reset()
agent.observe([0])
agent.infer_states()
expected_free_energy, _ = agent.infer_policies()
action = agent.select_action()
Use DeepTemporalInference(horizon=...) for policy-dependent beliefs over
multiple time steps. Deep preferences have shape
(number_of_outcomes, horizon).
Agent lifecycle
The supported component-based lifecycle is:
agent.reset(trial=trial)
agent.observe(observation, time_step=t)
agent.infer_states()
agent.infer_policies()
action = agent.select_action()
agent.learn() # only when parameter learning is enabled
The older matrix-heavy constructor and methods such as choose_action() and
perform_learning() remain available while examples migrate, but new projects
should use the component constructor and lifecycle above.
Model structure
GenerativeModelowns state transitions (B), initial-state priors (D), control dimensions, controllable factors, and optional policies.CategoricalLikelihoodowns observation likelihoods (A), preferences (C), and modality-to-factor dependencies.ShallowInferenceperforms single-step factorised inference.DeepTemporalInferenceperforms marginal message passing over a horizon.PyAIF.learningcontains reusable categorical updates forA,B,C,D, andE.
See Model shapes and Public API for details.
Examples
examples/quickstart_discrete.py: minimal categorical agent.examples/learning_under_uncertainty/: deep temporal parameter-learning experiments under epistemic and aleatoric uncertainty.
Model-selection and bounded-rationality experiments are maintained separately from the reusable PyAIF package.
The automated regression suite covers the reusable component API and executes one trial of each parameter-learning experiment.
Run the minimal component example with:
python examples/quickstart_discrete.py
Version policy
0.1.x: categorical observations and discrete parameter learning.0.2.x: planned continuous-observation likelihood components.
Behavioral changes are protected with numerical regression tests. Research experiments may evolve independently from the packaged API.
License
PyAIF is distributed under the BSD 3-Clause License. Copyright © 2026 Diluna A. Warnakulasuriya.
This software license does not automatically apply to papers, datasets, figures, trained models, or other research artifacts.
Development
ruff check PyAIF tests
ruff format --check PyAIF tests
pytest
python -m build
python -m twine check dist/*
See CONTRIBUTING.md for the pull-request and release process.
Metadata
Release files for pyaif-toolkit 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyaif_toolkit-0.1.0.tar.gz | 1.1 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyaif_toolkit-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 1.2 MB
Release files / pyaif_toolkit-0.1.0.tar.gz
| Download URL | pyaif_toolkit-0.1.0.tar.gz |
|---|---|
| Size | 1.1 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
201b74d718cd243e985ff1cf5777c297960a508a9a697517134a52fb84e66393
|
|
BLAKE2b-256 checksum How to use checksums |
594227031770ced68ac33c4b92fc3d8e7af80432f04c846678790006e1743702
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 24, 2026.
Transparency logRelease files / pyaif_toolkit-0.1.0-py3-none-any.whl
| Download URL | pyaif_toolkit-0.1.0-py3-none-any.whl |
|---|---|
| Size | 45.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
63a53fe63c31e78d182e74672df75fa75a2ef7dc333c2c9420bde1956e23dd44
|
|
BLAKE2b-256 checksum How to use checksums |
28ec7d6c01406dc4639f13a8865bf56341f526e35f53fd8980a693954e564a39
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 24, 2026.
Transparency log