# Properly sampling mixture models

**URL:** <https://discourse.pymc.io/t/properly-sampling-mixture-models/986>\
**Category:** Questions\
**Created:** [March 22, 2018, 5:21am UTC](https://discourse.pymc.io/t/properly-sampling-mixture-models/986 "2018-03-22T05:21:17Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![jdhooghe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jdhooghe/32/590_2.png) [@jdhooghe](https://discourse.pymc.io/u/jdhooghe)\
**Post date:** [March 25, 2018, 12:46am UTC](https://discourse.pymc.io/t/properly-sampling-mixture-models/986/7 "2018-03-25T00:46:06Z")

</div>

So I am attempting to use the mixture class via:

```auto
with pm.Model() as Model:
    
    y = theano.shared(X_)
    NumberOfData = g.shape[1]
    #Create a covariance matrix for each potential cluster which relates all features of our data
    Lower = tt.stack([pm.LKJCholeskyCov('Sigma_{}'.format(k), n=NumberOfFeatures, eta=2., 
                                        sd_dist=pm.HalfNormal.dist(sd = 1.)) for k in range(NumberOfClusters)])
    Chol = tt.stack([pm.expand_packed_triangular(NumberOfFeatures, Lower[k]) for k in range(NumberOfClusters)])

    #The center of each cluster
    Mus = tt.stack([pm.Uniform('Mu_{}'.format(k), lower = 0., upper = 1., shape=NumberOfFeatures) for k in range(NumberOfClusters)])

    #Create the multivariate normal distribution for each cluster
    MultivariateNormals = [pm.MvNormal.dist(Mus[k], chol=Chol[k], shape = NumberOfFeatures) for k in range(NumberOfClusters)]

    #Create the weights for each cluster which measures how much impact they have 
    Weights = pm.Dirichlet('w', np.ones(NumberOfClusters)/NumberOfClusters)
      
    #Due to software bugs, we create a log likelihood by hand
    #logpcomp = tt.stack([Dist.logp(theano.shared(y)) for Dist in MultivariateNormals], axis=1)
    #Prob = pm.Deterministic("Probabilities", logpcomp)
    
    Prob = pm.Mixture("prob", w=Weights, comp_dists=MultivariateNormals, shape = NumberOfDataPoints, observed = y)

```

but I am receiving

```auto
/anaconda3/lib/python3.6/site-packages/six.py in reraise(tp, value, tb)
    691 if value. __traceback__ is not tb:
    692 raise value.with_traceback(tb)
--> 693 raise value
    694 finally:
    695 value = None

ValueError: Input dimension mis-match. (input[0].shape[1] = 5, input[1].shape[1] = 1000)

```

where 5 is the number of clusters and 1000 is the number of data points. Thank you so much for your help!

---

_[View the full topic](https://discourse.pymc.io/t/properly-sampling-mixture-models/986)._
