Density of a multivariate normal distribution
1. Density of a multivariate normal distribution
In this lesson, we will learn how to calculate the density of a multivariate normal distribution using the dmvnorm() function from the mvtnorm library.2. Why calculate the density of a distribution?
Consider the measurement of sepal width from the iris dataset. Suppose we know that for each species the measurements follow a normal as shown in the plot, where the red curve is setosa, green is versicolor, and blue is virginica.3. Why calculate the density of a distribution?
Suppose we are given a new flower whose sepal width is 3 point 5 centimeters and asked to identify its species. We can simply calculate its density height from zero given by the three arrows for the three species, then choose setosa as the species, since it has the highest density height.4. Generalization of univariate normal function dnorm
The dnorm() function can be used to calculate the density height of univariate normal density at any value x. In this figure, the red line gives the density height of the univariate normal density, shown by the black curve, at x equals 1 point 5.5. Probability density of a bivariate normal
Now suppose we are given a new flower, whose sepal width is 3 point 5 centimeters and length is 4 point 5 centimeters, and asked to identify its species. Assuming the two measurements have a bivariate normal distribution, we need to calculate the height of the density surface at sepal width equals 3 point 5 and length equals 4 point 5. For illustration, the blue animated surface is the density function of a standard bivariate normal, defined by means at zero and variance-covariance matrix with diagonals 1 and 1 and off-diagonals as zeroes. The plot on the right shows the density heights at several xy coordinates, which are simply the heights of the surface at the specified xy coordinates.6. Density using dmvnorm
The density heights can be calculated using the dmvnorm() function. dmvnorm() takes three main arguments. The first argument, x, gives the coordinates of the location where we want to calculate the density. x can be a vector or matrix. The second and the third arguments specify the mean and the variance-covariance matrix. For the bivariate normal, specified by mu1 and sigma1, if x is 0 0, dmvnorm() returns a scalar, specifying the density at a single point.7. Density at multiple points using dmvnorm
If instead, x is a matrix with three rows, zero zero, one one, and zero one, then dmvnorm() returns 3 values, representing the height of the density for each row of the matrix.8. Plotting bivariate densities with perspective plot
Let's discuss how to use these density heights to plot a smoothed bivariate normal density over an x y grid. First, we create the grid locations specified by their x and y coordinates. Now, using the dmvnorm() function, we calculate the density height at every location, represented by the blue bars in the animation. This gives us a vector of density heights.9. Plotting bivariate densities with perspective plot
The persp() function takes a matrix as the primary input, so we convert the vector of density heights to a matrix, and then use the persp() function to produce the 3D density plot.10. Code for plotting bivariate densities
In this example, we create a 50 by 50 grid of xy coordinates ranging from -3 to 6 in each coordinate. The dmvnorm() function calculates the densities on this grid with the specified mean and variance-covariance matrix and stores it in dens, a vector of length 2500. The persp() function uses the dens1 matrix, obtained by converting the vector dens into a matrix of 50 rows and 50 columns. The other arguments in the persp() function specify the viewing angle, colors, and wireframe of the plot.11. Changing viewing angle in perspective plot
Changing the theta parameter to 30 rotates the viewing angle, whereas changing the phi parameter gives less of a view from above.12. Let's practice!
Let's practice using the dmvnorm() function.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.