# Help with Mixture of Weibull Distribution

**URL:** <https://discourse.pymc.io/t/help-with-mixture-of-weibull-distribution/1079>\
**Category:** Questions\
**Created:** [April 11, 2018, 2:23pm UTC](https://discourse.pymc.io/t/help-with-mixture-of-weibull-distribution/1079 "2018-04-11T14:23:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TMaheshGoud](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@TMaheshGoud](https://discourse.pymc.io/u/TMaheshGoud)\
**Post date:** [April 11, 2018, 2:23pm UTC](https://discourse.pymc.io/t/help-with-mixture-of-weibull-distribution/1079/1 "2018-04-11T14:23:27Z")

</div>

import pymc3 as pm  
import numpy as np

#Use iterable of distributions instead of array of random variables  
with pm.Model() as weibull\_2seg\_2clust\_withspike:  
gamma\_alpha = pm.Uniform(‘gamma\_alpha’, lower=0.0, upper=30.0)  
gamma\_beta = pm.Uniform(‘gamma\_beta’, lower=0.0, upper=30.0)  
shape1 = pm.Uniform(‘shape1’, lower=0.0, upper=30.0)  
scale1 = pm.Gamma(‘scale1’, alpha=gamma\_alpha, beta=gamma\_beta)  
shape2 = pm.Uniform(‘shape2’, lower=0.0, upper=30.0)  
scale2 = pm.Gamma(‘scale2’, alpha=gamma\_alpha, beta=gamma\_beta)

```
weibull1 = pm.Weibull('weibull1', alpha=shape1, beta=scale1)
weibull2 = pm.Weibull('weibull2', alpha=shape2, beta=scale2)   
#obs = pm.Normal('obs', mu=weibull1 + weibull2 , sd=10, observed=[1,2,3,4,5])

#weibull1 = pm.Weibull.dist(alpha=shape1, beta=scale1)
#weibull2 = pm.Weibull.dist(alpha=shape2, beta=scale2)

w = pm.Dirichlet('w', a=np.array([1, 1]))
obs = pm.Mixture('obs', w=w, comp_dists=[weibull1, weibull2], observed=[1,2,3,4,5])

trace = pm.sample(1000)

```

Above is a sample code reproducing my error. Any help is appreciated.

---

<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:** [April 11, 2018, 3:13pm UTC](https://discourse.pymc.io/t/help-with-mixture-of-weibull-distribution/1079/2 "2018-04-11T15:13:37Z")

</div>

You should uncommented and use below.

```python
# weibull1 = pm.Weibull.dist(alpha=shape1, beta=scale1)
# weibull2 = pm.Weibull.dist(alpha=shape2, beta=scale2)

```

---

<div class="post-metadata">

**Author:** ![TMaheshGoud](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@TMaheshGoud](https://discourse.pymc.io/u/TMaheshGoud)\
**Post date:** [April 12, 2018, 8:01am UTC](https://discourse.pymc.io/t/help-with-mixture-of-weibull-distribution/1079/3 "2018-04-12T08:01:39Z")

</div>

Thanks @junpenglao

I was trying to create a mixture of 2 weibull distributions and am facing few issues with that line

```auto
import pymc3 as pm
import numpy as np

#Use iterable of distributions instead of array of random variables
with pm.Model() as weibull_2seg_2clust_withspike:
    gamma_alpha = pm.Uniform('gamma_alpha', lower=0.0, upper=30.0)
    gamma_beta = pm.Uniform('gamma_beta', lower=0.0, upper=30.0)
    shape1 = pm.Uniform('shape1', lower=0.0, upper=30.0)
    scale1 = pm.Gamma('scale1', alpha=gamma_alpha, beta=gamma_beta)
    shape2 = pm.Uniform('shape2', lower=0.0, upper=30.0)
    scale2 = pm.Gamma('scale2', alpha=gamma_alpha, beta=gamma_beta)   
    
    #weibull1 = pm.Weibull('weibull1', alpha=shape1, beta=scale1)
    #weibull2 = pm.Weibull('weibull2', alpha=shape2, beta=scale2)   
    #obs = pm.Normal('obs', mu=weibull1 + weibull2 , sd=10, observed=[1,2,3,4,5])
    
    weibull1 = pm.Weibull.dist(alpha=shape1, beta=scale1)
    weibull2 = pm.Weibull.dist(alpha=shape2, beta=scale2)
    
    w = pm.Dirichlet('w', a=np.array([1, 1]))
    obs = pm.Mixture('obs', w=w, comp_dists=[weibull1, weibull2], observed=[1,2,3,4,5])
    
    trace = pm.sample(1000)

```

---

<div class="post-metadata">

**Author:** ![TMaheshGoud](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@TMaheshGoud](https://discourse.pymc.io/u/TMaheshGoud)\
**Post date:** [April 12, 2018, 8:03am UTC](https://discourse.pymc.io/t/help-with-mixture-of-weibull-distribution/1079/4 "2018-04-12T08:03:14Z")

</div>

I am getting a huge stack trace with above code with a final error of **ValueError: Mass matrix contains zeros on the diagonal. Some derivatives might always be zero.**

---

<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:** [April 12, 2018, 8:09am UTC](https://discourse.pymc.io/t/help-with-mixture-of-weibull-distribution/1079/5 "2018-04-12T08:09:57Z")

</div>

That usually is an indication of bad prior causing your model either unidentifiable or difficult to inference ([Model diagnostics for “Mass matrix contains zeros on the diagonal”](https://discourse.pymc.io/t/model-diagnostics-for-mass-matrix-contains-zeros-on-the-diagonal/957/3)).  
Try changing replacing the uniform prior (usually a bad choice for NUTS) to something more informative, eg  
`shape1 = pm.HalfNormal('shape1', 1.)`

---

<div class="post-metadata">

**Author:** ![TMaheshGoud](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@TMaheshGoud](https://discourse.pymc.io/u/TMaheshGoud)\
**Post date:** [April 12, 2018, 8:20am UTC](https://discourse.pymc.io/t/help-with-mixture-of-weibull-distribution/1079/6 "2018-04-12T08:20:48Z")

</div>

Thanks. Trying it.

By the way when i used

```auto
    #trace = pm.sample(1000)
    
    inference = pm.fit(method='advi') # this is mean field ADVI. something like 'fullrank_advi' might be tried too
    trace = inference.sample(1000)

```

original code worked

---

<div class="post-metadata">

**Author:** ![TMaheshGoud](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@TMaheshGoud](https://discourse.pymc.io/u/TMaheshGoud)\
**Post date:** [April 12, 2018, 8:25am UTC](https://discourse.pymc.io/t/help-with-mixture-of-weibull-distribution/1079/7 "2018-04-12T08:25:54Z")

</div>

NUTS sampler worked smoothly with HalfNormal prior as well
