Targets and data
A target returns the log density or log of a nonnegative integrand. The proposal determines which parameters are sampled and their shape.
Target context
using ImportanceSamplers, PureRNGs, Statisticsfunction logtarget(theta, p) return -sum(abs2, theta .- p.centre) / (2p.scale^2)endp = (; centre=[1.0, -1.0, 0.5], scale=0.8)algorithm = ImportanceSampling(SphericalGaussian(zeros(3), 2.0); nsamples=5_000)samples = importance_sample(Threefry4x32(42), logtarget, p, algorithm)mean(samples)3-element Vector{Float64}:
0.9573082089238764
-0.968808451913431
0.4952604801171917Each theta is a three-element vector. The sampler does not estimate p. The same context also works with prepare_sampler(rng, logtarget, p, algorithm).
A target without data can use logtarget(theta) instead. A named target can use theta.scale or theta.weights through a transform layout.
Keyword functions
Julia keyword arguments need an ordinary callable wrapper:
model(theta; centre, scale) = -sum(abs2, theta .- centre) / (2scale^2)wrapped(theta, p) = model(theta; p...)other = importance_sample(Threefry4x32(42), wrapped, p, algorithm)mean(other)3-element Vector{Float64}:
0.9573082089238764
-0.968808451913431
0.4952604801171917There is no reserved keyword name or fixed number of keywords. The wrapper converts the function signature to the package's (theta, p) convention.
Pass numerical arrays through p for device execution. A closure over host arrays does not transfer those arrays to a GPU.
Density interfaces
Objects implementing DensityInterface.logdensityof can serve as targets. For example, with Distributions.jl installed:
using Distributions, ImportanceSamplers, PureRNGstarget = MvNormal(zeros(3), ones(3))proposal = MvNormal(zeros(3), fill(1.5, 3))samples = importance_sample( Threefry4x32(42), target, ImportanceSampling(proposal; nsamples=5_000),)The unknown is still the three-dimensional vector drawn from proposal. The target distribution supplies its density, not a separate parameter list.
LogDensityProblems objects can supply logdensity, dimension, and capabilities. Preparation checks their declared dimension against the proposal. A first-order interface can also supply gradients.
Use LogTarget(callable) to select callable semantics explicitly when an object also advertises a density interface. The selected interface does not change after an execution error.
Numerical requirements
A log target must return Float32 or Float64 with an inferable return type. Finite values and -Inf are valid. -Inf means zero target density. NaN, +Inf, and exceptions fail the run.
Match constants and arrays to the intended precision. For example, use 0.5f0 and Float32 data with a single-precision model.
Retaining mapped parameters
An adapter may evaluate a target through an expensive parameter map. RetainCoordinates saves that map's outputs during retained importance draws. It avoids repeating the map when constructing the final result. Ordinary use does not require this interface.
lognormal_coordinates(z) = -sum(abs2, z) / 2mapped(z) = (lognormal_coordinates(z), exp.(z), sum(z))retain = RetainCoordinates(mapped, zeros(2))mapped_target = LogTarget(lognormal_coordinates; retain)algorithm = ImportanceSampling(SphericalGaussian(zeros(2), 1.0); nsamples=5_000)result = importance_sample(Threefry4x32(43), mapped_target, algorithm)x = result.retained.coordinateslogdensity_x = result.logtargets .- result.retained.logjacobians5000-element Vector{Float64}:
-2.1185482305489
0.7588155771512252
-3.848928760359804
-2.994304803629053
0.6080149593511712
0.7051147483503525
-1.7360326360042193
0.9506186502220451
0.7201069297795328
-1.2790571981871603
⋮
-4.16424674553247
-1.10782702308276
0.706469481548808
0.4288576807999982
-1.0756210948331648
-2.680507297089627
0.9833005556704602
0.7758902054469353
-1.831143081257888Here z has a Gaussian target and x = exp.(z) has the corresponding lognormal target. The saved Jacobian is sum(z). The unnormalized weights remain those of the original Gaussian-coordinate estimator.
The callback must return the same log target as the ordinary callable. It runs only for returned importance draws, not adaptation or MCMC trials. Its arrays are copied into result buffers, so it may return input views or scratch. Concurrent calls still require a thread-safe callback. The optional retained batch callback writes device buffers directly. See RetainCoordinates for its scalar, context and batch contracts.
Scalar targets may run concurrently. Treat the input sample and context as read-only. Avoid hidden mutable scratch storage. Use an explicit batch callback when evaluation should share work across samples.
Reference measures
The target and proposal must describe densities against the same reference measure. A transformed target needs exactly one Jacobian adjustment.
Omitting a target constant preserves normalized expectations. It changes lognormalizer, so it is not valid when you need absolute evidence.