Skip to content

Commit 04370ed

Browse files
committed
Slighly optimize LiuWestResampler
1 parent ea693d9 commit 04370ed

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/qinfer/resamplers.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,30 +299,25 @@ def __call__(self, model, particle_dist,
299299

300300
idxs_to_resample = np.arange(n_particles, dtype=int)
301301

302-
# Preallocate js and mus so that we don't have rapid allocation and
303-
# deallocation.
304-
js = np.empty(idxs_to_resample.shape, dtype=int)
305-
mus = np.empty(new_locs.shape, dtype=l.dtype)
306-
307302
# Loop as long as there are any particles left to resample.
308303
n_iters = 0
309304

310305
# Draw j with probability self.particle_weights[j].
311306
# We do this by drawing random variates uniformly on the interval
312307
# [0, 1], then see where they belong in the CDF.
313-
js[:] = cumsum_weights.searchsorted(
308+
js = cumsum_weights.searchsorted(
314309
np.random.random((idxs_to_resample.size,)),
315310
side='right'
316311
)
317312

313+
# Set mu_i to a x_j + (1 - a) mu.
314+
# FIXME This should use particle_dist.particle_mean
315+
mus = a * l[js,:] + (1 - a) * mean
316+
318317
while idxs_to_resample.size and n_iters < self._maxiter:
319318
# Keep track of how many iterations we used.
320319
n_iters += 1
321320

322-
# Set mu_i to a x_j + (1 - a) mu.
323-
# TODO This is a particle mean :(
324-
mus[...] = a * l[js,:] + (1 - a) * mean
325-
326321
# Draw x_i from N(mu_i, S).
327322
new_locs[idxs_to_resample, :] = mus + np.dot(S, self._kernel(n_rvs, mus.shape[0])).T
328323

0 commit comments

Comments
 (0)