We begin with a short recap of all the previous algorithms we discussed that are prerequisite to Sequential Monte Carlo. Then I intuitively describe how SMC works. After, we dig into the math behind SMC. We first discuss how importance weights are calculated at each time step, and then we discuss how SMC maintains this notion of survival of the fittest with sample trajectories through resampling.
Before we start, I’d like to thank Jan-Willem van de Meent for his lectures in his Advance Machine Learning Class for PhD students at Northeastern University. The images shown are inspired by his lectures. In addition, I’d like to address that more details on SMC can be found in An Introduction to Probabilistic Programming available [arXiv]. This book is intended as a graduate-level introduction to probabilistic programming languages and methods for inference in probabilistic programs.
Before we move on to other Monte Carlo estimators we might use in machine learning, let’s recap:
- We have discussed what a Monte Carlo Estimator is. It helps us estimate an expectation of some function via sampling.
- We introduce Importance Sampling as our first Monte Carlo Estimator. We learn that Importance Sampling requires us to evaluate samples using a normalized density, which is something we cannot do (almost always).
- Then we describe Self-Normalized Importance Sampling (SNIS). SNIS uses two Monte Carlo Estimators; the same one in Importance Sampling and a second one to estimate the normalization constant. This allows us to evaluate samples via an unnormalized density, which we have access to.
- Importance Sampling methods give us marginal likelihoods, but the downfall is that they do random guessing.
- We then discuss Monte Carlo Markov Chains (MCMC). MCMC uses transition kernels that let us stay in the target density once we have a good sample (one from the target density). However, (almost always) successful MCMC algorithms need to satisfy detailed balance.
- We learn that Metropolis-Hastings (a fancy MCMC algorithm) let’s us use any proposal due to an acceptance probability it uses to manage detailed balance for us.
- In the previous post, we describe an algorithm that marries Importance Sampling and MCMC, Annealing Importance Sampling. This algorithm successfully breaks a hard problem into a series of simpler problems. It provides a marginal likelihood and uses a transition kernel.
Now we will introduce another Monte Carlo Estimator that is used for a set of different problems in machine learning, Sequential Monte Carlo.
Tick Tock: Introducing Time to the Problem!
All of the algorithms we have discuss so far are for problems for which we do not need to consider the element of time! What does that mean? Let’s consider an example of looking at a sequence of video frames of a basketball moving from time frame to time frame.
We record that at time (which can be any arbitrary time stamp), the ball to be up high. We continue to watch the ball move at and , steadily moving downward. The question we have is, “Where will the ball be in the future? This time-dependent data is referred to as time-series data.
There are many combinations of future movements the ball can make. Several can make sense by following the laws of physics, others can be absolutely garbage (like the ball suddenly shooting upwards, or spelling out a word).
Now consider using any of the Monte Carlo Estimating algorithms we have already discussed. If our data is time dependent, then Importance Sampling would have a very difficult time picking up a pattern that correlates movements between frames. It would probably take ages to get a good set of samples from the posterior! Instead, we can use a more appropriate algorithm for time-dependent problems, such as Sequential Monte Carlo (SMC).
Intuition behind Sequential Monte Carlo
SAMPLING BUDGET. Let’s start off by stating that every sampling scheme has this notion of a sampling budget. This sampling budget (or sample budget) is the maximum number of samples we can afford to use. The number in our budget may depend on the computer we use, the dimensionality of the problem, or even just the amount of time we can afford running our algorithm (because these algorithms do take time!). Usually we use the notation to represent the number of samples in our sample budget, .
In time-series problems, we perform inference (using some form of Monte Carlo Estimator) at each time step. This means we use the same number of samples for each time step, i.e. the number of samples in our sampling budget, .
NOTE: People have adopted the term particle to replace the term sample. Instead of saying we have a sampling budget of 10 samples… it’s common to say we have a sampling budget of 10 particles.
So let’s now consider an example where we have a sampling budget of four, , and we are working with a small problem with five time steps, . Imagine that each sample represents the position of the ball at its respective time frame. Below we show an example where we have 4 samples at , represented by circles, . You can image that these samples represent 4 guesses of the position of the ball for the data from the video frame. Now imagine that each sample (or ball position) has an associated importance weight (just like we discussed in Importance Sampling), . The weight tells us how well the ball position matches the data in the video frame. In the figure below, I drew larger circles to represent higher importance weights and smaller circles to represent lower importance weights.
So what SMC does next is decide which samples we should keep and which ones which throw away. This is a way of saying that some guesses of the ball’s position are going to be good ones, and some will be not so good. We want to keep working with our already good guesses at the next time step, so that we can build on those guesses with other guesses. We do this by resampling. This means we will sample with replacement 4 new samples to build on in the next time step, . We resample based by their weights (we’ll go more into detail in a bit).
In the figure above, it so happened that we resampled twice and again, twice. This means that those samples for the ball’s position were really good, and we want to keep working with those samples. Now at , we sample new ball positions for that time step, , but keep into account the ball positions we resampled from the past (red notation above, ).
If we move on to , we start to notice how certain trajectories (such as the one in gray) start dying out. This means that some sequences of ball positions will NOT be used for at future time steps.
We can continue this pattern until we reach the last time step, . We will see that by then we still have 4 samples. However these 4 samples represent full trajectories, ; these samples connect back to previous time steps. Full trajectories represent entire sequences of ball movements. The ones that survived are the ones that best match our video frame data.
Now that we have described how SMC behaves, let’s be a bit more precise and dig into the math. Don’t worry, I’ll explain it as I use it.
Math Behind Sequential Monte Carlo
Let’s break this discussion into 2 parts. We’ll first discuss the Importance Sampling bit of SMC, and then we’ll discuss Importance Resampling.
Idea 1: Sequential Importance Sampling
As we previously mentioned, our samples have an associated importance weight. In a Monte Carlo estimate, an importance weight of a sample is evaluated in the same pattern we have seen before, with some unnormalized density and the proposal,
Since we are now working with time-series data, we are working with a sequence of samples. Remember that with any problem we have data we observe, , and data we are trying to learn, (sometimes referred to as states, or hypothesis). In the example above, would be the video frame data and would be the positions of the ball that we are trying to learn/predict.
So in Sequential Monte Carlo, our importance weight really looks more like this:
where represents our unnormalized density and is our proposal. These two distributions take time into consideration; meaning that the densities account for all the samples at each time step.
NOTE: is equivalent to
Let’s expand our unnormalized density using the chain rule:
This is a way of expressing the density that predicts the video frame and ball position given the previous video frames and ball positions.
And let’s do the same for our proposal distribution:
Now that we have these two expanded forms of the distribution, let’s see how they look as an importance weight.
This looks kind of messy. Let’s clean it up a bit. Remember that using chain rule, we could expand our unnormalized density as . Notice how we can solve for :
If we replace in the importance weight with the fraction above:
we can recognized that the second fraction is an importance weight for the previous time step:
which means our sequential importance weights are updated from time step to time step based on previous weights:
We can generalize using unnormalized densities if we say:
Which means we can formulate a generalized update of importance weights at each next time step:
And there you have it! This is how we do the importance sampling in sequential problems. The importance weight of current samples depend on the importance weights of previous samples.
Idea 2: Importance Resampling
If we refer back to the intuition behind SMC, you’ll remember that we pick which sequence of samples we decide to reuse for future sampling.
When we decide that a sequence of samples no longer matches our observations well enough, or aren’t strong enough explanations of the data as other sequences of samples, we stop using them (refer to grey sequences in the figure above). These sequences die out because they were not chosen to continue based on their importance weights. We can think of this process as natural selection. The best sequences will continue to the end, while others will die out on their journey to the end.
Remember that at each time step, we work with a sample budget. We’re going to change notation a bit and now say that we have a sample budget of samples. So in SMC, at each time step, we sample particles, and compute an importance weight for each.
At this point, all of the particles’ importance weights are unnormalized. This means that they do not sum to 1. So in order to choose which samples are more fit than others (i.e. have proportionally higher weights), we normalize the importance weights.
Now we can use these normalized weights in order to choose which ones will continue on. We do this by sampling particle indexes proportionally to their normalized weight by means of a discrete (commonly known as categorical) distribution.
represents a single number, 1 through , that is tells us which particle to pick. We do this K times (sample budget), and sample with replacement. This means that we can pick the same particle more than once (this usually happens with high weighted particles).
So now that we have number of numbers that are between 1 through , we pick them out!
where represents a fit sample that we will use for the next time step.
So far we have chosen which samples are fit, . Our next step is to reweigh these fit samples. Since they were all chosen according to their weight, they do not sum up to 1 anymore. In fact, their weights do not fully represent their value anymore. So how do we fix this? Well, we can do this by preserving the average of the original weights, . Let’s say that represents the average:
If we decide to set all the new weights, , of all the fit samples, to , then the average is the same!
So let’s talk about SMC some more with a sequence of question-answer pairs.
- Why do we use SMC?
We use it when we are working with time-dependent data.
- How is SMC like previous Monte Carlo Estimators we have talked about?
It’s Importance Sampling, except we update our weights with past weights, and there’s a notion of pruning out bad sequences of guesses through resampling.
- Why do we update our weights after resampling?
After resampling, we have a new collection of fit guesses/samples/particles. We do it to preserve the average of the original weights.
Thanks for reading this blog! Check out past blogs that build up to SMC.