Skip to content

Commit

Permalink
Reduce computation and test instability in stratified resampling.
Browse files Browse the repository at this point in the history
  • Loading branch information
ztangent committed Jan 20, 2021
1 parent 42b0464 commit 0269780
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GenParticleFilters"
uuid = "56b76ac4-72ef-411e-b419-6d312ed86a6f"
authors = ["Xuan <tanqazx@gmail.com>"]
version = "0.1.3"
version = "0.1.4"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
17 changes: 10 additions & 7 deletions src/resample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,17 @@ function pf_stratified_resample!(state::ParticleFilterState;
state.log_weights : priority_fn.(state.log_weights)
weights = exp.(lognorm(log_priorities))
# Optionally sort particles by weight before resampling
order = sort_particles ? sortperm(log_priorities) : collect(1:n_particles)
order = sort_particles ?
sortperm(log_priorities, rev=true) : collect(1:n_particles)
# Sample particles within each weight stratum [i, i+1/n)
i_old, accum_weight = 0, 0.0
for (i_new, lower) in enumerate((0:n_particles-1)/n_particles)
u = rand() * (1/n_particles) + lower
while accum_weight < u
accum_weight += weights[order[i_old+1]]
i_old += 1
i_old, weight_step, accum_weight = 0, 1/n_particles, 0.0
for (i_new, lower) in enumerate(0.0:weight_step:1.0-weight_step)
if lower + weight_step > accum_weight
u = rand() * weight_step + lower
while accum_weight < u
accum_weight += weights[order[i_old+1]]
i_old += 1
end
end
state.parents[i_new] = order[i_old]
state.new_traces[i_new] = state.traces[order[i_old]]
Expand Down

2 comments on commit 0269780

@ztangent
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/28392

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.4 -m "<description of version>" 026978070c5a89f0e0103232aa6e9eef12cce637
git push origin v0.1.4

Please sign in to comment.