From c1cea00cd31fe9231518abdc34c4772e4243eca9 Mon Sep 17 00:00:00 2001 From: Kasper Kristensen Date: Wed, 17 Apr 2024 15:15:26 +0200 Subject: [PATCH] dmvnorm / dgmrf: mu must be a vector corresponding to Sigma / Q #24 --- RTMB/R/mvgauss.R | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/RTMB/R/mvgauss.R b/RTMB/R/mvgauss.R index a5c846c..5695e78 100644 --- a/RTMB/R/mvgauss.R +++ b/RTMB/R/mvgauss.R @@ -7,6 +7,10 @@ ##' @param scale Extra scale parameter - see section 'Scaling'. ##' @return Vector of densities. dmvnorm <- function(x, mu=0, Sigma, log=FALSE, scale=1) { + if (!identical(mu, 0)) { + p <- length(mu) + if (!all(dim(Sigma) == c(p, p))) stop("incompatible arguments") + } if (!unit(scale)) { return (dscale("dmvnorm", x, mu, Sigma, log=log, scale=scale, vectorize=TRUE)) @@ -43,6 +47,10 @@ dmvnorm <- function(x, mu=0, Sigma, log=FALSE, scale=1) { ##' @details The function `dgmrf()` is essentially identical to `dmvnorm()` with the only difference that `dgmrf()` is specified via the *precision* matrix (inverse covariance) assuming that this matrix is *sparse*. ##' @param Q Sparse precision matrix dgmrf <- function(x, mu=0, Q, log=FALSE, scale=1) { + if (!identical(mu, 0)) { + p <- length(mu) + if (!all(dim(Sigma) == c(p, p))) stop("incompatible arguments") + } if (!unit(scale)) { return (dscale("dgmrf", x, mu, Q, log=log, scale=scale, vectorize=TRUE))