Numerical integration
Importance sampling also estimates ordinary integrals. No Bayesian model or observed data is required.
Consider
\[I=\int_{\mathbb R^3}\exp(-\lVert x\rVert^2)\,dx=\pi^{3/2}.\]
Nonnegative integrands
using ImportanceSamplers, PureRNGs, Statisticslogintegrand(x) = -sum(abs2, x)proposal = SphericalGaussian(zeros(3), 1.0)samples = importance_sample( Threefry4x32(8), logintegrand, ImportanceSampling(proposal; nsamples=20_000),)(estimate=exp(lognormalizer(samples)), reference=pi^(3/2))(estimate = 5.558791881234938, reference = 5.568327996831708)The log target is the logarithm of the integrand. The normalized proposal supplies draws over the integration domain.
For plain IS, the estimate is the average of $f(x_i)/q(x_i)$. The package stores this ratio in log form.
Monte Carlo error
These plain-IS draws are independent. For finite-variance contributions, use their ordinary sample standard error:
contributions = exp.(samples.logweights)(estimate=mean(contributions), monte_carlo_se=std(contributions) / sqrt(length(samples)))(estimate = 5.558791881234935, monte_carlo_se = 0.029084437056175593)This describes numerical error, not variation under a normalized target. It is not a general adaptive/MIS formula and cannot certify missed tails. This example has safely scaled contributions. Extreme log weights require rescaling before this calculation.
Signed integrands
A signed integrand has no real logarithm everywhere. Choose a positive envelope $g$ and write the integrand as $g(x)h(x)$ instead.
The same samples estimate
\[J=\int_{\mathbb R^3}e^{-\lVert x\rVert^2}\cos(x_1)\,dx =\pi^{3/2}e^{-1/4}.\]
estimate = exp(lognormalizer(samples)) * mean(x -> cos(x[1]), samples)(estimate=estimate, reference=pi^(3/2) * exp(-1/4))(estimate = 4.32542273706988, reference = 4.336618204330962)Here logintegrand defines $\log g$ and the function passed to mean defines $h$. The product equals the ordinary weighted average $n^{-1}\sum_i g(x_i)h(x_i)/q(x_i)$.
This estimate can have either sign. It needs proposal support wherever $g h$ contributes and finite variance for conventional Monte Carlo error estimates. The displayed product can overflow if its positive normalizer is too large. Large-scale signed integrals need sign/log-absolute accumulation instead of that product.
Constrained domains
A proposal may live directly on the domain, or a transform may map unconstrained draws into it. The target must use the same reference measure as the proposal.
Constraints and named parameters explains Jacobian adjustments. The standalone integration example also treats a one-dimensional integral.