Skip to content

Commit c5440fb

Browse files
authored
Merge pull request #29 from FredericWantiez/fix/ref_particle
Propagate ref particle in PG
2 parents 986776d + 5c5723e commit c5440fb

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/container.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,16 @@ The resampling steps use the given `resampler`.
314314
Del Moral, P., Doucet, A., & Jasra, A. (2006). Sequential monte carlo samplers.
315315
Journal of the Royal Statistical Society: Series B (Statistical Methodology), 68(3), 411-436.
316316
"""
317-
function sweep!(rng::Random.AbstractRNG, pc::ParticleContainer, resampler)
317+
function sweep!(
318+
rng::Random.AbstractRNG,
319+
pc::ParticleContainer,
320+
resampler,
321+
ref::Union{Particle,Nothing}=nothing,
322+
)
318323
# Initial step:
319324

320325
# Resample and propagate particles.
321-
resample_propagate!(rng, pc, resampler)
326+
resample_propagate!(rng, pc, resampler, ref)
322327

323328
# Compute the current normalizing constant ``Z₀`` of the unnormalized logarithmic
324329
# weights.
@@ -339,7 +344,7 @@ function sweep!(rng::Random.AbstractRNG, pc::ParticleContainer, resampler)
339344
# For observations ``y₂, …, yₜ``:
340345
while !isdone
341346
# Resample and propagate particles.
342-
resample_propagate!(rng, pc, resampler)
347+
resample_propagate!(rng, pc, resampler, ref)
343348

344349
# Compute the current normalizing constant ``Z₀`` of the unnormalized logarithmic
345350
# weights.

src/smc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function AbstractMCMC.step(
114114
particles = ParticleContainer(x)
115115

116116
# Perform a particle sweep.
117-
logevidence = sweep!(rng, particles, sampler.resampler)
117+
logevidence = sweep!(rng, particles, sampler.resampler, particles.vals[nparticles])
118118

119119
# Pick a particle to be retained.
120120
newtrajectory = rand(rng, particles)

test/container.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@
5151
)
5252
@test AdvancedPS.logZ(pc) log(sum(exp, 2 .* logps))
5353

54+
# Resample and propagate particles with reference particle
55+
particles_ref = [AdvancedPS.Trace(fpc(logp)) for logp in logps]
56+
pc_ref = AdvancedPS.ParticleContainer(particles_ref)
57+
AdvancedPS.resample_propagate!(
58+
Random.GLOBAL_RNG, pc_ref, AdvancedPS.resample_systematic, particles_ref[end]
59+
)
60+
@test pc_ref.logWs == zeros(3)
61+
@test AdvancedPS.getweights(pc_ref) == fill(1 / 3, 3)
62+
@test all(AdvancedPS.getweight(pc_ref, i) == 1 / 3 for i in 1:3)
63+
@test AdvancedPS.logZ(pc_ref) log(3)
64+
@test AdvancedPS.effectiveSampleSize(pc_ref) == 3
65+
@test pc_ref.vals[end] === particles_ref[end]
66+
5467
# Resample and propagate particles.
5568
AdvancedPS.resample_propagate!(Random.GLOBAL_RNG, pc)
5669
@test pc.logWs == zeros(3)

0 commit comments

Comments
 (0)