示例#1
0
def test_stepsampler_cubeslice(plot=False):
    np.random.seed(3)
    sampler = ReactiveNestedSampler(paramnames, loglike, transform=transform1)
    sampler.stepsampler = CubeSliceSampler(nsteps=len(paramnames))
    r = sampler.run(log_interval=50, min_num_live_points=400)
    sampler.print_results()
    a = (np.abs(r['samples'] - 0.7) < 0.1).all(axis=1)
    b = (np.abs(r['samples'] - 0.3) < 0.1).all(axis=1)
    assert a.sum() > 1
    assert b.sum() > 1
示例#2
0
def test_stepsampler_adapt_when_stuck(plot=False):
    # check that a stuck sampler can free itself
    np.random.seed(1)
    us = np.random.normal(0.7, 0.001, size=(1000, len(paramnames)))
    region = make_region(len(paramnames), us=us)
    Ls = loglike(us)
    Lmin = Ls.min()

    print('CubeMHSampler')
    stepsampler = CubeMHSampler(nsteps=1, region_filter=True)
    np.random.seed(23)
    old_scale = stepsampler.scale
    for i in range(1000):
        if i > 100:
            assert False, i
        unew, pnew, Lnew, nc = stepsampler.__next__(region,
                                                    Lmin,
                                                    us,
                                                    Ls,
                                                    transform,
                                                    loglike,
                                                    ndraw=10)
        if unew is not None:
            break

    new_scale = stepsampler.scale
    assert new_scale != old_scale
    assert new_scale < 0.01, new_scale

    print('CubeSliceSampler')
    stepsampler = CubeSliceSampler(nsteps=1, region_filter=True)
    np.random.seed(23)
    old_scale = stepsampler.scale
    for j in range(100):
        for i in range(1000):
            if i > 100:
                assert False, i
            unew, pnew, Lnew, nc = stepsampler.__next__(region,
                                                        Lmin,
                                                        us,
                                                        Ls,
                                                        transform,
                                                        loglike,
                                                        ndraw=10)
            if unew is not None:
                break

    new_scale = stepsampler.scale
    assert new_scale != old_scale
    assert new_scale < 0.01, new_scale
