I am attempting to differentiate the log likelihood function of a multivariate normal distribution,
NLLmvnorm <- function(data, mu, sigma) {
a <- qr(sigma)
logdet <- sum(log(abs(diag(a$qr))))
sigma.inv <- fast.ginv(sigma)
-0.5 * (logdet + t(data-mu) %*% sigma.inv %*% (data-mu))
}
Using D
, I differentiate with respect to mu
and sigma
and store each result in a separate variable:
du <- D(expression(NLLmvnorm),"mu")
ds <- D(expression(NLLmvnorm),"sigma")
However, both of these results return 0
. Am I doing something incorrect, or am I misinterpreting what D
does? If it helps, my goal is to find the roots of du
and ds
to find maximum likelihood using solve
.