def test_gmm_bf(kmax=4, verbose=0): # perform a model selection procedure on a gmm with Bayes factor # estimations kmax : range of values that are tested # # fixme : this one often fails. I don't really see why n = 30 dim = 2 x = nr.randn(n, dim) # x[:30] += 2 niter = 3000 bbf = -np.infty for k in range(1, kmax): b = BGMM(k, dim) b.guess_priors(x) b.initialize(x) b.sample(x, 100) w, cent, prec, pz = b.sample(x, niter=niter, mem=1) bplugin = BGMM(k, dim, cent, prec, w) bplugin.guess_priors(x) bfk = bplugin.Bfactor(x, pz.astype(np.int), 1) if verbose: print k, bfk if bfk > bbf: bestk = k bbf = bfk assert bestk < 3
def test_bgmm_gibbs(verbose=0): # perform the estimation of a gmm using Gibbs sampling n = 100 k = 2 dim = 2 niter = 1000 x = nr.randn(n, dim) x[:30] += 2 b = BGMM(k, dim) b.guess_priors(x) b.initialize(x) b.sample(x, 1) w, cent, prec, pz = b.sample(x, niter, mem=1) b.plugin(cent, prec, w) z = pz[:, 0] # fixme : find a less trivial test assert z.max() + 1 == b.k
def test_gmm_bf(kmax=4, seed=1, verbose=1): """ perform a model selection procedure on a gmm with Bayes factor estimations Parameters ---------- kmax : range of values that are tested seed=False: int, optionnal If seed is not False, the random number generator is initialized at a certain value fixme : this one often fails. I don't really see why """ n=30 dim=2 if seed: nr = np.random.RandomState([seed]) else: import numpy.random as nr x = nr.randn(n,dim) niter = 1000 bbf = -np.infty for k in range(1, kmax): b = BGMM(k, dim) b.guess_priors(x) b.initialize(x) b.sample(x, 100) w, cent, prec, pz = b.sample(x, niter=niter, mem=1) bplugin = BGMM(k, dim, cent, prec, w) bplugin.guess_priors(x) bfk = bplugin.bayes_factor(x, pz.astype(np.int)) if verbose: print k, bfk if bfk>bbf: bestk = k bbf = bfk assert(bestk<3)