Eawag-SIAM / SimulatedAnnealingABC.jl

Approximate Bayesian Computation algorithm based on simulated annealing
GNU General Public License v3.0
1 stars 0 forks source link
approximate-bayesian-computation approximate-bayesian-inference bayesian-inference mcmc-sampling

SimulatedAnnealingABC

Dev Build Status Coverage

This package provides methods for Approximate Bayesian Computation (ABC) (sometimes also called simulation-based inference or likelihood-free inference). The algorithms are based on simulated annealing.

[!NOTE] Can you evaluate the density of your posterior? Then you should most likely not be using this or any other ABC package! Conventional MCMC algorithm will be much more efficient.

Installation

] add SimulatedAnnealingABC

Note, Julia 1.9 or newer is needed.

Usage

A minimal example how to use this package for approximate Bayesian inference:

# Define a stochastic model.
# Your real model should be so complex, that it would be too
# complicated to compute it's likelihood function.
function my_stochastic_model(θ, n)
    rand(Normal(θ[1], θ[2]), n)
end

# define prior of the parameters
prior = product_distribution([Normal(0,2),   # theta[1]
                              Uniform(0,2)]) # theta[2]

# Simulate some observation data
y_obs = rand(Normal(2, 0.5), 10)

# Define a function that simulate with my_stochastic_model and then
# measure the distances of the simulated and the observed data with
# two summary statistics
function f_dist(θ; y_obs)
    y_sim = my_stochastic_model(θ, length(y_obs))

    (
        abs(mean(y_obs) - mean(y_sim)),
        abs(sum(abs2, y_obs) - sum(abs2, y_sim))
    )
end

## Sample Posterior
res = sabc(f_dist, prior;
           n_particles = 1000, n_simulation = 100_000, y_obs=y_obs)

## Improve the results by running the inference for longer
res2 = update_population!(res, f_dist, prior;
                          n_simulation = 50_000, y_obs=y_obs)

References

Albert, C., Künsch, H. R., & Scheidegger, A. (2015). A simulated annealing approach to approximate Bayes computations. Statistics and Computing, 25(6), 1217–1232.