示例#1
0
def run_aharm_sampler():
    for seed in [733] + list(range(10)):
        print()
        print("SEED=%d" % seed)
        print()
        np.random.seed(seed)
        nsteps = max(1, int(10**np.random.uniform(0, 3)))
        Nlive = int(10**np.random.uniform(1.5, 3))
        print("Nlive=%d nsteps=%d" % (Nlive, nsteps))
        sampler = AHARMSampler(nsteps, adaptive_nsteps=False, region_filter=False)
        us = np.random.uniform(0.6, 0.8, size=(4000, 2))
        Ls = loglike_vectorized(us)
        i = np.argsort(Ls)[-Nlive:]
        us = us[i,:]
        Ls = Ls[i]
        Lmin = Ls.min()
        
        transformLayer = ScalingLayer()
        transformLayer.optimize(us, us)
        region = MLFriends(us, transformLayer)
        region.maxradiussq, region.enlarge = region.compute_enlargement()
        region.create_ellipsoid()
        nfunccalls = 0
        ncalls = 0
        while True:
            u, p, L, nc = sampler.__next__(region, Lmin, us, Ls, transform, loglike)
            nfunccalls += 1
            ncalls += nc
            if u is not None:
                break
            if nfunccalls > 100 + nsteps:
                assert False, ('infinite loop?', seed, nsteps, Nlive)
        print("done in %d function calls, %d likelihood evals" % (nfunccalls, ncalls))
示例#2
0
def test_transform():
    np.random.seed(1)
    corrs = np.arange(-1, 1, 0.1)
    corrs *= 0.999
    for corr in corrs:
        for scaleratio in [1, 0.001]:
            covmatrix = np.array([[1., corr], [corr, 1.]])
            points = np.random.multivariate_normal(np.zeros(2),
                                                   covmatrix,
                                                   size=1000)
            print(corr, scaleratio, covmatrix.flatten(), points.shape)
            points[:, 0] = points[:, 0] * 0.01 * scaleratio + 0.5
            points[:, 1] = points[:, 1] * 0.01 + 0.5

            layer = ScalingLayer()
            layer.optimize(points, points)
            tpoints = layer.transform(points)
            assert tpoints.shape == points.shape, (tpoints.shape, points.shape)
            points2 = layer.untransform(tpoints)
            assert tpoints.shape == points2.shape, (tpoints.shape,
                                                    points2.shape)

            assert (points2 == points).all(), (points, tpoints, points2)

            # transform a single point
            points = points[0]
            tpoints = layer.transform(points)
            assert tpoints.shape == points.shape, (tpoints.shape, points.shape)
            points2 = layer.untransform(tpoints)
            assert tpoints.shape == points2.shape, (tpoints.shape,
                                                    points2.shape)

            assert (points2 == points).all(), (points, tpoints, points2)
示例#3
0
def test_aharm_sampler():
    def loglike(theta):
        return -0.5 * (((theta - 0.5)/0.01)**2).sum(axis=1)
    def transform(x):
        return x

    seed = 1
    Nlive = 10
    np.random.seed(seed)
    us = np.random.uniform(size=(Nlive, 2))
    Ls = loglike(us)
    Lmin = Ls.min()
    transformLayer = ScalingLayer()
    transformLayer.optimize(us, us)
    region = MLFriends(us, transformLayer)
    region.maxradiussq, region.enlarge = region.compute_enlargement()
    region.create_ellipsoid()
    assert region.inside(us).all()
    nsteps = 10
    sampler = AHARMSampler(nsteps=nsteps, region_filter=True)

    nfunccalls = 0
    ncalls = 0
    while True:
        u, p, L, nc = sampler.__next__(region, Lmin, us, Ls, transform, loglike)
        nfunccalls += 1
        ncalls += nc
        if u is not None:
            break
        if nfunccalls > 100 + nsteps:
            assert False, ('infinite loop?', seed, nsteps, Nlive)
    print("done in %d function calls, %d likelihood evals" % (nfunccalls, ncalls))
示例#4
0
def test_region_sampling_scaling(plot=False):
    np.random.seed(1)
    upoints = np.random.uniform(0.2, 0.5, size=(1000, 2))
    upoints[:, 1] *= 0.1

    transformLayer = ScalingLayer(wrapped_dims=[])
    transformLayer.optimize(upoints, upoints)
    region = MLFriends(upoints, transformLayer)
    region.maxradiussq, region.enlarge = region.compute_enlargement(
        nbootstraps=30)
    print("enlargement factor:", region.enlarge, 1 / region.enlarge)
    region.create_ellipsoid()
    nclusters = transformLayer.nclusters
    assert nclusters == 1
    assert np.allclose(region.unormed, region.transformLayer.transform(
        upoints)), "transform should be reproducible"
    assert region.inside(
        upoints).all(), "live points should lie near live points"
    if plot:
        plt.plot(upoints[:, 0], upoints[:, 1], 'x ')
        for method in region.sampling_methods:
            points, nc = method(nsamples=400)
            plt.plot(points[:, 0],
                     points[:, 1],
                     'o ',
                     label=str(method.__name__))
        plt.legend(loc='best')
        plt.savefig('test_regionsampling_scaling.pdf', bbox_inches='tight')
        plt.close()

    for method in region.sampling_methods:
        print("sampling_method:", method)
        newpoints = method(nsamples=4000)
        lo1, lo2 = newpoints.min(axis=0)
        hi1, hi2 = newpoints.max(axis=0)
        assert 0.15 < lo1 < 0.25, (method.__name__, newpoints, lo1, hi1, lo2,
                                   hi2)
        assert 0.015 < lo2 < 0.025, (method.__name__, newpoints, lo1, hi1, lo2,
                                     hi2)
        assert 0.45 < hi1 < 0.55, (method.__name__, newpoints, lo1, hi1, lo2,
                                   hi2)
        assert 0.045 < hi2 < 0.055, (method.__name__, newpoints, lo1, hi1, lo2,
                                     hi2)
        assert region.inside(newpoints).mean() > 0.99, region.inside(
            newpoints).mean()

    region.maxradiussq = 1e-90
    assert np.allclose(region.unormed, region.transformLayer.transform(
        upoints)), "transform should be reproducible"
    assert region.inside(
        upoints).all(), "live points should lie very near themselves"