示例#3
0
def main(args):
    nlive = args.num_live_points
    ndim = args.x_dim
    nsteps = args.nsteps
    problemname = args.problem

    samplers = [
        #CubeMHSampler(nsteps=16), #CubeMHSampler(nsteps=4), CubeMHSampler(nsteps=1),
        #RegionMHSampler(nsteps=16), #RegionMHSampler(nsteps=4), RegionMHSampler(nsteps=1),
        ##DESampler(nsteps=16), DESampler(nsteps=4), #DESampler(nsteps=1),
        CubeSliceSampler(
            nsteps=2 * ndim
        ),  #CubeSliceSampler(nsteps=ndim), CubeSliceSampler(nsteps=max(1, ndim//2)),
        #RegionSliceSampler(nsteps=ndim), RegionSliceSampler(nsteps=max(1, ndim//2)),
        #RegionSliceSampler(nsteps=2), RegionSliceSampler(nsteps=4),
        #RegionSliceSampler(nsteps=ndim), RegionSliceSampler(nsteps=4*ndim),
        #RegionBallSliceSampler(nsteps=2*ndim), RegionBallSliceSampler(nsteps=ndim), RegionBallSliceSampler(nsteps=max(1, ndim//2)),
        # RegionSequentialSliceSampler(nsteps=2*ndim), RegionSequentialSliceSampler(nsteps=ndim), RegionSequentialSliceSampler(nsteps=max(1, ndim//2)),

        #SpeedVariableRegionSliceSampler([Ellipsis]*ndim), SpeedVariableRegionSliceSampler([slice(i, ndim) for i in range(ndim)]),
        #SpeedVariableRegionSliceSampler([Ellipsis]*ndim + [slice(1 + ndim//2, None)]*ndim),
    ]
    if ndim < 14:
        samplers.insert(0, MLFriendsSampler())
    colors = {}
    linestyles = {1: ':', 2: ':', 4: '--', 16: '-', 32: '-', 64: '-', -1: '-'}
    markers = {1: 'x', 2: 'x', 4: '^', 16: 'o', 32: 's', 64: 's', -1: 'o'}
    for isteps, ls, m in (max(1, ndim // 2), ':',
                          'x'), (ndim, '--',
                                 '^'), (ndim + 1, '--',
                                        '^'), (ndim * 2, '-',
                                               'o'), (ndim * 4, '-.',
                                                      '^'), (ndim * 8, '-',
                                                             'v'), (ndim * 16,
                                                                    '-', '>'):
        if isteps not in markers:
            markers[isteps] = m
        if isteps not in linestyles:
            linestyles[isteps] = ls
    Lsequence_ref = None
    label_ref = None
    axL = plt.figure('Lseq').gca()
    axS = plt.figure('shrinkage').gca()
    axspeed = plt.figure('speed').gca()
    plt.figure('stepsize', figsize=(14, 6))
    axstep1 = plt.subplot(1, 3, 1)
    axstep2 = plt.subplot(1, 3, 2)
    axstep3 = plt.subplot(1, 3, 3)
    lastspeed = None, None, None
    for sampler in samplers:
        print("evaluating sampler: %s" % sampler)
        Lsequence, ncalls, steps = evaluate_warmed_sampler(
            problemname, ndim, nlive, nsteps, sampler)

        loglike, grad, volume, warmup = get_problem(problemname, ndim=ndim)
        assert np.isfinite(Lsequence).all(), Lsequence
        vol = np.asarray([volume(Li, ndim) for Li in Lsequence])
        assert np.isfinite(vol).any(), (
            "Sampler has not reached interesting likelihoods", vol, Lsequence)
        shrinkage = 1 - (vol[np.isfinite(vol)][1:] /
                         vol[np.isfinite(vol)][:-1])**(1. / ndim)

        fullsamplername = str(sampler)
        samplername = fullsamplername.split('(')[0]

        label = fullsamplername + ' %d evals' % ncalls
        if Lsequence_ref is None:
            label_ref = label
            Lsequence_ref = Lsequence
            ls = '-'
            color = 'pink'
        else:
            color = colors.get(samplername)
            ls = '-' if sampler.adaptive_nsteps else linestyles[sampler.nsteps]
            l, = axL.plot(Lsequence_ref,
                          Lsequence,
                          label=label,
                          color=color,
                          linestyle=ls,
                          lw=1)
            colors[samplername] = l.get_color()

        # convert to a uniformly distributed variable, according to expectations
        cdf_expected = 1 - (1 - shrinkage)**(ndim * nlive)
        axS.hist(cdf_expected,
                 cumulative=True,
                 density=True,
                 histtype='step',
                 bins=np.linspace(0, 1, 4000),
                 label=label,
                 color=color,
                 ls=ls)
        print("%s shrunk %.4f, from %d shrinkage samples" %
              (fullsamplername, cdf_expected.mean(), len(shrinkage)))
        axspeed.plot(cdf_expected.mean(),
                     ncalls,
                     markers[1 if sampler.adaptive_nsteps else sampler.nsteps],
                     label=label,
                     color=color)
        if lastspeed[0] == samplername:
            axspeed.plot([lastspeed[1], cdf_expected.mean()],
                         [lastspeed[2], ncalls],
                         '-',
                         color=color)
        lastspeed = [samplername, cdf_expected.mean(), ncalls]

        stepsize, angular_step, radial_step = steps.transpose()
        assert len(stepsize) == len(Lsequence), (len(stepsize), len(Lsequence))
        # here we estimate the volume differently: from the expected shrinkage per iteration
        it = np.arange(len(stepsizesq))
        vol = (1 - 1. / nlive)**it
        assert np.isfinite(vol).all(), vol
        assert (vol > 0).all(), vol
        assert (vol <= 1).all(), vol
        relstepsize = stepsize / vol**(1. / ndim)
        relradial_step = radial_step / vol**(1. / ndim)
        axstep1.hist(relstepsize[np.isfinite(relstepsize)],
                     bins=1000,
                     cumulative=True,
                     density=True,
                     histtype='step',
                     label=label,
                     color=color,
                     ls=ls)
        axstep2.hist(angular_step,
                     bins=1000,
                     cumulative=True,
                     density=True,
                     histtype='step',
                     label=label,
                     color=color,
                     ls=ls)
        axstep3.hist(relradial_step,
                     bins=1000,
                     cumulative=True,
                     density=True,
                     histtype='step',
                     label=label,
                     color=color,
                     ls=ls)
        sampler.plot(filename='evaluate_sampling_%s_%dd_N%d_%s.png' %
                     (args.problem, ndim, nlive, samplername))

    print('range:', Lsequence_ref[0], Lsequence_ref[-1])
    axL.plot([Lsequence_ref[0], Lsequence_ref[-1]],
             [Lsequence_ref[0], Lsequence_ref[-1]],
             '-',
             color='k',
             lw=1,
             label=label_ref)
    axL.set_xlabel('logL (reference)')
    axL.set_ylabel('logL')
    lo, hi = Lsequence_ref[int(len(Lsequence_ref) * 0.1)], Lsequence_ref[-1]
    axL.set_xlim(lo, hi)
    axL.set_ylim(lo, hi)
    axL.legend(loc='best', prop=dict(size=6))
    filename = 'evaluate_sampling_%s_%dd_N%d_L.pdf' % (args.problem, ndim,
                                                       nlive)
    print("plotting to %s ..." % filename)
    plt.figure('Lseq')
    plt.savefig(filename, bbox_inches='tight')
    plt.close()

    plt.figure('shrinkage')
    plt.xlabel('Shrinkage Volume')
    plt.ylabel('Cumulative Distribution')
    plt.xlim(0, 1)
    plt.plot([0, 1], [0, 1], '--', color='k')
    plt.legend(loc='upper left', prop=dict(size=6))
    filename = 'evaluate_sampling_%s_%dd_N%d_shrinkage.pdf' % (args.problem,
                                                               ndim, nlive)
    print("plotting to %s ..." % filename)
    plt.savefig(filename, bbox_inches='tight')
    plt.close()

    plt.figure('speed')
    plt.xlabel('Bias')
    plt.ylabel('# of function evaluations')
    plt.yscale('log')
    lo, hi = plt.xlim()
    hi = max(0.5 - lo, hi - 0.5, 0.04)
    plt.xlim(0.5 - hi, 0.5 + hi)
    lo, hi = plt.ylim()
    plt.vlines(0.5, lo, hi)
    plt.ylim(lo, hi)
    plt.legend(loc='best', prop=dict(size=6), fancybox=True, framealpha=0.5)
    filename = 'evaluate_sampling_%s_%dd_N%d_speed.pdf' % (args.problem, ndim,
                                                           nlive)
    print("plotting to %s ..." % filename)
    plt.savefig(filename, bbox_inches='tight')
    plt.savefig(filename.replace('.pdf', '.png'), bbox_inches='tight')
    plt.close()

    plt.figure('stepsize')
    axstep1.set_ylabel('Cumulative Distribution')
    axstep1.set_xlabel('Euclidean distance')
    axstep1.legend(loc='lower right', prop=dict(size=6))
    axstep2.set_ylabel('Cumulative Distribution')
    axstep2.set_xlabel('Angular distance')
    #axstep2.legend(loc='best', prop=dict(size=6))
    axstep3.set_ylabel('Cumulative Distribution')
    axstep3.set_xlabel('Radial distance')
    #axstep3.legend(loc='best', prop=dict(size=6))
    filename = 'evaluate_sampling_%s_%dd_N%d_step.pdf' % (args.problem, ndim,
                                                          nlive)
    print("plotting to %s ..." % filename)
    plt.savefig(filename, bbox_inches='tight')
    plt.close()