• Stars
    star
    179
  • Rank 214,039 (Top 5 %)
  • Language
    Jupyter Notebook
  • License
    MIT License
  • Created over 2 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Julia package for automated Bayesian inference on a factor graph with reactive message passing

Official page Stable Dev Examples Build Status Coverage DOI Zenodo

Overview

RxInfer.jl is a Julia package for automatic Bayesian inference on a factor graph with reactive message passing.

Given a probabilistic model, RxInfer allows for an efficient message-passing based Bayesian inference. It uses the model structure to generate an algorithm that consists of a sequence of local computations on a Forney-style factor graph (FFG) representation of the model.

Performance and scalability

RxInfer.jl has been designed with a focus on efficiency, scalability and maximum performance for running Bayesian inference with message passing. Below is a comparison between RxInfer.jl and Turing.jl on latent state estimation in a linear multi-variate Gaussian state-space model. Turing.jl is a state-of-the-art Julia-based general-purpose probabilistic programming package and is capable of running inference in a broader class of models. Still, RxInfer.jl executes the inference task in various models faster and more accurately. RxInfer.jl accomplishes this by taking advantage of any conjugate likelihood-prior pairings in the model, which have analytical posteriors that are known by RxInfer.jl. As a result, in models with conjugate pairings, RxInfer.jl often beats general-purpose probabilistic programming packages in terms of computational load, speed, memory and accuracy. Note, however, that RxInfer.jl also supports non-conjugate inference and is continually improving in order to support a larger class of models.

Turing comparison Scalability performance

Faster inference with better results

RxInfer.jl not only beats generic-purpose Bayesian inference methods in conjugate models, executes faster, and scales better, but also provides more accurate results. Check out the documentation for more examples!

Inference with RxInfer Inference with HMC

The benchmark and accuracy experiment, which generated these plots, is available in the benchmarks/ folder. Note, that the execution speed and accuracy of the HMC estimator heavily depends on the choice of hyper-parameters. In this example, RxInfer executes exact inference consistently and does not depend on any hyper-parameters.

References

Installation

Install RxInfer through the Julia package manager:

] add RxInfer

Optionally, use ] test RxInfer to validate the installation by running the test suite.

Getting Started

There are examples available to get you started in the examples/ folder. Alternatively, preview the same examples in the documentation.

Coin flip simulation

Here we show a simple example of how to use RxInfer.jl for Bayesian inference problems. In this example we want to estimate a bias of a coin in a form of a probability distribution in a coin flip simulation.

Let's start by creating some dataset. For simplicity in this example we will use static pre-generated dataset. Each sample can be thought of as the outcome of single flip which is either heads or tails (1 or 0). We will assume that our virtual coin is biased, and lands heads up on 75% of the trials (on average).

First let's setup our environment by importing all needed packages:

using RxInfer, Random

Next, let's define our dataset:

n = 500  # Number of coin flips
p = 0.75 # Bias of a coin

distribution = Bernoulli(p) 
dataset      = float.(rand(Bernoulli(p), n))

Model specification

In a Bayesian setting, the next step is to specify our probabilistic model. This amounts to specifying the joint probability of the random variables of the system.

Likelihood

We will assume that the outcome of each coin flip is governed by the Bernoulli distribution, i.e.

$$y_i \sim \mathrm{Bernoulli}(\theta)$$

where $y_i = 1$ represents "heads", $y_i = 0$ represents "tails". The underlying probability of the coin landing heads up for a single coin flip is $\theta \in [0,1]$.

Prior

We will choose the conjugate prior of the Bernoulli likelihood function defined above, namely the beta distribution, i.e.

$$\theta \sim Beta(a, b)$$

where $a$ and $b$ are the hyperparameters that encode our prior beliefs about the possible values of $\theta$. We will assign values to the hyperparameters in a later step.

Joint probability

The joint probability is given by the multiplication of the likelihood and the prior, i.e.

$$P(y_{1:N}, \theta) = P(\theta) \prod_{i=1}^N P(y_i | \theta).$$

Now let's see how to specify this model using GraphPPL's package syntax.

# GraphPPL.jl export `@model` macro for model specification
# It accepts a regular Julia function and builds an FFG under the hood
@model function coin_model(n)

    # `datavar` creates data 'inputs' in our model
    # We will pass data later on to these inputs
    # In this example we create a sequence of inputs that accepts Float64
    y = datavar(Float64, n)
    
    # We endow θ parameter of our model with some prior
    θ ~ Beta(2.0, 7.0)
    
    # We assume that outcome of each coin flip 
    # is governed by the Bernoulli distribution
    for i in 1:n
        y[i] ~ Bernoulli(θ)
    end
    
end

As you can see, RxInfer offers a model specification syntax that resembles closely to the mathematical equations defined above. We use datavar function to create "clamped" variables that take specific values at a later date. $\theta \sim \mathrm{Beta}(2.0, 7.0)$ expression creates random variable $θ$ and assigns it as an output of $\mathrm{Beta}$ node in the corresponding FFG.

Inference specification

Once we have defined our model, the next step is to use RxInfer API to infer quantities of interests. To do this we can use a generic inference function from RxInfer.jl that supports static datasets.

