I understand the basic principle of a particle filter and tried to implement one. However, I got hung up on the resampling part.
Theoretically speaking, it is quite simple: From the old (and weighted) set of particles, draw a new set of particles with replacement. While doing so, favor those particles that have high weights. Particles with high weights get drawn more often and particles with low weights less often. Perhaps only once or not at all. After resampling, all weights get assigned the same weight.
My first idea on how to implement this was essentially this:
- Normalize the weights
- Multiply each weight by the total number of particles
- Round those scaled weights to the nearest integer (e.g. with
int()
in Python)
Now I should know how often to draw each particle, but due to the roundoff errors, I end up having less particles than before the resampling step.
The Question: How do I "fill up" the missing particles in order to get to the same number of particles as before the resampling step? Or, in case I am completely off track here, how do I resample correctly?