Exemplo n.º 1
0
def plotPosteriors(omegafull,
                   weights,
                   labels,
                   names,
                   numgals=8,
                   markmin=0.3,
                   markmax=3):
    n = len(omegafull)
    gals = np.random.choice(len(omegafull[0]), size=numgals, replace=False)
    for i in range(n):
        il, ih = np.percentile(omegafull[i, gals, :], [2.5, 97.5])
        for j in range(i + 1, n):
            jl, jh = np.percentile(omegafull[j, gals, :], [2.5, 97.5])
            fig, ax = plt.subplots()
            for k, gal in enumerate(gals):
                sampi = dyfunc.resample_equal(omegafull[i, gal, :],
                                              weights[gal])
                sampj = dyfunc.resample_equal(omegafull[j, gal, :],
                                              weights[gal])
                wei = weights[gal]
                # s = (markmax-markmin)/(max(wei)-min(wei)) * (wei-min(wei)) + markmin
                ax.scatter(sampi, sampj, marker=next(markers), s=1, alpha=0.5)
            ax.set_xlabel(labels[i])
            ax.set_ylabel(labels[j])
            ax.set_xlim([il, ih])
            ax.set_ylim([jl, jh])
            fig.savefig("TwoParamPlots/Posterior_%s_vs_%s.png" %
                        (names[j], names[i]),
                        bbox_inches='tight',
                        dpi=300)
Exemplo n.º 2
0
def check_results_gau(results, g, rstate, sig=5, logz_tol=None):
    if logz_tol is None:
        logz_tol = sig * results.logzerr[-1]
    mean_tol, cov_tol = bootstrap_tol(results, rstate)
    # just check that resample_equal works
    dyfunc.resample_equal(results.samples,
                          np.exp(results.logwt - results.logz[-1]))
    check_results(results,
                  g.mean,
                  g.cov,
                  g.logz_truth,
                  mean_tol,
                  cov_tol,
                  logz_tol,
                  sig=sig)
Exemplo n.º 3
0
    def run(self, kwargs_run):
        """
        run the Dynesty nested sampler

        see https://dynesty.readthedocs.io for content of kwargs_run

        :param kwargs_run: kwargs directly passed to DynamicNestedSampler.run_nested
        :return: samples, means, logZ, logZ_err, logL, results
        """
        print("prior type :", self.prior_type)
        print("parameter names :", self.param_names)

        self._sampler.run_nested(**kwargs_run)

        results = self._sampler.results
        samples_w = results.samples  # weighted samples
        logL = results.logl
        logZ = results.logz
        logZ_err = results.logzerr

        # Compute weighted mean and covariance.
        weights = np.exp(results.logwt - logZ[-1])  # normalized weights
        if np.sum(weights) != 1.:
            # TODO : clearly this is not optimal...
            # weights should by definition be normalized, but it appears that for very small
            # number of live points (typically in test routines),
            # it is not *quite* the case (up to 6 decimals)
            weights = weights / np.sum(weights)

        means, covs = dyfunc.mean_and_cov(samples_w, weights)

        # Resample weighted samples to get equally weighted (aka unweighted) samples
        samples = dyfunc.resample_equal(samples_w, weights)

        return samples, means, logZ, logZ_err, logL, results
Exemplo n.º 4
0
def getPosteriorFromResult(result):
    ####################################################################################################
    from dynesty import utils as dyfunc
    weights = np.exp(result.logwt - result.logz[-1])  #normalized weights
    samples = dyfunc.resample_equal(result.samples,
                                    weights)  #Compute 10%-90% quantiles.
    return samples
Exemplo n.º 5
0
 def test_posterior_samples(self, sampler_setup):
     res = sampler_setup.results # get results dictionary from sampler
     # draw posterior samples
     weights = np.exp(res['logwt'] - res['logz'][-1])
     samples_dynesty = resample_equal(res.samples, weights)
     np.testing.assert_allclose(np.array([0.0, 0.0]),
                     np.mean(samples_dynesty, axis=0),
                     rtol=1.0, atol=0.0001)
 def run_uniform_weight(unweightedouput_bulge, unweightedouput_disc):
     """
     Method to produce uniformly weighted samples from the bayesian analysis.
     input:
         unweightedouput_bulge: Dictionary of unweighted bulge samples.
         unweightedouput_disc: Dictionary of unweighted disk samples.
     output:
         samplesbulge_equal, samplesdisc_equal: Numpy array of uniformly weighted samples
         for bulge and disc respectively.
     """
     samplesbulge, weightsbulge = unweightedouput_bulge.samples, \
         np.exp(unweightedouput_bulge.logwt - unweightedouput_bulge.logz[-1])
     samplesbulge_equal = dyfunc.resample_equal(samplesbulge, weightsbulge)
     samplesdisc, weightsdisc = unweightedouput_disc.samples, \
         np.exp(unweightedouput_disc.logwt - unweightedouput_disc.logz[-1])
     samplesdisc_equal = dyfunc.resample_equal(samplesdisc, weightsdisc)
     return samplesbulge_equal, samplesdisc_equal
Exemplo n.º 7
0
    def _samples(self, kappa, beta=1, return_full=False, print_progress=False):
        r"""
        Draws samples from the surrogate model (optionally the acquisition
        function). Calls `dynesty.NestedSampler`. Runs on a single thread even
        if `self.nthreads > 1`.

        Parameters
        ----------
        kappa : int
            Acquisition function parameter. See class documentation for more
            information.
        beta : float, optional
            Parallel tempering parameter :math:`L^{\beta}`, where :math:`L`
            is the target function. By default 1.
        return_full : bool, optional
            Whether to also return the sampled log-target values and the
            target evidence.
        print_progress : bool, optional
            Whether to print the sampler's progress bar.

        Returns
        -------
        samples : np.ndarray
            Sampled points from the surrogate model.
        logtarget : np.ndarray
            Optionally returned if `return_full=True`, the surrogate model
            target values.
        logz : int
            Optionally returned if `return_full=True`, the surrogate model
            evidence.
        """
        sampler = NestedSampler(self.surrogate_predict,
                                self._prior_transform,
                                ndim=len(self.params),
                                logl_kwargs={
                                    'kappa': kappa,
                                    'beta': beta
                                },
                                rstate=self.generator,
                                **self._sampler_kwargs)
        sampler.run_nested(print_progress=print_progress)

        results = sampler.results
        logz = results.logz[-1]
        weights = numpy.exp(results.logwt - logz)
        # Resample from the posterior
        samples = dyfunc.resample_equal(results.samples, weights)
        if return_full:
            logtarget = self.surrogate_predict(samples)
            # We're only interested in the contribution to the evidence the
            # likelihood (our target function). The uniform prior is used only
            # to provide samples, hence undo its contribution.
            logprior = -numpy.log(self._prior_max - self._prior_min).sum()
            logz -= logprior
            return samples, logtarget, logz
        return samples
Exemplo n.º 8
0
def draw_ns_posterior_samples(results, Nsamples=None):
    '''
    ! posterior samples are drawn as resampled weighted samples !
    ! do not confuse posterior_samples (weighted, resampled) with results['samples'] (unweighted) !
    '''
    weights = np.exp(results['logwt'] - results['logz'][-1])
    posterior_samples = dyutils.resample_equal(results['samples'], weights)    
    if Nsamples:
        posterior_samples = posterior_samples[np.random.randint(len(posterior_samples), size=Nsamples)]
    return posterior_samples
