johnhw / pfilter

Basic Python particle filter
MIT License
172 stars 25 forks source link

Resampling algorithm #9

Closed argentum2f closed 4 years ago

argentum2f commented 4 years ago

Can you tell what type of resampling is implemented in the code you used from the scipy cookbook? The code is pretty opaque to me. I was trying to compare it to these examples: https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python/blob/master/12-Particle-Filters.ipynb and it does look very similar to a couple.

Also, I don't understand why a scipy code example would be using a list comprehension in place of cumsum. That can't be as fast, can it?

In any case, I think would make sense to have the option to supply a different re-sample function to the ParticleFilter class. (And/or include a few built in options to choose from).

johnhw commented 4 years ago

The algorithm is the one given here: https://scipy-cookbook.readthedocs.io/items/ParticleFilter.html which I believe is systematic resampling, though the implementation is weird. I've maderesample_fn an optional parameter, so you can hook in your own, e.g.

def basic_resample(weights):
    return np.random.choice(np.arange(len(weights)), p=weights, size=len(weights))

pf = ParticleFilter(..., resample_fn=basic_resample)

Also, I don't think anything in pfilter is going to be very performant :)