Great! We’re done! 🎉 … Wait, why is there more text below?
The bad news is that analytical solutions as simple as that above are rarely applicable in real-world scenarios.
This mathematical dilemma has meant that Bayesian inference was, until very recently, available to only those with very powerful computers.
Nowadays, however, the average laptop can run the necessary calculations in reasonable time.
MCMC sampling
MCMC (Markov Chain Monte Carlo) sampling is a technique used to estimate probability distributions, particularly when direct calculation is difficult or impossible.
At its core, MCMC sampling works by ‘exploring’ the parameter space (the set of all possible parameter values) more or less randomly.
Having found a ‘nice spot’ (parameters with high probability density, meaning they’re more likely given the observed data and prior beliefs), most samplers will be more likely to hang around and sample a little more.
On the contrary, if the current spot is terrible, the sampler will quickly move away to try to sample elsewhere.
Each new sample depends only on the current sample, creating what is called a Markov Chain 1.
This simple rule allows us – after many, many samples – to learn about the posterior distribution without analytically solving it.
Tada! We can now approximate complex probability distributions and make inferences about parameters in statistical models which we cannot solve analytically.
The simplest of these samplers is the so-called Metropolis-Hastings algorithm2.
It is fine if you simply copy this code chunk for the final visualization.
For the curious, have a look and try to understand what it is doing – it is surprisingly straightforward!
Visualization is a cornerstone to aid interpretation in the Bayesian workflow.
Typically, we would not only plot prior and posterior predictions, but also check the MCMC sampling for issues.
We will have a look at that in a separate tutorial.