示例#5
0
def test_clusteringcase_eggbox():
    from ultranest.mlfriends import update_clusters, ScalingLayer, MLFriends
    points = np.loadtxt(os.path.join(here, "eggboxregion.txt"))
    transformLayer = ScalingLayer()
    transformLayer.optimize(points, points)
    region = MLFriends(points, transformLayer)
    maxr = region.compute_maxradiussq(nbootstraps=30)
    assert 1e-10 < maxr < 5e-10
    print('maxradius:', maxr)
    nclusters, clusteridxs, overlapped_points = update_clusters(
        points, points, maxr)
    # plt.title('nclusters: %d' % nclusters)
    # for i in np.unique(clusteridxs):
    #    x, y = points[clusteridxs == i].transpose()
    #    plt.scatter(x, y)
    # plt.savefig('testclustering_eggbox.pdf', bbox_inches='tight')
    # plt.close()
    assert 14 < nclusters < 20, nclusters
示例#6
0
def test_wrap(plot=False):
    np.random.seed(1)
    for Npoints in 10, 100, 1000:
        for wrapids in [[], [0], [1], [0, 1]]:
            print("Npoints=%d wrapped_dims=%s" % (Npoints, wrapids))
            #wrapids = np.array(wrapids)
            points = np.random.normal(0.5, 0.01, size=(Npoints, 2))
            for wrapi in wrapids:
                points[:, wrapi] = np.fmod(points[:, wrapi] + 0.5, 1)

            assert (points > 0).all(), points
            assert (points < 1).all(), points
            layer = ScalingLayer(wrapped_dims=wrapids)
            layer.optimize(points, points)
            tpoints = layer.transform(points)
            assert tpoints.shape == points.shape, (tpoints.shape, points.shape)
            points2 = layer.untransform(tpoints)
            assert tpoints.shape == points2.shape, (tpoints.shape,
                                                    points2.shape)

            if plot:
                plt.subplot(1, 2, 1)
                plt.scatter(points[:, 0], points[:, 1])
                plt.scatter(points2[:, 0], points2[:, 1], marker='x')
                plt.subplot(1, 2, 2)
                plt.scatter(tpoints[:, 0], tpoints[:, 1])
                plt.savefig("testtransform_%d_wrap%d.pdf" %
                            (Npoints, len(wrapids)),
                            bbox_inches='tight')
                plt.close()

            assert np.allclose(points2, points), (points, tpoints, points2)

            layer = AffineLayer(wrapped_dims=wrapids)
            layer.optimize(points, points)
            tpoints = layer.transform(points)
            assert tpoints.shape == points.shape, (tpoints.shape, points.shape)
            points2 = layer.untransform(tpoints)
            assert tpoints.shape == points2.shape, (tpoints.shape,
                                                    points2.shape)
示例#7
0
def test_overclustering_eggbox_txt():
    from ultranest.mlfriends import update_clusters, ScalingLayer, MLFriends
    np.random.seed(1)
    for i in [20, 23, 24, 27, 49]:
        print()
        print("==== TEST CASE %d =====================" % i)
        print()
        points = np.loadtxt(os.path.join(here, "overclustered_u_%d.txt" % i))

        for k in range(3):
            transformLayer = ScalingLayer(wrapped_dims=[])
            transformLayer.optimize(points, points)
            region = MLFriends(points, transformLayer)
            maxr = region.compute_maxradiussq(nbootstraps=30)
            region.maxradiussq = maxr
            nclusters = transformLayer.nclusters

            print("manual: r=%e nc=%d" % (region.maxradiussq, nclusters))
            # assert 1e-10 < maxr < 5e-10
            nclusters, clusteridxs, overlapped_points = update_clusters(
                points, points, maxr)
            print("reclustered: nc=%d" % (nclusters))

        if False:
            plt.title('nclusters: %d' % nclusters)
            for k in np.unique(clusteridxs):
                x, y = points[clusteridxs == k].transpose()
                plt.scatter(x, y)
            plt.savefig('testoverclustering_eggbox_%d.pdf' % i,
                        bbox_inches='tight')
            plt.close()
        assert 14 < nclusters < 20, (nclusters, i)

        for j in range(3):
            nclusters, clusteridxs, overlapped_points = update_clusters(
                points, points, maxr)
            assert 14 < nclusters < 20, (nclusters, i)