void maximization(llna_model* model, llna_ss* ss) { int i, j; double sum; // mean maximization for (i = 0; i < model->k-1; i++) vset(model->mu, i, vget(ss->mu_ss, i) / ss->ndata); // covariance maximization for (i = 0; i < model->k-1; i++) { for (j = 0; j < model->k-1; j++) { mset(model->cov, i, j, (1.0 / ss->ndata) * (mget(ss->cov_ss, i, j) + ss->ndata * vget(model->mu, i) * vget(model->mu, j) - vget(ss->mu_ss, i) * vget(model->mu, j) - vget(ss->mu_ss, j) * vget(model->mu, i))); } } if (PARAMS.cov_estimate == SHRINK) { cov_shrinkage(model->cov, ss->ndata, model->cov); } matrix_inverse(model->cov, model->inv_cov); model->log_det_inv_cov = log_det(model->inv_cov); // topic maximization for (i = 0; i < model->k; i++) { sum = 0; for (j = 0; j < model->log_beta->size2; j++) sum += mget(ss->beta_ss, i, j); if (sum == 0) sum = safe_log(sum) * model->log_beta->size2; else sum = safe_log(sum); for (j = 0; j < model->log_beta->size2; j++) mset(model->log_beta, i, j, safe_log(mget(ss->beta_ss, i, j)) - sum); } }
rowvec laNRb(const rowvec &data, const mat &iS, const double &detS, const rowvec &mu0, const rowvec &mu1, const rowvec &mu2, const rowvec &lambda0, const rowvec &lambda1, const rowvec &lambda2, const rowvec &beta0, const rowvec &beta1, const rowvec &beta2, const rowvec &gamma, const rowvec &gamma2, const double &Dtol, const unsigned &niter, const double &lambda) { rowvec eta = zeros(1,3); for (unsigned i=0; i<niter; i++) { hObj K = hb(eta,data,iS, mu0,mu1,mu2,lambda0,lambda1,lambda2,beta0,beta1,beta2,gamma,gamma2); double Sabs = as_scalar(trans(K.grad)*K.grad); if (Sabs<Dtol) break; // mat Delta = trans(K.grad)*inv(K.hess + 0.1*eye(K.hess.n_cols,K.hess.n_cols)); mat Delta = trans(K.grad)*inv(K.hess); eta = eta-lambda*Delta; // hObj K = h(eta1,data,iS, // mu1,mu2,lambda1,lambda2,beta,gamma); } hObj K = hb(eta,data,iS, mu0,mu1,mu2,lambda0,lambda1,lambda2,beta0,beta1,beta2,gamma,gamma2); // cerr << "K.grad=" << K.grad << endl; double logHdet; double sign; log_det(logHdet,sign,K.hess); // log(det(-K.hess)) if (std::isnan(logHdet)) logHdet = -1000; double logI = K.hSh - 0.5*(logHdet+log(detS)); // double logI = K.hSh - 0.5*log(detS); // cerr << "logI" << logI << endl; // cerr << "hess" << -K.hess << endl; // cerr << "hSh" << -K.hSh << endl; rowvec res(4); res(0) = logI; for (unsigned i=0; i<3; i++) res(i+1) = eta(i); return(res); }
double log_like_plus_log_prior(vec y, mat X, vec param, mat I, vec priors) { double ls2 = param[0]; double w = param[1]; double ltau = param[2]; vec d_vec = param.tail( param.size()-3 ); vec d2 = d_vec % d_vec; mat D_mat = xDx(X,d2); // ORIGINAL and best so far //mat D_mat = xDx(X,d2/sum(d2)); double a_s2 = priors[0];//2; double b_s2 = priors[1];//5; double a_phi = priors[2];//0; double b_phi = priors[3];//5; double a_tau = priors[4];//2; double b_tau = priors[5];//5; double a_d = priors[6]; //1; double b_d = priors[7]; //1; double s2 = exp(ls2); double phi = (b_phi*exp(w) + a_phi) / (exp(w)+1); // inverse logit double tau = exp(ltau); mat K = tau * exp(-phi*D_mat); double ldet_K,sign; mat s2I_plus_K = s2*I + K; log_det(ldet_K, sign, s2I_plus_K); double sum_log_dj_prior = sum( (-b_d-1) * log(1+abs(d_vec) / (a_d * b_d)) ); double log_prior = ( w-2*log(exp(w)+1) ) - a_s2*ls2 - b_s2/s2 - a_tau*ltau - b_tau/tau + sum_log_dj_prior; double log_like = (-.5 * ldet_K - .5 * y.t() * s2I_plus_K.i() * y)[0]; return log_like + log_prior; }
//Calculation of BICreg List Vect::bicReggen(vector<int> vectH, vector<int> vectY, int numr) { double reg = 0.0, sign, val; // Ici, H est la matrice réponse. mat H=Vect::const_matrix(vectH); int n = H.n_rows,v = H.n_cols; //construction of the matrix X int a; if (vectY.empty()) a=0; else a=vectY.size(); mat X(n,a+1); if (vectY.empty()) X.col(0) = ones<colvec>(n); else { mat Y = Vect::const_matrix(vectY); Y.insert_cols(0, ones<colvec>(n)); X = Y; } //Parameter estimation mat XtX = X.t() * X; mat B = inv_sympd(XtX) * X.t() *H; //mat B = pinv(XtX) * X.t() *H; double lambda; mat A=X*B; if (numr==3) //(r=[LC]) { mat Omega = (1.0/n)*(H.t()*(H-A)); Omega = 2*M_PI*Omega; log_det(val, sign, Omega); double det = log(sign*exp(val)); lambda = ((a+1)*v) + (0.5*v*(v+1)); reg = (-n*det)-(n*v)-(lambda*log(n)); } if (numr==2) //(r=[LB]) { mat H_A = (1.0/n)*(H - A)%(H - A); rowvec sigma2 = sum(H_A, 0); sigma2 = log(sigma2); lambda=(v*(a+1)) +v; reg=-(n*v*log(2*M_PI)) - (n* sum(sigma2)) -(n*v) - (lambda*log(n)); } if (numr==1) //(r=[LI]) { mat Aux=H-A; double sigma=(1.0/(n*v))* accu(Aux % Aux); lambda=(v*(a+1)) + 1; reg=-(n*v*log(2*M_PI*sigma)) -(n*v) - (lambda*log(n)); } return List::create(Named("bicvalue") = reg, Named("B") = B); }//end Vect::bicReggen