question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can emcee be run deterministically?

See original GitHub issue

I want to be able to run emcee multiple times and get exactly the same answer. How can this be accomplished? It does not seem to be sufficient to use random.seed(0), or numpy.random.seed(0), or to capture numpy.random.mtrand.RandomState(0) and assign it to the sampler, or in fact all three.

This is relevant, among others, for making sure that our example notebook doesn’t change when rerun, so that it can be checked in to version control without endless merge conflicts.

A quick test in raw emcee; this passes, mystifyingly, sometimes but not always:

def test_raw_emcee():
    r = []
    for i in range(2):
        random.seed(0)
        numpy.random.seed(0)
        s = numpy.random.mtrand.RandomState(0)

        def log_prob(x, ivar):
            return -0.5 * np.sum(ivar * x ** 2)

        ndim, nwalkers = 5, 100
        ivar = 1. / np.random.rand(ndim)
        # r.append(ivar[0])
        p0 = np.random.randn(nwalkers, ndim)
        #r.append(p0[0, 0])

        sampler = emcee.EnsembleSampler(nwalkers, ndim, log_prob, args=[ivar])
        sampler.random_state = s
        sampler.run_mcmc(p0, 100)

        samples = sampler.chain.reshape((-1, ndim))
        r.append(samples[0, 0])
    assert r[0] == r[1]

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
aarchibacommented, Nov 2, 2019

Thank you, that works great! In the event I think we’re going to depend on a recent version of emcee in order to have long double walkers.

1reaction
Alexis-Prelcommented, Nov 2, 2019

@aarchiba would you mind letting me know if #324 indeed addresses your needs?

Read more comments on GitHub >

github_iconTop Results From Across the Web

emcee + PyMC3 | Dan Foreman-Mackey
And now we can run the sampler: import emcee with model: # First we work out the shapes of all of the deterministic...
Read more >
Deterministic versus Probabilistic Schedule - Herding Cats
Deterministic schedules - are networks of tasks connected to each other with dependencies that describe the work to be performed, ...
Read more >
Parallel Programming Must Be Deterministic by Default
In contrast, we argue for a parallel programming model that is deterministic by default: deterministic behavior is guaranteed unless the ...
Read more >
Internally Deterministic Parallel Algorithms Can Be Fast
In this paper, we advocate a particular form of internal de- terminism as providing such a sweet spot for a class of nested-...
Read more >
Observed Deterministic - Questions - PyMC Discourse
Hi there :) As the title says, I'm looking for a way to build a model where the observed variable is a combination...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found