pymc icon indicating copy to clipboard operation
pymc copied to clipboard

Disconnected node in model graph after deterministic operations

Open williambdean opened this issue 8 months ago • 0 comments

The models that are now allowed following https://github.com/pymc-devs/pymc/pull/7656 have a disconnected node in the model graph.

The sampling is as expected. It is just the graphviz representation that is incorrect.

import numpy as np
import pymc as pm
from pymc.model_graph import ModelGraph

seed = sum(map(ord, "Observed disconnected node"))
rng = np.random.default_rng(seed)

true_mu = 100
true_sigma = 30

n_obs = 10
coords = {
    "date": np.arange(n_obs),
}

dist = pm.Normal.dist(mu=true_mu, sigma=true_sigma, shape=n_obs)
data = pm.draw(dist, random_seed=rng)

scaling = data.max()

with pm.Model(coords=coords) as model:
    mu = pm.Normal("mu")
    sigma = pm.HalfNormal("sigma")

    target = pm.Data("target", data, dims="date")
    scaled_target = target / scaling

    pm.Normal("observed", mu=mu, sigma=sigma, observed=scaled_target, dims="date")

pm.model_to_graphviz(model).render("scaled_target")

ModelGraph(model).make_compute_graph()

The observed should have "target" in the compute_graph

defaultdict(set,
            {'mu': set(),
             'sigma': set(),
             'target': set(),
             'observed': {'mu', 'sigma'}})

Seems like it needs a fix here:

https://github.com/pymc-devs/pymc/blob/af81955f799e1fba1adcf839454ccc0be851008a/pymc/model_graph.py?plain=1#L322-L343

williambdean avatar Mar 14 '25 13:03 williambdean