ImportanceSamplers.jl

Importance sampling estimates integrals and expectations using weighted draws. ImportanceSamplers provides fixed and adaptive proposals, multiple-proposal methods, and explicit CPU or accelerator execution.

Inputs are a log target, a normalized proposal, and a random-number generator. The result contains samples and raw log weights.

Installation

Requires Julia 1.13 or later. Install the package with Pkg:

using PkgPkg.add("ImportanceSamplers")

Accelerator and automatic-differentiation packages are optional.

Weighted sampling

Estimate the mean of a three-dimensional standard Gaussian. Its log density is -sum(abs2, x) / 2, up to an additive constant. Each x is a vector of the three parameters being sampled.

using ImportanceSamplers, Random, Statisticslogtarget(x) = -sum(abs2, x) / 2proposal = SphericalGaussian(zeros(3), 1.5)algorithm = ImportanceSampling(proposal; nsamples=10_000)samples = importance_sample(Xoshiro(42), logtarget, algorithm)(    mean=round.(mean(samples); digits=3),    variance=round.(var(samples); digits=3),    shape=size(samples.samples),)
(mean = [-0.007, -0.007, -0.01], variance = [1.0, 0.994, 0.998], shape = (3, 10000))

The proposal is a Gaussian with mean zeros(3) and standard deviation 1.5 in each coordinate. The result stores one draw per column, with a matching raw log weight in samples.logweights.

The reference mean is [0, 0, 0] and the marginal variances are [1, 1, 1]. These estimates use the importance weights. mean(samples.samples; dims=2) ignores them and describes the proposal instead.

For plain IS, each log weight is logtarget(x) - logdensityof(proposal, x). The weights correct for drawing from the proposal rather than the target. The proposal must cover every region that contributes to the expectation. Weight ESS measures concentration, not whether a run found every mode or tail.

Tutorials

Weighted estimation samples a three-dimensional target and computes its mean, covariance, and normalizing constant.

The application tutorials cover:

Every tutorial defines its data and runs from top to bottom.

User guide

TaskPage
Pass data or use an existing densityTargets and data
Choose a proposal family and scaleProposals
Use positive, bounded, or simplex parametersConstraints and named parameters
Compute expectations, intervals, or resampled drawsWeighted samples
Reuse a fitted proposalAdaptation and reuse
Choose an importance-sampling methodMethod comparison
Evaluate many samples with matrix operationsBatch targets
Supply gradients or use automatic differentiationGradients
Execute on CUDA, Metal, or ReactantDevices
Look up a signatureAPI reference

Documentation versions

dev documents the current dev branch. Tagged releases retain their own documentation. stable points to the latest release. The site's version selector provides documentation for each release.