예제 #1
0
def test_periodic(sampler, dynamic):
    # hard test of dynamic sampler with high dlogz_init and small number
    # of live points
    logz_true = np.log(np.sqrt(2 * np.pi) * erf(win / np.sqrt(2)) / (2 * win))
    thresh = 5
    rstate = get_rstate()
    if dynamic:
        dns = dynesty.DynamicNestedSampler(loglike,
                                           prior_transform,
                                           ndim,
                                           nlive=nlive,
                                           sample=sampler,
                                           reflective=[0],
                                           rstate=rstate)
    else:
        dns = dynesty.NestedSampler(loglike,
                                    prior_transform,
                                    ndim,
                                    nlive=nlive,
                                    sample=sampler,
                                    reflective=[0],
                                    rstate=rstate)
    dns.run_nested(print_progress=printing)
    assert (np.abs(dns.results.logz[-1] - logz_true) <
            thresh * dns.results.logzerr[-1])
예제 #2
0
def test_overlap():
    rstate = get_rstate()
    ndim = 2
    cen1 = np.array([0, 0])
    cen2 = np.array([1, 0])
    rad = 0.7
    sig = np.eye(ndim) * rad**2

    ell1 = db.Ellipsoid(cen1, sig)
    ell2 = db.Ellipsoid(cen2, sig)
    ell = db.MultiEllipsoid([ell1, ell2])
    nsamp = 10000
    xs = rstate.uniform(size=(nsamp, ndim))
    ind1 = np.sum((xs - cen1[None, :])**2, axis=1) < rad**2
    ind2 = np.sum((xs - cen2[None, :])**2, axis=1) < rad**2
    for i in range(nsamp):
        n1 = int(ind1[i])
        n2 = int(ind2[i])
        assert ell.overlap(xs[i]) == n1 + n2
        assert ell.overlap(xs[i], j=0) == n2
        within = ell.within(xs[i])
        within2 = []
        if n1 == 1:
            within2.append(0)
        if n2 == 1:
            within2.append(1)
        within2 = np.array(within2)
        assert np.all(within == within2)
예제 #3
0
def test_neff():
    # test of neff functionality
    ndim = 2
    rstate = get_rstate()
    sampler = dynesty.NestedSampler(loglike,
                                    prior_transform,
                                    ndim,
                                    nlive=nlive,
                                    rstate=rstate)
    assert sampler.n_effective == 0
    sampler.run_nested(print_progress=printing)
    assert sampler.n_effective > 10

    sampler = dynesty.DynamicNestedSampler(loglike,
                                           prior_transform,
                                           ndim,
                                           nlive=nlive,
                                           rstate=rstate)
    assert sampler.n_effective == 0
    sampler.run_nested(dlogz_init=1, n_effective=1000, print_progress=printing)
    assert sampler.n_effective > 1000
    sampler = dynesty.DynamicNestedSampler(loglike,
                                           prior_transform,
                                           ndim,
                                           nlive=nlive,
                                           rstate=rstate)
    sampler.run_nested(dlogz_init=1,
                       n_effective=10000,
                       print_progress=printing)
    assert sampler.n_effective > 10000
예제 #4
0
def test_pickle(dynamic, with_pool):
    # test of pickling functionality
    ndim = 2
    rstate = get_rstate()

    if with_pool:
        kw = dict(pool=Pool(2), queue_size=100)
    else:
        kw = {}

    if dynamic:
        sampler = dynesty.DynamicNestedSampler(loglike,
                                               prior_transform,
                                               ndim,
                                               nlive=nlive,
                                               rstate=rstate,
                                               **kw)
    else:
        sampler = dynesty.NestedSampler(loglike,
                                        prior_transform,
                                        ndim,
                                        nlive=nlive,
                                        rstate=rstate,
                                        **kw)
    sampler.run_nested(print_progress=printing, maxiter=100)
    # i do it twice as there were issues previously
    # with incorrect pool restoring
    S = pickle.dumps(sampler)
    sampler = pickle.loads(S)
    S = pickle.dumps(sampler)
    sampler = pickle.loads(S)
    sampler.run_nested(print_progress=printing, maxiter=100)
    if with_pool:
        kw['pool'].close()
        kw['pool'].join()
