double BLSSS::log_model_prob(const Selector &g)const{ // borrowed from MLVS.cpp double num = vpri_->logp(g); if(num==BOOM::negative_infinity() || g.nvars() == 0) { // If num == -infinity then it is in a zero support point in the // prior. If g.nvars()==0 then all coefficients are zero // because of the point mass. The only entries remaining in the // likelihood are sums of squares of y[i] that are independent // of g. They need to be omitted here because they are omitted // in the non-empty case below. return num; } SpdMatrix ivar = g.select(pri_->siginv()); num += .5*ivar.logdet(); if(num == BOOM::negative_infinity()) return num; Vector mu = g.select(pri_->mu()); Vector ivar_mu = ivar * mu; num -= .5*mu.dot(ivar_mu); bool ok=true; ivar += g.select(suf().xtx()); Matrix L = ivar.chol(ok); if(!ok) return BOOM::negative_infinity(); double denom = sum(log(L.diag())); // = .5 log |ivar| Vector S = g.select(suf().xty()) + ivar_mu; Lsolve_inplace(L,S); denom-= .5*S.normsq(); // S.normsq = beta_tilde ^T V_tilde beta_tilde return num-denom; }
int main(int argc, char **argv) { SpdMatrix *pMat; /* * 1st test */ pMat = new SpdMatrix(4, 0); pMat->setEntry(0, 0, 9); pMat->setEntry(0, 1, 3); pMat->setEntry(0, 2, -6); pMat->setEntry(0, 3, 12); pMat->setEntry(1, 1, 26); pMat->setEntry(1, 2, -7); pMat->setEntry(1, 3, -11); pMat->setEntry(2, 2, 9); pMat->setEntry(2, 3, 7); pMat->setEntry(3, 3, 65); cout << "Original matrix:" << endl; pMat->print(); SquareMatrix l(pMat->chol()); // L matrix should be: // 3 0 0 0 // 1 5 0 0 // -2 -1 2 0 // 4 -3 6 2 cout << "L matrix:" << endl; l.print(); delete pMat; }
Vector rmvn_ivar_mt(RNG &rng, const Vector &mu, const SpdMatrix &ivar) { // Draws a multivariate normal with mean mu and precision matrix // ivar. bool ok = false; Matrix U = ivar.chol(ok).transpose(); if (!ok) { report_error("Cholesky decomposition failed in rmvn_ivar_mt."); } return rmvn_precision_upper_cholesky_mt(rng, mu, U); }
Matrix chol(const SpdMatrix &S, bool & ok){return S.chol(ok);}
Matrix chol(const SpdMatrix &S){ return S.chol();}
Vector rmvn_mt(RNG &rng, const Vector &mu, const SpdMatrix &V) { bool okay = true; Matrix L = V.chol(okay); if (okay) return rmvn_L_mt(rng, mu, L); return rmvn_robust_mt(rng, mu, V); }