Adaptation and reuse
An algorithm describes a sampling method. A prepared sampler stores the target, random stream, workspaces, and learned proposal.
Repeated execution
using ImportanceSamplers, PureRNGs, Statisticslogtarget(x, p) = -sum(abs2, x .- p.centre) / 2p = (; centre=[1.0, -1.0])algorithm = AMIS( SphericalGaussian(zeros(2), 2.0); rounds=3, round_size=1_000,)prepared = prepare_sampler(Threefry4x32(42), logtarget, p, algorithm)first_run = importance_sample!(prepared)second_run = importance_sample!(prepared)next_rng = current_rng(prepared)(mean(first_run), mean(second_run))([0.972261411992743, -0.9436980081831987], [1.0072649055617142, -1.0098402445554353])The second call starts from the proposal learned by the first call. Its result contains only its own samples, not samples from earlier calls. Earlier result arrays remain unchanged.
A successful adaptive call retains its final fitted proposal even when that proposal generated no sample in the completed call. AMIS also starts a new retrospective history on the next call.
Sample counts
Plain and static IS take nsamples. Adaptive methods take rounds and round_size:
AMIS(proposal; rounds=3, round_size=1_000) # 3,000 returned drawsAMIS(proposal; rounds=3, round_size=[500, 1_000, 2_000]) # 3,500 drawsround_size counts all draws in a round, not draws per proposal. Every adaptive result retains all rounds.
Schedules are fixed during preparation. Round-size callbacks and changing the budget of an existing prepared sampler are not supported. Construct another algorithm to change its budget.
Population methods have additional allocation requirements. A prepared call restarts round-indexed schedules at round one. LAIS transition state follows its separate tuning rules. HAIS retains its final post-sampling HMC/cooperation update for the next call. HMC warmup completes once and its production step size stays fixed during reuse.
Proposal snapshots
proposal = current_proposal(prepared)final_samples = importance_sample( Threefry4x32(43), logtarget, p, ImportanceSampling(proposal; nsamples=5_000),)mean(final_samples)2-element Vector{Float64}:
0.9858416563218307
-0.9870101489365035This separates a pilot from production. The new algorithm controls its own count and method. current_proposal returns an independent snapshot, not a mutable view into the sampler.
For a named layout or QuadraticCoupling, the snapshot uses latent sampling coordinates. Pass the same transform to the new prepared sampler.
For an accelerator sampler, request an explicit CPU snapshot with current_proposal(MLDataDevices.CPUDevice(), prepared). This transfers the proposal, not the complete result or sampler.
Retargeting
new_p = (; centre=[1.5, -0.5])new_sampler = retarget(Threefry4x32(44), prepared, logtarget, new_p)new_samples = importance_sample!(new_sampler)mean(new_samples)2-element Vector{Float64}:
1.5184487692273787
-0.5013519681989081retarget keeps the learned proposal, algorithm controls, count schedule, transform, execution policy, and device. It creates fresh target-dependent state and starts from the supplied RNG. The original sampler remains usable.
This operation supports every implemented adaptive method, including LAIS. For plain or static IS, call prepare_sampler again with the existing algorithm.
The reused proposal must cover the new target. Retargeting does not prove that condition.
On an accelerator, retargeting stages the learned proposal through CPU memory before constructing fresh state on the original device. This is a setup cost, not a per-sample transfer.
Random streams and failures
Each call stores its continued RNG, available with current_rng(prepared). A native RNG is immutable. Passing the same snapshot to separate samplers repeats its stream. For independent runs, split once with left, right = splitrng(rng) and use each continuation separately.
On Reactant, current_rng returns a compiled stream. Use it with retarget or resample_next on the same device. Fresh preparation does not accept a compiled stream.
A prepared sampler is mutable and non-reentrant. An adaptive failure preserves the proposal committed before that call. It returns no partial result and does not restore consumed random numbers.
Device placement must occur before the first execution. See Devices for transfer and migration rules.