Exemplo n.º 9
0
def get_params_fit(results, return_sample=False):
    """ Get median, mean, covairance (and samples) from fitting results """
    samples = results.samples                                 # samples
    weights = np.exp(results.logwt - results.logz[-1])        # normalized weights 
    pmean, pcov = dyfunc.mean_and_cov(samples, weights)       # weighted mean and covariance
    samples_eq = dyfunc.resample_equal(samples, weights)      # resample weighted samples
    pmed = np.median(samples_eq,axis=0)
    
    if return_sample:
        return pmed, pmean, pcov, samples_eq
    else:
        return pmed, pmean, pcov
    def load_dynesty_samples(self, chain_file, save=False):
        """Load dynesty samples and get samples + log Z"""

        res = pickle.load(open(chain_file, 'rb'))

        samples = res.samples  # samples
        weights = np.exp(res.logwt - res.logz[-1])  # normalized weights
        samples = dyfunc.resample_equal(samples, weights) # Resample weighted samples.

        if save:
            np.save(chain_file.replace('.pickle','_samples'), samples)

        return samples, res.logz[-1]
Exemplo n.º 11
0
    def posterior_samples(self):
        """
        Returns posterior samples from nested samples and weights
        given by dynsety sampler
        """

        dynesty_samples = self._sampler.results['samples']
        wt = numpy.exp(self._sampler.results['logwt'] -
                       self._sampler.results['logz'][-1])
        # Make sure that sum of weights equal to 1
        weights = wt / numpy.sum(wt)
        posterior_dynesty = resample_equal(dynesty_samples, weights)
        return posterior_dynesty
Exemplo n.º 12
0
def calm2l_dynesty(in_res, alfvar, use_keys, outname, pool):
    print('creating results file:\n {0}results_dynesty/res_dynesty_{1}.hdf5'.
          format(ALFPY_HOME, outname))
    f1 = h5py.File(
        "{0}results_dynesty/res_dynesty_{1}.hdf5".format(ALFPY_HOME, outname),
        "w")
    for ikey in [
            'samples', 'logwt', 'logl', 'logvol', 'logz', 'logzerr',
            'information'
    ]:
        dset = f1.create_dataset(ikey,
                                 dtype=np.float16,
                                 data=np.array(getattr(in_res, ikey),
                                               dtype=np.float16))

    samples, weights = in_res.samples, np.exp(in_res.logwt - in_res.logz[-1])
    mean, cov = dyfunc.mean_and_cov(samples, weights)
    samples = dyfunc.resample_equal(in_res.samples, weights)

    dset = f1.create_dataset('samples_eq',
                             dtype=np.float16,
                             data=np.array(samples, dtype=np.float16))
    dset = f1.create_dataset('mean',
                             dtype=np.float16,
                             data=np.array(mean, dtype=np.float16))
    dset = f1.create_dataset('cov',
                             dtype=np.float16,
                             data=np.array(cov, dtype=np.float16))
    #dset = f1.create_dataset('use_keys', dtype=dt, data=use_keys)

    nspec = samples.shape[0]
    select_ind = np.random.choice(np.arange(nspec), size=1000)
    samples = np.copy(samples[select_ind, :])

    tstart = time.time()

    pwork = partial(worker_m2l, alfvar, use_keys)
    ml_res = pool.map(pwork, samples)

    ndur = time.time() - tstart
    print('\npost processing dynesty results: {:.2f}minutes'.format(ndur /
                                                                    60.))

    dset = f1.create_dataset('m2l',
                             dtype=np.float16,
                             data=np.array(ml_res, dtype=np.float16))
    f1.close()
Exemplo n.º 13
0
    def run_engine(engine):

        # -------------------- Run nested sampler ---------------------------
        engine.run_nested(dlogz=0.5, print_progress=True)

        # re-scale weights to have a maximum of one
        res = engine.results
        weights = np.exp(res['logwt'] - res['logz'][-1])
        weights[-1] = 1 - np.sum(weights[0:-1])

        post_samples = resample_equal(res.samples, weights)

        # Pull the evidence and the evidence error
        logz = res['logz']
        logzerr = res['logzerr']

        return post_samples, logz, logzerr
Exemplo n.º 14
0
    def retrieve(self,
                 retriever='DynamicNestedSampler',
                 dlogz=0.01,
                 nlive=1000,
                 wt_kwargs=None,
                 **dynesty_kwargs):
        if retriever == 'NestedSampler':
            fn = dynesty.NestedSampler
            dynesty_kwargs['nlive'] = nlive
            dynesty_kwargs['dlogz'] = dlogz
        elif retriever == 'DynamicNestedSampler':
            fn = dynesty.DynamicNestedSampler
            dynesty_kwargs['wt_kwargs'] = wt_kwargs
            dynesty_kwargs['nlive_init'] = nlive
            dynesty_kwargs['dlogz_init'] = dlogz
        else:
            raise ValueError("Unrecognized Sampler!")
        samp_kw = fn.__code__.co_varnames
        filter_by_key = lambda keys: {x: dynesty_kwargs[x] for \
         x in keys if x in dynesty_kwargs}
        samp_kwargs = filter_by_key(samp_kw)

        sampler = fn(self.log_likelihood,
                     self.prior_transform,
                     self.ndim,
                     logl_args=(self.data, self.errs, self.epochs),
                     periodic=self.periodic,
                     **samp_kwargs)

        run_kw = sampler.run_nested.__code__.co_varnames
        run_kwargs = filter_by_key(run_kw)
        sampler.run_nested(**run_kwargs)

        results = sampler.results
        self.results = results

        samples = results.samples
        weights = np.exp(results.logwt - results.logz[-1])
        samples_equal = dyfunc.resample_equal(samples, weights)
        self.samples_equal = samples_equal

        ind = np.argmax(results.logl)
        max_like_result = samples[ind]
        self.max_like_result = max_like_result

        return self.results
Exemplo n.º 15
0
def doit(sample='rslice', nlive=500, seed=1):
    # return uniformly weighted chain
    ndim = 2
    rstate = get_rstate(seed)
    ns = dynesty.NestedSampler(like,
                               cube,
                               ndim,
                               nlive=nlive,
                               sample=sample,
                               rstate=rstate)
    ns.run_nested(print_progress=printing)
    res = ns.results

    C = dyutil.resample_equal(res.samples,
                              np.exp(res.logwt - res.logz[-1]),
                              rstate=rstate)
    return C
