Posted on

Learn MATLAB Episode #31: Multivariate Gaussian

In this class we’re going to talk about the multivariate Gaussian. So, in all our past lectures we’ve looked at the one-dimensional case, so one-dimensional distributions both discrete and continuous. When we talk about the multivariate Gaussian distribution we’re talking about two or more dimensions, and of course MATLAB is perfect for this because it works with all matrices and vectors. So now what happens when you extend the Gaussian like this? So the first thing is that the mean becomes a vector, and again this doesn’t really change the definition of the mean. You still add up all the values of X and divide by n to get your sample mean. So as an example let’s say we have a random matrix with 100 values and with 10 dimensions. Alright, so, if we do mean(X) we get a 1 by 10 matrix, and each component is the 100 values in that column summed up together and divided by 100. The thing that is kind of more different than the one dimensional case is the variance. So we see this Sigma symbol here and what this stands for is the covariance matrix. So, with the variance it told us how far X was spread out from the mean, the covariance does that too but only on the diagonals. So, on the off diagonals it tells us how correlated one dimension is with the other, and so you can see here the definition of the covariance matrix is the covariance of the if dimension with the jf dimension, and that would be the ij of element of the covariance matrix. We don’t really need to worry about how to calculate it because we are in the end going to end up using the matlab function cov. So let’s check that covariance is 10 by 10, so our dimensionality is 10. What’s interesting about the PDF of the Gaussian in the multi-dimensional case is that we never use the covariance directly. We use the inverse right here in the exponent and the determinant, and again since we already wrote a function to calculate Gaussians before I’m not going to ask you to do it again. There’s already a function called mvnpdf in matlab that will do this for you. So I’ve got an example here that I want to show you. We set our mean and our covariance… Don’t worry about this code too much since it is not the focus of this lecture, we’re simply setting the x-axis, so the X1 and X2 values. Note that the dimensionality of X is 2, so mu is two-dimensional and Sigma is two by two. So here we calculate PDF value using mvnpdf, and we’re going to do a surface plot of F versus X1 and X2. So now some points worth talking about is this distribution is more spread out on the X2 axis than the X1 axis, and that’s because you see here this distribution has a variance of 1 on the X2 axis, but a variance of point .25 on the X 1 axis, and since the off diagonals are not zero what we get is a Gaussian that is not perpendicular to the X1 and X2 axes.