Hi! I’m trying to implement a simple recursive function that works with PyMC3 random variables. Let’s say it sums all numbers from 1 to X. I know that we can’t use if..else
when working with random variables.
def sum (x):
return x + sum(x-1) if x > 0 else 0
That produces TypeError: Variables do not support boolean operations.
But pm.math.switch
doesn’t work either:
import pymc3 as pm
def sum (x):
return pm.math.switch(x>0, x + sum(x-1), 0)
with pm.Model() as model:
b = pm.Binomial('b', n=10, p=0.5)
y = pm.Deterministic('y', sum(b))
result = pm.sample()
The result is RecursionError: maximum recursion depth exceeded while calling a Python object
What am I doing wrong?
Upd.: Using pm.math.gt(x,0)
instead of x>0
doesn’t help