Hi, I was playing with this post on Bayesian MMM.
coords = {"date": date, "fourier_mode": np.arange(2 * n_order)}
with pm.Model(coords=coords) as base_model:
# --- coords ---
base_model.add_coord(name="dat", values=date, mutable=True)
base_model.add_coord(name="fourier_mode", values=np.arange(2 * n_order), mutable=False)
# --- data containers ---
z_scaled_ = pm.MutableData(name="z_scaled", value=z_scaled, dims="date")
# --- priors ---
## intercept
a = pm.Normal(name="a", mu=0, sigma=4)
## trend coefficient
b_trend = pm.Normal(name="b_trend", mu=0, sigma=2)
## seasonality coefficient
b_fourier = pm.Laplace(name="b_fourier", mu=0, b=2, dims="fourier_mode")
## regressor coefficient
b_z = pm.HalfNormal(name="b_z", sigma=2)
## standard deviation of the normal likelihood
sigma = pm.HalfNormal(name="sigma", sigma=0.5)
# degrees of freedom of the t distribution
nu = pm.Gamma(name="nu", alpha=25, beta=2)
# --- model parametrization (coeff * data) ---
trend = pm.Deterministic(name="trend", var=a + b_trend * t, dims="date")
seasonality = pm.Deterministic(
name="seasonality", var=pm.math.dot(fourier_features, b_fourier), dims="date"
)
z_effect = pm.Deterministic(name="z_effect", var=b_z * z_scaled_, dims="date")
# expected value of the outcome
mu = pm.Deterministic(name="mu", var=trend + seasonality + z_effect, dims="date")
# --- likelihood ---
pm.StudentT(name="likelihood", nu=nu, mu=mu, sigma=sigma, observed=y_scaled, dims="date")
# --- prior samples ---
base_model_prior_predictive = pm.sample_prior_predictive()
pm.model_to_graphviz(model=base_model)
What I was struggling with is that once I don’t assign pm.Deterministic(name="mu", var=trend + seasonality + z_effect, dims="date")
to mu
the code raises error in sampling step
with base_model:
base_model_trace = pm.sample(
nuts_sampler="numpyro",
draws=6_000,
chains=4,
idata_kwargs={"log_likelihood": True},
)
base_model_posterior_predictive = pm.sample_posterior_predictive(
trace=base_model_trace
)
the above code throws the ValueError: Random variables detected in the logp graph: {a, b_fourier, b_z, b_trend}.
Is there anyone can help me with why this happens?