def energy(self,data): # TODO TODO this function is horrible assert data.ndim == 1 if np.isnan(data).any(): return 0. from util.stats import sample_discrete likes = np.array([c.log_likelihood(data) for c in self.components]).reshape((-1,)) likes += np.log(self.weights.weights) label = sample_discrete(np.exp(likes - likes.max())) return self.components[label].energy(data)
def rvs(self,customer_counts): ''' Number of distinct tables. Not complete CRPs. customer_counts is a list of customer counts, and len(customer_counts) is the number of restaurants. ''' assert isinstance(customer_counts,list) or isinstance(customer_counts,int) if isinstance(customer_counts,int): customer_counts = [customer_counts] restaurants = [] for num in customer_counts: tables = [] for c in range(num): newidx = sample_discrete(np.array(tables + [self.concentration])) if newidx == len(tables): tables += [1] else: tables[newidx] += 1 restaurants.append(tables) return restaurants if len(restaurants) > 1 else restaurants[0]
def _generate(self,N): # run a CRP forwards alpha_0 = self.alpha_0 self.z = np.zeros(N,dtype=np.int32) for n in range(N): self.z[n] = sample_discrete(np.concatenate((np.bincount(self.z[:n]),(alpha_0,))))
def rvs(self,size=None): return sample_discrete(self.weights,size)
def sample_next(self): next_table = sample_discrete(self._get_distr()) self.assignments.append(next_table) return next_table
def sample_next(self,**kwargs): label = sample_discrete(self.counts) self.counts[label] += 1 return self.components[label].sample_next(**self.arggetters[label](kwargs))
def sample_next(self, taboo): next_table = sample_discrete(self._get_distr(taboo)) self.assignments.append(next_table) return next_table
def sample_next(self, **kwargs): label = sample_discrete(self.counts) self.counts[label] += 1 return self.components[label].sample_next( **self.arggetters[label](kwargs))