# Defining prior as mixture of two normal distribution

**URL:** <https://discourse.pymc.io/t/defining-prior-as-mixture-of-two-normal-distribution/9942>\
**Category:** General\
**Created:** [July 19, 2022, 1:24am UTC](https://discourse.pymc.io/t/defining-prior-as-mixture-of-two-normal-distribution/9942 "2022-07-19T01:24:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amita](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/amita/32/4738_2.png) [@Amita](https://discourse.pymc.io/u/Amita)\
**Post date:** [July 19, 2022, 1:24am UTC](https://discourse.pymc.io/t/defining-prior-as-mixture-of-two-normal-distribution/9942/1 "2022-07-19T01:24:53Z")

</div>

I want to define a prior of a variable (gamma) as a mixture of two normal distributions:  
2/3_N(mu=5, sigma=5)+1/3_N(mu=60, sigma=30)

I am doing something like this, but is this how it should be done?  
gamma1 = pm.Normal(“gamma1”, mu=5, sigma=5)  
gamma2 = pm.Normal(“gamma2”, mu=60, sigma=30)  
gamma = pm.Deterministic(“gamma”, (2/3)\*gamma1+(1/3)\*gamma2)

---

<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:** [July 19, 2022, 6:30am UTC](https://discourse.pymc.io/t/defining-prior-as-mixture-of-two-normal-distribution/9942/2 "2022-07-19T06:30:10Z")

</div>

You can use pm.Mixture, or if you only have Normal components pm.NormalMixture

---

<div class="post-metadata">

**Author:** ![drbenvincent](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/drbenvincent/32/4313_2.png) [@drbenvincent](https://discourse.pymc.io/u/drbenvincent)\
**Post date:** [July 19, 2022, 9:25am UTC](https://discourse.pymc.io/t/defining-prior-as-mixture-of-two-normal-distribution/9942/3 "2022-07-19T09:25:22Z")

</div>

See the fully worked through example here [Gaussian Mixture Model — PyMC example gallery](https://www.pymc.io/projects/examples/en/latest/mixture_models/gaussian_mixture_model.html)

---

<div class="post-metadata">

**Author:** ![Amita](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/amita/32/4738_2.png) [@Amita](https://discourse.pymc.io/u/Amita)\
**Post date:** [July 19, 2022, 11:17pm UTC](https://discourse.pymc.io/t/defining-prior-as-mixture-of-two-normal-distribution/9942/4 "2022-07-19T23:17:27Z")

</div>

Thanks! I tried running the worked out example for Gaussian mixture model and encountered this error:

```auto
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [25], in <cell line: 1>()
     10 σ = pm.HalfNormal("σ", sigma=1, dims="cluster")
     11 weights = pm.Dirichlet("w", np.ones(k), dims="cluster")
---> 12 pm.NormalMixture("x", w=weights, mu=μ, sigma=σ, observed=x)

File ~/anaconda3/lib/python3.9/site-packages/pymc/distributions/distribution.py:267, in Distribution. __new__ (cls, name, rng, dims, initval, observed, total_size, transform, *args, **kwargs)
    263 rng = model.next_rng()
    265 # Create the RV and process dims and observed to determine
    266 # a shape by which the created RV may need to be resized.
--> 267 rv_out, dims, observed, resize_shape = _make_rv_and_resize_shape(
    268 cls=cls, dims=dims, model=model, observed=observed, args=args, rng=rng, **kwargs
    269 )
    271 if resize_shape:
    272 # A batch size was specified through `dims`, or implied by `observed`.
    273 rv_out = change_rv_size(rv_var=rv_out, new_size=resize_shape, expand=True)

File ~/anaconda3/lib/python3.9/site-packages/pymc/distributions/distribution.py:166, in _make_rv_and_resize_shape(cls, dims, model, observed, args, **kwargs)
    163 """Creates the RV and processes dims or observed to determine a resize shape."""
    164 # Create the RV without dims information, because that's not something tracked at the Aesara level.
    165 # If necessary we'll later replicate to a different size implied by already known dims.
--> 166 rv_out = cls.dist(*args, **kwargs)
    167 ndim_actual = rv_out.ndim

```

```auto
168 resize_shape = None
TypeError: dist() missing 1 required positional argument: 'dist_params'

```

I am using PyMC version 4

---

<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:** [July 20, 2022, 6:02am UTC](https://discourse.pymc.io/t/defining-prior-as-mixture-of-two-normal-distribution/9942/5 "2022-07-20T06:02:24Z")

</div>

Seems like you are using an old version of pymc4 (perhaps a beta still) where NormalMixture hadn’t been refactored yet?

Can you try to update to pymc\>=4.1 ?

---

<div class="post-metadata">

**Author:** ![Amita](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/amita/32/4738_2.png) [@Amita](https://discourse.pymc.io/u/Amita)\
**Post date:** [July 21, 2022, 12:17am UTC](https://discourse.pymc.io/t/defining-prior-as-mixture-of-two-normal-distribution/9942/6 "2022-07-21T00:17:02Z")

</div>

Thanks! I upgraded pymc and now it works!