예제 #5
0
def test_results(dyn):
    # test of various results interfaces functionality
    ndim = 2
    rstate = get_rstate()
    if dyn:
        sampler = dynesty.DynamicNestedSampler(loglike,
                                               prior_transform,
                                               ndim,
                                               nlive=nlive,
                                               rstate=rstate)
    else:
        sampler = dynesty.NestedSampler(loglike,
                                        prior_transform,
                                        ndim,
                                        nlive=nlive,
                                        rstate=rstate)
    sampler.run_nested(print_progress=printing)
    res = sampler.results
    for k in res.keys():
        pass
    for k, v in res.items():
        pass
    for k, v in res.asdict().items():
        pass
    print(res)
    print(str(res))
    print('logl' in res)
    res1 = res.copy()
    # check it's pickleable
    S = pickle.dumps(res)
    res = pickle.loads(S)
예제 #6
0
def do_gaussians(sample='rslice',
                 bound='single',
                 nthreads=36,
                 ndim_min=2,
                 ndim_max=30,
                 nlive=5000):
    """
    Run many tests 
    """
    pool = mp.Pool(nthreads)
    res = []
    ndims = np.arange(ndim_min, ndim_max + 1)
    for ndim in ndims:
        rstate = get_rstate(ndim)
        co = Config(rstate, ndim)
        slices = None
        res.append((ndim, co,
                    pool.apply_async(
                        do_gaussian, (co, ),
                        dict(sample=sample,
                             bound=bound,
                             rstate=rstate,
                             slices=slices,
                             nlive=nlive))))
    for ndim, co, curres in res:
        curres = curres.get()
        print(ndim, curres[0], curres[1], curres[2], co.logz_truth_gau)
예제 #7
0
def test_saving(dopool, maxiter):
    # test saving
    ndim = 2
    fname = 'dynesty_test_%d.h5' % (os.getpid())
    rstate = get_rstate()
    kw = {}
    if dopool:
        pool = mp.Pool(2)
        kw['pool'] = pool
        kw['queue_size'] = 100

    sampler = dynesty.NestedSampler(loglike,
                                    prior_transform,
                                    ndim,
                                    nlive=nlive,
                                    save_history=True,
                                    history_filename=fname,
                                    rstate=rstate,
                                    **kw)
    sampler.run_nested(dlogz=0.01, print_progress=printing, maxiter=maxiter)
    assert (os.path.exists(fname))
    try:
        os.unlink(fname)
    except FileNotFoundError:
        pass
    if dopool:
        try:
            pool.close()
            pool.join()
        except Exception:
            pass
예제 #8
0
def test_periodic():
    # hard test of dynamic sampler with high dlogz_init and small number
    # of live points
    logz_true = np.log(np.sqrt(2 * np.pi) * erf(win / np.sqrt(2)) / (2 * win))
    thresh = 5
    ndim = 2
    rstate = get_rstate()
    sampler = dynesty.DynamicNestedSampler(loglike,
                                           prior_transform,
                                           ndim,
                                           nlive=nlive,
                                           periodic=[0],
                                           rstate=rstate)
    sampler.run_nested(dlogz_init=1, print_progress=printing)
    assert (np.abs(sampler.results.logz[-1] - logz_true) <
            thresh * sampler.results.logzerr[-1])
    sampler = dynesty.NestedSampler(loglike,
                                    prior_transform,
                                    ndim,
                                    nlive=nlive,
                                    periodic=[0],
                                    rstate=rstate)
    sampler.run_nested(dlogz=1, print_progress=printing)
    assert (np.abs(sampler.results.logz[-1] - logz_true) <
            thresh * sampler.results.logzerr[-1])
예제 #9
0
def test_sample():
    # test sampling of two overlapping ellipsoids that samples are uniform
    # within
    rad = 1
    shift = 0.75
    ndim = 10
    cen1 = np.zeros(ndim)
    cen2 = np.zeros(ndim)
    cen2[0] = shift
    sig = np.eye(10) * rad**2
    ells = [db.Ellipsoid(cen1, sig), db.Ellipsoid(cen2, sig)]
    mu = db.MultiEllipsoid(ells)
    R = []
    nsim = 100000
    rstate = get_rstate()
    for i in range(nsim):
        R.append(mu.sample(rstate=rstate)[0])
    R = np.array(R)
    assert (all([mu.contains(_) for _ in R]))

    # here I'm checking that all the points are uniformly distributed
    # within each ellipsoid
    for curc in [cen1, cen2]:
        dist1 = (np.sqrt(np.sum((R - curc)**2, axis=1)) / rad)
        # radius from 0 to 1
        xdist1 = dist1**ndim
        # should be uniformly distributed from 0 to 1
        xdist1 = xdist1[xdist1 < 1]
        pval = scipy.stats.kstest(xdist1,
                                  scipy.stats.uniform(loc=0.0, scale=1).cdf)[1]
        assert ((pval > 0.003) & (pval < 0.997))
    nhalf = (R[:, 0] > shift / 2.).sum()
    print(nhalf, nsim)
    assert (np.abs(nhalf - 0.5 * nsim) < 5 * np.sqrt(0.5 * nsim))
