deterministics_to_flat#

pymc_marketing.model_graph.deterministics_to_flat(model, names)[source]#

Replace all specified Deterministic nodes in a pm.Model with Flat.

This is useful to capture some state from a model and to then sample from the model using that state. For example, capturing the mean of a distribution or a value of a deterministic variable.

See pymc_marketing.mmm.hsgp.SoftPlusHSGP for an example of how this is used to keep a variable centered around 1.0 during sampling but stay continuous with new values.

Parameters:
modelpm.Model

PyMC model to be transformed

nameslist[str]

Names of the deterministic variables to be replaced by flat

Returns:
new_modelpm.Model

New model with all priors replaced by flat priors

Examples

Replace single Deterministic with Flat and sample as if it were zeros.

import pymc as pm
import numpy as np
import xarray as xr
from pymc_marketing.model_graph import deterministics_to_flat

with pm.Model() as model:
    x = pm.Normal("x", mu=0, sigma=1)
    y = pm.Deterministic("y", x ** 2)
    z = pm.Deterministic("z", x + y)

new_model = deterministics_to_flat(model, ["y"])

chains, draws = 2, 100
mock_posterior = xr.Dataset({
    "y": (("chain", "draw"), np.zeros((chains, draws))),
}, coords={"chain": np.arange(chains), "draw": np.arange(draws)})

x_z_given_y = pm.sample_posterior_predictive(
    mock_posterior,
    model=new_model,
    var_names=["x", "z"],
).posterior_predictive

np.testing.assert_allclose(
    x_z_given_y["x"],
    x_z_given_y["z"],
)