result = inference(
    model = coin_model(length(dataset)),
    data  = (y = dataset, )
)

Coin Flip

Where to go next?

There are a set of examples available in RxInfer repository that demonstrate the more advanced features of the package. Alternatively, you can head to the documentation that provides more detailed information of how to use RxInfer to specify more complex probabilistic models.

Additionally, checkout our video from JuliaCon 2023 for a high-level overview of the package

Ecosystem

The RxInfer framework consists of three core packages developed by BIASlab:

  • ReactiveMP.jl - the underlying message passing-based inference engine
  • GraphPPL.jl - model and constraints specification package
  • Rocket.jl - reactive extensions package for Julia

License

MIT License Copyright (c) 2021-2023 BIASlab

More Repositories

1

Rocket.jl

Functional reactive programming extensions library for Julia
Julia
161
star
2

ForneyLab.jl

Julia package for automatically generating Bayesian inference algorithms through message passing on Forney-style factor graphs.
Julia
150
star
3

ReactiveMP.jl

High-performance reactive message-passing based Bayesian inference engine
Julia
79
star
4

hugo-academic-group

An academic group website theme for Hugo.
HTML
54
star
5

GraphPPL.jl

DSL for probabilistic models specification and probabilistic programming.
Julia
18
star
6

RxEnvironments.jl

Reactive environments for self-learning agents
Julia
15
star
7

biaslab-hugo

Repository with content of BIASlab website for Hugo static site generator.
HTML
12
star
8

LAIF

Experiments with deriving epistemics aware message passing algos
Julia
6
star
9

MultiAgentTrajectoryPlanning

Experiments of the "Multi-Agent Trajectory Planning with NUV Priors" paper
Julia
6
star
10

ai_workshop_2020

Code to run potential based agents and generate figures for the submission to the 2020 Workshop on Active Inference
Julia
6
star
11

ExponentialFamily.jl

Julia
5
star
12

AIDA

Julia
5
star
13

RxAgent-Zoo

Archive of active inference agents based on reactive message passing.
Jupyter Notebook
5
star
14

PrincipledPruningBNN

Jupyter Notebook
5
star
15

ReactiveMP_JuliaCon2021

ReactiveMP JuliaCon2021 presentation notebooks
Julia
3
star
16

LCSS2024-NARXEFE

Code, figures, animations for a NARX-EFE based agent.
Jupyter Notebook
3
star
17

nsi-silverbox

Online system identification in Silverbox by minimising free energy
Jupyter Notebook
2
star
18

SituatedSoundscaping

Derivations and experiments of the "A Bayesian Modeling Approach to Situated Design of Personalized Soundscaping Algorithms" paper, published in the MDPI journal of Applied Sciences in the special issue on AI, Machine Learning and Deep Learning in Signal Processing, 2021.
Julia
2
star
19

IWAI2020-onlinesysid

Code and experiments for IWAI 2020 submission on online system identification
HTML
1
star
20

rxinfer-website

Repository with content of RxInfer website for Hugo static site generator.
JavaScript
1
star
21

CDC-2022

Experiments and derivations for CDC2022 paper on message passing-based inference for NARMAX system identification.
Jupyter Notebook
1
star
22

ExtendedVMP

Experiments for numerical approximations to VMP
Jupyter Notebook
1
star
23

TVAR_FFG

Time-varying autoregressive models in Forney-Style Factor Graphs
Jupyter Notebook
1
star
24

NARX

ForneyLab.jl factor node for a nonlinear autoregressive model with exogenous input.
Julia
1
star
25

IWAI2024-ambiguity

Expected free energy minimization with approximations to nonlinear observation functions
Jupyter Notebook
1
star
26

ReactiveInferencePendulum

A small demonstration of reactive Bayesian inference for pendulum dynamics
Jupyter Notebook
1
star
27

ACC2022-vmpNARMAX

Experiments and derivations for ACC2022 paper on variational message passing for online NARMAX identification.
Jupyter Notebook
1
star
28

SiPS2022-EfficientModelEvidenceComputation

Experiments and derivations for SiPS2022 paper "Efficient model evidence computation in tree-structured factor graph"
Jupyter Notebook
1
star
29

LAR

AR extension of ForneyLab.jl
Jupyter Notebook
1
star
30

PGM2022-SourceSeparationNAR

Experiments and code of the paper "Online Single-Microphone Source Separation using Non-Linear Autoregressive Models"
Jupyter Notebook
1
star
31

CVMP

Implementation of https://github.com/Nimrais/CCMPPaper
Julia
1
star
32

ThesisParallelMP

Master thesis project of Mattia Vicari about parallel computing in message passing-based Bayesian inference programs
Jupyter Notebook
1
star
33

CCTA2024-BIDconvection

Experiments for CCTA 2024 submission on fast Bayesian gray-box identification of convection in heat transfer dynamics.
Jupyter Notebook
1
star
34

Chengfeng_intent_inference

Jupyter Notebook
1
star
35

preds-not-commands

Experiment with sending predictions, not commands, to an actuator that reacts to minimize the prediction error.
Jupyter Notebook
1
star
36

OnlineMessagePassingDirichletProcess

Julia
1
star