diff --git a/GeneralisedFilters/CHANGELOG.md b/GeneralisedFilters/CHANGELOG.md index 2ded0701..ce2cdade 100644 --- a/GeneralisedFilters/CHANGELOG.md +++ b/GeneralisedFilters/CHANGELOG.md @@ -28,6 +28,10 @@ - Compute only the triangular factor in square-root backward messages for larger static Float32/Float64 matrices. This avoids forming Q and contended small-matrix BLAS calls, speeding up CSMC ancestor sampling and backward simulation, especially with threads. +- Fix systematic and stratified resampling reading past the weights when rounding puts a + threshold beyond the final cumulative weight, as with Float32 weights; scans now stop at + the last positive-weight particle. Conditional variants on CPU and GPU now always keep the + reference, including one whose weight is below the precision of the cumulative weights. ## 0.5.0 diff --git a/GeneralisedFilters/ext/CUDAExt.jl b/GeneralisedFilters/ext/CUDAExt.jl index 471a5f20..884c137f 100644 --- a/GeneralisedFilters/ext/CUDAExt.jl +++ b/GeneralisedFilters/ext/CUDAExt.jl @@ -92,7 +92,10 @@ function GeneralisedFilters.conditional_sample_ancestors( rng::AbstractRNG, ::Systematic, weights::CuVector, ref_idx::Integer ) offspring, K = sample_conditional_offspring(rng, Systematic(), weights, ref_idx) - return offspring_to_ancestors(offspring; shift=K - 1) + as = offspring_to_ancestors(offspring; shift=K - 1) + # Rounding can move the reference slot's threshold out of a narrow interval. + CUDA.@allowscalar as[1] = ref_idx + return as end # Following Code 8 of Murray et. al (2015) @@ -153,7 +156,10 @@ function GeneralisedFilters.conditional_sample_ancestors( rng::AbstractRNG, ::Stratified, weights::CuVector, ref_idx::Integer ) offspring, K = sample_conditional_offspring(rng, Stratified(), weights, ref_idx) - return offspring_to_ancestors(offspring; shift=K - 1) + as = offspring_to_ancestors(offspring; shift=K - 1) + # Rounding can move the reference slot's threshold out of a narrow interval. + CUDA.@allowscalar as[1] = ref_idx + return as end # Following Code 7 of Murray et. al (2015) diff --git a/GeneralisedFilters/src/resamplers.jl b/GeneralisedFilters/src/resamplers.jl index b7c1310d..19f0647f 100644 --- a/GeneralisedFilters/src/resamplers.jl +++ b/GeneralisedFilters/src/resamplers.jl @@ -326,6 +326,12 @@ function conditional_sample_ancestors( return a end +# Rounding can place a threshold at or beyond the final scaled cumulative weight, so the +# inverse-CDF scans stop at the last particle that has positive weight. +function _last_positive(weights::AbstractVector{WT}) where {WT<:Real} + return findlast(>(zero(WT)), weights) +end + struct Systematic <: AbstractResampler end function sample_ancestors( @@ -333,7 +339,8 @@ function sample_ancestors( ) where {WT<:Real} # pre-calculations vs = cumsum(weights) - vs *= n + vs .*= n + idx_max = _last_positive(weights) u0 = rand(rng, WT) @@ -343,7 +350,7 @@ function sample_ancestors( @inbounds for i in 1:n u = u0 + (i - 1) - while vs[idx] <= u + while idx < idx_max && vs[idx] <= u idx += 1 end a[i] = idx @@ -363,19 +370,23 @@ function conditional_sample_ancestors( n = length(weights) vs = cumsum(weights) - vs *= n + vs .*= n + idx_max = _last_positive(weights) u0, K = _reference_offset(rng, vs, ref_idx, n) + # Slot `K`'s threshold lies in the reference's interval only in exact arithmetic, and + # rounding can step past an interval narrower than the spacing of `vs`, so the slot is + # assigned directly. This corrects rounding; the law of the other slots is unchanged. a = Vector{Int64}(undef, n) idx = 1 @inbounds for i in 1:n u = u0 + (i - 1) - while vs[idx] <= u + while idx < idx_max && vs[idx] <= u idx += 1 end - a[mod1(i - K + 1, n)] = idx + a[mod1(i - K + 1, n)] = i == K ? ref_idx : idx end return a @@ -388,7 +399,8 @@ function sample_ancestors( ) where {WT<:Real} # pre-calculations vs = cumsum(weights) - vs *= n + vs .*= n + idx_max = _last_positive(weights) # initialize sampling algorithm a = Vector{Int64}(undef, n) @@ -396,7 +408,7 @@ function sample_ancestors( @inbounds for i in 1:n u = rand(rng, WT) + (i - 1) - while vs[idx] <= u + while idx < idx_max && vs[idx] <= u idx += 1 end a[i] = idx @@ -415,7 +427,8 @@ function conditional_sample_ancestors( n = length(weights) vs = cumsum(weights) - vs *= n + vs .*= n + idx_max = _last_positive(weights) u_ref, K = _reference_offset(rng, vs, ref_idx, n) @@ -424,10 +437,10 @@ function conditional_sample_ancestors( @inbounds for i in 1:n u = (i == K ? u_ref : rand(rng, WT)) + (i - 1) - while vs[idx] <= u + while idx < idx_max && vs[idx] <= u idx += 1 end - a[mod1(i - K + 1, n)] = idx + a[mod1(i - K + 1, n)] = i == K ? ref_idx : idx end return a diff --git a/GeneralisedFilters/test/components/resamplers.jl b/GeneralisedFilters/test/components/resamplers.jl index ed5b480d..9ae7e0b9 100644 --- a/GeneralisedFilters/test/components/resamplers.jl +++ b/GeneralisedFilters/test/components/resamplers.jl @@ -168,6 +168,38 @@ end end end +@testitem "Inverse-CDF scans stop at the last positive weight" begin + using StableRNGs + + # Weights summing to less than one exaggerate the shortfall that rounding produces in + # the scaled cumulative weights: every threshold past the first lies beyond the CDF. + ws = Float32[0.3, 0.3, 0.0, 0.0] + rng = StableRNG(1234) + for resampler in (Systematic(), Stratified()), _ in 1:100 + @test GeneralisedFilters.sample_ancestors(rng, resampler, ws) ⊆ 1:2 + for ref_idx in 1:2 + idxs = GeneralisedFilters.conditional_sample_ancestors( + rng, resampler, ws, ref_idx + ) + @test idxs[1] == ref_idx + @test idxs ⊆ 1:2 + end + end +end + +@testitem "Conditional resampling retains a reference below CDF resolution" setup = [ + ConditionalResamplingTestSetup +] begin + # The reference's weight vanishes when added to the cumulative weight before it, so + # its interval has zero width and no threshold can select it. + ws = Float32[0.5, 1.0f-10, 0.5] + rng = StableRNG(1234) + for resampler in RESAMPLERS, _ in 1:100 + idxs = GeneralisedFilters.conditional_sample_ancestors(rng, resampler, ws, 2) + @test idxs[1] == 2 + end +end + @testitem "Conditional resampling is invariant" setup = [ConditionalResamplingTestSetup] begin for resampler in RESAMPLERS rng = StableRNG(1234) @@ -244,6 +276,19 @@ end end end +@testitem "GPU conditional resampling retains a reference below CDF resolution" tags = [ + :gpu +] begin + using CUDA + + ws = CuVector(Float32[0.5, 1.0f-10, 0.5]) + rng = CUDA.RNG(1234) + for resampler in (Systematic(), Stratified()), _ in 1:20 + idxs = GeneralisedFilters.conditional_sample_ancestors(rng, resampler, ws, 2) + @test CUDA.@allowscalar idxs[1] == 2 + end +end + @testitem "GPU conditional resampling matches the CPU law" tags = [:gpu] begin using CUDA using StableRNGs