TypeError: only size-1 arrays can be converted to Python scalars

21 views
Skip to first unread message

Carlo Cannarozzo

unread,
Jun 7, 2023, 3:06:38 PM6/7/23
to emcee users
Hi all,

I'm running a very complex model that before adding the new two priors I'm going to describe below, was working correctly.

The final likelihood is the following:
            lp = lp + ((np.log((1/np.sqrt(2*np.pi*(sigma2_s)) * np.exp(-(log_s[i]-mu_s)**2/(2*sigma2_s))))) +\
                       (np.log((1/np.sqrt(2*np.pi*(sigma2_r))  * np.exp(-(log_reff [i]-mu_r)**2 /(2*sigma2_r))))) +\
                       (np.log(gaussian_ch1)) +\
                       (np.log(gaussian_ch2)) +\
                       (np.log(integral)))

Before adding the terms (np.log(gaussian_ch1) and (np.log(gaussian_ch2) (two priors) to the likelihood, the code was working. However, in the new implementation, after some steps, I end up with the following error and I'm not figuring out:

TypeError Traceback (most recent call last) ~/anaconda3/lib/python3.7/site-packages/emcee/ensemble.py in compute_log_prob(self, coords) 491 try: --> 492 log_prob = np.array([float(l[0]) for l in results]) 493 blob = [l[1:] for l in results] ~/anaconda3/lib/python3.7/site-packages/emcee/ensemble.py in <listcomp>(.0) 491 try: --> 492 log_prob = np.array([float(l[0]) for l in results]) 493 blob = [l[1:] for l in results] TypeError: 'float' object is not subscriptable During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) <ipython-input-61-097b6174e6c6> in <module> 10 backend = backend_initial_run 11 ) ---> 12 pos_1, prob_1, blobs_1, state_1 = sampler_initial_run.run_mcmc(pos_0, Nsteps, progress=True, store=True) ~/anaconda3/lib/python3.7/site-packages/emcee/ensemble.py in run_mcmc(self, initial_state, nsteps, **kwargs) 441 442 results = None --> 443 for results in self.sample(initial_state, iterations=nsteps, **kwargs): 444 pass 445 ~/anaconda3/lib/python3.7/site-packages/emcee/ensemble.py in sample(self, initial_state, log_prob0, rstate0, blobs0, iterations, tune, skip_initial_state_check, thin_by, thin, store, progress, progress_kwargs) 400 401 # Propose --> 402 state, accepted = move.propose(model, state) 403 state.random_state = self.random_state 404 ~/anaconda3/lib/python3.7/site-packages/emcee/moves/red_blue.py in propose(self, model, state) 91 92 # Compute the lnprobs of the proposed position. ---> 93 new_log_probs, new_blobs = model.compute_log_prob_fn(q) 94 95 # Loop over the walkers and update them accordingly. ~/anaconda3/lib/python3.7/site-packages/emcee/ensemble.py in compute_log_prob(self, coords) 493 blob = [l[1:] for l in results] 494 except (IndexError, TypeError): --> 495 log_prob = np.array([float(l) for l in results]) 496 blob = None 497 else: ~/anaconda3/lib/python3.7/site-packages/emcee/ensemble.py in <listcomp>(.0) 493 blob = [l[1:] for l in results] 494 except (IndexError, TypeError): --> 495 log_prob = np.array([float(l) for l in results]) 496 blob = None 497 else: TypeError: only size-1 arrays can be converted to Python scalars

Does someone have any ideas where the problem could be? Many thanks in advance
Carlo

Dan Foreman-Mackey

unread,
Jun 7, 2023, 3:19:37 PM6/7/23
to Carlo Cannarozzo, Emcee Users
Hi Carlo,
This suggests that your log probability function is returning something that isn’t just a number. What exactly is returned if you just call your log probability function directly?
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/21b29107-d45f-4c7d-8667-94ed1b8a16fen%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages