# Add bounds to gaussian unmixture model

**URL:** https://discourse.pymc.io/t/add-bounds-to-gaussian-unmixture-model/13454
**Category:** v5
**Created:** [December 12, 2023, 5:04am UTC](https://discourse.pymc.io/t/add-bounds-to-gaussian-unmixture-model/13454 "2023-12-12T05:04:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jamaltas](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@jamaltas](https://discourse.pymc.io/u/jamaltas)
#### Post date: [December 12, 2023, 5:04am UTC](https://discourse.pymc.io/t/add-bounds-to-gaussian-unmixture-model/13454/1 "2023-12-12T05:04:02Z")

</div>

I have a dataset that consists of two truncated normal distributions that I’m trying to unmix. I know the mu/sigma of one of the two and I’m trying to extract the weights, as well as the mu/sigma of the unknown gaussian. The follow code works most of the time:

```auto
with pm.Model() as model:
                w = pm.Dirichlet('w', a=np.array([1, 1])) # 2 mixture weights
                mu1 = pm.Normal("mu1", mu=np.mean(x), sigma=np.std(x))
                sig1 = pm.HalfNormal('sig1', sigma=np.std(x))
                
                tn1 = pm.TruncatedNormal.dist(mu=mu1,
                                            sigma=sig1,
                                            lower=low_cutoff,
                                            upper=high_cutoff)
                
                tn2 = pm.TruncatedNormal.dist(mu=known_mean,
                                            sigma=known_sigma,
                                            lower=low_cutoff,
                                            upper=high_cutoff)
                
                like = pm.Mixture(name="like",
                                w=w,
                                comp_dists=[tn1, tn2],
                                observed = x)

```

However sometimes it converges on some obviously wrong results. I know things about the distribution such as the bound on the weights (no distribution should be less than 5% of the mixture, sig1 should be bounded between 0.5 and 2), but I can’t quite figure out how to add bounds like this.

I tried:

`pm.Bound(pm.HalfNormal, lower=0.5, upper=2.0)('x', mu=np.mean(x), sigma=np.std(x))`

and I get an error about dist:

```auto
TypeError: __new__ () missing 1 required positional argument: 'dist'

```

It seems like this package would have a nice way to give the model this range before hand, but I just can’t quite figure it out in the documentation/googling around.

---

<div class="post-metadata">

### Author: ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)
#### Post date: [December 12, 2023, 5:12am UTC](https://discourse.pymc.io/t/add-bounds-to-gaussian-unmixture-model/13454/2 "2023-12-12T05:12:10Z")

</div>

I suggest you use `pm.Truncated` instead of the old `pm.Bound`: [Truncated — PyMC dev documentation](https://www.pymc.io/projects/docs/en/latest/api/distributions/truncated.html#pymc.Truncated).

There is an examples in the docs of the API. Bound is actually similar if you really insist on it

---

<div class="post-metadata">

### Author: ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)
#### Post date: [December 12, 2023, 5:16am UTC](https://discourse.pymc.io/t/add-bounds-to-gaussian-unmixture-model/13454/3 "2023-12-12T05:16:12Z")

</div>

Cor your weights you can give a slightly larger alpha prior like [10, 10], that is a bit more compatible with your knowledge that no distribution can be less than 5. Instead it’s a bit more concentrated towards the 50-50 split

Otherwise just scale the weights, like `w = 0.05 + w*0.9` or something more gradual that goes in the same direction

---

<div class="post-metadata">

### Author: ![jamaltas](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@jamaltas](https://discourse.pymc.io/u/jamaltas)
#### Post date: [December 12, 2023, 5:20am UTC](https://discourse.pymc.io/t/add-bounds-to-gaussian-unmixture-model/13454/4 "2023-12-12T05:20:27Z")

</div>

So I tried this about an hour ago and maybe I just didn’t quite get it right? But it gave me a solution outside of the bounds I have given:

```auto
sig1 = pm.Truncated('sig1', pm.Normal.dist(mu=np.mean(x), sigma=np.std(x)), lower=0.50, upper=2.0)

```

Gave me values for sig1 that were all at 3.5ish (and were not good fits with the underlying data).

Did I mess that up somehow?

---

<div class="post-metadata">

### Author: ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)
#### Post date: [December 12, 2023, 5:23am UTC](https://discourse.pymc.io/t/add-bounds-to-gaussian-unmixture-model/13454/5 "2023-12-12T05:23:05Z")

</div>

It’s not possible to give you values outside of the range
