[R] The gradient of a multivariate normal density with respect to its parameters

Karl Ove Hufthammer Karl.Hufthammer at math.uib.no
Mon Jun 22 16:09:45 CEST 2009


Does anybody know of a function that implements the derivative (gradient) of 
the multivariate normal density with respect to the *parameters*?

It’s easy enough to implement myself, but I’d like to avoid reinventing the 
wheel (with some bugs) if possible. Here’s a simple example of the result 
I’d like, using numerical differentiation:

library(mvtnorm)
library(numDeriv)
f=function(pars, xx, yy)
{
  mu=pars[1:2]
  sig1=pars[3]
  sig2=pars[4]
  rho=pars[5]
  sig=matrix(c(sig1^2,rho*sig1*sig2,rho*sig1*sig2,sig2^2),2)
  dmvnorm(cbind(x,y),mu,sig)
}

mu1=1
mu2=2
sig1=3
sig2=4
rho=.5
x=2 # or a x vector
y=3 # or a y vector
jacobian(f,c(mu1,mu2,sig1,sig2,rho),xx=x,yy=y)
# (Can replace ‘jacobian’ with ‘grad’ if x and y have length 1.)

-- 
Karl Ove Hufthammer




More information about the R-help mailing list