BayesDensity.jl

BayesDensity.jl is a Julia package for univariate nonparametric Bayesian density estimation. The package provides access to several density estimators from the Bayesian nonparametrics literature. For most of the implemented methods, posterior inference is possible through both Markov chain Monte Carlo (MCMC) methods and variational inference (VI).

Installation

Each of the models implemented in BayesDensity can be installed independently from all the others by downloading the corresponding module. For instance, if we would like to use the HistSmoother model we need to install the BayesDensityHistSmoother package:

using Pkg
Pkg.add("BayesDensityHistSmoother")

We can now use the model by importing the downloaded package:

using BayesDensityHistSmoother

Alternatively, if one wants to have access to more models, one can install the BayesDensity package instead:

using Pkg
Pkg.add("BayesDensity")

We can now import all the models implemented in this package by running the following code snippet:

using BayesDensity

Quick start

To illustrate the basic use of the package, we show how one can fit a histogram smoother to a simulated dataset.

using BayesDensityHistSmoother, Distributions, Random
rng = Random.Xoshiro(1) # for reproducibility

# Simulate some data:
d_true = MixtureModel([Normal(-0.2, 0.25), Normal(0.5, 0.15)], [0.4, 0.6])
x = rand(rng, d_true, 1000)

# Create a HistSmoother model object:
smoother = HistSmoother(x)

Having specified a model for the data, we can perform posterior inference through Markov chain Monte Carlo methods or variational inference:

mcmc_fit = sample(rng, smoother, 2100; n_burnin=100) # MCMC
vi_fit, info = varinf(smoother)                      # VI

The resulting fits can easily be plotted using the Plots.jl and Makie.jl package extensions. For example, the posterior mean and $95 \%$ pointwise credible bands can be plotted via Makie as follows:

using CairoMakie
plot(mcmc_fit) # Based on MCMC
plot(vi_fit)   # Based on VI

Examples

Although nonparametric density estimation is often used for simple univariate data visualizations, it is routinely used for more complex modeling tasks. We encourage you to also have a look at the Examples section of the documentation, which showcases the application of BayesDensity.jl to problems beyond mere data visualization.