Skip to content

Commit

Permalink
handle multivariate bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Jan 11, 2021
1 parent fe91dbd commit 9c52b71
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/resampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,21 @@
# end

for PT in ParticleSymbols
# Constructors
@eval begin

"""
bootstrap([rng::AbstractRNG,] p::Particles, n = nparticles(p))
Return Particles resampled with replacement. `n` specifies the number of samples to draw.
Return Particles resampled with replacement. `n` specifies the number of samples to draw. Also works for arrays of Particles, in which case a single set of indices are drawn and used to extract samples from all elements in the array.
"""
function bootstrap(rng::AbstractRNG, p::$PT, n = nparticles(p))
$PT(p.particles[[rand(rng, 1:nparticles(p)) for _ in 1:n]])
end
bootstrap(p::$PT, n = nparticles(p)) = bootstrap(Random.GLOBAL_RNG, p, n)

function bootstrap(rng::AbstractRNG, p::AbstractArray{<:$PT}, n = nparticles(p))
inds = [rand(rng, 1:nparticles(p)) for _ in 1:n]
newpart = [p.particles[inds] for p in p]
$PT.(newpart)
end
end
end
end
bootstrap(p, n::Integer = nparticles(p)) = bootstrap(Random.GLOBAL_RNG, p, n)
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ Random.seed!(0)
p2 = bootstrap(rng,p)
@test p1 == p2
@test nparticles(bootstrap(p, 10)) == 10
@test_nowarn bootstrap([p; p])
end

@time @testset "mutation" begin
Expand Down

0 comments on commit 9c52b71

Please sign in to comment.