コード例 #1
0
ファイル: test_bgmm.py プロジェクト: Garyfallidis/nipy
def test_evidence(verbose=0,k=1):
    """
    Compare the evidence estimated by Chib's method
    with the variational evidence (free energy)
    fixme : this one really takes time
    """
    n=50
    dim=2
    x = nr.randn(n,dim)
    x[:15] += 3
    
    b = VBGMM(k,dim)
    b.guess_priors(x)
    b.initialize(x)
    b.estimate(x)
    vbe = b.evidence(x),
    if verbose:
        print 'vb: ',vbe, 

    niter = 1000
    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)
    bfchib = bplugin.bayes_factor(x, pz.astype(np.int), 1)
    if verbose:
        print ' chib:', bfchib
    assert(bfchib>vbe)
コード例 #2
0
def test_evidence(verbose=0, k=1):
    # Compare the evidence estimated by Chib's method with the
    # variational evidence (free energy) fixme : this one really takes
    # time
    n = 100
    dim = 2
    x = nr.randn(n, dim)
    x[:30] += 3
    show = 0

    b = VBGMM(k, dim)
    b.guess_priors(x)
    b.initialize(x)
    b.estimate(x)
    vbe = (b.evidence(x),)
    if verbose:
        print "vb: ", vbe,

    niter = 1000
    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)
    bfchib = bplugin.Bfactor(x, pz.astype(np.int), 1)
    if verbose:
        print " chib:", bfchib
    assert bfchib > vbe
コード例 #3
0
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
コード例 #4
0
ファイル: test_bgmm.py プロジェクト: Garyfallidis/nipy
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)
コード例 #5
0
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