Пример #1
0
    def sample(self, i):
        s = self.data[i]
        ss = self.dd[i]
        
        probs = []
        for t in range(self.K):
            prior = math.log(self.counts[t])
            
            ll = self.mvNormalLL(s, ss, 1, self.mu[t], self.precision[t], self.logdet[t])
            probs.append(prior + ll)
        if self.K < self.con.pruningfactor:
            prior = self.con.logalpha

            ll = self.integrated[i]     
            probs.append(prior + ll)
        normed = logNormalize(probs)
        return sampleIndex(normed)    
Пример #2
0
    def sample(self, i):
        probs = []
        for t in range(self.K):
            prior = math.log(self.counts[t])
            ll = self.posteriorPredictive(set([i]), self.data[i], self.dd[i], t)
            indices = self.getIndices(t)
            suffprior = np.zeros(len(indices))
            for j, ind in enumerate(indices):
                suffprior[j] = self.prior[i, ind]
            avgprior = np.log(suffprior.sum() / len(indices))
            
            probs.append(prior + avgprior + ll)
        if self.K < self.con.pruningfactor:
            prior = self.con.logalpha

            ll = self.integrated[i]     
            probs.append(prior + ll)
        normed = logNormalize(probs)
        return sampleIndex(normed) 
Пример #3
0
    def sample(self, i, oldt, oldf, followset, s, ss):      
        llcache = np.zeros(self.K)
        n = self.n
        if self.con.seq:
            n = i + 1

        probs = self.probs[:n]
        probs.fill(0.)
        
        newll = 0

        for j in xrange(n):
            if i == j:
                prior = self.con.logalpha
            else:
                prior = -self.featsetvals[self.features[i,j]]
            
            t = self.assignments[j]
            
            if t == -1:
                ll = newll
                if ll == 0:
                    ll = newll = self.integrateOverParameters(len(followset), s, ss)
            else:
                ll = llcache[t]
                if ll == 0:
                    if t == oldt and self.counts[t] == len(followset):
                        ll = self.integrateOverParameters(len(followset), s, ss)
                    else:
                        ll = self.mvNormalLL(s, ss, len(followset), self.mu[t], self.precision[t], self.logdet[t])
                    llcache[t] = ll

            probs[j]  = prior + ll                           

        normed = np.exp(probs - logsumexp(probs))
        #np.testing.assert_allclose(normed, normed2)
        ind = sampleIndex(normed)
        table = self.assignments[ind]
        return table, ind
Пример #4
0
    def sample(self, i, oldt, oldf, followset, s, ss):    
        if oldt == 26:
            pass
        #    import pdb; pdb.set_trace()
        llcache = np.zeros(self.K)
        n = self.n
        if self.con.seq:
            n = i + 1

        #probs = self.probs[:n]
        #probs.fill(0.)
        probs = []
        #inds = []
        
        newll = 0
        priors = []
        dist_priors = []
        
        for j in xrange(n):
            if i == j:
                prior = self.con.logalpha
            else:
                prior = -self.featsetvals[self.features[i,j]]
            priors.append(prior)
            distprior = -self.mdist[i,j]
            dist_priors.append(distprior)
            #if prior < self.con.priorth:
            #    continue
            #inds.append(j)
            t = self.assignments[j]
            
            if t == -1:
                ll = newll
                if ll == 0:
                    ll = newll = self.integrateOverParameters(len(followset), s, ss)
            else:
                ll = llcache[t]
                if ll == 0:
                    ll = self.posteriorPredictive(len(followset), s, ss, t)
                    llcache[t] = ll
            #probs[j]  = prior + ll   
            if oldt == 26:
                pass  
            prob = self.con.a * (prior + distprior) + ll
            probs.append(prob)    
            #probs.append(prior + ll)
        sorted_prior = sorted(zip(priors, range(n)), reverse=True) 
        sorted_dist_prior = sorted(zip(dist_priors, range(n)), reverse=True)
        probs = np.array(probs)              

        normed = np.exp(probs - logsumexp(probs))
        sorted_normed = sorted(zip(normed, range(n)), reverse=True)
        if oldt == 26:
            for score, ind_ in sorted_normed[:10]:
                print self.vocab[ind_], self.assignments[ind_]
            print
        #np.testing.assert_allclose(normed, normed2)
        ind = sampleIndex(normed)
        if oldt == 26:
            pass
        #ind = inds[ind]
        table = self.assignments[ind]
        return table, ind