How to use the emcee.ensemble.EnsembleSampler function in emcee

To help you get started, we’ve selected a few emcee examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dfm / emcee / emcee / tests.py View on Github external
def lnprob(theta, x, y, yerr):
            lp = lnprior(theta)
            if not np.isfinite(lp):
                return -np.inf
            return lp + lnlike(theta, x, y, yerr)

        nll = lambda *args: -lnlike(*args)

        # minimize(nll, [m_true, b_true, np.log(f_true)], args=(x, y, yerr))
        init_guess = np.array([-0.95612643,  4.23596208, -0.66826006])

        ndim, nwalkers = 3, 100
        pos = [init_guess + 1e-4 * np.random.randn(ndim) for
               i in range(nwalkers)]

        sampler = EnsembleSampler(nwalkers, ndim, lnprob, args=(x, y, yerr))
        s = sampler.sample(pos, iterations=65, thin=2)
        for i in range(65):
            next(s)
        np.testing.assert_equal(sampler.chain.shape, (32, nwalkers, ndim))

        sampler = EnsembleSampler(nwalkers, ndim, lnprob, args=(x, y, yerr))
        s = sampler.sample(pos, iterations=65, thin=3)
        for i in range(65):
            next(s)
github dfm / emcee / emcee / tests.py View on Github external
nll = lambda *args: -lnlike(*args)

        # minimize(nll, [m_true, b_true, np.log(f_true)], args=(x, y, yerr))
        init_guess = np.array([-0.95612643,  4.23596208, -0.66826006])

        ndim, nwalkers = 3, 100
        pos = [init_guess + 1e-4 * np.random.randn(ndim) for
               i in range(nwalkers)]

        sampler = EnsembleSampler(nwalkers, ndim, lnprob, args=(x, y, yerr))
        s = sampler.sample(pos, iterations=65, thin=2)
        for i in range(65):
            next(s)
        np.testing.assert_equal(sampler.chain.shape, (32, nwalkers, ndim))

        sampler = EnsembleSampler(nwalkers, ndim, lnprob, args=(x, y, yerr))
        s = sampler.sample(pos, iterations=65, thin=3)
        for i in range(65):
            next(s)
github dfm / emcee / emcee / tests.py View on Github external
def test_parallel(self):
        self.sampler = EnsembleSampler(self.nwalkers, self.ndim,
                                       lnprob_gaussian, args=[self.icov],
                                       threads=2)
        self.check_sampler()
github dfm / emcee / emcee / tests.py View on Github external
def test_nan_lnprob(self):
        self.sampler = EnsembleSampler(self.nwalkers, self.ndim,
                                       lnprob_gaussian_nan,
                                       args=[self.icov])

        # If a walker is right at zero, ``lnprobfn`` returns ``np.nan``.
        p0 = self.p0
        p0[0] = 0.0

        try:
            self.check_sampler(p0=p0)
        except ValueError:
            # This should fail *immediately* with a ``ValueError``.
            return

        assert False, "We should never get here."
github dfm / emcee / emcee / tests.py View on Github external
def test_blobs(self):
        lnprobfn = lambda p: (-0.5 * np.sum(p ** 2), np.random.rand())
        self.sampler = EnsembleSampler(self.nwalkers, self.ndim, lnprobfn)
        self.check_sampler()

        # Make sure that the shapes of everything are as expected.
        assert (self.sampler.chain.shape == (self.N, self.nwalkers, self.ndim)
                and len(self.sampler.blobs) == self.N
                and len(self.sampler.blobs[0]) == self.nwalkers), \
            "The blob dimensions are wrong."

        # Make sure that the blobs aren't all the same.
        blobs = self.sampler.blobs
        assert np.any([blobs[-1] != blobs[i] for i in range(len(blobs) - 1)])
github dfm / emcee / emcee / tests.py View on Github external
def test_run_mcmc_resume(self):

        self.sampler = s = EnsembleSampler(self.nwalkers, self.ndim,
                                           lnprob_gaussian, args=[self.icov])

        # first time around need to specify p0
        try:
            s.run_mcmc(None, self.N)
        except ValueError:
            pass

        s.run_mcmc(self.p0, N=self.N)
        assert s.chain.shape[0] == self.N

        # this doesn't actually check that it resumes with the right values, as
        # that's non-trivial... so we just make sure it does *something* when
        # None is given and that it records whatever it does
        s.run_mcmc(None, N=self.N)
        assert s.chain.shape[0] == 2 * self.N
github dfm / emcee / emcee / tests.py View on Github external
def test_ensemble(self):
        self.sampler = EnsembleSampler(self.nwalkers, self.ndim,
                                       lnprob_gaussian, args=[self.icov])
        self.check_sampler()
github dfm / emcee / emcee / tests.py View on Github external
def test_inf_nan_params(self):
        self.sampler = EnsembleSampler(self.nwalkers, self.ndim,
                                       lnprob_gaussian, args=[self.icov])

        # Set one of the walkers to have a ``np.nan`` value.
        p0 = self.p0
        p0[0][0] = np.nan

        try:
            self.check_sampler(p0=p0)

        except ValueError:
            # This should fail *immediately* with a ``ValueError``.
            pass

        else:
            assert False, "The sampler should have failed by now."