Exemplo n.º 16
0
def nested_sampling(relationship,
                    prior_function=None,
                    progress=True,
                    dynamic=False,
                    **kwargs):
    """
    Perform the nested sampling, or dynamic nested sampling, in order to determine the Bayesian natural log evidence. See the :py:func:`dynesty.NestedSampler.run_nested()` documentation.

    Args:
        relationship (:py:class:`~uravu.relationship.Relationship`): The relationship to estimate the evidence for.
        prior_function (:py:attr:`callable`, optional): The function to populated some prior distributions. Default is the broad uniform priors in :func:`~uravu.relationship.Relationship.prior()`.
        progress (:py:attr:`bool`, optional): Show :py:mod:`tqdm` progress for sampling. Default is :py:attr:`True`.
        dynamic (:py:attr:`bool`, optional): Should dynamic nested sampling be used?. Default is :py:attr:`False`.

    Returns:
        :py:attr:`dict`: The results from :py:func:`dynesty.NestedSampler.run_nested()`.
    """
    if prior_function is None:
        prior_function = relationship.prior
    priors = prior_function()
    nested_sampler = dynesty.NestedSampler
    if dynamic:
        nested_sampler = dynesty.DynamicNestedSampler
    logl_args = [
        relationship.function, relationship.abscissa, relationship.ordinate
    ]
    sampler = nested_sampler(optimize.ln_likelihood,
                             nested_prior,
                             len(relationship.variables),
                             logl_args=logl_args,
                             ptform_args=[priors])

    sampler.run_nested(print_progress=progress, **kwargs)
    results = sampler.results
    samples = results['samples']
    weights = np.exp(results['logwt'] - results['logz'][-1])
    new_samples = dyfunc.resample_equal(samples, weights)
    distributions = []
    for i in range(new_samples.shape[1]):
        distributions.append(Distribution(new_samples[:, i]))
    results['distributions'] = distributions

    return results
 def posterior_sampler(self, t, sample_idx, ndims=2):
     nlive = 1024  # number of (initial) live points
     bound = 'multi'  # use MutliNest algorithm
     sample = 'rwalk'
     self.X = np.zeros(shape=(self.N, len(sample_idx)))
     self.S = np.zeros(shape=(self.N, len(sample_idx)))
     for i in range(self.N):
         _, sample_trajectories = self.simulate(t, self.action, sample_idx)
         self.X[i, :] = sample_trajectories[:, 0]
         self.S[i, :] = sample_trajectories[:, 1]
     dsampler = DynamicNestedSampler(self.loglikelihood,
                                     self.prior_transform,
                                     ndims,
                                     bound=bound)
     dsampler.run_nested(nlive_init=nlive)
     res = dsampler.results
     weights = np.exp(res['logwt'] - res['logz'][-1])
     samples_dynesty = resample_equal(res.samples, weights)
     return dsampler
Exemplo n.º 18
0
def calm2l_dynesty(in_res, alfvar, use_keys, outname, ncpu=1):
    print('creating results file:\n {0}results/res_dynesty_{1}.hdf5'.format(
        ALFPY_HOME, outname))
    f1 = h5py.File(
        "{0}results/res_dynesty_{1}.hdf5".format(ALFPY_HOME, outname), "w")
    for ikey in [
            'samples', 'logwt', 'logl', 'logvol', 'logz', 'logzerr',
            'information'
    ]:
        dset = f1.create_dataset(ikey,
                                 dtype=np.float16,
                                 data=getattr(in_res, ikey))

    samples, weights = in_res.samples, np.exp(in_res.logwt - in_res.logz[-1])
    mean, cov = dyfunc.mean_and_cov(samples, weights)
    samples = dyfunc.resample_equal(in_res.samples, weights)

    dset = f1.create_dataset('samples_eq', dtype=np.float16, data=samples)
    dset = f1.create_dataset('mean', dtype=np.float16, data=mean)
    dset = f1.create_dataset('cov', dtype=np.float16, data=cov)
    dset = f1.create_dataset('use_keys', data=use_keys)

    nspec = samples.shape[0]
    select_ind = np.random.choice(np.arange(nspec), size=1000)
    samples = np.copy(samples[select_ind, :])

    tstart = time.time()
    pwork = partial(worker_m2l, alfvar, use_keys)
    nspec = samples.shape[0]
    m2l_res = Parallel(n_jobs=ncpu)(delayed(pwork)(samples[i])
                                    for i in tqdm(range(nspec)))
    #m2l_res = executor.map(pwork, [ispec for ispec in samples])
    #pool = multiprocessing.Pool(ncpu)
    #m2l_res = pool.map(pwork, [ispec for ispec in samples])
    #pool.close()
    #pool.join()
    ndur = time.time() - tstart
    print('\npost processing dynesty results: {:.2f}minutes'.format(ndur /
                                                                    60.))

    dset = f1.create_dataset('m2l', dtype=np.float16, data=m2l_res)
    f1.close()
Exemplo n.º 19
0
def draw_ns_posterior_samples(results, Nsamples=None, as_type='2d_array'):
    '''
    ! posterior samples are drawn as resampled weighted samples !
    ! do not confuse posterior_samples (weighted, resampled) with results['samples'] (unweighted) !
    '''
    weights = np.exp(results['logwt'] - results['logz'][-1])
    np.random.seed(42)
    posterior_samples = dyutils.resample_equal(results['samples'], weights)    
    if Nsamples:
        posterior_samples = posterior_samples[np.random.randint(len(posterior_samples), size=Nsamples)]

    if as_type=='2d_array':
        return posterior_samples
    
    elif as_type=='dic':
        posterior_samples_dic = {}
        for key in config.BASEMENT.fitkeys:
            ind = np.where(config.BASEMENT.fitkeys==key)[0]
            posterior_samples_dic[key] = posterior_samples[:,ind].flatten()
        return posterior_samples_dic
