# Get the Likelihood Generative Distribution of Observation from a Mixture Model

**URL:** <https://discourse.pymc.io/t/get-the-likelihood-generative-distribution-of-observation-from-a-mixture-model/2196>\
**Category:** Questions\
**Created:** [November 11, 2018, 11:46pm UTC](https://discourse.pymc.io/t/get-the-likelihood-generative-distribution-of-observation-from-a-mixture-model/2196 "2018-11-11T23:46:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pwitkow](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/pwitkow/32/1241_2.png) [@pwitkow](https://discourse.pymc.io/u/pwitkow)\
**Post date:** [November 11, 2018, 11:46pm UTC](https://discourse.pymc.io/t/get-the-likelihood-generative-distribution-of-observation-from-a-mixture-model/2196/1 "2018-11-11T23:46:25Z")

</div>

Hi,

Sorry if this is a basic question, but I haven’t been able to find an answer for this anywhere. I’ve fit a mixture model using a combination of Von Mises and Uniform distributions (code below) using a set of observations. Now I want to see whether each observation is more likely to have come from the Von Mises or a Uniform distribution - similar to pomegranate’s “predict” method.

```auto
        model=pm.Model()
            
        with model:
            #uniform
            uni = pm.Uniform.dist(lower = -np.pi, upper = np.pi)    
            #Von-mises
            kappa = pm.Uniform('kappa', lower = 0, upper = 200)  
            #mu = pm.Normal('mu', mu=0, sd=3) 
            von = pm.VonMises.dist( mu = 0, kappa = kappa)        
            #mixture model 
            w = pm.Dirichlet('w', a=np.array([1, 1]), shape = 2)
            like = pm.Mixture('like', w=w, comp_dists = [von, uni], observed=data)

```

---

<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 12, 2018, 6:16am UTC](https://discourse.pymc.io/t/get-the-likelihood-generative-distribution-of-observation-from-a-mixture-model/2196/2 "2018-11-12T06:16:52Z")

</div>

We dont have a built-in function for that: a workaround is to wrap the comp\_dist loglike into a function, something like:

```python
complogp = like.distribution._comp_logp(theano.shared(data))
f_complogp = model.model.fastfn(complogp)
y_ = []
for point in trace:
    # get prediction
    y_.append(np.argmax(f_complogp(point), axis=1))

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![pwitkow](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/pwitkow/32/1241_2.png) [@pwitkow](https://discourse.pymc.io/u/pwitkow)\
**Post date:** [November 12, 2018, 9:20pm UTC](https://discourse.pymc.io/t/get-the-likelihood-generative-distribution-of-observation-from-a-mixture-model/2196/3 "2018-11-12T21:20:15Z")

</div>

Thanks for the help! I’m getting an error back though when I pass point to the f\_complogp function. So I ran a trace with the model (which I though was implicit in your for-loop). This make a MultiTrace object I can loop over an pass each dict in the trace to f\_complogp. However, I’m getting this error :

```
    795 
    796 if len(args) + len(kwargs) > len(self.input_storage):
    797 raise TypeError("Too many parameter passed to theano function")
    798 
    799 # Set positional arguments
    TypeError: Too many parameter passed to theano function

```

This stems directly from trying to pass these dicts to the f\_complogp function. Any idea what this is about?

---

<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 12, 2018, 9:54pm UTC](https://discourse.pymc.io/t/get-the-likelihood-generative-distribution-of-observation-from-a-mixture-model/2196/4 "2018-11-12T21:54:17Z")

</div>

yes… the problem is that the point in a trace object contains also deterministic transformation, and theano function only takes input of “raw” value. Something like below should work:

```python
import theano
complogp = like.distribution._comp_logp(theano.shared(y))
f_complogp = model.model.fastfn(complogp)
testpoint = model.test_point
y_ = []
for i in range(trace.nchains): # to get all the points from a multi trace
    tr = trace._straces[i]
    for point in tr:
        d2 = dict((k,v) for k,v in point.items() if k in testpoint.keys())
        # get prediction
        y_.append(np.argmax(f_complogp(d2), axis=1))

```
