6 Multivariate Distributions
6.1 Multinomial distribution
We can draw from a multinomial distribution as follows
= 5 # number of distinct values
m = 1:m
p = p/sum(p) # a distribution on {1, ..., 5}
p = 20 # number of trials
n = rmultinom(10, n, p) # each column is a realization
out 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
= c(2, 3, 4, 3, 8) # needs to sum to n
y 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\)
= function(x, y){
dunif2 0 <= x)*(x <= 1)*(0 <= y)*(y <= 1)
(
}= seq(-1, 2, len = 100)
x = seq(-1, 2, len = 100)
y = outer(x, y, dunif2)
z 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)
= function(x, y, mu = rep(0, 2), Sigma = diag(2)){
dnorm2 = as.vector(c(x, y) - mu)
v = (2*pi*sqrt(det(Sigma)))^{-1} * exp(-(1/2) * t(v) %*% solve(Sigma) %*% v)
w as.vector(w)
}require(mvtnorm)
= function(x, y, mu = rep(0, 2), Sigma = diag(2)){
dnorm2 dmvnorm(cbind(x, y), mean = mu, sigma = Sigma)
}= seq(-4, 4, len = 100)
x = seq(-4, 4, len = 100)
y = outer(x, y, dnorm2)
z 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}\)
= matrix(c(1, 2, 2, 5), 2, 2)
M = seq(-4, 4, len = 100)
x = seq(-6, 6, len = 100)
y = outer(x, y, dnorm2, Sigma = M)
z 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)