Just to clarify. Here is the code all together. With the errors:
with pm.Model() as boe_multilevel:
data_time = pm.Data('data_time',train1['time'].values)
data_oil = pm.Data('data_oil',np.log(train1['oil_prod'].values))
data_codes = pm.intX(pm.Data('data_codes',train1['cat_codes']))
data_wedgeflg = pm.Data('data_wedgeflg', train1['capital_year_flag'].values)
#data = pm.Data('data', train1[['boe','time','cat_codes','capital_year_flag']].values)
# Hyperpriors
mu_b1 = pm.TruncatedNormal('mu_b1', mu=-0.09, sigma = 1, upper = -0.03)
# Priors
a = pm.Normal('a', mu = 0, sigma = 10, shape=cat_len)
b1 = pm.TruncatedNormal('b1', mu = mu_b1, sigma = 1, upper=0.0, shape = cat_len)
a2 = pm.Normal('a2', mu = 0, sigma = 10, shape = cat_len)
b2 = pm.Normal('b2', mu = 0, sigma = 10, shape= cat_len)
eps = pm.HalfCauchy('eps',0.5, shape=cat_len)
sigma_y = eps[cat_codes]
#nu = pm.DiscreteUniform('nu',1,10,shape=cat_len)[cat_codes]
oil_est = a[data_codes] + b1[data_codes]*data_time + a2[data_codes]*data_wedgeflg + b2[data_codes]*data_time*data_wedgeflg
y = pm.StudentT('y', mu = oil_est, nu = 5 , sigma = sigma_y, observed=data_oil)
with boe_multilevel:
boe_trace = pm.sample(4000,cores=2)
with boe_multilevel:
pm.set_data({'data_time': np.linspace(7,10,36),'data_wedgeflg': np.zeros(36),'data_codes': np.ones(36)})
ppc_y = pm.sample_posterior_predictive(trace=boe_trace)
ValueError Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\shape_utils.py in broadcast_dist_samples_shape(shapes, size)
165 try:
--> 166 broadcast_shape = shapes_broadcasting(*sp_shapes, raise_exception=True)
167 except ValueError:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\shape_utils.py in shapes_broadcasting(raise_exception, *args)
91 "Supplied shapes {} do not broadcast together".format(
---> 92 ", ".join(["{}".format(a) for a in args])
93 )
ValueError: Supplied shapes (), (36,), (952,), (952,) do not broadcast together
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\distribution.py in _draw_value(param, point, givens, size)
575 try:
--> 576 return dist_tmp.random(point=point, size=size)
577 except (ValueError, TypeError):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\continuous.py in random(self, point, size)
1963 dist_shape=self.shape,
-> 1964 size=size)
1965
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\distribution.py in generate_samples(generator, *args, **kwargs)
696 [np.asarray(i).shape for i in inputs] + [_dist_shape],
--> 697 size=size_tup
698 )
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\shape_utils.py in broadcast_dist_samples_shape(shapes, size)
169 "Cannot broadcast provided shapes {} given size: {}".format(
--> 170 ", ".join(["{}".format(s) for s in shapes]), size
171 )
ValueError: Cannot broadcast provided shapes (), (36,), (952,), (952,) given size: ()
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\shape_utils.py in broadcast_dist_samples_shape(shapes, size)
165 try:
--> 166 broadcast_shape = shapes_broadcasting(*sp_shapes, raise_exception=True)
167 except ValueError:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\shape_utils.py in shapes_broadcasting(raise_exception, *args)
91 "Supplied shapes {} do not broadcast together".format(
---> 92 ", ".join(["{}".format(a) for a in args])
93 )
ValueError: Supplied shapes (), (36,), (952,), () do not broadcast together
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-241-4ff2bd752351> in <module>
1 with boe_multilevel:
2 pm.set_data({'data_time': np.linspace(7,10,36),'data_wedgeflg': np.zeros(36),'data_codes': np.ones(36)})
----> 3 ppc_y = pm.sample_posterior_predictive(trace=boe_trace)
4 #ppc_coefs = pm.sample_posterior_predictive(trace=boe_trace, model=boe_multilevel, var_names = ['a','a2','b1','b2'])
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\sampling.py in sample_posterior_predictive(trace, samples, model, vars, var_names, size, random_seed, progressbar)
1112 param = trace[idx % len_trace]
1113
-> 1114 values = draw_values(vars, point=param, size=size)
1115 for k, v in zip(vars, values):
1116 ppc_trace[k.name].append(v)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\distribution.py in draw_values(params, point, size)
393 point=point,
394 givens=temp_givens,
--> 395 size=size)
396 givens[next_.name] = (next_, value)
397 drawn[(next_, size)] = value
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\distribution.py in _draw_value(param, point, givens, size)
583 with _DrawValuesContextBlocker():
584 val = np.atleast_1d(dist_tmp.random(point=point,
--> 585 size=None))
586 # Sometimes point may change the size of val but not the
587 # distribution's shape
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\continuous.py in random(self, point, size)
1962 return generate_samples(stats.t.rvs, nu, loc=mu, scale=lam**-0.5,
1963 dist_shape=self.shape,
-> 1964 size=size)
1965
1966 def logp(self, value):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\distribution.py in generate_samples(generator, *args, **kwargs)
695 broadcast_shape = broadcast_dist_samples_shape(
696 [np.asarray(i).shape for i in inputs] + [_dist_shape],
--> 697 size=size_tup
698 )
699 # We do this instead of broadcast_distribution_samples to avoid
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\shape_utils.py in broadcast_dist_samples_shape(shapes, size)
168 raise ValueError(
169 "Cannot broadcast provided shapes {} given size: {}".format(
--> 170 ", ".join(["{}".format(s) for s in shapes]), size
171 )
172 )
ValueError: Cannot broadcast provided shapes (), (36,), (952,), () given size: ()
EDIT: I FIGURED IT OUT!!!
sigma_y = eps[cat_codes]
should be
sigma_y = eps[data_codes]