예제 #10
0
def test_periodic(sampler, dynamic):
    # hard test of dynamic sampler with high dlogz_init and small number
    # of live points
    logz_true = np.log(np.sqrt(2 * np.pi) * erf(win / np.sqrt(2)) / (2 * win))
    thresh = 8
    # This is set up to higher level
    # becasue of failures at ~5ssigma level
    # this needs to be investigated
    rstate = get_rstate()
    if dynamic:
        dns = dynesty.DynamicNestedSampler(loglike,
                                           prior_transform,
                                           ndim,
                                           nlive=nlive,
                                           periodic=[0],
                                           rstate=rstate,
                                           sample=sampler)
        dns.run_nested(dlogz_init=1, print_progress=printing)
    else:
        dns = dynesty.NestedSampler(loglike,
                                    prior_transform,
                                    ndim,
                                    nlive=nlive,
                                    periodic=[0],
                                    rstate=rstate,
                                    sample=sampler)
        dns.run_nested(dlogz=1, print_progress=printing)
    assert (np.abs(dns.results.logz[-1] - logz_true) <
            thresh * dns.results.logzerr[-1])
예제 #11
0
def test_error():
    rstate = get_rstate()
    with pytest.raises(ValueError):
        dynesty.DynamicNestedSampler(loglike,
                                     prior_transform,
                                     ndim,
                                     nlive=nlive,
                                     reflective=[22],
                                     rstate=rstate)
예제 #12
0
def test_slice_nograd():
    rstate = get_rstate()
    g = Gaussian()
    sampler = dynesty.NestedSampler(g.loglikelihood,
                                    g.prior_transform,
                                    g.ndim,
                                    nlive=nlive,
                                    sample='hslice',
                                    rstate=rstate)
    sampler.run_nested(print_progress=printing)
    check_results_gau(sampler.results, g, rstate)
예제 #13
0
def test_exc():
    # Test of exceptions that the exception is reraised
    ndim = 2
    rstate = get_rstate()
    with pytest.raises(MyException):
        sampler = dynesty.NestedSampler(loglike_exc,
                                        prior_transform,
                                        ndim,
                                        nlive=nlive,
                                        rstate=rstate)
        sampler.run_nested(print_progress=printing)
예제 #14
0
def test_dynamic():
    # check dynamic nested sampling behavior
    logz_tol = 1
    rstate = get_rstate()
    dsampler = dynesty.DynamicNestedSampler(loglikelihood_gau,
                                            prior_transform_gau,
                                            ntotdim,
                                            ncdim=ndim_gau,
                                            rstate=rstate)
    dsampler.run_nested(print_progress=printing)
    check_results_gau(dsampler.results, rstate, logz_tol)
예제 #15
0
def test_transform_tuple():
    # This is to test that the logzerr calculation is all right
    # if there are very large (negative) logl vaues
    # See bug #360
    ndim = 2
    rstate = get_rstate()
    sampler = dynesty.NestedSampler(loglike_large_logl,
                                    prior_transform_large_logl,
                                    ndim,
                                    rstate=rstate)

    sampler.run_nested(print_progress=printing, maxiter=50)
예제 #16
0
def test_run(ndim, sample):
    rstate = get_rstate()
    co = Config(rstate, ndim)
    nlive = 5000
    bound = 'multi'
    res = do_gaussian(co,
                      sample=sample,
                      bound=bound,
                      rstate=rstate,
                      nlive=nlive)
    assert (np.abs(co.logz_truth_gau - res[1]) < 5 * res[2])
    return res[3]
예제 #17
0
def test_cube_overlap():
    rstate = get_rstate()
    ndim = 10
    cen = np.zeros(ndim) + .5
    cen[0] = 0
    sig = np.eye(ndim) * .5**2
    ell = db.Ellipsoid(cen, sig)
    nsamp = 10000
    frac = ell.unitcube_overlap(nsamp, rstate=rstate)
    true_answer = 0.5
    uncertainty = np.sqrt(true_answer * (1 - true_answer) / nsamp)
    assert ((frac - true_answer) < 5 * uncertainty)
