Discussion: November 7th
Deadline: November 6th, 18:00
In this assignment, we will be investigating Monte Carlo Methods with a few simple examples. There is a lot of text for explanation, but the actual tasks are rather compact.
Some starter code can be found on Gitlab! Please see assignment02_starter.ipynb.
We first want to practice running a Markov chain for a simple example. This is easiest when you have a small number of discrete states (numbered 0 to n), and a given transition matrix (we call this A) giving the probabilities to move from one state to the next. The starter notebook already defines such a matrix, but you can make your own if you like. The one requirement is that each colum sums up to 1, as a column gives the probabilities to move from a given state to others.
Now, run a Markov chain:
np.random.choice.
You don’t need torch for this first part at all.To go a little bit deeper, we consider some theoretical analysis in section 17.3 of the deep learning book. Try the following (you don’t have to read the section):
Does the vector v’ match the empirical distribution computed above, when you actually ran a Markov chain? If not, you might need to run the chain for more steps – or you made a mistake somewhere. :)
In most of our use cases, the situation is not as in the example above: We don’t have a transition distribution given and just start running a Markov chain on it to get some marginal distribution. Instead, we have a desired target distribution (e.g. our model distribution) and need to figure out how to get there, i.e. how to sample from it.
Let’s try to sample from a mixture of Gaussians via Gibbs sampling.
torch.distributions to build the distributions.
You will likely want to use MixtureSameFamily of Normal distributions.torch.distributions to sample from the conditional, one-dimensional, mixtures of Gaussians as mentioned
above.
You might say:
Why not just sample from a two-dimensional mixture of Gaussians in the first place?
The reason we don’t do this is that this is a contrived example to show the concept of Gibbs sampling. ;)Note that many of the steps outlined here have already been implemented in the starter notebook. You don’t need to do much more!
You should collect a reasonable number of samples (1000 or more) and plot both the target distribution (mixture of Gaussians) and your samples. Do the samples reflect the distribution well? In particular are both modes of the Gaussian mixture covered equally? You can do this visually and/or using statistics. Also, experiment with different locations/scales for the Gaussians. That is, move the components further apart or closer together and repeat the sampling process each time. The quality of the samples should vary dramatically based on the distance between components!