6
6
const Particle = Trace
7
7
8
8
function Trace (f)
9
- ctask = let f= f
9
+ ctask = let f = f
10
10
Libtask. CTask () do
11
11
res = f ()
12
12
Libtask. produce (nothing )
@@ -34,7 +34,7 @@ reset_model(f) = deepcopy(f)
34
34
delete_retained! (f) = nothing
35
35
36
36
# Task copying version of fork for Trace.
37
- function fork (trace:: Trace , isref:: Bool = false )
37
+ function fork (trace:: Trace , isref:: Bool = false )
38
38
newtrace = copy (trace)
39
39
isref && delete_retained! (newtrace. f)
40
40
48
48
# Create new task and copy randomness
49
49
function forkr (trace:: Trace )
50
50
newf = reset_model (trace. f)
51
- ctask = let f= trace. ctask. task. code
51
+ ctask = let f = trace. ctask. task. code
52
52
Libtask. CTask () do
53
53
res = f ()
54
54
Libtask. produce (nothing )
105
105
function Base. push! (pc:: ParticleContainer , p:: Particle )
106
106
push! (pc. vals, p)
107
107
push! (pc. logWs, 0.0 )
108
- pc
108
+ return pc
109
109
end
110
110
111
111
# clones a theta-particle
@@ -116,7 +116,7 @@ function Base.copy(pc::ParticleContainer)
116
116
# copy weights
117
117
logWs = copy (pc. logWs)
118
118
119
- ParticleContainer (vals, logWs)
119
+ return ParticleContainer (vals, logWs)
120
120
end
121
121
122
122
"""
@@ -183,9 +183,9 @@ of the particle `weights`. For Particle Gibbs sampling, one can provide a refere
183
183
function resample_propagate! (
184
184
rng:: Random.AbstractRNG ,
185
185
pc:: ParticleContainer ,
186
- randcat = resample_systematic,
187
- ref:: Union{Particle, Nothing} = nothing ;
188
- weights = getweights (pc)
186
+ randcat= resample_systematic,
187
+ ref:: Union{Particle,Nothing} = nothing ;
188
+ weights= getweights (pc),
189
189
)
190
190
# check that weights are not NaN
191
191
@assert ! any (isnan, weights)
@@ -232,24 +232,24 @@ function resample_propagate!(
232
232
pc. vals = children
233
233
reset_logweights! (pc)
234
234
235
- pc
235
+ return pc
236
236
end
237
237
238
238
function resample_propagate! (
239
239
rng:: Random.AbstractRNG ,
240
240
pc:: ParticleContainer ,
241
241
resampler:: ResampleWithESSThreshold ,
242
- ref:: Union{Particle,Nothing} = nothing ;
243
- weights = getweights (pc)
242
+ ref:: Union{Particle,Nothing} = nothing ;
243
+ weights= getweights (pc),
244
244
)
245
245
# Compute the effective sample size ``1 / ∑ wᵢ²`` with normalized weights ``wᵢ``
246
246
ess = inv (sum (abs2, weights))
247
247
248
248
if ess ≤ resampler. threshold * length (pc)
249
- resample_propagate! (rng, pc, resampler. resampler, ref; weights = weights)
249
+ resample_propagate! (rng, pc, resampler. resampler, ref; weights= weights)
250
250
end
251
251
252
- pc
252
+ return pc
253
253
end
254
254
255
255
"""
@@ -290,9 +290,13 @@ function reweight!(pc::ParticleContainer)
290
290
291
291
# The posterior for models with random number of observations is not well-defined.
292
292
if numdone != 0
293
- error (" mis-aligned execution traces: # particles = " , n,
294
- " # completed trajectories = " , numdone,
295
- " . Please make sure the number of observations is NOT random." )
293
+ error (
294
+ " mis-aligned execution traces: # particles = " ,
295
+ n,
296
+ " # completed trajectories = " ,
297
+ numdone,
298
+ " . Please make sure the number of observations is NOT random." ,
299
+ )
296
300
end
297
301
298
302
return false
0 commit comments