Пример #1
0
def example_imm_1d():
    n = 100
    dim = 1
    alpha = .5
    x = np.random.randn(n, dim)
    x[:.3*n] *= 2
    x[:.1*n] += 3
    igmm = IMM(alpha, dim)
    igmm.set_priors(x)

    # warming
    igmm.sample(x, niter=100, init=True)
    print 'number of components: ', igmm.k

    from gmm import GridDescriptor
    gd = GridDescriptor(1, [-9,11], 201)

    #sampling
    like =  igmm.sample(x, niter=1000, sampling_points=gd.make_grid())
    print 'number of components: ', igmm.k

    print 'density sum', 0.1*like.sum()
    igmm.show(x, gd, density=like)
    
    return igmm 
Пример #2
0
def example_igmm_wnc():
    """
    Example of IMM with a null class
    points are generated in [0,1] interval witha  higher density close to 0
    the null points are assumed to occur uniformly over [0,1]
    
    the algorithm correctly identifies the points  close to 0
    as the 'active' set
    """
    n = 50
    dim = 1
    alpha = .5
    g0 = 1.
    x = np.random.rand(n, dim)
    x[:.3*n] *= .2
    x[:.1*n] *= .3
    migmm = MixedIMM(alpha, dim)
    migmm.set_priors(x)
    migmm.set_constant_densities(null_dens=g0)

    # burn-in
    ncp = 0.5*np.ones(n)
    migmm.sample(x, null_class_proba=ncp, niter=100, init=True)
    print 'number of components: ', migmm.k
    
    from gmm import GridDescriptor
    gd = GridDescriptor(1, [0,1], 101)

    #sampling
    like, pproba =  migmm.sample(x, null_class_proba=ncp, niter=1000,
                         sampling_points=gd.make_grid(), kfold=10)
    print 'number of components: ', migmm.k

    # the density should sum to 1
    print 'density sum', 0.01*like.sum()
    migmm.show(x, gd, density=like)

    import pylab
    pylab.figure()
    pylab.plot(x, pproba, '.')
    pylab.title('posterior probability of being in the null class')
    pylab.xlabel('data')
    pylab.ylabel('proba of being in the null class')
    pylab.show()
    
    return migmm