예제 #18
0
def test_mc_logvol():
    def capvol(n, r, h):
        # see https://en.wikipedia.org/wiki/Spherical_cap
        Cn = np.pi**(n / 2.) / scipy.special.gamma(1 + n / 2.)
        return (
            Cn * r**n *
            (1 / 2 - (r - h) / r * scipy.special.gamma(1 + n / 2) /
             np.sqrt(np.pi) / scipy.special.gamma(
                 (n + 1.) / 2) * scipy.special.hyp2f1(1. / 2,
                                                      (1 - n) / 2, 3. / 2,
                                                      ((r - h) / r)**2)))

    def sphere_vol(n, r):
        Cn = np.pi**(n / 2.) / scipy.special.gamma(1 + n / 2.) * r**n
        return Cn

    def two_sphere_vol(c1, c2, r1, r2):
        D = np.sqrt(np.sum((c1 - c2)**2))
        n = len(c1)
        if D >= r1 + r2:
            return sphere_vol(n, r1) + sphere_vol(n, r2)
        # now either one is fully inside or the is overlap
        if D + r1 <= r2 or D + r2 <= r1:
            #fully inside
            return max(sphere_vol(n, r1), sphere_vol(n, r2))
        else:
            x = 1. / 2 / D * np.sqrt(2 * r1**2 * r2**2 + 2 * D**2 * r1**2 +
                                     2 * D**2 * r2**2 - r1**4 - r2**4 - D**4)
            capsize1 = r1 - np.sqrt(r1**2 - x**2)
            capsize2 = r2 - np.sqrt(r2**2 - x**2)
            V = (sphere_vol(n, r1) + sphere_vol(n, r2) -
                 capvol(n, r1, capsize1) - capvol(n, r2, capsize2))
            return V

    rstate = get_rstate()
    ndim = 10
    cen1 = np.zeros(ndim)
    cen2 = np.zeros(ndim)
    r1 = 1
    r2 = 0.5
    sig1 = np.eye(ndim) * r1**2
    sig2 = np.eye(ndim) * r2**2
    Ds = np.linspace(0, 2, 30)
    nsamp = 10000
    for D in Ds:
        cen2[0] = D
        ell = db.MultiEllipsoid(
            [db.Ellipsoid(cen1, sig1),
             db.Ellipsoid(cen2, sig2)])
        lv = ell.monte_carlo_logvol(nsamp, rstate=rstate)[0]
        vtrue = two_sphere_vol(cen1, cen2, r1, r2)
        assert (np.abs(np.log(vtrue) - lv) < 1e-2)
예제 #19
0
def test_stop_nmc():
    # test stopping relying in n_mc
    ndim = 2
    rstate = get_rstate()
    sampler = dynesty.DynamicNestedSampler(loglike,
                                           prior_transform,
                                           ndim,
                                           nlive=nlive,
                                           rstate=rstate)
    sampler.run_nested(dlogz_init=1,
                       n_effective=None,
                       stop_kwargs=dict(n_mc=25),
                       print_progress=printing)
예제 #20
0
def test_samples_multi():
    rstate = get_rstate()
    ndim = 10
    cen = np.zeros(ndim) + .5
    sig = np.eye(ndim)
    ell = db.MultiEllipsoid([db.Ellipsoid(cen, sig)])
    nsamp = 10000
    X = ell.samples(nsamp, rstate=rstate)
    R = np.sqrt(((X - cen[None, :])**2).sum(axis=1))
    xdist1 = R**ndim
    pval = scipy.stats.kstest(xdist1,
                              scipy.stats.uniform(loc=0.0, scale=1).cdf)[1]
    assert ((pval > 1e-3) and (pval < 1 - 1e-3))
예제 #21
0
def test_pool():
    # test pool on egg problem
    rstate = get_rstate()
    with mp.Pool(2) as pool:
        sampler = dynesty.NestedSampler(loglike_egg,
                                        prior_transform_egg,
                                        ndim,
                                        nlive=nlive,
                                        pool=pool,
                                        queue_size=100,
                                        rstate=rstate)
        sampler.run_nested(dlogz=0.1, print_progress=printing)
        assert (abs(LOGZ_TRUTH_EGG - sampler.results.logz[-1]) <
                5. * sampler.results.logzerr[-1])
예제 #22
0
def test_bounds():
    rstate = get_rstate()
    Ndim = 20
    Ngood = 10
    eigv = np.abs(rstate.normal(size=Ngood))
    full_eig = np.concatenate((eigv, np.zeros(Ndim - Ngood)))
    randM = rstate.normal(size=((Ndim, Ndim)))
    M = randM.T @ np.diag(full_eig) @ randM
    R = db.improve_covar_mat(M)[1]
    assert ((scipy.linalg.eigh(R)[0] > 0).all())

    M = np.zeros((Ndim, Ndim))
    R = db.improve_covar_mat(M)[1]
    assert ((scipy.linalg.eigh(R)[0] > 0).all())
