# Shape mismatch error in 'sample\_ppc'

**URL:** <https://discourse.pymc.io/t/shape-mismatch-error-in-sample-ppc/2270>\
**Category:** Questions\
**Created:** [November 24, 2018, 2:25am UTC](https://discourse.pymc.io/t/shape-mismatch-error-in-sample-ppc/2270 "2018-11-24T02:25:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![blueone](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@blueone](https://discourse.pymc.io/u/blueone)\
**Post date:** [November 24, 2018, 2:25am UTC](https://discourse.pymc.io/t/shape-mismatch-error-in-sample-ppc/2270/1 "2018-11-24T02:25:04Z")

</div>

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:

```auto
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:

```auto
# 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:

```auto
with model:
    ppc = pm.sample_ppc(trace, 2)

```

```auto
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!

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [November 24, 2018, 9:41am UTC](https://discourse.pymc.io/t/shape-mismatch-error-in-sample-ppc/2270/2 "2018-11-24T09:41:26Z")

</div>

Can confirm - the random for multidimensional mixture is wrong 😰  
I opened an issue: [https://github.com/pymc-devs/pymc3/issues/3270](https://github.com/pymc-devs/pymc3/issues/3270) but I am not sure if it would really be fixed - as I have modified this code multiple times and each time I thought that I fixed everything…

---

<div class="post-metadata">

**Author:** ![blueone](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@blueone](https://discourse.pymc.io/u/blueone)\
**Post date:** [November 24, 2018, 3:09pm UTC](https://discourse.pymc.io/t/shape-mismatch-error-in-sample-ppc/2270/3 "2018-11-24T15:09:34Z")

</div>

Thanks. Hope this could be fixed soon 🙂

---

<div class="post-metadata">

**Author:** ![blueone](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@blueone](https://discourse.pymc.io/u/blueone)\
**Post date:** [November 24, 2018, 3:11pm UTC](https://discourse.pymc.io/t/shape-mismatch-error-in-sample-ppc/2270/4 "2018-11-24T15:11:25Z")

</div>

FYI, are the inference results for GMM still reliable?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [November 24, 2018, 9:20pm UTC](https://discourse.pymc.io/t/shape-mismatch-error-in-sample-ppc/2270/5 "2018-11-24T21:20:43Z")

</div>

Yes, but be careful of model switching - with sampling one chain only and check the result carefully you should be fine.

---

<div class="post-metadata">

**Author:** ![blueone](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@blueone](https://discourse.pymc.io/u/blueone)\
**Post date:** [November 25, 2018, 5:13am UTC](https://discourse.pymc.io/t/shape-mismatch-error-in-sample-ppc/2270/6 "2018-11-25T05:13:43Z")

</div>

Thanks! Do you have any suggestions for hand coding PPC for GMM since I am going to use that for my project?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [November 25, 2018, 8:31am UTC](https://discourse.pymc.io/t/shape-mismatch-error-in-sample-ppc/2270/7 "2018-11-25T08:31:10Z")

</div>

extracting the posterior from trace, and simulate it in scipy/numpy should work.  
Otherwise, you can try setting up a gmm from scikit-learn, using the posterior mean as parameter input - I never actually do it this way, but it should work as well.

---

<div class="post-metadata">

**Author:** ![breadbaron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/breadbaron/32/1592_2.png) [@breadbaron](https://discourse.pymc.io/u/breadbaron)\
**Post date:** [March 6, 2019, 8:44pm UTC](https://discourse.pymc.io/t/shape-mismatch-error-in-sample-ppc/2270/8 "2019-03-06T20:44:30Z")

</div>

I have run into this problem as well - is it still unfixed?

---

<div class="post-metadata">

**Author:** ![breadbaron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/breadbaron/32/1592_2.png) [@breadbaron](https://discourse.pymc.io/u/breadbaron)\
**Post date:** [March 6, 2019, 9:08pm UTC](https://discourse.pymc.io/t/shape-mismatch-error-in-sample-ppc/2270/9 "2019-03-06T21:08:40Z")

</div>

Ah ok - when cloning the most recent version from github, things seem to work. Thanks for fixing!
