Model problem but no error

@pymc-bot
I try to run this model:
import pymc as pm

with pm.Model() as m:
x = pm.Normal(“x”)
if x > 0:
mu = x + 1
else:
mu = x - 1
y = pm.Normal(“y”, mu=mu, observed=10)

there is no error message but it doesn’t seem to work as expected. what could be the problem?

The issue with your PyMC model is likely due to the way conditional statements are being used. In PyMC, you cannot use standard Python control flow statements like if and else to define model parameters because these statements are not compatible with the symbolic nature of PyMC’s random variables.

Instead, you should use PyMC’s built-in functions that are designed to handle conditional logic. For example, you can use pm.math.switch to implement conditional logic in a way that is compatible with PyMC’s symbolic computation. Here’s how you can modify your model:

import pymc as pm

with pm.Model() as m:
    x = pm.Normal("x")
    mu = pm.math.switch(x > 0, x + 1, x - 1)
    y = pm.Normal("y", mu=mu, observed=10)

In this modified version, pm.math.switch is used to choose between x + 1 and x - 1 based on the condition x > 0. This should resolve the issue and allow your model to work as expected.

This answer was generated by the PyMC Assistant bot in response to your command. Please let us know if it was helpful!