npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

active-inference

v0.2.1

Published

Active Inference Framework implementation for JavaScript/TypeScript

Downloads

17

Readme

Active Inference

TypeScript implementation of the Active Inference framework based on Karl Friston's Free Energy Principle.

What is Active Inference?

Active Inference is a theory of how biological agents perceive and act in the world. Agents maintain beliefs about hidden states and select actions to minimize Expected Free Energy — a quantity that balances:

  • Risk: avoiding unpreferred outcomes
  • Ambiguity: seeking informative observations

This library provides building blocks for creating agents that perceive, learn, plan, and act using these principles.

Installation

npm install active-inference

Quick Start

import {
    createAgent,
    DiscreteBelief,
    DiscreteTransition,
    DiscreteObservation,
} from 'active-inference';

const agent = createAgent({
    belief: new DiscreteBelief({ left: 0.5, right: 0.5 }),
    transitionModel: new DiscreteTransition({
        go_left: {
            left: { left: 1.0, right: 0.0 },
            right: { left: 0.8, right: 0.2 },
        },
        go_right: {
            left: { left: 0.2, right: 0.8 },
            right: { left: 0.0, right: 1.0 },
        },
    }),
    observationModel: new DiscreteObservation({
        see_reward: { left: 0.9, right: 0.1 },
        see_nothing: { left: 0.1, right: 0.9 },
    }),
    preferences: { see_reward: 0, see_nothing: -5 },
});

const action = agent.step('see_reward');

API

createAgent(config)

| Parameter | Description | |-----------|-------------| | belief | Initial belief over hidden states | | transitionModel | P(s'|s, a) — how actions change states | | observationModel | P(o|s) — how states generate observations | | preferences | Log probabilities of preferred observations | | planningHorizon | Steps to look ahead (default: 1) | | precision | Action selection temperature (default: 1) | | habits | Prior over actions / E matrix (default: uniform) | | seed | Random seed for reproducibility |

Agent

| Method | Description | |--------|-------------| | step(obs) | Observe and act | | observe(obs) | Update beliefs from observation | | act() | Select action minimizing EFE | | state | Most likely hidden state | | uncertainty | Belief entropy (confidence) | | freeEnergy | Variational Free Energy | | exportBelief() | Get full belief distribution |

Learning

The library supports Dirichlet-categorical learning — agents that update their generative models from experience. Instead of fixed probability matrices, learnable models maintain pseudo-count concentrations that are refined over time.

  • DirichletObservation and DirichletTransition are drop-in replacements for their Discrete counterparts. Learning happens automatically on every step() call.
  • DirichletPreferences provides learnable preferred observations — call .learn() manually and pass .preferences to the agent config.

Low concentrations encode weak priors (learns fast). High concentrations encode strong priors (resists change).

import {
    createAgent,
    DiscreteBelief,
    DirichletTransition,
    DirichletObservation,
} from 'active-inference';

const agent = createAgent({
    belief: new DiscreteBelief({ safe: 0.5, danger: 0.5 }),
    transitionModel: new DirichletTransition({
        flee: {
            safe:   { safe: 1, danger: 1 },
            danger: { safe: 1, danger: 1 },
        },
        stay: {
            safe:   { safe: 1, danger: 1 },
            danger: { safe: 1, danger: 1 },
        },
    }),
    observationModel: new DirichletObservation({
        see_safe:   { safe: 1, danger: 1 },
        see_danger: { safe: 1, danger: 1 },
    }),
    preferences: { see_safe: 0, see_danger: -5 },
    seed: 42,
});

// Models update automatically on each step
const action = agent.step('see_safe');

Examples

Cart-Pole Balancing

Interactive browser demo — an Active Inference agent balances an inverted pendulum using a 49-state generative model with 3-step planning horizon.

npm run build:examples
open examples/cart-pole/index.html

Contributing

git clone https://github.com/codevanger/active-inference
cd active-inference
npm install
npm test

PRs welcome

References

License

MIT