That loop will only be evaluated once when you create the variables and not be rerun or updated during sampling.
Okay, so this disappointed me a lot. Because now it looks like that I cannot even implement the if-statement sections correctly. There is no way to fix this either?
An example of regular python, and pymc3 giving different results:
Pymc3
with pm.Model() as model: # discreteuniform
i = pm.Bernoulli('i',p=0.3)
d = pm.Bernoulli('d',p=0.4)
if i.tag.test_value == 0 and d.tag.test_value == 0:
g = pm.Bernoulli('g',p=0.7)
elif i.tag.test_value == 0 and d.tag.test_value == 1:
g = pm.Bernoulli('g',p=0.95)
elif i.tag.test_value == 1 and d.tag.test_value == 0:
g = pm.Bernoulli('g',p=0.1)
else:
g = pm.Bernoulli('g',p=0.5)
if i.tag.test_value == 0:
s = pm.Bernoulli('s',p=0.05)
else:
s = pm.Bernoulli('s',p=0.8)
if g.tag.test_value == 0:
l = pm.Bernoulli('l',p=0.1)
else:
l = pm.Bernoulli('l',p=0.6)
z = pm.Deterministic(f'z', l)
step1 = pm.Metropolis([i,d,g,s,l,z])
trace2 = pm.sample(samples,return_inferencedata=0,step= step1)
python3, random library
from random import randint,uniform
samples = 1000000
si = 0
sd = 0
sg = 0
ss = 0
sl = 0
for _ in range(samples):
i = 1 if uniform(0,1) <= 0.3 else 0
d = 1 if uniform(0,1) <= 0.4 else 0
if not (i and d):
g = 1 if uniform(0,1) <= 0.7 else 0
elif (not i) and d:
g = 1 if uniform(0,1) <= 0.95 else 0
elif i and (not d):
g = 1 if uniform(0,1) <= 0.1 else 0
else:
g = 1 if uniform(0,1) <= 0.5 else 0
if not i:
s = 1 if uniform(0,1) <= 0.05 else 0
else:
s = 1 if uniform(0,1) <= 0.8 else 0
if not g:
l = 1 if uniform(0,1) <= 0.1 else 0
else:
l = 1 if uniform(0,1) <= 0.6 else 0
si += i
sd += d
sg += g
ss += s
sl += l
print(si/samples,sd/samples,sg/samples,ss/samples,sl/samples)