Skip to content
Prev 42691 / 63421 Next

Conditional means and variances of a multivariate normal distribution

Hi,

My previous version of the conditional MVN function had a bug in that it would not work when conditional distribution was required for a single variable. I fixed this and also made a few minor changes. Here is the new version. 

condNormal <- function(x.given, mu, sigma, given.ind, req.ind){
# Returns conditional mean and variance of x[req.ind] 
# Given x[given.ind] = x.given
# where X is multivariate Normal with
# mean = mu and covariance = sigma
# 
B <- sigma[req.ind, req.ind]
C <- sigma[req.ind, given.ind, drop=FALSE]
D <- sigma[given.ind, given.ind]
CDinv <- C %*% solve(D)
cMu <- c(mu[req.ind] + CDinv %*% (x.given - mu[given.ind]))
cVar <- B - CDinv %*% t(C)
list(condMean=cMu, condVar=cVar)
}

n <- 10
A <- matrix(rnorm(n^2), n, n)
A <- A %*% t(A)
condNormal(x=c(1,1,0,0,-1), mu=rep(1,n), sigma=A, req=c(2,3,5), given=c(1,4,7,9,10))
condNormal(x=c(1,1,0,0,-1), mu=rep(1,n), sigma=A, req=2, given=c(1,4,7,9,10))

As far as I know, there is nothing related to multivariate normal distributions in "stats".  Hence, it seems like this function might be more useful in a contributed package such as "fMultivar" or "mvtnorm".

Best,
Ravi.