How to use emcee - 10 common examples

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 / tests / unit / test_backends.py View on Github external
def test_uninit(tmpdir):
    fn = str(tmpdir.join("EMCEE_TEST_FILE_DO_NOT_USE.h5"))
    if os.path.exists(fn):
        os.remove(fn)

    with backends.HDFBackend(fn) as be:
        run_sampler(be)

    assert os.path.exists(fn)
    os.remove(fn)
github dfm / emcee / tests / unit / test_backends.py View on Github external
def run_sampler(backend, nwalkers=32, ndim=3, nsteps=25, seed=1234, thin_by=1,
                dtype=None, blobs=True, lp=None):
    if lp is None:
        lp = normal_log_prob_blobs if blobs else normal_log_prob
    if seed is not None:
        np.random.seed(seed)
    coords = np.random.randn(nwalkers, ndim)
    sampler = EnsembleSampler(nwalkers, ndim, lp,
                              backend=backend, blobs_dtype=dtype)
    sampler.run_mcmc(coords, nsteps, thin_by=thin_by)
    return sampler
github dfm / emcee / tests / unit / test_backends.py View on Github external
def test_reload(backend, dtype):
    with backend() as backend1:
        run_sampler(backend1, dtype=dtype)

        # Test the state
        state = backend1.random_state
        np.random.set_state(state)

        # Load the file using a new backend object.
        backend2 = backends.HDFBackend(backend1.filename, backend1.name,
                                       read_only=True)

        with pytest.raises(RuntimeError):
            backend2.reset(32, 3)

        assert state[0] == backend2.random_state[0]
        assert all(np.allclose(a, b)
                   for a, b in zip(state[1:], backend2.random_state[1:]))

        # Check all of the components.
        for k in ["chain", "log_prob", "blobs"]:
            a = backend1.get_value(k)
            b = backend2.get_value(k)
            _custom_allclose(a, b)

        last1 = backend1.get_last_sample()
github dfm / emcee / tests / unit / test_autocorr.py View on Github external
def test_autocorr_multi_works():
    np.random.seed(42)
    xs = np.random.randn(16384, 2)

    # This throws exception unconditionally in buggy impl's
    acls_multi = integrated_time(xs)
    acls_single = np.array([integrated_time(xs[:, i])
                            for i in range(xs.shape[1])])

    assert np.all(np.abs(acls_multi - acls_single) < 2)
github dfm / emcee / tests / unit / test_autocorr.py View on Github external
def test_nd(seed=1234, ndim=3, N=150000):
    x = get_chain(seed=seed, ndim=ndim, N=N)
    tau = integrated_time(x)
    assert np.all(np.abs(tau - 19.0) / 19. < 0.2)
github dfm / emcee / tests / unit / test_autocorr.py View on Github external
def test_too_short(seed=1234, ndim=3, N=100):
    x = get_chain(seed=seed, ndim=ndim, N=N)
    with pytest.raises(AutocorrError):
        integrated_time(x)
    tau = integrated_time(x, quiet=True)  # NOQA
github dfm / emcee / tests / unit / test_autocorr.py View on Github external
def test_autocorr_multi_works():
    np.random.seed(42)
    xs = np.random.randn(16384, 2)

    # This throws exception unconditionally in buggy impl's
    acls_multi = integrated_time(xs)
    acls_single = np.array([integrated_time(xs[:, i])
                            for i in range(xs.shape[1])])

    assert np.all(np.abs(acls_multi - acls_single) < 2)
github dfm / emcee / tests / unit / test_backends.py View on Github external
from __future__ import division, print_function

import os
from itertools import product

import numpy as np

import pytest

from emcee import backends, EnsembleSampler

import h5py

__all__ = ["test_backend", "test_reload"]

all_backends = backends.get_test_backends()
other_backends = all_backends[1:]
dtypes = [
    None,
    [("log_prior", float), ("mean", int)]
]


def normal_log_prob(params):
    return -0.5 * np.sum(params**2)


def normal_log_prob_blobs(params):
    return normal_log_prob(params), 0.1, int(5)


def run_sampler(backend, nwalkers=32, ndim=3, nsteps=25, seed=1234, thin_by=1,
github dfm / emcee / tests / unit / test_sampler.py View on Github external
# -*- coding: utf-8 -*-

from __future__ import division, print_function

import pickle
from itertools import product

import pytest
import numpy as np

from emcee import moves, backends, EnsembleSampler

__all__ = ["test_shapes", "test_errors", "test_thin", "test_vectorize"]

all_backends = backends.get_test_backends()


def normal_log_prob(params):
    return -0.5 * np.sum(params**2)


@pytest.mark.parametrize("backend, moves", product(
    all_backends,
    [
        None,
        moves.GaussianMove(0.5),
        [moves.StretchMove(), moves.GaussianMove(0.5)],
        [(moves.StretchMove(), 0.3), (moves.GaussianMove(0.5), 0.1)],
    ])
)
def test_shapes(backend, moves, nwalkers=32, ndim=3, nsteps=10, seed=1234):
github dfm / emcee / tests / integration / test_nuts.py View on Github external
@pytest.mark.parametrize("metric", [None, IdentityMetric(3),
                                    IsotropicMetric(3),
                                    DiagonalMetric(np.ones(3)),
                                    DenseMetric(np.eye(3))])
@pytest.mark.parametrize("pool", [True, False])
@pytest.mark.parametrize("tune", [True, False])
@pytest.mark.parametrize("blobs", [True, False])
def test_normal_nuts(pool, metric, tune, blobs, **kwargs):
    if pool:
        kwargs["pool"] = Pool()
    if tune:
        move = moves.NoUTurnMove(ntune=300)
    else:
        move = moves.NoUTurnMove()
    kwargs["ndim"] = 3
    kwargs["check_acceptance"] = False
    kwargs["nsteps"] = 100
    kwargs["blobs"] = blobs
    _test_normal(move, **kwargs)
    if pool:
        kwargs["pool"].close()