multivariate_density = function(x, mean, cov_matrix) {
x_demeaned = x - mean
x_demeaned = as.matrix(x_demeaned)
inv_matrix <- solve(cov_matrix)
mahalanobis_distance = apply(x_demeaned, 1, function(x) {
t(x) %*% inv_matrix %*% x
})
ev <- eigen(cov_matrix, symmetric = TRUE, only.values = TRUE)$values
logdet <- sum(log(ev))
if(is.null(ncol(x))) {
variables = length
} else {
variables = ncol(x)
}
logretval <- -(variables * log(2 * pi) + logdet + mahalanobis_distance)/2
return(exp(logretval))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.