def get_logZ(datadirs, quiet=False):

    logZ = []
    logZ_err = []

    for rname in np.atleast_1d(datadirs):

        try:  #new version
            fname = os.path.join(rname, 'results', 'save_ns.pickle.gz')
            if not quiet:
                print('--------------------------')
                print(fname)
            #::: load the save_ns.pickle
            f = gzip.GzipFile(fname, 'rb')
            results = pickle.load(f)
            f.close()

        except:  #old version
            fname = os.path.join(rname, 'results', 'save_ns.pickle')
            if not quiet:
                print('--------------------------')
                print(fname)
            #::: load the save_ns.pickle
            with open(fname, 'rb') as f:
                results = pickle.load(f)

        #::: get the results
        logZdynesty = results.logz[
            -1]  # value of ln(Z) - the NATURAL logarithm of Z, see https://dynesty.readthedocs.io/en/latest/api.html
        logZerrdynesty = results.logzerr[
            -1]  # estimate of the statistcal uncertainty on ln(Z)

        #::: recalculate logZ error if it was NaN (bug in dynesty 0.9.2b)
        if np.isnan(logZerrdynesty) or np.isinf(logZerrdynesty) or (
                logZerrdynesty / logZdynesty > 1):
            if not quiet:
                print('recalculating ln(Z) error...')
            sys.stdout.flush()
            lnzs = np.zeros((10, len(results.logvol)))
            for i in tqdm(range(10), disable=quiet):
                results_s = dyutils.simulate_run(results)
                lnzs[i] = np.interp(-results.logvol, -results_s.logvol,
                                    results_s.logz)
            lnzerr = np.std(lnzs, axis=0)
            logZerrdynesty = lnzerr[-1]

        if not quiet:
            print('ln(Z) = {} +- {}'.format(logZdynesty, logZerrdynesty))

        logZ.append(logZdynesty)
        logZ_err.append(logZerrdynesty)

    return logZ, logZ_err
def get_delta_logZ_and_delta_labels(run_names, labels):

    logZ = []
    logZ_err = []

    for rname in run_names:
        fname = os.path.join(rname, 'results', 'save_ns.pickle')
        print('--------------------------')
        print(fname)

        #::: load the save_ns.pickle
        with open(fname, 'rb') as f:
            results = pickle.load(f)

        #::: get the results
        logZdynesty = results.logz[-1]  # value of logZ
        logZerrdynesty = results.logzerr[
            -1]  # estimate of the statistcal uncertainty on logZ

        #::: recalculate logZ error if it was NaN (bug in dynesty 0.9.2b)
        if np.isnan(logZerrdynesty) or np.isinf(logZerrdynesty) or (
                logZerrdynesty / logZdynesty > 1):
            print('recalculating logZ error...')
            sys.stdout.flush()
            lnzs = np.zeros((10, len(results.logvol)))
            for i in tqdm(range(10)):
                results_s = dyutils.simulate_run(results)
                lnzs[i] = np.interp(-results.logvol, -results_s.logvol,
                                    results_s.logz)
            lnzerr = np.std(lnzs, axis=0)
            logZerrdynesty = lnzerr[-1]

        print('log(Z) = {} +- {}'.format(logZdynesty, logZerrdynesty))

        logZ.append(logZdynesty)
        logZ_err.append(logZerrdynesty)

    #::: calculate delta_logZ
    delta_logZ = np.array(logZ) - logZ[0]
    delta_logZ_err = np.sqrt(np.array(logZ_err)**2 + np.array(logZ_err[0])**2)

    #::: remove the null hypothesis from the plot
    delta_logZ = delta_logZ[1:]
    delta_logZ_err = delta_logZ_err[1:]
    delta_labels = [
        labels[i + 1] + '\nvs.\n' + labels[0] for i in range(len(delta_logZ))
    ]

    return delta_logZ, delta_logZ_err, delta_labels
