Defining prior as mixture of two normal distribution

I want to define a prior of a variable (gamma) as a mixture of two normal distributions:
2/3N(mu=5, sigma=5)+1/3N(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)

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

1 Like

See the fully worked through example here Gaussian Mixture Model — PyMC example gallery

1 Like

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

---------------------------------------------------------------------------
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
168 resize_shape = None
TypeError: dist() missing 1 required positional argument: 'dist_params'

I am using PyMC version 4

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 ?

Thanks! I upgraded pymc and now it works!