def resample(self):
al, o = np.log(self.alpha_0), self.obs_distn
self.z = ma.masked_array(self.z,mask=np.zeros(self.z.shape))
model = self.model
for n in np.random.permutation(self.data.shape[0]):
# mask out n
self.z.mask[n] = True
# form the scores and sample them
ks = list(model._get_occupied())
scores = np.array([
np.log(model._get_counts(k))+ o.log_predictive(self.data[n],model._get_data_withlabel(k)) \
for k in ks] + [al + o.log_marginal_likelihood(self.data[n])])
idx = sample_discrete_from_log(scores)
if idx == scores.shape[0]-1:
self.z[n] = self._new_label(ks)
else:
self.z[n] = ks[idx]
# sample
# note: the mask gets fixed by assigning into the array
self.z[n] = sample_discrete_from_log(np.array(scores))
评论列表
文章目录