def p_star(state, X, obs): K = state.U.shape[1] total = log_poisson(K, 1.) var_prior = distributions.InverseGammaDistribution(A, B) total += var_prior.loglik(state.ssq_U).sum() assert np.isfinite(total) U_dist = distributions.GaussianDistribution(0., state.ssq_U[nax, :]) total += U_dist.loglik(state.U).sum() assert np.isfinite(total) V_dist = distributions.GaussianDistribution(0., 1.) total += V_dist.loglik(state.V).sum() assert np.isfinite(total) pred = np.dot(state.U, state.V) X_dist = distributions.GaussianDistribution(pred, state.ssq_N) total += X_dist.loglik(X)[obs].sum() assert np.isfinite(total) return total
def cond_mu_Z(state, by_column=False): if by_column: mu = state.Z.mean(0) sigma_sq = state.sigma_sq_Z / state.Z.shape[0] * np.ones(state.Z.shape[1]) else: mu = state.Z.mean() sigma_sq = state.sigma_sq_Z / state.Z.size return distributions.GaussianDistribution(mu, sigma_sq)
def cond_u(self, i): idxs = np.where(self.obs[i, :])[0] #lam = np.dot(self.v[idxs], self.v[idxs]) / self.ssq_N + 1. / self.ssq_u v = self.v[idxs] lam = (v**2).sum() / self.ssq_N + 1. / self.ssq_u h = (self.resid[i, idxs] * v).sum() / self.ssq_N if np.abs(h / lam) < 1e-10: raise InstabilityError() return distributions.GaussianDistribution(h / lam, 1. / lam)
def make_proposal(resid, obs, ssq_N, order=None): pi = ProposalInfo(resid, obs, ssq_N) N, D = resid.shape if order is None: order = range(N) for i in order: if i == order[0]: dist = distributions.GaussianDistribution(0., 1.) else: dist = pi.cond_u(i) pi.update_u(i, dist.sample()) pi.fit_v_and_var() v = pi.cond_v().sample() ssq_u = pi.cond_ssq_u().sample() return Proposal(pi.u.copy(), v, ssq_u)
def proposal_probability(resid, obs, ssq_N, proposal, order=None): pi = ProposalInfo(resid, obs, ssq_N) N, D = resid.shape if order is None: order = range(N) total = 0. for i in order: if i == order[0]: dist = distributions.GaussianDistribution(0., 1.) else: dist = pi.cond_u(i) total += dist.loglik(proposal.u[i]) pi.update_u(i, proposal.u[i]) pi.fit_v_and_var() total += pi.cond_v().loglik(proposal.v).sum() total += pi.cond_ssq_u().loglik(proposal.ssq_u) return total
def cond_v(self): return distributions.GaussianDistribution(self.h / self.lam, 1. / self.lam)