예제 #23
0
def test_bounds(bound, sample):
    # stress test various boundaries
    ndim = 2
    rstate = get_rstate()
    sampler = dynesty.NestedSampler(loglike_egg,
                                    prior_transform_egg,
                                    ndim,
                                    nlive=nlive,
                                    bound=bound,
                                    sample=sample,
                                    rstate=rstate)
    sampler.run_nested(dlogz=0.01, print_progress=printing)
    logz_truth = 235.856
    assert (abs(logz_truth - sampler.results.logz[-1]) <
            5. * sampler.results.logzerr[-1])
예제 #24
0
def test_pool_dynamic():
    # test pool in dynamic mode
    # here for speed I do a gaussian
    rstate = get_rstate()
    with mp.Pool(2) as pool:
        sampler = dynesty.DynamicNestedSampler(loglike_gau,
                                               prior_transform_gau,
                                               ndim,
                                               nlive=nlive,
                                               pool=pool,
                                               queue_size=100,
                                               rstate=rstate)
        sampler.run_nested(dlogz_init=1, print_progress=printing)
        assert (abs(LOGZ_TRUTH_GAU - sampler.results.logz[-1]) <
                5. * sampler.results.logzerr[-1])
예제 #25
0
def test_bounding_bootstrap(bound, sample):
    # check various bounding methods

    rstate = get_rstate()
    g = Gaussian()
    sampler = dynesty.NestedSampler(g.loglikelihood,
                                    g.prior_transform,
                                    g.ndim,
                                    nlive=nlive,
                                    bound=bound,
                                    sample=sample,
                                    bootstrap=5,
                                    rstate=rstate)
    sampler.run_nested(print_progress=printing)
    check_results_gau(sampler.results, g, rstate)
예제 #26
0
def test_generator():
    # Test that we can use the sampler as a generator
    rstate = get_rstate()
    g = Gaussian()
    sampler = dynesty.NestedSampler(g.loglikelihood,
                                    g.prior_transform,
                                    g.ndim,
                                    nlive=nlive,
                                    rstate=rstate)
    for it in sampler.sample():
        pass
    for it in sampler.add_live_points():
        pass
    res = sampler.results
    check_results_gau(res, g, rstate)
예제 #27
0
def test_oldstop():
    # test of old stopping function functionality
    ndim = 2
    rstate = get_rstate()
    import dynesty.utils as dyutil
    stopfn = dyutil.old_stopping_function
    sampler = dynesty.DynamicNestedSampler(loglike,
                                           prior_transform,
                                           ndim,
                                           nlive=nlive,
                                           rstate=rstate)
    sampler.run_nested(dlogz_init=1,
                       n_effective=None,
                       stop_function=stopfn,
                       print_progress=printing)
예제 #28
0
def test_livepoints():
    # Test the providing of initial live-points to the sampler
    ndim = 2
    rstate = get_rstate()
    live_u = rstate.uniform(size=(nlive, ndim))
    live_v = np.array([prior_transform(_) for _ in live_u])
    live_logl = np.array([loglike(_) for _ in live_v])
    live_points = [live_u, live_v, live_logl]
    sampler = dynesty.NestedSampler(loglike,
                                    prior_transform,
                                    ndim,
                                    nlive=nlive,
                                    live_points=live_points,
                                    rstate=rstate)
    sampler.run_nested(print_progress=printing)
    dyutil.unravel_run(sampler.results)
예제 #29
0
def test_saving():
    # test saving
    ndim = 2
    fname = 'xx.h5'
    rstate = get_rstate()
    sampler = dynesty.NestedSampler(loglike_egg,
                                    prior_transform_egg,
                                    ndim,
                                    nlive=nlive,
                                    bound='multi',
                                    sample='unif',
                                    save_history=True,
                                    history_filename=fname,
                                    rstate=rstate)
    sampler.run_nested(dlogz=1, print_progress=printing)
    assert (os.path.exists(fname))
예제 #30
0
def test_dyn():
    # hard test of dynamic sampler with high dlogz_init and small number
    # of live points
    ndim = 2
    THRESHOLD = 5  # in sigmas
    rstate = get_rstate()
    # this is expected to use unif sampler and multi bound
    sampler = dynesty.DynamicNestedSampler(loglike_egg,
                                           prior_transform_egg,
                                           ndim,
                                           nlive=nlive,
                                           rstate=rstate)
    sampler.run_nested(dlogz_init=1, print_progress=printing)
    assert (abs(LOGZ_TRUTH - sampler.results.logz[-1]) <
            THRESHOLD * sampler.results.logzerr[-1])
    print(sampler.citations)