From rvaradhan at jhmi.edu  Sun Mar  4 16:32:53 2012
From: rvaradhan at jhmi.edu (Ravi Varadhan)
Date: Sun, 4 Mar 2012 15:32:53 +0000
Subject: [Rd] Conditional means and variances of a multivariate normal
 distribution
Message-ID: <2F9EA67EF9AE1C48A147CB41BE2E15C3196A25@DOM-EB-MAIL2.win.ad.jhu.edu>

Hi,



Let X = (x_1, x_2, ... ,  x_p) be multivariate normal with mean, mu = (mu_1, ... , mu_p) and covariance = Sigma.  I was looking for an R function to compute conditional mean and conditional variance of a given subset of X given another subset of X.  While this is trivially easy to do, there is nothing in "base" for doing this, at least nothing that I am aware of.  I am also not aware of anything in the contributed packages (although my search was not comprehensive).  I feel that this would be a useful addition, if it is not already there.  I have written this following function, which I am sure can be improved a lot (including better argument names!). I would like to hear your thought on this.



condNormal <- function(x.given, mu, sigma, req.ind, given.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]

D <- sigma[given.ind, given.ind]

cMu <- drop(mu[req.ind] + C %*% solve(D) %*% (x.given - mu[given.ind]))

cVar <- B - C %*% solve(D) %*% 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))



Best regards,

Ravi



[Rd] Conditional means and variances of a multivariate normal distribution
Ravi Varadhan