Пример #1
0
    def _compute_nlive(self, logL_birth):
        if is_int(logL_birth):
            nlive = logL_birth
            self['nlive'] = nlive
            descending = np.arange(nlive, 0, -1)
            self.loc[len(self) - nlive:, 'nlive'] = descending
        else:
            self['logL_birth'] = logL_birth
            self.tex['logL_birth'] = r'$\log\mathcal{L}_{\rm birth}$'
            self['nlive'] = compute_nlive(self.logL, self.logL_birth)

        self.tex['nlive'] = r'$n_{\rm live}$'
        self.beta = self._beta
Пример #2
0
    def recompute(self, logL_birth=None, inplace=False):
        """Re-calculate the nested sampling contours and live points.

        Parameters
        ----------
        logL_birth: array-like or int, optional
            array-like: the birth contours.
            int: the number of live points.
            default: use the existing birth contours to compute nlive

        inplace: bool, optional
            Indicates whether to modify the existing array, or return a new
            frame with contours resorted and nlive recomputed
            default: False
        """
        samples = self.sort_values('logL').reset_index(drop=True)

        if is_int(logL_birth):
            nlive = logL_birth
            samples['nlive'] = nlive
            descending = np.arange(nlive, 0, -1)
            samples.loc[len(samples) - nlive:, 'nlive'] = descending
        else:
            if logL_birth is not None:
                samples['logL_birth'] = logL_birth
                samples.tex['logL_birth'] = r'$\log\mathcal{L}_{\rm birth}$'

            if 'logL_birth' not in samples:
                raise RuntimeError("Cannot recompute run without "
                                   "birth contours logL_birth.")

            invalid = samples.logL <= samples.logL_birth
            n_bad = invalid.sum()
            n_equal = (samples.logL == samples.logL_birth).sum()
            if n_bad:
                warnings.warn(
                    "%i out of %i samples have logL <= logL_birth,"
                    "\n%i of which have logL == logL_birth."
                    "\nThis may just indicate numerical rounding "
                    "errors at the peak of the likelihood, but "
                    "further investigation of the chains files is "
                    "recommended."
                    "\nDropping the invalid samples." %
                    (n_bad, len(samples), n_equal), RuntimeWarning)
                samples = samples[~invalid].reset_index(drop=True)

            samples['nlive'] = compute_nlive(samples.logL, samples.logL_birth)

        samples.tex['nlive'] = r'$n_{\rm live}$'
        samples.beta = samples._beta
        return modify_inplace(self, samples, inplace)
Пример #3
0
    def recompute(self, logL_birth=None, inplace=False):
        """Re-calculate the nested sampling contours and live points.

        Parameters
        ----------
        logL_birth, array-like or int, optional
            array-like: the birth contours.
            int: the number of live points.
            default: use the existing birth contours to compute nlive

        inplace: bool, optional
            Indicates whether to modify the existing array, or return a new
            frame with contours resorted and nlive recomputed
            default: False
        """
        samples = self.sort_values('logL').reset_index(drop=True)

        if is_int(logL_birth):
            nlive = logL_birth
            samples['nlive'] = nlive
            descending = np.arange(nlive, 0, -1)
            samples.loc[len(samples) - nlive:, 'nlive'] = descending
        else:
            if logL_birth is not None:
                samples['logL_birth'] = logL_birth
                samples.tex['logL_birth'] = r'$\log\mathcal{L}_{\rm birth}$'

            if 'logL_birth' not in samples:
                raise RuntimeError("Cannot recompute run without "
                                   "birth contours logL_birth.")

            if (samples.logL <= samples.logL_birth).any():
                raise RuntimeError("Not a valid nested sampling run. "
                                   "Require logL > logL_birth.")

            samples['nlive'] = compute_nlive(samples.logL, samples.logL_birth)

        samples.tex['nlive'] = r'$n_{\rm live}$'
        samples.beta = samples._beta
        return modify_inplace(self, samples, inplace)
Пример #4
0
def test_compute_nlive():
    # Generate a 'pure' nested sampling run
    np.random.seed(0)
    nlive = 500
    ncompress = 100
    logL = np.cumsum(np.random.rand(nlive, ncompress), axis=1)
    logL_birth = np.concatenate((np.ones((nlive, 1)) * -1e30, logL[:, :-1]),
                                axis=1)
    i = np.argsort(logL.flatten())
    logL = logL.flatten()[i]
    logL_birth = logL_birth.flatten()[i]

    # Compute nlive
    nlives = compute_nlive(logL, logL_birth)

    # Check the first half are constant
    assert_array_equal(nlives[:len(nlives) // 2], nlive)

    # Check one point at the end
    assert (nlives[-1] == 1)

    # Check never more than nlive
    assert (nlives.max() <= nlive)