Esempio n. 3
0
def test_gaussian():
    logz_tol = 1
    sampler = dynesty.NestedSampler(loglikelihood_gau,
                                    prior_transform_gau,
                                    ntotdim,
                                    nlive=nlive,
                                    ncdim=ndim_gau)
    sampler.run_nested(print_progress=printing)
    # check that jitter/resample/simulate_run work
    # for not dynamic sampler
    dyfunc.jitter_run(sampler.results)
    dyfunc.resample_run(sampler.results)
    dyfunc.simulate_run(sampler.results)

    # add samples
    # check continuation behavior
    sampler.run_nested(dlogz=0.1, print_progress=printing)

    # get errors
    nerr = 2
    result_list = []
    for i in range(nerr):
        sampler.reset()
        sampler.run_nested(print_progress=False)
        results = sampler.results
        result_list.append(results)
        pos = results.samples
        wts = np.exp(results.logwt - results.logz[-1])
        mean, cov = dyfunc.mean_and_cov(pos, wts)
        logz = results.logz[-1]
        assert (np.abs(logz - logz_truth_gau) < logz_tol)
    res_comb = dyfunc.merge_runs(result_list)
    assert (np.abs(res_comb.logz[-1] - logz_truth_gau) < logz_tol)
    # check summary
    res = sampler.results
    res.summary()
Esempio n. 4
0
def get_logZ(run_names):

    logZ = []
    logZ_err = []

    for rname in run_names:

        try:  #new version
            fname = os.path.join(rname, 'results', 'save_ns.pickle.gz')
            print('--------------------------')
            print(fname)
            #::: load the save_ns.pickle
            f = gzip.GzipFile(fname, 'rb')
            results = pickle.load(f)
            f.close()

        except:  #old version
            fname = os.path.join(rname, 'results', 'save_ns.pickle')
            print('--------------------------')
            print(fname)
            #::: load the save_ns.pickle
            with open(fname, 'rb') as f:
                results = pickle.load(f)

        #::: get the results
        logZdynesty = results.logz[-1]  # value of logZ
        logZerrdynesty = results.logzerr[
            -1]  # estimate of the statistcal uncertainty on logZ

        #::: recalculate logZ error if it was NaN (bug in dynesty 0.9.2b)
        if np.isnan(logZerrdynesty) or np.isinf(logZerrdynesty) or (
                logZerrdynesty / logZdynesty > 1):
            print('recalculating logZ error...')
            sys.stdout.flush()
            lnzs = np.zeros((10, len(results.logvol)))
            for i in tqdm(range(10)):
                results_s = dyutils.simulate_run(results)
                lnzs[i] = np.interp(-results.logvol, -results_s.logvol,
                                    results_s.logz)
            lnzerr = np.std(lnzs, axis=0)
            logZerrdynesty = lnzerr[-1]

        print('log(Z) = {} +- {}'.format(logZdynesty, logZerrdynesty))

        logZ.append(logZdynesty)
        logZ_err.append(logZerrdynesty)

    return logZ, logZ_err
Esempio n. 5
0
def test_dynamic():
    # check dynamic nested sampling behavior
    logz_tol = 1
    dsampler = dynesty.DynamicNestedSampler(loglikelihood_gau,
                                            prior_transform_gau, ndim_gau)
    dsampler.run_nested(print_progress=printing)
    check_results_gau(dsampler.results, logz_tol)

    # check error analysis functions
    # IMPORTANT I had to bump up the agreement threshold to 6 sigma
    # this is too much and needs to be checked
    dres = dyfunc.jitter_run(dsampler.results)
    check_results_gau(dres, logz_tol)
    dres = dyfunc.resample_run(dsampler.results)
    check_results_gau(dres, logz_tol, sig=6)
    dres = dyfunc.simulate_run(dsampler.results)
    check_results_gau(dres, logz_tol, sig=6)
    # I bump the threshold
    # because we have the error twice
    dyfunc.kld_error(dsampler.results)
