def prob(z): return (counts[z] + alpha[z]) / (sum(counts) + sum(alpha)) print "alpha\t", alpha lp = 0 for z in data: lp += np.log(prob(z)) counts[z] += 1 print "counts\t", counts print "urn process\t", lp, np.exp(lp) x = mcmc.dcm(np.array(counts), np.array(alpha), single=True) print "DCM-1 \t", x, np.exp(x) x = mcmc.dcm(np.array(counts), np.array(alpha), single=False) print "DCM full\t", x, np.exp(x) lp = 0 for z in data: counts[z] -= 1 lp += np.log(prob(z)) counts[z] += 1 print "posthoc-1\t", lp, np.exp(lp) lp = 0 for z in data:
def loglik(log_conc): alpha = np.tile(np.exp(log_conc)/K, K) loglik = sum(mcmc.dcm(c,alpha) for c in counts) #prior = r.dgamma(np.exp(log_conc), .5, scale=1000, log=True) #prior = 0 return loglik
counts = np.tile(0, K) def prob(z): return (counts[z]+alpha[z]) / (sum(counts) + sum(alpha)) print "alpha\t",alpha lp = 0 for z in data: lp += np.log(prob(z)) counts[z] += 1 print "counts\t",counts print "urn process\t", lp, np.exp(lp) x = mcmc.dcm(np.array(counts),np.array(alpha), single=True) print "DCM-1 \t", x, np.exp(x) x = mcmc.dcm(np.array(counts),np.array(alpha), single=False) print "DCM full\t", x, np.exp(x) lp = 0 for z in data: counts[z] -= 1 lp += np.log(prob(z)) counts[z] += 1 print "posthoc-1\t", lp, np.exp(lp) lp = 0
import scipy.sparse S = scipy.sparse.csr_matrix(C) print sp.gammaln(C+A).sum() print statutil.faster_lgamma_sum(C,A[0]) print statutil.sparse_lgamma_sum(S,A[0]) #In [1]: timeit util.sparse_lgamma_sum(S,A[0]) #10000 loops, best of 3: 23.1 us per loop # #In [2]: timeit util.faster_lgamma_sum(C,A[0]) #10000 loops, best of 3: 23.7 us per loop # #In [3]: timeit sp.gammaln(C+A).sum() #1000 loops, best of 3: 569 us per loop import mcmc print mcmc.dcm_symm(C,A[0], single=True) #Out[8]: -824.251394401741 print mcmc.dcm(C,A, single=True) #Out[9]: -824.25139440437488 #In [10]: timeit mcmc.dcm(C,A, single=True) #1000 loops, best of 3: 1.15 ms per loop # #In [11]: timeit mcmc.dcm_symm(C,A[0], single=True) #10000 loops, best of 3: 40.1 us per loop
import scipy.special as sp import scipy.sparse S = scipy.sparse.csr_matrix(C) print sp.gammaln(C + A).sum() print statutil.faster_lgamma_sum(C, A[0]) print statutil.sparse_lgamma_sum(S, A[0]) #In [1]: timeit util.sparse_lgamma_sum(S,A[0]) #10000 loops, best of 3: 23.1 us per loop # #In [2]: timeit util.faster_lgamma_sum(C,A[0]) #10000 loops, best of 3: 23.7 us per loop # #In [3]: timeit sp.gammaln(C+A).sum() #1000 loops, best of 3: 569 us per loop import mcmc print mcmc.dcm_symm(C, A[0], single=True) #Out[8]: -824.251394401741 print mcmc.dcm(C, A, single=True) #Out[9]: -824.25139440437488 #In [10]: timeit mcmc.dcm(C,A, single=True) #1000 loops, best of 3: 1.15 ms per loop # #In [11]: timeit mcmc.dcm_symm(C,A[0], single=True) #10000 loops, best of 3: 40.1 us per loop