Exemplo n.º 20
0
def plot_batch(path):
    """
    Plot the results of a batch run.

    """
    # Get the posterior means and covariances (w/o baseline mean and var)
    files = glob.glob(os.path.join(path, "*", "mean_and_cov.npz"))
    mean = np.empty((len(files), 5))
    cov = np.empty((len(files), 5, 5))
    for k, file in enumerate(files):
        data = np.load(file)
        mean[k] = data["mean"][:5]
        cov[k] = data["cov"][:5, :5]

    # Get the true values
    kwargs = update_with_defaults(**json.load(
        open(files[0].replace("mean_and_cov.npz", "kwargs.json"), "r")))
    truths = [
        kwargs["generate"]["radius"]["mu"],
        kwargs["generate"]["latitude"]["mu"],
        kwargs["generate"]["latitude"]["sigma"],
        kwargs["generate"]["contrast"]["mu"],
        kwargs["generate"]["nspots"]["mu"],
    ]
    labels = [r"$r$", r"$\mu_\phi$", r"$\sigma_\phi$", r"$c$", r"$n$"]

    # Misc plotting kwargs
    batch_bins = kwargs["plot"]["batch_bins"]
    batch_alpha = kwargs["plot"]["batch_alpha"]
    batch_nsig = kwargs["plot"]["batch_nsig"]

    # Plot the distribution of posterior means & variances
    fig, ax = plt.subplots(
        2,
        len(truths) + 1,
        figsize=(16, 6),
        gridspec_kw=dict(width_ratios=[1, 1, 1, 1, 1, 0.01]),
    )
    fig.subplots_adjust(hspace=0.4)
    for n in range(len(truths)):

        # Distribution of means
        ax[0, n].hist(mean[:, n],
                      histtype="step",
                      bins=batch_bins,
                      lw=2,
                      density=True)
        ax[0, n].axvline(np.mean(mean[:, n]), color="C0", ls="--")
        ax[0, n].axvline(truths[n], color="C1")

        # Distribution of errors (should be ~ std normal)
        deltas = (mean[:, n] - truths[n]) / np.sqrt(cov[:, n, n])
        ax[1, n].hist(
            deltas,
            density=True,
            histtype="step",
            range=(-4, 4),
            bins=batch_bins,
            lw=2,
        )
        ax[1, n].hist(
            np.random.randn(10000),
            density=True,
            range=(-4, 4),
            bins=batch_bins,
            histtype="step",
            lw=2,
        )

        ax[0, n].set_title(labels[n], fontsize=16)
        ax[0, n].set_xlabel("posterior mean")
        ax[1, n].set_xlabel("posterior error")
        ax[0, n].set_yticks([])
        ax[1, n].set_yticks([])

    # Tweak appearance
    ax[0, -1].axis("off")
    ax[0, -1].plot(0, 0, "C0", ls="--", label="mean")
    ax[0, -1].plot(0, 0, "C1", ls="-", label="truth")
    ax[0, -1].legend(loc="center left")
    ax[1, -1].axis("off")
    ax[1, -1].plot(0, 0, "C0", ls="-", lw=2, label="measured")
    ax[1, -1].plot(0, 0, "C1", ls="-", lw=2, label=r"$\mathcal{N}(0, 1)$")
    ax[1, -1].legend(loc="center left")
    fig.savefig(os.path.join(path, "calibration_bias.pdf"),
                bbox_inches="tight")
    plt.close()

    # Now let's plot all of the posteriors on a corner plot
    files = glob.glob(os.path.join(path, "*", "results.pkl"))
    samples = [None for k in range(len(files))]
    nsamp = 1e9
    ranges = [None for k in range(len(files))]
    for k in tqdm(range(len(files)),
                  disable=bool(int(os.getenv("NOTQDM", "0")))):

        # Get the samples (w/o baseline mean and var)
        with open(files[k], "rb") as f:
            results = pickle.load(f)
        samples[k] = np.array(results.samples)[:, :5]
        samples[k][:, 1], samples[k][:,
                                     2] = beta2gauss(samples[k][:, 1],
                                                     samples[k][:, 2])
        try:
            weights = np.exp(results["logwt"] - results["logz"][-1])
        except:
            weights = results["weights"]
        samples[k] = dyfunc.resample_equal(samples[k], weights)
        np.random.shuffle(samples[k])
        if len(samples[k]) < nsamp:
            nsamp = len(samples[k])

        # Get the 4-sigma ranges
        mu = np.mean(samples[k], axis=0)
        std = np.std(samples[k], axis=0)
        ranges[k] = np.array([mu - batch_nsig * std, mu + batch_nsig * std]).T

    # We need all runs to have the same number of samples
    # so our normalizations are correct in the histograms
    print("Keeping {} samples from each run.".format(nsamp))

    # Set plot limits to the maximum of the ranges
    ranges = np.array(ranges)
    ranges = np.array(
        [np.min(ranges[:, :, 0], axis=0),
         np.max(ranges[:, :, 1], axis=0)]).T

    span = [
        (kwargs["sample"]["rmin"], kwargs["sample"]["rmax"]),
        (0, 90),
        (0, 45),
        (kwargs["sample"]["cmin"], kwargs["sample"]["cmax"]),
        (kwargs["sample"]["nmin"], kwargs["sample"]["nmax"]),
    ]

    # Go!
    color = lambda i, alpha: "{}{}".format(
        colors.to_hex("C{}".format(i)),
        ("0" + hex(int(alpha * 256)).split("0x")[-1])[-2:],
    )
    fig = None
    cum_samples = np.empty((0, 5))
    for k in tqdm(range(len(mean)),
                  disable=bool(int(os.getenv("NOTQDM", "0")))):

        # Plot the 2d hist
        fig = corner(
            samples[k][:nsamp, :5],
            fig=fig,
            labels=labels,
            plot_datapoints=False,
            plot_density=False,
            fill_contours=True,
            no_fill_contours=True,
            color=color(k, 0.1 * batch_alpha),
            contourf_kwargs=dict(),
            contour_kwargs=dict(alpha=0),
            bins=20,
            hist_bin_factor=5,
            smooth=2.0,
            hist_kwargs=dict(alpha=0),
            levels=(1.0 - np.exp(-0.5 * np.array([1.0])**2)),
            range=ranges,
        )

        # Plot the 1d hist
        if k == len(mean) - 1:
            truths_ = truths
        else:
            truths_ = None
        fig = corner(
            samples[k][:nsamp, :5],
            fig=fig,
            labels=labels,
            plot_datapoints=False,
            plot_density=False,
            plot_contours=False,
            fill_contours=False,
            no_fill_contours=True,
            color=color(k, batch_alpha),
            bins=500,
            smooth1d=10.0,
            hist_kwargs=dict(alpha=0.5 * batch_alpha),
            range=ranges,
            truths=truths_,
            truth_color="#ff7f0e",
        )

        # Running list
        cum_samples = np.vstack((cum_samples, samples[k][:nsamp, :5]))

    # Plot the cumulative posterior
    np.random.shuffle(cum_samples)
    fig = corner(
        cum_samples,
        fig=fig,
        labels=labels,
        plot_datapoints=False,
        plot_density=False,
        fill_contours=False,
        no_fill_contours=True,
        color="k",
        contourf_kwargs=dict(),
        contour_kwargs=dict(linewidths=1),
        bins=100,
        hist_bin_factor=5,
        smooth=1.0,
        hist_kwargs=dict(alpha=0),
        levels=(1.0 - np.exp(-0.5 * np.array([1.0])**2)),
        range=ranges,
    )
    fig = corner(
        cum_samples[:nsamp],
        fig=fig,
        labels=labels,
        plot_datapoints=False,
        plot_density=False,
        plot_contours=False,
        fill_contours=False,
        no_fill_contours=True,
        color="k",
        bins=500,
        smooth1d=10.0,
        hist_kwargs=dict(lw=1),
        range=ranges,
    )

    # Fix the axis limits
    ax = np.array(fig.axes).reshape(5, 5)
    for k in range(5):
        axis = ax[k, k]
        ymax = np.max([line._y.max() for line in axis.lines])
        axis.set_ylim(0, 1.1 * ymax)
        for axis in ax[:, k]:
            axis.set_xlim(*span[k])
        for axis in ax[k, :k]:
            axis.set_ylim(*span[k])

    # We're done
    fig.savefig(
        os.path.join(path, "calibration_corner.png"),
        bbox_inches="tight",
        dpi=300,
    )

    # Get the inclination posterior means and covariances (if present)
    inc_files = glob.glob(os.path.join(path, "*", "inclinations.npz"))
    if len(inc_files):

        data_files = [
            file.replace("inclinations", "data") for file in inc_files
        ]
        x = np.linspace(-90, 90, 1000)
        deltas = []

        # Compute the "posterior error" histogram
        for k in tqdm(range(len(inc_files)),
                      disable=bool(int(os.getenv("NOTQDM", "0")))):
            data = np.load(data_files[k])
            results = np.load(inc_files[k])
            truths = data["incs"]
            inc = results["inc"]
            lp = results["lp"]
            nlc = len(truths)
            nsamples = lp.shape[1]
            for n in range(nlc):
                for j in range(nsamples):
                    pdf = np.exp(lp[n, j] - np.max(lp[n, j]))
                    pdf /= np.trapz(pdf)
                    mean = np.trapz(inc * pdf)
                    var = np.trapz(inc**2 * pdf) - mean**2
                    deltas.append((mean - truths[n]) / np.sqrt(var))

        # Plot it
        fig, ax = plt.subplots(1, figsize=(6, 3))
        ax.hist(deltas, bins=50, range=(-5, 5), density=True, label="measured")
        ax.hist(
            Normal.rvs(size=len(deltas)),
            bins=50,
            range=(-5, 5),
            histtype="step",
            color="C1",
            density=True,
            label=r"$\mathcal{N}(0, 1)$",
        )
        ax.legend(loc="upper right", fontsize=10)
        ax.set_xlabel("inclination posterior error")
        ax.set_ylabel("density")
        fig.savefig(os.path.join(path, "inclinations.pdf"),
                    bbox_inches="tight")
