Example #1
0
    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))
Example #2
0
 def _resample_logseriesaug(self,data=[],niter=20):
     # an alternative algorithm, kind of opaque and no advantages...
     if getdatasize(data) == 0:
         self.p = np.random.beta(self.alpha_0,self.beta_0)
         self.r = np.random.gamma(self.k_0,self.theta_0)
     else:
         data = flattendata(data)
         N = data.shape[0]
         logF = self.logF
         L_i = np.zeros(N)
         data_nz = data[data > 0]
         for itr in range(niter):
             logR = np.arange(1,logF.shape[1]+1)*np.log(self.r) + logF
             L_i[data > 0] = sample_discrete_from_log(logR[data_nz-1,:data_nz.max()],axis=1)+1
             self.r = np.random.gamma(self.k_0 + L_i.sum(), 1/(1/self.theta_0 - np.log(1-self.p)*N))
             self.p = np.random.beta(self.alpha_0 + data.sum(), self.beta_0 + N*self.r)
Example #3
0
 def resample(self):
     scores = self._compute_scores()
     self.z, lognorms = sample_discrete_from_log(scores,axis=1,return_lognorms=True)
     self._normalizer = lognorms[~np.isnan(self.data).any(1)].sum()