Hi everyone,
I am trying to implement a Gaussian Mixture Model to describe a multimodal dataset. Unlike what is implemented here, where μ follows a Normal distribution, I want to implement a linear model.
----------------------------------------------------------------------------------------------------------------------------------------
Code:
Generate synthetic data:
RANDOM_SEED = 8927
rng = np.random.default_rng(RANDOM_SEED)
az.style.use("arviz-darkgrid")
def standardize(series):
"""Standardize a pandas series"""
return (series - series.mean()) / series.std()
N, k = 500, 3
centers = np.array([-5, 0, 5])
sds = np.array([0.40, 2.0, 0.20])
idx = rng.integers(0, k, N)
true_a, true_b, predictor = 0.5, 3.0, rng.normal(loc=centers[idx], scale=sds[idx], size=N)
true_mu = true_a + true_b * predictor
outcome = rng.normal(loc=true_mu, scale=sds[idx], size=N)
predictor_scaled = standardize(predictor)
outcome_scaled = standardize(outcome)
plt.hist(outcome_scaled)
plt.show()
----------------------------------------------------------------------------------------------------------------------------------------
Model:
with pm.Model(coords={"cluster": range(k)}) as model:
a = pm.Normal("a", 0.0, 10.0)
b = pm.Normal("b", 0.0, 10.0)
μ = pm.Deterministic("μ", var=a + b * predictor_scaled, dims="cluster")
σ = pm.HalfNormal("σ", sigma=1, dims="cluster")
weights = pm.Dirichlet("w", np.ones(k), dims="cluster")
pm.NormalMixture("x", w=weights, mu=μ, sigma=σ, observed=outcome_scaled)
idata = pm.sample()
---------------------------------------------------------------------------------------------------------------------------------------
Problem:
----------------------------------------------------------------------------------------------------------------------------------------
How can I change pm.Deterministic() to accept the predictor_scaled variable and the “dims” parameter without producing incompatibility of dimensions?