fundamentals

Learning and inference

When the parameters that govern the likelihood are not known in advance, the agent has to learn them from data — usually before, sometimes jointly with, hidden-state inference. The codebase ships four learning paths, each picked according to whether the parameters are treated as deterministic (MLE / OLS / GD) or as random variables (Bayesian linear regression, EM).

In this codebase

End-to-end snippet

import numpy as np
from active_inference import (
    BayesianLinearRegression, fit_factor_analysis, mle_linear_regression,
)

rng = np.random.default_rng(0)
N, C = 200, 3

# Linear regression — closed form.
X = rng.normal(size=(N, C))
y = X @ np.array([1.0, -0.5, 0.7]) + 2.0 + rng.normal(scale=0.1, size=N)
theta = mle_linear_regression(X, y)

# Bayesian linear regression — full posterior over θ.
blr = BayesianLinearRegression(
    prior_mean=np.zeros(C + 1), prior_cov=np.eye(C + 1) * 4.0,
    sigma2_y=0.01,
)
posterior = blr.fit(X, y)
print(posterior.summary())

# Factor analysis — joint learning of loadings and noise via EM.
Y = rng.normal(size=(400, 5))
result = fit_factor_analysis(Y, n_factors=2, rng=rng)
print(result.summary())

Pitfalls

See also