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:
- Logistic regression: define a posterior, adapt a proposal, and predict a probability.
- Numerical integration: estimate ordinary integrals, including signed integrands.
Every tutorial defines its data and runs from top to bottom.
User guide
| Task | Page |
|---|---|
| Pass data or use an existing density | Targets and data |
| Choose a proposal family and scale | Proposals |
| Use positive, bounded, or simplex parameters | Constraints and named parameters |
| Compute expectations, intervals, or resampled draws | Weighted samples |
| Reuse a fitted proposal | Adaptation and reuse |
| Choose an importance-sampling method | Method comparison |
| Evaluate many samples with matrix operations | Batch targets |
| Supply gradients or use automatic differentiation | Gradients |
| Execute on CUDA, Metal, or Reactant | Devices |
| Look up a signature | API 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.