Esempio n. 1
0
def main(num_samples, burn, lag, w):
    
    alpha = 1.0
    N = 25
    Z, Nk, K = utils.crp_gen(N, alpha)

    # crp with gamma prior
    log_prior_fun = lambda a: -a
    log_pdf_lambda = lambda a : utils.unorm_lcrp_post(a, N, K, log_prior_fun)
    proposal_fun = lambda : gamrnd(1.0,1.0)
    D = (0, float('Inf'))

    samples = su.slice_sample(proposal_fun, log_pdf_lambda, D, num_samples=num_samples, burn=burn, lag=lag, w=w)

    minval = min(samples)
    maxval = max(samples)
    xvals = numpy.linspace(minval, maxval, 100)

    yvals = numpy.array([ math.exp(log_pdf_lambda(x)) for x in xvals])
    yvals /= trapz(xvals, yvals)



    ax=pylab.subplot(2,1,1)
    pylab.hist(X,50,normed=True)

    ax_1=pylab.subplot(2,1,2)
    pylab.hist(samples,100,normed=True)
    pylab.plot(xvals,-yvals,c='red',lw=3, alpha=.8)
    pylab.xlim(ax.get_xlim())
    pylab.ylim(ax.get_ylim())

    pylab.show()
Esempio n. 2
0
 def __transition_state_alpha(self):
     
     logps = numpy.zeros(self.n_grid)
     for i in range(self.n_grid):
         alpha = self.alpha_grid[i]
         logps[i] = utils.unorm_lcrp_post(alpha, self.n_cols, self.V, lambda x: 0)
     # log_pdf_lambda = lambda a : utils.lcrp(self.n_cols, self.Nv, a) + self.alpha_prior_lambda(a)
     
     index = utils.log_pflip(logps)
     self.alpha = self.alpha_grid[index]
Esempio n. 3
0
 def transition_alpha(self):
     """
     Calculate CRP alpha conditionals over grid and transition
     """
     logps = numpy.zeros(len(self.alpha_grid))
     for i in range(len(self.alpha_grid)):
         alpha = self.alpha_grid[i]
         logps[i] = utils.unorm_lcrp_post(alpha, self.N, self.K, lambda x: 0)
     # log_pdf_lambda = lambda a : utils.lcrp(self.n_cols, self.Nv, a) + self.alpha_prior_lambda(a)
     
     index = utils.log_pflip(logps)
     self.alpha = self.alpha_grid[index]
Esempio n. 4
0
    def transition_alpha(self):
        """
        Calculate CRP alpha conditionals over grid and transition
        """
        logps = numpy.zeros(len(self.alpha_grid))
        for i in range(len(self.alpha_grid)):
            alpha = self.alpha_grid[i]
            logps[i] = utils.unorm_lcrp_post(alpha, self.N, self.K,
                                             lambda x: 0)
        # log_pdf_lambda = lambda a : utils.lcrp(self.n_cols, self.Nv, a) + self.alpha_prior_lambda(a)

        index = utils.log_pflip(logps)
        self.alpha = self.alpha_grid[index]