diff --git a/pymc/printing.py b/pymc/printing.py index 6695cf38fce..56445ab9ea8 100644 --- a/pymc/printing.py +++ b/pymc/printing.py @@ -232,6 +232,12 @@ def _expand(x): if x.owner and isinstance(x.owner.op, RandomVariable | SymbolicRandomVariable): parents.append(x) xname = x.name + if xname is None: + # If the variable is unnamed, we show the op's name as we do + # with constants + opname = x.owner.op.name + if opname is not None: + xname = rf"<{opname}>" assert xname is not None names.append(xname) diff --git a/tests/test_printing.py b/tests/test_printing.py index 95a1e812ec1..406032b124c 100644 --- a/tests/test_printing.py +++ b/tests/test_printing.py @@ -125,8 +125,11 @@ def setup_class(self): # add a potential as well pot = Potential("pot", mu**2) + # add a deterministic that depends on an unnamed random variable + pred = Deterministic("pred", Normal.dist(0, 1)) + self.distributions = [alpha, sigma, mu, b, Z, nb2, zip, w, nested_mix, Y_obs, pot] - self.deterministics_or_potentials = [mu, pot] + self.deterministics_or_potentials = [mu, pot, pred] # tuples of (formatting, include_params) self.formats = [("plain", True), ("plain", False), ("latex", True), ("latex", False)] self.expected = { @@ -146,6 +149,7 @@ def setup_class(self): ), r"Y_obs ~ Normal(mu, sigma)", r"pot ~ Potential(f(beta, alpha))", + r"pred ~ Deterministic(f())", ], ("plain", False): [ r"alpha ~ Normal", @@ -159,6 +163,7 @@ def setup_class(self): r"nested_mix ~ MarginalMixture", r"Y_obs ~ Normal", r"pot ~ Potential", + r"pred ~ Deterministic", ], ("latex", True): [ r"$\text{alpha} \sim \operatorname{Normal}(0,~10)$", @@ -176,6 +181,7 @@ def setup_class(self): ), r"$\text{Y_obs} \sim \operatorname{Normal}(\text{mu},~\text{sigma})$", r"$\text{pot} \sim \operatorname{Potential}(f(\text{beta},~\text{alpha}))$", + r"$\text{pred} \sim \operatorname{Deterministic}(f(\text{}))", ], ("latex", False): [ r"$\text{alpha} \sim \operatorname{Normal}$", @@ -189,6 +195,7 @@ def setup_class(self): r"$\text{nested_mix} \sim \operatorname{MarginalMixture}$", r"$\text{Y_obs} \sim \operatorname{Normal}$", r"$\text{pot} \sim \operatorname{Potential}$", + r"$\text{pred} \sim \operatorname{Deterministic}", ], }