Esempio n. 6
0
def pyorbit_dynesty(config_in, input_datasets=None, return_output=None):

    output_directory = './' + config_in['output'] + '/dynesty/'

    mc = ModelContainerDynesty()
    pars_input(config_in, mc, input_datasets)

    if mc.nested_sampling_parameters['shutdown_jitter']:
        for dataset in mc.dataset_dict.itervalues():
            dataset.shutdown_jitter()

    mc.model_setup()
    mc.create_variables_bounds()
    mc.initialize_logchi2()

    mc.create_starting_point()

    results_analysis.results_resumen(mc, None, skip_theta=True)

    mc.output_directory = output_directory

    print()
    print('Reference Time Tref: ', mc.Tref)
    print()
    print('*************************************************************')
    print()

    import dynesty

    # "Standard" nested sampling.
    sampler = dynesty.NestedSampler(mc.dynesty_call, mc.dynesty_priors,
                                    mc.ndim)
    sampler.run_nested()
    results = sampler.results

    # "Dynamic" nested sampling.
    dsampler = dynesty.DynamicNestedSampler(mc.dynesty_call, mc.dynesty_priors,
                                            mc.ndim)
    dsampler.run_nested()
    dresults = dsampler.results

    from dynesty import plotting as dyplot

    # Plot a summary of the run.
    rfig, raxes = dyplot.runplot(results)

    # Plot traces and 1-D marginalized posteriors.
    tfig, taxes = dyplot.traceplot(results)

    # Plot the 2-D marginalized posteriors.
    cfig, caxes = dyplot.cornerplot(results)

    from dynesty import utils as dyfunc

    # Extract sampling results.
    samples = results.samples  # samples
    weights = np.exp(results.logwt - results.logz[-1])  # normalized weights

    # Compute 5%-95% quantiles.
    quantiles = dyfunc.quantile(samples, [0.05, 0.95], weights=weights)

    # Compute weighted mean and covariance.
    mean, cov = dyfunc.mean_and_cov(samples, weights)

    # Resample weighted samples.
    samples_equal = dyfunc.resample_equal(samples, weights)

    # Generate a new set of results with statistical+sampling uncertainties.
    results_sim = dyfunc.simulate_run(results)
    """ A dummy file is created to let the cpulimit script to proceed with the next step"""
    nested_sampling_create_dummy_file(mc)

    if return_output:
        return mc
    else:
        return
Esempio n. 7
0
sampler = dynesty.NestedSampler(loglikelihood,
                                prior_transform,
                                ndim,
                                nlive=nlive,
                                sample='hslice',
                                gradient=grad_u)
sampler.run_nested(print_progress=printing)
check_results(sampler.results, lz_tol, m_tol, c_tol)

# check dynamic nested sampling behavior
sys.stderr.write('\nDynamic Nested Sampling\n')
dsampler = dynesty.DynamicNestedSampler(loglikelihood, prior_transform, ndim)
dsampler.run_nested(print_progress=printing)
check_results(dsampler.results, lz_tol, m_tol, c_tol)

# check error analysis functions
sys.stderr.write('\nError Analysis\n')
sys.stderr.write('Jittering')
dres = dyfunc.jitter_run(dsampler.results)
check_results(dres, lz_tol, m_tol, c_tol)
sys.stderr.write('Resampling')
dres = dyfunc.resample_run(dsampler.results)
check_results(dres, lz_tol, m_tol, c_tol)
sys.stderr.write('Combined')
dres = dyfunc.simulate_run(dsampler.results)
check_results(dres, lz_tol, m_tol, c_tol)
sys.stderr.write('KLD Error\n')
dyfunc.kld_error(dsampler.results)

# if we got to the end, then we know things at least aren't totally broken!
Esempio n. 8
0
# we can post-process results

# Extract sampling results.
samples = results.samples  # samples
weights = np.exp(results.logwt - results.logz[-1])  # normalized weights

# Compute 10%-90% quantiles.
quantiles = [dyfunc.quantile(samps, [0.1, 0.9], weights=weights)
             for samps in samples.T]

# Compute weighted mean and covariance.
mean, cov = dyfunc.mean_and_cov(samples, weights)

# Resample weighted samples.
samples_equal = dyfunc.resample_equal(samples, weights)

# Generate a new set of results with statistical+sampling uncertainties.
results_sim = dyfunc.simulate_run(results)

cfig, caxes = dyplot.cornerplot(results_sim)


# print citations specfic to the configuration I am using
print(sampler.citations)


# this time, linear regression