I’ve become really interested in strategies for sampling from intractable probability distributions, especially MCMC. Not only does sampling crop up all over machine learning but I also find the problem intrinsically intellectually appealing. Sampling is deceptively difficult - a very easy problem to state but hugely complex to solve.

Recently my attention has been caught by MCMC strategies that involve learning how to sample. There have been a couple of papers in this vein: Generalising Hamiltonain Monte Carlo with Neural Nets and A-NICE-MC are both excellent examples.

In this post I will:

  • Describe the problem MCMC is trying to solve.
  • Recap the basics of Metropolis Hastings.

Next post:

  • Delve into some of the cutting edge strategies for learning samplers.

The Problem

Concretely the problem that frequently crops up in ML is either:

  • Draw samples from a probability distribution or
  • Compute the expectation

where the distribution is very complicated. Often we only know up to some normalisation constant and we can only evaluate at a point.

This problem comes up in inference when marginalising over variables, learning with intractable normalisers and in model comparison when computing the model evidence.

One Concrete Example - Bayesian Deep learning

I find it helps to have a motivating example in mind. One cool example of MCMC that bridges the worlds of traditional probabilistic learning and Deep Learning is sampling the posterior over the weights of a neural network. I think Radford Neal was one of the first to notice the opportunity here and applied an MCMC algorithm called Hamiltonian Montecarlo to the problem and got excellent results.

Imagine your using a neural net for a regression task. Given an input your neural net outputs a prediction . Typically neural nets are trained by optimising a cost function, maybe with some kind of regulariser:

Another equivalent view, is that this cost function corresponds to MAP inference in the following probabilistic model. The likelihood is just a gaussian whose mean is :

and the prior is:

and the data are assumed to be drawn i.i.d. In this case our cost function is equivalent to:

Rather than doing MAP inference in this model we could actually draw samples from the posterior over model parameters, . If we do this, not only will our model predictions likely be more accurate than just taking the MAP but we can also calculate uncertainty estimates for all of our predictions. Our predictions now become:

where:

and our uncertainty becomes:

Being able to estimate uncertainty is incredibly important when deploying deep learning in the real world. For example when used in medical diagnosis, we may not want to trust our deep learning model when the uncertainty is high.

In order to get these uncertainty estimates we need to able to sample from . This is far from trivial.

What makes this so hard?

Sampling requires global information but if we can only evaluate the density point-wise, our information is inherently local. By definition, to be able to draw samples from a distribution we need samples on average to come from places that contain a large fraction of the total probability mass. However, knowing the fraction of probability mass in a given region is an inherently global property. We need to know not just how big the unnormalised density is around a point of interest but how this compares to other parts of this space. Since we can only evaluate the density locally, we have to somehow combine sparse local information to get a picture of the whole. This becomes increasingly hard as the dimensionality of the problem grows.

Markov Chain Monte Carlo (MCMC) and Metropolis-Hastings

Markov Chain Monte Carlo is a strategy for approximately solving the sampling problem. MCMC works by simulating a particle roaming around the sampling space in such a way that, in the limit of infinite exploration, the amount of time it spends in each part of the space is proportional to the probability of that part of the space. In practice a good approximation can be achieved after a finite amount of exploration.

How do we run the simulation?

We initialise our particle at some random point and then sample from a carefully chosen proposal distribution . As long as we are judicious in our choice of proposal we can prove that the particle will eventually be a collection of samples from the target distribution .

In order for this to work we need our Markov transition distribution to have two very important properties. First we need to know that our target distribution is a fixed point of this transition operator. i.e. We need to know that if we sample a point from and then repeatedly sample from that the distribution over our samples will marginally still be . Formally we require:

One sufficient (but not necessary) condition for this to be true is that satisfies detailed balance:

If detailed balance holds than we are guaranteed that the target distribution is a fixed point. You can see this by substituting the condition into the stationarity equation. (The downside of detailed balance is that we’re always as likely to go forward in our simulation as we are to go backwards and this slows us down)

Once we’ve established that is a fixed point of our operator, we need to know that no matter where we start we will end up at this fixed point. That is, we need our chain of samples to be “ergodic”. We can guarantee ergodicity by making sure that no point in the sample space is visited with a fixed period and no parts of the sample space are inaccessible from each other.

Metropolis-Hastings

The Metropolis-Hastings algorithm is a strategy for shoe-horning any Markov proposal distribution into one that has the above desired properties. The way that this is done is to introduce an accept-reject step at each stage of the simulation. Roughly we sample from any Markovian transition operator and then either accept that point as our next sample or reject it, depending on how likely the proposed point is under our target distribution. Concretely we sample and then accept or reject with probability:

The transition from this combined process of sampling and rejecting is then:

and this transition has the property, that NO MATTER what is we have satisfied detailed balance! This is because (assuming w.l.o.g the acceptance fraction is less than 1):

and

so detailed balance holds.

The power of Metropolis-Hastings is that it gives us enormous flexibility over our choice of proposal distribution whilst still guaranteeing that we’ll eventually get accurate samples. This is why it forms the back-bone of many popular sampling algorithms including the learning to sample methods that I will talk about in my next post but also Gibbs sampling, Hamiltonain Monte-Carlo, NUTS and Slice sampling.

However, if we make a poor choice of we will find that the probability of accepting a new point is very low and it’ll take a very long time for our simulation to converge to the distribution of interest. Also, we shoe-horn into a valid transition operator by enforcing detailed balance and as was mentioned before, detailed balance can seriously slow down the generation of samples.

Summary

MCMC is one very powerful technique to solving the sampling problem. I personally find it pretty amazing that a single point particle wandering around a high-dimensional sampling space can gather enough information to give us a representative summary of the overall distribution.

Whilst Metropolis-Hastings represents a good starting place for building sampling algorithms its performance depends heavily on the choice of proposal distribution. MCMC only gives us exact samples in the limit of infinite time, how good an approximation we get in finite time is very variable and actually quite hard to even assess.

There are many variants of MCMC algorithms but a lot of them can be viewed as Metropolis-Hastings with different carefully crafted choices for the proposal distribution . One strategy that’s recently been proposed has been to parameterise and somehow learn the parameters of to provide efficient sampling for a given distribution.