Hi!
I am trying to use PPC to do the criticism for mixture model but encountered shape mismatch error in sample_ppc
.
The data generation process is:
def build_toy_dataset(N, K):
pi = np.array([0.2, 0.5, 0.3])
mus = [[1, 1], [-1, -1], [2,-2]]
stds = [[0.1, 0.1], [0.1, 0.2], [0.2, 0.3]]
x = np.zeros((N, 2), dtype=np.float32)
y = np.zeros((N,), dtype=np.int)
for n in range(N):
k = np.argmax(np.random.multinomial(1, pi))
x[n, :] = np.random.multivariate_normal(mus[k], np.diag(stds[k]))
y[n] = k
return x,y
N = 500 # number of data points
D = 2 # dimensionality of data
X, y = build_toy_dataset(N, 3)
Model and NUTS inference:
# set up model
K = 3
with pm.Model() as model:
pi = pm.Dirichlet('pi', np.ones(K))
comp_dist = []
mu = []
packed_chol = []
chol = []
for i in range(K):
mu.append(pm.Normal('mu%i'%i, 0, 10, shape=2))
packed_chol.append(pm.LKJCholeskyCov('chol_cov_%i'%i,
eta=2,
n=2,
sd_dist=pm.HalfNormal.dist(2.5)))
chol.append(pm.expand_packed_triangular(2, packed_chol[i], lower=True))
comp_dist.append(pm.MvNormal.dist(mu=mu[i], chol=chol[i]))
xobs = pm.Mixture('x_obs', pi, comp_dist,
observed=X)
with model:
trace = pm.sample(1000, tune=1000, chains=1)
However, when I use sample_ppc
to generate posterior predictive samples, I get the following error:
with model:
ppc = pm.sample_ppc(trace, 2)
IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (1000,) (500,2)
It seems like somehow ppc flattens X of shape(500,2) into (1000,). But I don’t know how to fix that.
I would really appreciate if you guys could help me fix this!