コード例 #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
ファイル: 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)