Exemplo n.º 21
0
ndims = 2         # two parameters

dsampler = DynamicNestedSampler(loglikelihood_dynesty, prior_transform, ndims,
                                bound=bound, sample=sample)
dsampler.run_nested(nlive_init=nlive)
dres = dsampler.results

dlogZdynesty = dres.logz[-1]        # value of logZ
dlogZerrdynesty = dres.logzerr[-1]  # estimate of the statistcal uncertainty on logZ

# output marginal likelihood
print('Marginalised evidence (using dynamic sampler) is {} ± {}'.format(dlogZdynesty, dlogZerrdynesty))

# get the posterior samples
dweights = np.exp(dres['logwt'] - dres['logz'][-1])
dpostsamples = resample_equal(dres.samples, dweights)

print('Number of posterior samples (using dynamic sampler) is {}'.format(dpostsamples.shape[0]))

# Now run with the static sampler
sampler = NestedSampler(loglikelihood_dynesty, prior_transform, ndims,
                        bound=bound, sample=sample, nlive=nlive)
sampler.run_nested(dlogz=0.1)

res = sampler.results

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

# output marginal likelihood
print('Marginalised evidence (using static sampler) is {} ± {}'.format(logZdynesty, logZerrdynesty))
Exemplo n.º 22
0
    def write_current_state(self, plot=True):
        """
        Write the current state of the sampler to disk.

        The required information to reconstruct the state of the run are written
        to an hdf5 file.
        All but the most recent removed live point in the chain are removed from
        the sampler to reduce memory usage.
        This means it is necessary to not append the first live point to the
        file if updating a previous checkpoint.

        Parameters
        ----------
        sampler: `dynesty.NestedSampler`
            NestedSampler to write to disk.
        """
        check_directory_exists_and_if_not_mkdir(self.outdir)
        print("")
        logger.info("Writing checkpoint file {}".format(self.resume_file))

        end_time = datetime.datetime.now()
        if hasattr(self, 'start_time'):
            self.sampling_time += end_time - self.start_time
            self.start_time = end_time

        current_state = dict(
            unit_cube_samples=self.sampler.saved_u,
            physical_samples=self.sampler.saved_v,
            sample_likelihoods=self.sampler.saved_logl,
            sample_log_volume=self.sampler.saved_logvol,
            sample_log_weights=self.sampler.saved_logwt,
            cumulative_log_evidence=self.sampler.saved_logz,
            cumulative_log_evidence_error=self.sampler.saved_logzvar,
            cumulative_information=self.sampler.saved_h,
            id=self.sampler.saved_id,
            it=self.sampler.saved_it,
            nc=self.sampler.saved_nc,
            boundidx=self.sampler.saved_boundidx,
            bounditer=self.sampler.saved_bounditer,
            scale=self.sampler.saved_scale,
            sampling_time=self.sampling_time.total_seconds())

        current_state.update(ncall=self.sampler.ncall,
                             live_logl=self.sampler.live_logl,
                             iteration=self.sampler.it - 1,
                             live_u=self.sampler.live_u,
                             live_v=self.sampler.live_v,
                             nlive=self.sampler.nlive,
                             live_bound=self.sampler.live_bound,
                             live_it=self.sampler.live_it,
                             added_live=self.sampler.added_live)

        try:
            weights = np.exp(current_state['sample_log_weights'] -
                             current_state['cumulative_log_evidence'][-1])
            from dynesty.utils import resample_equal

            current_state['posterior'] = resample_equal(
                np.array(current_state['physical_samples']), weights)
            current_state['search_parameter_keys'] = self.search_parameter_keys
        except ValueError:
            logger.debug("Unable to create posterior")

        with open(self.resume_file, 'wb') as file:
            pickle.dump(current_state, file)

        if plot and self.check_point_plot:
            import dynesty.plotting as dyplot
            labels = [
                label.replace('_', ' ') for label in self.search_parameter_keys
            ]
            filename = "{}/{}_checkpoint_trace.png".format(
                self.outdir, self.label)
            try:
                fig = dyplot.traceplot(self.sampler.results, labels=labels)[0]
                fig.tight_layout()
                fig.savefig(filename)
                plt.close('all')
            except (RuntimeError, np.linalg.linalg.LinAlgError,
                    ValueError) as e:
                logger.warning(e)
                logger.warning(
                    'Failed to create dynesty state plot at checkpoint')
Exemplo n.º 23
0
from astropy.time import Time
import matplotlib.pyplot as plt

# base data path for the TESS known planet project
pathdata = os.environ['KNWN_DATA_PATH'] + '/'
pathdatapost = os.environ['KNWN_DATA_PATH'] + '/postproc/'

# name of the exoplanet
strgplan = sys.argv[1]

pathdataplan = pathdata + '%s/allesfit_global/allesfit_onlytess_full/' % strgplan
config.init(pathdataplan)
fileobjt = open(pathdataplan + 'results/save_ns.pickle', 'rb')
objtrest = pickle.load(fileobjt)
weig = np.exp(objtrest['logwt'] - objtrest['logz'][-1])
chan = dyutils.resample_equal(objtrest.samples, weig)

listkeys = config.BASEMENT.fitkeys
listlabl = config.BASEMENT.fitlabels

# get period and epoch posteriors
for k, labl in enumerate(listlabl):
    if listkeys[k] == 'b_epoch':
        postepoc = chan[:, k]
    if listkeys[k] == 'b_period':
        postperi = chan[:, k]

# the time at which the transit is to be predicted
timejwst = '2022-01-01T00:00:00'

objttime = Time(timejwst, format='isot', scale='utc')
Exemplo n.º 24
0
                                bound=bound,
                                sample=sample)
dsampler.run_nested(nlive_init=nlive)
dres = dsampler.results

dlogZdynesty = dres.logz[-1]  # value of logZ
dlogZerrdynesty = dres.logzerr[
    -1]  # estimate of the statistcal uncertainty on logZ

# output marginal likelihood
print('Marginalised evidence (using dynamic sampler) is {} ± {}'.format(
    dlogZdynesty, dlogZerrdynesty))

# get the posterior samples
dweights = np.exp(dres['logwt'] - dres['logz'][-1])
dpostsamples = resample_equal(dres.samples, dweights)

print('Number of posterior samples (using dynamic sampler) is {}'.format(
    dpostsamples.shape[0]))

# Now run with the static sampler
sampler = NestedSampler(loglikelihood_dynesty,
                        prior_transform,
                        ndims,
                        bound=bound,
                        sample=sample,
                        nlive=nlive)
