6 Multivariate Distributions

6.1 Multinomial distribution

We can draw from a multinomial distribution as follows

m = 5 # number of distinct values
p = 1:m
p = p/sum(p) # a distribution on {1, ..., 5}
n = 20 # number of trials
out = rmultinom(10, n, p) # each column is a realization
rownames(out) = 1:m
colnames(out) = paste("Y", 1:10, sep = "")
out
  Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8 Y9 Y10
1  0  1  2  0  1  1  2  0  1   2
2  4  0  3  5  2  3  1  2  1   2
3  7  5  2  3  3  5  6  2  7   1
4  6  4  3  6  8  7  5 11  7   5
5  3 10 10  6  6  4  6  5  4  10

We can evaluate the probability of a particular draw

y = c(2, 3, 4, 3, 8) # needs to sum to n
dmultinom(y, n, p)
[1] 0.00170118

6.2 Uniform distributions

Here is the density of the uniform distribution on the unit square \([0,1]^2\)

dunif2 = function(x, y){
  (0 <= x)*(x <= 1)*(0 <= y)*(y <= 1)  
}
x = seq(-1, 2, len = 100)
y = seq(-1, 2, len = 100)
z = outer(x, y, dunif2)
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue", ltheta = 120, shade = 0.15, ticktype = "detailed")

6.3 Normal distributions

Here is the density of the standard normal distribution (perspective plot and contour plot)

dnorm2 = function(x, y, mu = rep(0, 2), Sigma = diag(2)){
  v = as.vector(c(x, y) - mu) 
  w = (2*pi*sqrt(det(Sigma)))^{-1} * exp(-(1/2) * t(v) %*% solve(Sigma) %*% v)
  as.vector(w)
}
require(mvtnorm)
dnorm2 = function(x, y, mu = rep(0, 2), Sigma = diag(2)){
  dmvnorm(cbind(x, y), mean = mu, sigma = Sigma)
}
x = seq(-4, 4, len = 100)
y = seq(-4, 4, len = 100)
z = outer(x, y, dnorm2)
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue", ltheta = 120, shade = 0.15, ticktype = "detailed")

filled.contour(x, y, z, asp = 1)

And here is the density of the normal distribution with mean zero and covariance matrix \(\begin{pmatrix}1 & 2 \\ 2 & 5\end{pmatrix}\)

M = matrix(c(1, 2, 2, 5), 2, 2)
x = seq(-4, 4, len = 100)
y = seq(-6, 6, len = 100)
z = outer(x, y, dnorm2, Sigma = M)
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue", ltheta = 120, shade = 0.15, ticktype = "detailed")

filled.contour(x, y, z, asp = 1)