def sample_delta_i(self, delta, r, zeta, sigma, alpha, beta, xi, tau, eta, i): _delta, _zeta, _sigma = self.clean_delta(delta, zeta, sigma, i) _dmax = _delta.max() njs = cu.counter(_delta, _dmax + 1 + self.m) ljs = njs + (njs == 0) * eta / self.m _zeta_new, _sigma_new = self.sample_zeta_sigma_new( alpha, beta, xi, tau, self.m) zeta_stack = np.vstack((_zeta, _zeta_new)) sigma_stack = np.vstack((_sigma, _sigma_new)) assert (zeta_stack.shape[0] == ljs.shape[0]) args = zip( repeat(r[i] * self.data.V[i]), zeta_stack, sigma_stack, ) res = map(logdgamma_wrapper, args) lps = np.array(list(res)) lps[np.where(np.isnan(lps))[0]] = -np.inf lps -= lps.max() unnormalized = np.exp(lps) * ljs normalized = unnormalized / unnormalized.sum() dnew = choice(range(zeta_stack.shape[0]), 1, p=normalized) if dnew > _dmax: zeta = np.vstack((_zeta, zeta_stack[dnew])) sigma = np.vstack((_sigma, sigma_stack[dnew])) delta = np.insert(_delta, i, _dmax + 1) else: delta = np.insert(_delta, i, dnew) zeta = _zeta.copy() sigma = _sigma.copy() return delta, zeta, sigma
def sample_delta_i(self, delta, r, alpha, beta, eta, mu, Sigma_chol, i): _delta, _alpha, _beta = self.clean_delta(delta, alpha, beta, i) _dmax = _delta.max() njs = cu.counter(_delta, _dmax + 1 + self.m) ljs = njs + (njs == 0) * (eta / self.m) alpha_new, beta_new = self.sample_alpha_beta_new(mu, Sigma_chol, self.m) alpha_stack = np.vstack((_alpha, alpha_new)) beta_stack = np.vstack((_beta, beta_new)) assert (alpha_stack.shape[0] == ljs.shape[0]) Y = self.data.Yl[i] * r[i] args = zip(repeat(Y), alpha_stack, beta_stack) lps = np.array(list(map(log_density_delta_i, args))) * self.inv_temper_temp lps[np.where(np.isnan(lps))] = - np.inf lps -= lps.max() unnormalized = np.exp(lps) * ljs normalized = unnormalized / unnormalized.sum() dnew = choice(range(_dmax + self.m + 1), 1, p = normalized) if dnew > _dmax: _alpha_ = np.vstack((_alpha, alpha_stack[dnew])) _beta_ = np.vstack((_beta, beta_stack[dnew])) _delta_ = np.insert(_delta, i, _dmax + 1) else: _delta_ = np.insert(_delta, i, dnew) _alpha_ = _alpha.copy() _beta_ = _beta.copy() return _delta_, _alpha_, _beta_
def generate_posterior_predictive_simplex_3d(self, cols, m = 20): keep_idx = np.array(sample(range(self.nSamp), self.nDat), dtype = int) delta = self.samples.delta[keep_idx] alpha = [self.samples.alpha[i][:,cols] for i in keep_idx] beta = [self.samples.beta[i][:,cols] for i in keep_idx] eta = self.samples.eta[keep_idx] mu = self.samples.mu[keep_idx][:,cols] Sigma = self.samples.Sigma[keep_idx][:,cols][:,:,cols] postpred = np.empty((self.nDat, len(cols))) for n in range(self.nDat): dmax = delta[n].max() njs = cu.counter(delta[n], dmax + 1 + m) ljs = njs + (njs == 0) * eta[n] / m new_log_alpha = mu[n].reshape(1,-1) + \ (cholesky(Sigma[n]) @ normal.rvs(size = (len(cols), m))).T new_alpha = np.exp(new_log_alpha) if cols[0] == 0: new_beta = np.hstack(( np.ones((m, 1)), gamma.rvs(a = 2., scale = 1/2., size = (m, len(cols) - 1)) )) else: new_beta = gamma.rvs(a = 2., scale = 1/2, size = (m, len(cols))) prob = ljs / ljs.sum() a = np.vstack((alpha[n], new_alpha)) b = np.vstack((beta[n], new_beta)) delta_new = np.random.choice(range(dmax + 1 + m), 1, p = prob) postpred[n] = gamma.rvs(a = a[delta_new], scale = 1 / b[delta_new]) np.nan_to_num(postpred, copy = False) return (postpred.T / postpred.sum(axis = 1)).T
def generate_posterior_predictive_gammas(self, n_per_sample=1, m=10): new_gammas = [] for s in range(self.nSamp): dmax = self.samples.delta[s].max() njs = cu.counter(self.samples.delta[s], int(dmax + 1 + m)) ljs = njs + (njs == 0) * self.samples.eta[s] / m new_zetas = gamma( shape=self.samples.alpha[s], scale=1. / self.samples.beta[s], size=(m, self.nCol), ) new_sigmas = np.hstack(( np.ones((m, 1)), gamma( shape=self.samples.xi[s], scale=self.samples.tau[s], size=(m, self.nCol - 1), ), )) prob = ljs / ljs.sum() deltas = cu.generate_indices(prob, n_per_sample) zeta = np.vstack((self.samples.zeta[s], new_zetas))[deltas] sigma = np.vstack((self.samples.sigma[s], new_sigmas))[deltas] new_gammas.append(gamma(shape=zeta, scale=1 / sigma)) return np.vstack(new_gammas)
def sample_delta_i(self, delta, zeta, r, alpha, beta, eta, i): _delta, _zeta = self.clean_delta(delta, zeta, i) _dmax = _delta.max() njs = cu.counter(_delta, _dmax + 1 + self.m) ljs = njs + (njs == 0) * eta / self.m _zeta_new = self.sample_zeta_new(self.m, alpha, beta) zeta_stack = np.vstack((_zeta, _zeta_new)) assert (zeta_stack.shape[0] == ljs.shape[0]) args = zip( repeat(r[i] * self.data.Yl[i]), zeta_stack, ) # res = self.pool.map(log_density_delta_i, args, chunksize = ceil(ljs.shape[0] / 8.)) res = map(log_density_delta_i, args) lps = np.array(list(res)) lps[np.where(np.isnan(lps))[0]] = -np.inf unnormalized = np.exp(lps) * ljs normalized = unnormalized / unnormalized.sum() dnew = choice(range(_dmax + 1 + self.m), 1, p=normalized) if dnew > _dmax: zeta = np.vstack((_zeta, zeta_stack[dnew])) delta = np.insert(_delta, i, _dmax + 1) else: delta = np.insert(_delta, i, dnew) zeta = _zeta.copy() return delta, zeta
def sample_delta_i(self, deltas, mus, Sigmas, mu0, Sigma0, eta, i): _deltas, _mus, _Sigmas = self.clean_delta(deltas, mus, Sigmas, i) _dmax = _deltas.max() njs = cu.counter(_deltas, _dmax + 1 + self.m) ljs = njs + (njs == 0) * eta / self.m mus_new, Sigmas_new = self.sample_mu_Sigma_new(mu0, Sigma0, self.m) mu_stack = np.vstack((_mus, mus_new)) Sigma_stack = np.vstack((_Sigmas, Sigmas_new)) assert (mu_stack.shape[0] == ljs.shape[0]) args = zip(repeat(self.data.pVi[i]), mu_stack, Sigma_stack) res = self.pool.map(log_density_mvnormal, args, chunksize=ceil(ljs.shape[0] / 8)) lps = np.array(list(res)) lps[np.where(np.isnan(lps))] = -np.inf lps -= lps.max() unnormalized = np.exp(lps) * ljs normalized = unnormalized / unnormalized.sum() dnew = choice(range(_dmax + self.m + 1), 1, p=normalized) if dnew > _dmax: mu = np.vstack((_mus, mu_stack[dnew])) Sigma = np.vstack((_Sigmas, Sigma_stack[dnew])) delta = np.insert(_deltas, i, _dmax + 1) else: delta = np.insert(_deltas, i, dnew) mu = _mus.copy() Sigma = _Sigmas.copy() return delta, mu, Sigma
def clean_delta(self, delta, alpha, i): assert (delta.max() + 1 == alpha.shape[0]) _delta = np.delete(delta, i) nj = cu.counter(_delta, _delta.max() + 2) fz = cu.first_zero(nj) _alpha = alpha[np.where(nj > 0)[0]] if (fz == delta[i]) and (fz <= _delta.max()): _delta[_delta > fz] = _delta[_delta > fz] - 1 return _delta, _alpha
def clean_delta(self, deltas, mus, Sigmas, i): assert (deltas.max() + 1 == mus.shape[0]) _deltas = np.delete(deltas, i) nj = cu.counter(_deltas, _deltas.max() + 2) fz = cu.first_zero(nj) _mus = mus[np.where(nj > 0)[0]] _Sigmas = Sigmas[np.where(nj > 0)[0]] if (fz == deltas[i]) and (fz <= _deltas.max()): _deltas[_deltas > fz] = _deltas[_deltas > fz] - 1 return _deltas, _mus, _Sigmas
def generate_posterior_predictive_gammas(self, n_per_sample = 10, m = 20): new_gammas = [] for i in range(self.nSamp): dmax = self.samples.delta[i].max() njs = cu.counter(self.samples.delta[i], dmax + 1 + m) ljs = njs + (njs == 0) * self.samples.eta[i] / m # This part needs to be fixed. new_alphas = self.generate_alpha_new(self.samples.mu[i], self.samples.Sigma[i], m) prob = ljs / ljs.sum() deltas = cu.generate_indices(prob, n_per_sample) alpha = np.vstack((self.samples.alpha[i], new_alphas))[deltas] new_gammas.append(gamma(shape = alpha)) new_gamma_arr = np.vstack(new_gammas) return new_gamma_arr
def generate_posterior_predictive_gammas(self, n_per_sample=10, m=20): new_gammas = [] for s in range(self.nSamp): dmax = self.samples.delta[s].max() njs = cu.counter(self.samples.delta[s], int(dmax + 1 + m)) ljs = njs + (njs == 0) * self.samples.eta[s] / m gnew = binomial(size=(m, self.nCol), n=1, p=self.samples.rho[s]) anew = self.samples.alpha[s][0] * ( 1 - gnew) + self.samples.alpha[s][1] * gnew bnew = self.samples.beta[s][0] * ( 1 - gnew) + self.samples.beta[s][1] * gnew new_zetas = gamma(shape=anew, scale=1. / bnew, size=(m, self.nCol)) prob = ljs / ljs.sum() deltas = cu.generate_indices(prob, n_per_sample) zeta = np.vstack((self.samples.zeta[s], new_zetas))[deltas] new_gammas.append(gamma(shape=zeta)) return np.vstack(new_gammas)
def generate_posterior_predictive_probit(self, n_per_sample, m=20): new_pVis = [] for i in range(self.nSamp): dmax = self.samples.delta[i].max() # njs = cu.counter(self.samples.delta[i], dmax + 1 + m) # ljs = njs + (njs == 0) * self.samples.eta[i] / m njs = cu.counter(self.samples.delta[i], dmax + 1) ljs = njs # new_mus = self.samples.mu0[i] + \ # self.samples.Sigma0[i] @ normal.rvs(size = (m, self.nCol)) # new_Sigmas = prob = ljs / ljs.sum() deltas = cu.generate_indices(prob, n_per_sample) mus = self.samples.mu[i][deltas] Sigmas = self.samples.Sigma[i][deltas] for j in range(n_per_sample): new_pVis.append( mus[j] + cholesky(Sigmas[j]) @ normal(size=self.nCol - 1)) return self.invprobit(np.vstack(new_pVis))
def generate_posterior_predictive_simplex_3d(self, cols, m = 20): keep_idx = np.array(sample(range(self.nSamp), self.nDat), dtype = int) delta = self.samples.delta[keep_idx] alpha = [self.samples.alpha[i][:,cols] for i in keep_idx] beta = [self.samples.beta[i][:,cols] for i in keep_idx] eta = self.samples.eta[keep_idx] alpha_shape = self.samples.alpha_shape[keep_idx][:,cols] if cols[0] == 0: # beta_shape = np.array( # [1, self.samples.beta_shape[keep_idx][:,(np.array(cols[1:], dtype = int) - 1)]] # ) beta_shape = np.hstack(( np.ones((alpha_shape.shape[0],1)), self.samples.beta_shape[keep_idx][:,(np.array(cols[1:], dtype = int) - 1)] )) else: beta_shape = self.samples.beta_shape[keep_idx][:,(np.array(cols, dtype = int) - 1)] postpred = np.empty((self.nDat, len(cols))) for n in range(self.nDat): dmax = delta[n].max() njs = cu.counter(delta[n], dmax + 1 + m) ljs = njs + (njs == 0) * eta[n] / m new_alphas = gamma.rvs(a = alpha_shape[n], size = (m, len(cols))) if cols[0] == 0: new_betas = np.hstack(( np.ones((m, 1)), gamma.rvs(a = beta_shape[n,1:], size = (m, len(cols) - 1)) )) else: new_betas = gamma.rvs(a = beta_shape[n], size = (m, len(cols))) prob = ljs / ljs.sum() a = np.vstack((alpha[n], new_alphas)) b = np.vstack((beta[n], new_betas)) new_delta = np.random.choice(range(dmax + 1 + m), 1, p = prob) postpred[n] = gamma.rvs(a = a[new_delta], scale = 1/b[new_delta]) np.nan_to_num(postpred, copy = False) return (postpred.T / postpred.sum(axis = 1)).T
def sample_pi(self, delta): shapes = cu.counter(delta, self.nMix) + self.priors.pi.a unnormalized = gamma(shape = shapes) return unnormalized / unnormalized.sum()