pymc icon indicating copy to clipboard operation
pymc copied to clipboard

Don't allow Deterministics in the graph of observed

Open ricardoV94 opened this issue 8 months ago • 2 comments

Description

In https://github.com/pymc-devs/pymc/pull/7656 we allow PyTensor deterministic operations in the graph of observed variables, but we probably don't want to allow PyMC determinstics. Doing that makes model cloning failing, for starters:

import pymc as pm

with pm.Model() as m:
    x_data = pm.Data("x_data", [0, 1, 2])
    y_data = pm.Deterministic("y", x_data + 1)
    y = pm.Normal("y", observed=y_data)

m.clone()  # ValueError: Variable name y already exists.

I am not sure about the implications of mixing the two, I suggest we be conservative and don't allow it for now, unless a good reason crops up. Also a Deterministic like this would be wastefully saved in every iteration of pm.sample!

CC @williambdean

ricardoV94 avatar Apr 28 '25 13:04 ricardoV94

You are using the same name for the Deterministic and the likelihood. Is that intended?

For example, this works:


import pymc as pm

from pymc.model.fgraph import clone_model

with pm.Model() as m:
    x_data = pm.Data("x_data", [0, 1, 2])
    # Change
    y_data = pm.Deterministic("y_data", x_data + 1)
    y = pm.Normal("y", observed=y_data)


m_clone = clone_model(m)

williambdean avatar May 08 '25 04:05 williambdean

@williambdean that means my example above was not a MWE unfortunately. @cetagostini found the original bug and I don't think it was a dumb name replication because the error only happened when we cloned the model. It was a similar error message that's why I didn't careful check if my MWE was failing at the right point

ricardoV94 avatar May 08 '25 07:05 ricardoV94