@@ -12,33 +12,33 @@ struct ResampleWithESSThreshold{R, T<:Real}
1212 threshold:: T
1313end
1414
15- function ResampleWithESSThreshold (resampler = resample )
15+ function ResampleWithESSThreshold (resampler = resample_systematic )
1616 ResampleWithESSThreshold (resampler, 0.5 )
1717end
1818
1919# More stable, faster version of rand(Categorical)
20- function randcat (p:: AbstractVector{<:Real} )
20+ function randcat (rng :: Random.AbstractRNG , p:: AbstractVector{<:Real} )
2121 T = eltype (p)
22- r = rand (T)
22+ r = rand (rng, T)
23+ cp = p[1 ]
2324 s = 1
24- for j in eachindex (p)
25- r -= p[j]
26- if r <= zero (T)
27- s = j
28- break
29- end
25+ n = length (p)
26+ while cp <= r && s < n
27+ @inbounds cp += p[s += 1 ]
3028 end
3129 return s
3230end
3331
3432function resample_multinomial (
33+ rng:: Random.AbstractRNG ,
3534 w:: AbstractVector{<:Real} ,
3635 num_particles:: Integer = length (w),
3736)
38- return rand (Distributions. sampler (Distributions. Categorical (w)), num_particles)
37+ return rand (rng, Distributions. sampler (Distributions. Categorical (w)), num_particles)
3938end
4039
4140function resample_residual (
41+ rng:: Random.AbstractRNG ,
4242 w:: AbstractVector{<:Real} ,
4343 num_particles:: Integer = length (weights),
4444)
@@ -57,19 +57,19 @@ function resample_residual(
5757 end
5858 residuals[j] = x - floor_x
5959 end
60-
60+
6161 # sampling from residuals
6262 if i <= num_particles
6363 residuals ./= sum (residuals)
64- rand! (Distributions. Categorical (residuals), view (indices, i: num_particles))
64+ rand! (rng, Distributions. Categorical (residuals), view (indices, i: num_particles))
6565 end
66-
66+
6767 return indices
6868end
6969
7070
7171"""
72- resample_stratified(weights, n)
72+ resample_stratified(rng, weights, n)
7373
7474Return a vector of `n` samples `x₁`, ..., `xₙ` from the numbers 1, ..., `length(weights)`,
7575generated by stratified resampling.
@@ -80,7 +80,11 @@ are selected according to the multinomial distribution defined by the normalized
8080i.e., `xᵢ = j` if and only if
8181``uᵢ \\ in [\\ sum_{s=1}^{j-1} weights_{s}, \\ sum_{s=1}^{j} weights_{s})``.
8282"""
83- function resample_stratified (weights:: AbstractVector{<:Real} , n:: Integer = length (weights))
83+ function resample_stratified (
84+ rng:: Random.AbstractRNG ,
85+ weights:: AbstractVector{<:Real} ,
86+ n:: Integer = length (weights),
87+ )
8488 # check input
8589 m = length (weights)
8690 m > 0 || error (" weight vector is empty" )
@@ -93,7 +97,7 @@ function resample_stratified(weights::AbstractVector{<:Real}, n::Integer = lengt
9397 sample = 1
9498 @inbounds for i in 1 : n
9599 # sample next `u` (scaled by `n`)
96- u = oftype (v, i - 1 + rand ())
100+ u = oftype (v, i - 1 + rand (rng ))
97101
98102 # as long as we have not found the next sample
99103 while v < u
@@ -114,7 +118,7 @@ function resample_stratified(weights::AbstractVector{<:Real}, n::Integer = lengt
114118end
115119
116120"""
117- resample_systematic(weights, n)
121+ resample_systematic(rng, weights, n)
118122
119123Return a vector of `n` samples `x₁`, ..., `xₙ` from the numbers 1, ..., `length(weights)`,
120124generated by systematic resampling.
@@ -125,14 +129,18 @@ numbers `u₁`, ..., `uₙ` where ``uₖ = (u + k − 1) / n``. Based on these n
125129normalized `weights`, i.e., `xᵢ = j` if and only if
126130``uᵢ \\ in [\\ sum_{s=1}^{j-1} weights_{s}, \\ sum_{s=1}^{j} weights_{s})``.
127131"""
128- function resample_systematic (weights:: AbstractVector{<:Real} , n:: Integer = length (weights))
132+ function resample_systematic (
133+ rng:: Random.AbstractRNG ,
134+ weights:: AbstractVector{<:Real} ,
135+ n:: Integer = length (weights),
136+ )
129137 # check input
130138 m = length (weights)
131139 m > 0 || error (" weight vector is empty" )
132140
133141 # pre-calculations
134142 @inbounds v = n * weights[1 ]
135- u = oftype (v, rand ())
143+ u = oftype (v, rand (rng ))
136144
137145 # find all samples
138146 samples = Array {Int} (undef, n)
@@ -158,6 +166,3 @@ function resample_systematic(weights::AbstractVector{<:Real}, n::Integer = lengt
158166
159167 return samples
160168end
161-
162- # Default resampling scheme
163- const resample = resample_systematic
0 commit comments