Github issue #2352 by nikzadb:
I have the attached data and following Hierarchical model (as a toy example of another model) and trying to draw posterior samples from it (of course to predict new values).
new_data.txt (1.5 KB)
Hierarchical model:
import numpy as np
import pandas as pd
import pymc3 as pm3
from theano import shared
import theano.tensor as tt
import matplotlib.pylab as plt
%matplotlib inline
new_data = pd.read_csv('new_data.csv', sep=',')
train = new_data.iloc[indexes[:-15], :]
test = new_data.iloc[indexes[-15:], :]
masked_x1 = shared(train.x1)
masked_x2 = shared(train.x2)
idx = train.idx.values.astype(int)
num_items = len(idx)
with pm3.Model() as model:
intercept_prior = pm3.Normal('intercept_prior',
mu=20,
sd=100,
shape=num_items)
mu_x1 = pm3.Normal('mu_x1', mu=0, sd=20)
std_x1 = pm3.Uniform('std_x1', lower=0, upper=100)
x1_prior = pm3.Normal('x1_prior',
mu=mu_x1,
sd=std_x1,
shape=num_items)
std_x2 = pm3.Uniform('std_x2', lower=0, upper=100)
x2_prior = pm3.HalfNormal('x2_prior',
sd=std_x2,
shape=num_items)
y_est = (
intercept_prior[idx] +
x1_prior[idx] * masked_x1.get_value() +
x2_prior[idx] * masked_x2.get_value()
)
y_est = pm3.Deterministic('y_est', y_est)
pm3.Normal('y_pred',
mu=y_est,
sd=100,
observed=train.y)
start, sds, elbo = pm3.variational.advi(n=20000)
step = pm3.Metropolis()
trace = pm3.sample(10000, start=start, step=step)
varnames = ['mu_x1', 'std_x1', 'std_x2']
pm3.traceplot(trace[-1000:], varnames)
pm3.plot_posterior(trace[-1000:], varnames)
in which the size of trace[āy_predā] is (10000, 43) since there are 10000 traces and 43 data in train set.
Prediction
masked_x1.set_value(test.x1)
masked_x2.set_value(test.x2)
ppc = pm3.sample_ppc(trace, model=model, samples=500)
What I expect here is the shape of ppc[āy_predā] be (500, 15), where 15 is the size of test dataset, but instead I get (500, 43) which 43 is the size of train dataset.
(I followed the suggestions at https://github.com/pymc-devs/pymc3/issues/1001 and ran the hierarchical model for rugby prediction example. For this example, everything is fine but can not figure out why the above model still goes wrong.)