Weighted estimation
This tutorial estimates the moments of a three-dimensional Gaussian. The preparation and estimation interface is shared by all samplers.
Log target
using ImportanceSamplers, PureRNGs, Statisticslogtarget(x) = -sum(abs2, x) / 2Here x is the vector being sampled. The function returns $\log\pi(x)=-\lVert x\rVert^2/2$, not $\pi(x)$. The corresponding normalized target is a standard Gaussian in three dimensions.
The omitted normalizing constant does not affect weighted means or variances.
Proposal and sampling
proposal = SphericalGaussian(zeros(3), 1.5)algorithm = ImportanceSampling(proposal; nsamples=20_000)samples = importance_sample(Threefry4x32(42), logtarget, algorithm)(size(samples.samples), length(samples.logweights))((3, 20000), 20000)The proposal determines the sample dimension. Its second argument is a standard deviation, not a variance. This proposal is wider than the target.
Threefry4x32(42) fixes the random stream for this execution environment. nsamples belongs to the algorithm and specifies the exact output count.
The result stores one sample per column: samples.samples[:, i] is the three-dimensional draw at index i.
Weighted moments
( mean=round.(mean(samples); digits=3), variance=round.(var(samples); digits=3), covariance=round.(cov(samples); digits=3),)(mean = [-0.005, 0.018, -0.004], variance = [0.983, 0.985, 0.988], covariance = [0.983 -0.008 -0.006; -0.008 0.985 0.001; -0.006 0.001 0.988])The reference mean is zeros(3). The reference covariance is the identity matrix. Finite-sample estimates differ from these reference values.
These methods use normalized importance weights. In contrast, mean(samples.samples; dims=2) ignores the weights and describes the proposal draws.
Functions of the sampled parameters use the same interface:
( squared_radius=mean(x -> sum(abs2, x), samples), first_coordinate_second_moment=mean(x -> x[1]^2, samples),)(squared_radius = 2.9573289377982315, first_coordinate_second_moment = 0.9834507946498896)The reference values are three and one, respectively.
Log weights and effective sample size
weights = normalized_weights(samples)weight_ess = inv(sum(abs2, weights))(weight_sum=sum(weights), weight_ess=weight_ess, draws=length(samples))(weight_sum = 1.0, weight_ess = 11514.142956968284, draws = 20000)The raw log weight is
\[\ell_i=\log\pi(x_i)-\log q(x_i).\]
samples.logweights stores these values. normalized_weights returns a new array $\bar w_i=\exp(\ell_i)/\sum_j\exp(\ell_j)$ without changing the raw log weights.
Weight ESS measures concentration. It lies between one and the draw count when the weights are valid. It does not test whether important regions were missed.
Normalizing constant
( estimate=lognormalizer(samples), reference=3log(2pi) / 2,)(estimate = 2.767734932986599, reference = 2.756815599614018)lognormalizer estimates the logarithm of $Z=\int\pi(x)\,dx$. Here $Z=(2\pi)^{3/2}$, since the target omitted the Gaussian normalization.
Adding a constant to the log target adds that constant to this result. For Bayesian evidence, retain all prior and likelihood normalization constants.
Related examples
Logistic regression adds observed data and an adaptive proposal. Weighted samples covers indexing, intervals, resampling, and diagnostics.