sampler.run_nested(dlogz=0.1)

res = sampler.results
Exemplo n.º 25
0
    def fit_nested(self):
        freekeys = list(self.bounds.keys())
        boundarray = np.array([self.bounds[k] for k in freekeys])
        bounddiff = np.diff(boundarray,1).reshape(-1)

        def loglike(pars):
            # chi-squared
            for i in range(len(pars)):
                self.prior[freekeys[i]] = pars[i]
            model = transit(self.time, self.prior)
            model *= (self.prior['a1']*np.exp(self.prior['a2']*self.airmass))
            return -0.5 * np.sum( ((self.data-model)/self.dataerr)**2 )

        def prior_transform(upars):
            # transform unit cube to prior volume
            return (boundarray[:,0] + bounddiff*upars)

        dsampler = dynesty.DynamicNestedSampler(
            loglike, prior_transform,
            ndim=len(freekeys), bound='multi', sample='unif',
            maxiter_init=5000, dlogz_init=1, dlogz=0.05,
            maxiter_batch=100, maxbatch=10, nlive_batch=100
        )
        dsampler.run_nested()
        self.results = dsampler.results

        # alloc data for best fit + error
        self.errors = {}
        self.quantiles = {}
        self.parameters = copy.deepcopy(self.prior)

        tests = [copy.deepcopy(self.prior) for i in range(6)]

        # Derive kernel density estimate for best fit
        weights = np.exp(self.results.logwt - self.results.logz[-1])
        samples = self.results['samples']
        logvol = self.results['logvol']
        wt_kde = gaussian_kde(resample_equal(-logvol, weights))  # KDE
        logvol_grid = np.linspace(logvol[0], logvol[-1], 1000)  # resample
        wt_grid = wt_kde.pdf(-logvol_grid)  # evaluate KDE PDF
        self.weights = np.interp(-logvol, -logvol_grid, wt_grid)  # interpolate

        # errors + final values
        mean, cov = dynesty.utils.mean_and_cov(self.results.samples, weights)
        mean2, cov2 = dynesty.utils.mean_and_cov(self.results.samples, self.weights)
        for i in range(len(freekeys)):
            self.errors[freekeys[i]] = cov[i,i]**0.5
            tests[0][freekeys[i]] = mean[i]
            tests[1][freekeys[i]] = mean2[i]

            counts, bins = np.histogram(samples[:,i], bins=100, weights=weights)
            mi = np.argmax(counts)
            tests[5][freekeys[i]] = bins[mi] + 0.5*np.mean(np.diff(bins))

            # finds median and +- 2sigma, will vary from mode if non-gaussian
            self.quantiles[freekeys[i]] = dynesty.utils.quantile(self.results.samples[:,i], [0.025, 0.5, 0.975], weights=weights)
            tests[2][freekeys[i]] = self.quantiles[freekeys[i]][1]

        # find minimum near weighted mean
        mask = (samples[:,0] < self.parameters[freekeys[0]]+2*self.errors[freekeys[0]]) & (samples[:,0] > self.parameters[freekeys[0]]-2*self.errors[freekeys[0]])
        bi = np.argmin(self.weights[mask])

        for i in range(len(freekeys)):
            tests[3][freekeys[i]] = samples[mask][bi,i]
            tests[4][freekeys[i]] = np.average(samples[mask][:,i],weights=self.weights[mask],axis=0)

        # find best fit
        chis = []
        for i in range(len(tests)):
            lightcurve = transit(self.time, tests[i])
            airmass = tests[i]['a1']*np.exp(tests[i]['a2']*self.airmass)
            residuals = self.data - (lightcurve*airmass)
            chis.append( np.sum(residuals**2) )

        mi = np.argmin(chis)
        self.parameters = copy.deepcopy(tests[mi])

        # final model
        self.create_fit_variables()
