-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_sigma.cpp
More file actions
45 lines (40 loc) · 1.05 KB
/
update_sigma.cpp
File metadata and controls
45 lines (40 loc) · 1.05 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
39
40
41
42
43
44
45
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace arma;
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector update_sigma(arma::mat Theta, arma::mat Y, arma::mat M, arma::vec lambda, int nu){
int n = Y.n_cols;
int K = Y.n_rows;
int R = Theta.n_cols;
double N = n;
arma::vec Den(n);
arma::mat Thetasq(n, R);
arma::mat Residual(K, n);
arma::mat Res_sq(K, n);
arma::mat Tmp(n, K);
//rowvec scale;
//rowvec sigma(K);
arma::rowvec scale;
arma::rowvec sigma(K);
for (int j = 0; j < R; j++){
Thetasq.col(j) = pow(Theta.col(j), 2);
}
for (int i = 0; i < n; i++){
Den(i) = sum(Thetasq.row(i));
}
Residual = Y - M*arma::trans(Theta);
for(int i = 0; i < K; i++){
Res_sq.row(i) = pow(Residual.row(i),2);
}
for(int i = 0; i < n; i++){
for(int j = 0; j < K; j++){
Tmp(i,j) = Res_sq(j,i)/Den(i);
}
}
scale = (nu*lambda)/(N + nu) + (1/(N + nu))*(sum(Tmp));
for (int k = 0; k < K; k++){
sigma(k) = ((N + nu)*scale(k))/(R::rchisq(N + nu));
}
return (wrap(sigma));
}