Ejemplo n.º 1
0
def test_run():
    from ultranest import NestedSampler

    def loglike(y):
        z = np.log10(y)
        a = np.array([-0.5 * sum([((xi - 0.83456 + i*0.1)/0.5)**2 for i, xi in enumerate(x)]) for x in z])
        b = np.array([-0.5 * sum([((xi - 0.43456 - i*0.1)/0.5)**2 for i, xi in enumerate(x)]) for x in z])
        loglike.ncalls += len(a)
        return np.logaddexp(a, b)
    loglike.ncalls = 0

    def transform(x):
        return 10**(10. * x - 5.)

    paramnames = ['Hinz', 'Kunz']

    sampler = NestedSampler(paramnames, loglike, transform=transform, num_live_points=400, vectorized=True)
    r = sampler.run(log_interval=50)

    ncalls = loglike.ncalls
    if sampler.mpi_size > 1:
        ncalls = sampler.comm.gather(ncalls, root=0)
        if sampler.mpi_rank == 0:
            print("ncalls on the different MPI ranks:", ncalls)
        ncalls = sum(sampler.comm.bcast(ncalls, root=0))
    assert abs(r['ncall'] - ncalls) <= 2 * sampler.mpi_size, (r['ncall'], ncalls)
    open('nestedsampling_results.txt', 'a').write("%.3f\n" % r['logz'])
    sampler.plot()
Ejemplo n.º 2
0
def main(args):
    from ultranest import NestedSampler

    #def loglike(z):
    #    return np.array([-sum(100.0 * (x[1:] - x[:-1] ** 2.0) ** 2.0 + (1 - x[:-1]) ** 2.0) for x in z])
    def loglike_(z):
        return np.array([
            -sum(100.0 * (x[1::2] - x[::2]**2.0)**2.0 + (1 - x[::2])**2.0)
            for x in z
        ])

    def loglike(z):
        a = np.array([
            -0.5 * sum([((xi - 0.83456 + i * 0.1) / 0.01)**2
                        for i, xi in enumerate(x)]) for x in z
        ])
        b = np.array([
            -0.5 * sum([((xi - 0.43456 - i * 0.1) / 0.01)**2
                        for i, xi in enumerate(x)]) for x in z
        ])
        return np.logaddexp(a, b)

    def transform(x):
        return 10. * x - 5.

    import string
    paramnames = list(string.ascii_lowercase)[:args.x_dim]

    sampler = NestedSampler(paramnames,
                            loglike,
                            transform=transform,
                            vectorized=True,
                            log_dir=args.log_dir)
    sampler.run(log_interval=20, num_live_points=args.num_live_points)
    sampler.plot()