How are mutable args handled in EnsembleSampler?

12 views
Skip to first unread message

nmov...@gmail.com

unread,
Dec 9, 2022, 4:44:32 PM12/9/22
to emcee users

Suppose

sampler = emcee.EnsembleSampler(nwalkers, ndims, lnprob, args=[obs])

where obs, say, is a user struct (mutable) that the lnprob method needs access to. What if lnprob modifies values in obs?

Can we trust that these stay local to a single step of a single walker? When lnprob is called next will it get a fresh copy of obs?

Thanks,
—naor

Dan Foreman-Mackey

unread,
Dec 10, 2022, 8:14:25 AM12/10/22
to nmov...@gmail.com, emcee users
No - the args are passed by reference. You can check this pretty easily:

import emcee
import numpy as np

def lp(x, state):
state[0] += 1.0
return -0.5*np.sum(x)

state = np.array([0.0])
init = np.random.randn(36, 3)
sampler = emcee.EnsembleSampler(*init.shape, lp, args=(state,))
sampler.run_mcmc(init, 5)
print(state)

which prints np.array([216.]). Hope this helps!
Dan

--
You received this message because you are subscribed to the Google Groups "emcee users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emcee-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emcee-users/ee26dee1-e645-4fb9-9096-851e2679e8c3n%40googlegroups.com.


--
Dan Foreman-Mackey
Research Scientist
Flatiron Institute

nmov...@gmail.com

unread,
Dec 10, 2022, 1:05:08 PM12/10/22
to emcee users
Thanks Dan! Those python mutables keep tripping me up even after all these years!
I imagine it's impossible to predict what the state of that variable would be if we ran the sample in a multiprocessing or even mpi pool? At least I wouldn't want to rely on that.  

Dan Foreman-Mackey

unread,
Dec 10, 2022, 3:14:52 PM12/10/22
to nmov...@gmail.com, emcee users
Yeah - I expect you'd get a different set of args for each thread, but I'm not totally sure! Not something you'd want to count on too much. Happy to chat offlist about options if you want to share more details about what you're trying to achieve.
Dan

Reply all
Reply to author
Forward
0 new messages