-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastMVNDensity.cpp
33 lines (28 loc) · 1.13 KB
/
fastMVNDensity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*************************************************************************************
* The original code can be found in: http://gallery.rcpp.org/articles/dmvnorm_arma/ *
*************************************************************************************/
#include "RcppArmadillo.h"
#include "Rcpp.h"
const double log2pi = std::log(2.0 * M_PI);
/*** Calculate Multivariate Normal Density ***/
// [[Rcpp::depends("RcppArmadillo")]]
// [[Rcpp::export]]
arma::vec fastMVNDensity(arma::mat x,
arma::rowvec mean,
arma::mat sigma,
bool logd = false) {
int n = x.n_rows;
int xdim = x.n_cols;
arma::vec out(n);
arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sigma))));
double rootisum = arma::sum(log(rooti.diag()));
double constants = -(static_cast<double>(xdim)/2.0) * log2pi;
for (int i=0; i < n; i++) {
arma::vec z = rooti * arma::trans( x.row(i) - mean) ;
out(i) = constants - 0.5 * arma::sum(z%z) + rootisum;
}
if (logd == false) {
out = exp(out);
}
return(out);
}