-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogLikelihood.cpp
More file actions
38 lines (37 loc) · 1.43 KB
/
Copy pathlogLikelihood.cpp
File metadata and controls
38 lines (37 loc) · 1.43 KB
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
34
35
36
37
38
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
double logLikelihood_cpp(rowvec alpha, rowvec beta, double sigma, vec e,
bool link, int fz_idx, double z_mu, double z_sigma, vec pi, vec mu,
vec xi, vec y, mat preds_all, int k1, int k2){
int n = y.n_rows;
int p = alpha.n_cols;
int p_all = preds_all.n_rows;
arma::mat x = preds_all.rows(0,p-1); //(p, n)
arma::rowvec z = alpha * x;
arma::vec f_z(n);
double logL;
if (p < p_all) {
arma::mat w = preds_all.rows(p, p_all - 1); // (L, n)
z = alpha * x + beta * w; //(1, n)
} else {
z = alpha * x;
}
if (!link){
for (int i=0; i < n; i++){
arma::vec pdfs = arma::normcdf(z(i), mu, sqrt(xi));
f_z(i) = sum(pi % pdfs);
}
} else {
if (fz_idx == 0){
f_z = 1 - exp(-(z.st() + z_mu) * z_sigma);
} else if (fz_idx == 1){
f_z = arma::normcdf(z.st(), z_mu, z_sigma);
}
}
arma::vec kernel = square(f_z) / e + 2 * (k1 - y / e) % f_z;
logL = - sum(kernel) / (2 * k2 * sigma);
return logL;
}