Skip to content

Commit 2fb7290

Browse files
Merge branch 'master' of https://github.com/FredericWantiez/AdvancedPS.jl into feature/traced_rng
2 parents 5cbbe40 + 350e2c1 commit 2fb7290

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "AdvancedPS"
22
uuid = "576499cb-2369-40b2-a588-c64705576edc"
33
authors = ["TuringLang"]
4-
version = "0.2.2"
4+
version = "0.2.4"
55

66
[deps]
77
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
@@ -13,6 +13,6 @@ StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
1313
[compat]
1414
AbstractMCMC = "2, 3"
1515
Distributions = "0.23, 0.24, 0.25"
16-
Libtask = "0.5"
16+
Libtask = "0.5.3"
1717
StatsFuns = "0.9"
1818
julia = "1.3"

src/container.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,16 @@ The resampling steps use the given `resampler`.
330330
Del Moral, P., Doucet, A., & Jasra, A. (2006). Sequential monte carlo samplers.
331331
Journal of the Royal Statistical Society: Series B (Statistical Methodology), 68(3), 411-436.
332332
"""
333-
function sweep!(rng::Random.AbstractRNG, pc::ParticleContainer, resampler)
333+
function sweep!(
334+
rng::Random.AbstractRNG,
335+
pc::ParticleContainer,
336+
resampler,
337+
ref::Union{Particle,Nothing}=nothing,
338+
)
334339
# Initial step:
335340

336341
# Resample and propagate particles.
337-
resample_propagate!(rng, pc, resampler)
342+
resample_propagate!(rng, pc, resampler, ref)
338343

339344
# Compute the current normalizing constant ``Z₀`` of the unnormalized logarithmic
340345
# weights.
@@ -355,7 +360,7 @@ function sweep!(rng::Random.AbstractRNG, pc::ParticleContainer, resampler)
355360
# For observations ``y₂, …, yₜ``:
356361
while !isdone
357362
# Resample and propagate particles.
358-
resample_propagate!(rng, pc, resampler)
363+
resample_propagate!(rng, pc, resampler, ref)
359364

360365
# Compute the current normalizing constant ``Z₀`` of the unnormalized logarithmic
361366
# weights.

src/smc.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ function AbstractMCMC.step(
118118
particles = ParticleContainer(x, rng)
119119

120120
# Perform a particle sweep.
121-
logevidence = sweep!(particles.rng, particles, sampler.resampler)
121+
logevidence = sweep!(
122+
particles.rng, particles, sampler.resampler, particles.vals[nparticles]
123+
)
122124

123125
# Pick a particle to be retained.
124126
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)