ValueError: Not enough dimensions on Elemwise{mul,no_inplace}.0 to reduce on axis -1


#1

I have been working on PyMc3 to build a probabilistic model that fits my data. As a part of it I’ve tried implementing updating priors on my data in which posteriors are being used as priors and will run for multiple times

observed_cell_delay = list_cell_delay[:5000]
def stick_breaking(beta):
  portion_remaining = tt.concatenate([[1], tt.extra_ops.cumprod(1 - beta)[:-1]])
  return beta * portion_remaining
K=30
with pm.Model() as cell_del_model:
  alpha = pm.Gamma('alpha', 1., 1.)
  beta = pm.Beta('beta', 1., alpha, shape=K)
  w = pm.Deterministic('w', stick_breaking(beta))
  tau = pm.Gamma('tau', 1., 1., shape=K) 
  mu = pm.Normal('mu',0.,10.,shape=K) 
  est_cell_del = pm.NormalMixture('est_cell_del', w, mu, tau=tau, observed=observed_cell_delay)

with cell_del_model:
  step = pm.Metropolis()
  #db = pm.backends.Text('samples')
  trace = pm.sample(2000,step=step,tune=1000,cores=8)#,discard_tuned_samples=True,n_init=1000,trace=db)
pm.traceplot(trace)

def from_posterior(param, samples):
  smin, smax = np.min(samples), np.max(samples)
  width = smax - smin
  x = np.linspace(smin, smax, 100)
  samples = samples[~np.isnan(samples)]
  y = stats.gaussian_kde(samples)(x)
  x = np.concatenate([[x[0] - 3 * width], x, [x[-1] + 3 * width]])
  y = np.concatenate([[0], y, [0]])
  return Interpolated(param, x, y)

new_cell_delay =   list_cell_delay[10000:]
with pm.Model() as cell_delay_model:
  alpha = from_posterior('alpha',trace['alpha'])
  beta = from_posterior('beta',trace['beta'])
  w = from_posterior('w',trace['w'])
  tau = from_posterior('tau',trace['tau'])
  mu = from_posterior('mu',trace['mu'])
  est_cell_del = pm.NormalMixture('est_cell_del', w, mu, tau=tau, observed=new_cell_delay)
  step = pm.Metropolis()
  trace1 = pm.sample(1000,step=step,tune=1000,cores=8)

I am getting the value error "ValueError: Not enough dimensions on Elemwise{mul,no_inplace}.0 to reduce on axis -1" at pm.NormalMixture while updating priors. Thanks in advance.


#2

The example here uses all univariate normal distributions, so from_posterior is a reasonable interpolation for the data. In your model, beta, tau, and mu are all K dimensional, and I am not sure how well Interpolated accepts a K dimensional input.

The specific problem is probably because from_posterior is calculating a 1d interpolation for a K dimensional density. I will give some thought, but will be interested if you figure out a from_posterior for high dimensional objects!


#3

It cannot work with n dimension input - it’s for 1d only.


#4

Thanks for answering @colcarroll. I’d want to know if there is a way to build a probability distribution for multidimensional data (replacement for Interpolated).