"ValueError length of mu cannot be determined": error for pm.Simulator with single parameter

I’m trying to do Approximate Bayesian computing, and am able to use the pm.Simulator class to estimate functions with 2 or more parameters. Here, each parameter is an array of multiple values.

However, when I try to estimate values of a function that accepts only a single parameter, I get an error. The simplest working example (loosely based on the actual code):

# 2 parameter pm.Simulator snippet 
import pymc3 as pm 
import numpy as np 

def get_mean_sig2(mu,sigma):
    multi_var = np.random.normal(mu,sigma)
    return multi_var

# create the observed data
obs2 = get_mean_sig2(np.array([10,5,2,1]), np.array([0.5,1,2,1]))

with pm.Model() as m91:
    mu = pm.Uniform('mu', lower=1, upper=15, shape=obs2.shape[0])
    sigma = pm.Uniform('sigma',lower=0.25, upper=3,shape=obs2.shape[0])
    sim = pm.Simulator('sim', get_mean_sig2 ,params=(mu,sigma), observed=obs2)
    sample_91 = pm.sample_smc(kernel='ABC')

When I simplify the problem to only estimating the mean with this code:

# 1 parameter pm.Simulator snippet that gives error
def get_only_mean(mu):
    multi_var = np.random.normal(mu,0.2)
    return multi_var
obs = get_only_mean(np.array([10,5,2,1]))

with pm.Model() as m90:
    mu = pm.Uniform('mu', lower=1, upper=15, shape=obs.shape[0])
    sim = pm.Simulator('sim', get_only_mean,params=(mu),observed=obs)
    sample_90 = pm.sample_smc(kernel='ABC')

I get the error message ValueError: Length of mu ~ Uniform cannot be determined . I have tried variations of inputting shape=(1,obs.shape[0]) or manually setting shape=4 for the ‘shape’ parameter’s input - but failed.

I’m unable to understand why this problem suddenly appears - any help would be appreciated.

The same error appears on both Linux Mint 19.2 (python 3.8.5, pymc3 3.11, numpy 1.19.5, theano 1.1.0) and Windows 10 (python 3.8.5, pymc3 3.11.2, numpy 1.20.1, theano 1.1.2)

Can you try to use a dictionary for the params: params = {'mu': mu}

Thanks for the reply!
Trying the suggested leads to another type of error though : AttributeError: 'str' object has no attribute 'name'

Thanks to your idea of changing the ‘container’ holding the params - I went back and checked the documentation of the pm.Simulator once more and it says params should be a ‘list’.

And so - taking the docs literally - I went back and changed it to params=[mu] and it worked!

In fact, even when the multi-parameter arguments are placed in a list the Model runs fine too (ie. . params=[mu,sigma], instead of params=(mu,sigma).

Glad you figured it out. We will try to give more informative errors in the future!

1 Like