Exemplo n.º 26
0
    def fit(self,
            sed,
            e_sed,
            parallax=[100, 0.001],
            nlive=250,
            distance=None,
            binary=False,
            plot_fit=True,
            plot_trace=False,
            plot_corner=False,
            progress=False,
            textx=0.025,
            textsize=12):

        if self.to_flux:
            sed = self.mag_to_flux(sed)
            e_sed = sed * e_sed  # magnitude error to flux error

        if not binary:
            ndim = 3

            def loglike(theta):
                teff, logg, plx = theta
                model = self.model_sed(teff, logg, plx)
                ivar = 1 / e_sed**2
                logchi = -0.5 * np.sum((sed - model)**2 * ivar)
                if np.isnan(logchi):
                    return -np.Inf
                else:
                    return logchi

            def prior_transform(u):
                x = np.array(u)
                x[0] = u[0] * (self.teff_range[1] -
                               self.teff_range[0]) + self.teff_range[0]
                x[1] = u[1] * (self.logg_range[1] -
                               self.logg_range[0]) + self.logg_range[0]
                t = stats.norm.ppf(u[2])
                x[2] = parallax[1] * t
                x[2] += parallax[0]
                return x

        elif binary:
            ndim = 5

            def loglike(theta):
                teff1, logg1, teff2, logg2, plx = theta

                model = self.model_binary_sed(teff1, logg1, teff2, logg2, plx)

                ivar = 1 / e_sed**2
                logchi = -0.5 * np.sum((sed - model)**2 * ivar)
                if np.isnan(logchi):
                    return -np.Inf
                elif teff1 > teff2:
                    return -np.Inf
                else:
                    return logchi

            def prior_transform(u):
                x = np.array(u)
                x[0] = u[0] * (self.teff_range[1] -
                               self.teff_range[0]) + self.teff_range[0]
                x[1] = u[1] * (self.logg_range[1] -
                               self.logg_range[0]) + self.logg_range[0]
                x[2] = u[2] * (self.teff_range[1] -
                               self.teff_range[0]) + self.teff_range[0]
                x[3] = u[3] * (self.logg_range[1] -
                               self.logg_range[0]) + self.logg_range[0]
                t = stats.norm.ppf(u[4])
                x[4] = parallax[1] * t
                x[4] += parallax[0]
                return x

        ########## DYNESTY ###################

        dsampler = dynesty.NestedSampler(loglike,
                                         prior_transform,
                                         ndim=ndim,
                                         nlive=nlive)
        dsampler.run_nested(print_progress=progress)

        result = dsampler.results

        samples, weights = result.samples, np.exp(result.logwt -
                                                  result.logz[-1])
        chis = -2 * np.array([loglike(sample) for sample in result.samples])
        bestfit = np.argmin(chis)
        resampled = dyfunc.resample_equal(samples, weights)
        cov = np.var(resampled, axis=0)

        mean = result.samples[bestfit]

        print(result.samples[bestfit])

        bandwls = []
        for band in self.bands:
            bandwls.append(self.mean_wl[band])

        ########## PLOTTING #################

        if plot_trace:

            f = dyplot.traceplot(dsampler.results,
                                 show_titles=True,
                                 trace_cmap='viridis')
            plt.tight_layout()
        if plot_corner:

            if binary:
                f = dyplot.cornerplot(dsampler.results,
                                      show_titles=True,
                                      labels=[
                                          '$T_{\mathrm{eff,1}}$',
                                          '$\log{g}_1$',
                                          '$T_{\mathrm{eff,2}}$',
                                          '$\log{g}_2$', r'$\varpi$'
                                      ])
            if not binary:
                f = dyplot.cornerplot(
                    dsampler.results,
                    show_titles=True,
                    labels=['$T_{\mathrm{eff}}$', '$\log{g}$', r'$\varpi$'])

            plt.tight_layout()

        if not binary:

            model = self.model_sed(*mean)
            ivar = 1 / e_sed**2
            redchi = np.sum((sed - model)**2 * ivar) / (len(sed) - ndim)

            if plot_fit:

                plt.figure(figsize=(10, 5))
                plt.errorbar(bandwls,
                             sed,
                             yerr=e_sed,
                             linestyle='none',
                             capsize=5,
                             color='k')
                plt.scatter(bandwls, model, color='k')
                plt.text(textx,
                         0.35,
                         '$T_{\mathrm{eff}}$ = %i ± %i' %
                         (mean[0], np.sqrt(cov[0])),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.text(textx,
                         0.25,
                         '$\log{g}$ = %.2f ± %.2f' %
                         (mean[1], np.sqrt(cov[1])),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.text(textx,
                         0.15,
                         'atm = %s' % (self.atm_type),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.text(textx,
                         0.05,
                         '$\chi_r^2$ = %.2f' % (redchi),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.xlabel('Wavelength ($\mathrm{\AA}$', fontsize=16)
                plt.ylabel(
                    '$f_\lambda\ [erg\ cm^{-2}\ s^{-1}\ \mathrm{\AA}^{-1}]$',
                    fontsize=16)
                plt.yscale('log')

            return [mean[0], np.sqrt(cov[0]), mean[1], np.sqrt(cov[1])], redchi

        elif binary:

            model = self.model_binary_sed(*mean)

            ivar = 1 / e_sed**2
            redchi = np.sum((sed - model)**2 * ivar) / (len(sed) - ndim)

            if plot_fit:

                plt.figure(figsize=(10, 5))
                plt.errorbar(bandwls,
                             sed,
                             yerr=e_sed,
                             linestyle='none',
                             capsize=5,
                             color='k')
                plt.scatter(bandwls, model, color='k')
                plt.text(textx,
                         0.45,
                         '$T_{\mathrm{eff,1}}$ = %i ± %i' %
                         (mean[0], np.sqrt(cov[0])),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.text(textx,
                         0.35,
                         '$\log{g}_1$ = %.2f ± %.2f' %
                         (mean[1], np.sqrt(cov[1])),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.text(textx,
                         0.25,
                         '$T_{\mathrm{eff,2}}$ = %i ± %i' %
                         (mean[2], np.sqrt(cov[2])),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.text(textx,
                         0.15,
                         '$\log{g}_2$ = %.2f ± %.2f' %
                         (mean[3], np.sqrt(cov[3])),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                #plt.text(0.15, 0.2, 'atm = %s' %(self.atm_type), transform = plt.gca().transAxes, fontsize = 12)
                plt.text(textx,
                         0.05,
                         '$\chi_r^2$ = %.2f' % (redchi),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.xlabel('Wavelength ($\mathrm{\AA}$)', fontsize=16)
                plt.ylabel(
                    '$f_\lambda\ [erg\ cm^{-2}\ s^{-1}\ \mathrm{\AA}^{-1}]$',
                    fontsize=16)
                plt.yscale('log')

            return [
                mean[0],
                np.sqrt(cov[0]), mean[1],
                np.sqrt(cov[1]), mean[2],
                np.sqrt(cov[2]), mean[3],
                np.sqrt(cov[3])
            ], redchi
Exemplo n.º 27
0
def plot_latitude_pdf(results, **kwargs):
    """
    Plot posterior draws from the latitude hyperdistribution.

    """
    # Get kwargs
    kwargs = update_with_defaults(**kwargs)
    plot_kwargs = kwargs["plot"]
    gen_kwargs = kwargs["generate"]
    mu_true = gen_kwargs["latitude"]["mu"]
    sigma_true = gen_kwargs["latitude"]["sigma"]
    nlat_pts = plot_kwargs["nlat_pts"]
    nlat_samples = plot_kwargs["nlat_samples"]

    # Resample to equal weight
    samples = np.array(results.samples)
    try:
        weights = np.exp(results["logwt"] - results["logz"][-1])
    except:
        weights = results["weights"]
    samples = dyfunc.resample_equal(samples, weights)

    # Function to compute the pdf for a draw
    _draw_pdf = lambda x, a, b: StarryProcess(a=a, b=b).latitude.pdf(x)
    _x = tt.dvector()
    _a = tt.dscalar()
    _b = tt.dscalar()

    # The true pdf
    draw_pdf = theano.function([_x, _a, _b], _draw_pdf(_x, _a, _b))
    x = np.linspace(-89.9, 89.9, nlat_pts)
    if np.isfinite(sigma_true):
        pdf_true = 0.5 * (Normal.pdf(x, mu_true, sigma_true) +
                          Normal.pdf(x, -mu_true, sigma_true))
    else:
        # Isotropic (special case)
        pdf_true = 0.5 * np.cos(x * np.pi / 180) * np.pi / 180

    # Draw sample pdfs
    pdf = np.empty((nlat_samples, nlat_pts))
    for k in range(nlat_samples):
        idx = np.random.randint(len(samples))
        pdf[k] = draw_pdf(x, samples[idx, 1], samples[idx, 2])

    # Plot
    fig, ax = plt.subplots(1)
    for k in range(nlat_samples):
        ax.plot(x, pdf[k], "C0-", lw=1, alpha=0.05, zorder=-1)
    ax.plot(x, pdf_true, "C1-", label="truth")
    ax.plot(x, np.nan * x, "C0-", label="samples")
    ax.legend(loc="upper right")
    ax.set_xlim(-90, 90)
    xticks = [-90, -75, -60, -45, -30, -15, 0, 15, 30, 45, 60, 75, 90]
    ax.set_xticks(xticks)
    ax.set_xticklabels(["{:d}$^\circ$".format(xt) for xt in xticks])
    ax.set_xlabel("latitude", fontsize=16)
    ax.set_ylabel("probability", fontsize=16)
    # Constrain y lims?
    mx1 = np.max(pdf_true)
    mx2 = np.sort(pdf.flatten())[int(0.9 * len(pdf.flatten()))]
    mx = max(2.0 * mx1, 1.2 * mx2)
    ax.set_ylim(-0.1 * mx, mx)
    ax.set_rasterization_zorder(1)
    return fig
Exemplo n.º 28
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
Exemplo n.º 29
0
def plot_a_results(results, pdfs, pdf_weights, suffix, a_min, a_max):
    samples = results.samples
    weights = np.exp(results.logwt - results.logz[-1])
    samples_equal = dyfunc.resample_equal(samples, weights)

    # results.summary()
    mean, cov = dyfunc.mean_and_cov(samples, weights)
    errors = np.diagonal(cov)**0.5

    maxL_index = results['logl'].argmax()
    maxL_params = samples[maxL_index]

    param_names = ['alpha1']  #, 'alpha2', 'log_a_break', 'amp']

    for ii in range(len(mean)):
        print('{0:5s} = {1:5.2f} +/- {2:5.2f}, maxL = {3:5.2f}'.format(
            param_names[ii], mean[ii], errors[ii], maxL_params[ii]))

    plt.close('all')

    # dyplot.runplot(results)
    # plt.savefig('dnest_a_run_' + suffix + '.png')

    dyplot.traceplot(results)
    plt.savefig('dnest_a_trace_' + suffix + '.png')

    dyplot.cornerplot(results)
    plt.savefig('dnest_a_corner_' + suffix + '.png')

    # Make a plot of the resulting distributions.
    # Note these bins have to match what we used to make the PDFs in the first place.
    a_bin = np.logspace(3, 8, 50)
    # a_bin = np.linspace(1e3, 1e6, 100)
    a_bin_mid = a_bin[:-1] + np.diff(a_bin)

    alpha1 = mean[0]
    # alpha2 = mean[1]
    # a_break = 10**mean[2]

    # p_a = broken_powerlaw_trunc(a_bin_mid, alpha1, alpha2, a_break, a_min=a_min, a_max=a_max)
    p_a = powerlaw_trunc(a_bin_mid, alpha1, a_min=a_min, a_max=a_max)

    N_samp = 1000
    p_a_nk = np.zeros((len(a_bin_mid), N_samp), dtype=float)
    for ss in range(N_samp):
        # p_a_nk[:, ss] = broken_powerlaw_trunc(a_bin_mid,
        #                                           samples_equal[ss, 0],
        #                                           samples_equal[ss, 1],
        #                                           10**samples_equal[ss, 2],
        #                                           a_min=a_min, a_max=a_max)
        p_a_nk[:, ss] = powerlaw_trunc(a_bin_mid,
                                       samples_equal[ss, 0],
                                       a_min=a_min,
                                       a_max=a_max)

    fix, ax = plt.subplots(2, 1, sharex=True)
    plt.subplots_adjust(hspace=0)

    for ss in range(N_samp):
        ax[0].loglog(a_bin_mid, p_a_nk[:, ss], 'r-', linewidth=1, alpha=0.05)

    ax[0].loglog(a_bin_mid, p_a, 'r-', linewidth=5)

    # Plot the individual star PDFs
    a_bin_widths = np.diff(a_bin)

    for ss in range(pdfs.shape[0]):
        an, ab = np.histogram(pdfs[ss],
                              bins=a_bin,
                              weights=pdf_weights[ss],
                              density=False)
        an /= a_bin_widths
        ax[1].loglog(a_bin_mid, an, 'k-', linewidth=2, alpha=0.5)

    # Joint PDF:
    an, ab = np.histogram(pdfs.ravel(),
                          bins=a_bin,
                          weights=pdf_weights.ravel(),
                          density=False)
    an /= a_bin_widths
    ax[1].loglog(a_bin_mid, an, 'g-', linewidth=3)

    ax[1].set_xlabel('Semi-major Axis (AU)')
    ax[1].set_ylabel('PDF')
    ax[0].set_ylabel('PDF')

    plt.savefig('dnest_a_dist_' + suffix + '.png')

    return
Exemplo n.º 30
0
#Ackerman & Marley 2001 Cloud parameters--physically motivated with Mie particles
logKzz = 9  #log Kzz (cm2/s)--valid range: 2 - 11 -- higher values make larger particles
fsed = 1.0  #sediminetation efficiency--valid range: 0.5 - 5--lower values make "puffier" more extended cloud
logPbase = 1.5  #cloud base pressure--valid range: -6.0 - 1.5
logCldVMR = -15.0  #cloud condensate base mixing ratio (e.g, see Fortney 2005)--valid range: -15 - -2.0

#simple 'grey+rayleigh' parameters just in case you don't want to use a physically motivated cloud
#(most are just made up anyway since we don't really understand all of the micro-physics.....)
logKcld = -40  #uniform in altitude and in wavelength "grey" opacity (it's a cross-section)--valid range: -50 - -10
logRayAmp = -30  #power-law haze amplitude (log) as defined in des Etangs 2008 "0" would be like H2/He scat--valid range: -30 - 3
RaySlope = 0  #power law index 4 for Rayleigh, 0 for "gray".  Valid range: 0 - 6

#weighting the posterior samples for appropriate random drawing
from dynesty import utils as dyfunc
samp, wts = samples.samples, np.exp(samples.logwt - samples.logz[-1])
samples2 = dyfunc.resample_equal(samp, wts)

#choosing random indicies to draw from properly weighted posterior samples
draws = np.random.randint(0, samples2.shape[0], Nspectra)
Nwno_bins = xsecs[2].shape[0]
y_mod_array = np.zeros((Nwno_bins, Nspectra))
y_binned_array = np.zeros((len(wlgrid), Nspectra))

for i in range(Nspectra):
    print(i)
    #make sure this is the same as in log-Like
    Tirr, logMet, logCtoO, logKzz, fsed, logPbase, logCldVMR, xRp = samples2[
        draws[i], :]
    x = np.array([
        Tirr, logKir, logg1, logMet, logCtoO, logPQCarbon, logPQNitrogen,
        Rp * xRp, Rstar, M, logKzz, fsed, logPbase, logCldVMR, logKcld,
Exemplo n.º 31
0
def compute_inclination_pdf(data, results, **kwargs):
    """

    """
    # Get kwargs
    kwargs = update_with_defaults(**kwargs)
    sample_kwargs = kwargs["sample"]
    gen_kwargs = kwargs["generate"]
    plot_kwargs = kwargs["plot"]
    ninc_pts = plot_kwargs["ninc_pts"]
    ninc_samples = plot_kwargs["ninc_samples"]
    ydeg = sample_kwargs["ydeg"]
    apply_jac = sample_kwargs["apply_jac"]
    normalized = gen_kwargs["normalized"]

    # Get the data
    t = data["t"]
    ferr = data["ferr"]
    period = data["period"]
    flux = data["flux"]
    nlc = len(flux)

    # Array of inclinations & log prob for each light curve
    inc = np.linspace(0, 90, ninc_pts)
    lp = np.empty((nlc, ninc_samples, ninc_pts))

    # Compile the likelihood function for a given inclination
    if sample_kwargs["fit_bm"]:
        baseline_mean = None
    else:
        baseline_mean = sample_kwargs["bm"]
    if sample_kwargs["fit_blv"]:
        baseline_log_var = None
    else:
        baseline_log_var = sample_kwargs["blv"]
    log_prob = get_log_prob(
        t,
        flux=None,
        ferr=ferr,
        p=period,
        ydeg=ydeg,
        baseline_mean=baseline_mean,
        baseline_log_var=baseline_log_var,
        apply_jac=apply_jac,
        normalized=normalized,
        marginalize_over_inclination=False,
    )

    # Resample posterior samples to equal weight
    samples = np.array(results.samples)
    try:
        weights = np.exp(results["logwt"] - results["logz"][-1])
    except:
        weights = results["weights"]
    samples = dyfunc.resample_equal(samples, weights)

    # Compute the posteriors
    for n in tqdm(range(nlc), disable=bool(int(os.getenv("NOTQDM", "0")))):
        for j in range(ninc_samples):
            idx = np.random.randint(len(samples))
            lp[n, j] = np.array([
                log_prob(flux[n].reshape(1, -1), *samples[idx], i) for i in inc
            ])

    return dict(inc=inc, lp=lp)