Skip to content

trace fn does not respect random seed for stochastic "Deterministics" #7588

Open
@ricardoV94

Description

@ricardoV94

Description

import pymc as pm

with pm.Model() as m:
    x = pm.Bernoulli("x", p=0.5) * 0 # Force it to be zero
    pm.Deterministic("y", x + pm.Normal.dist())

    idata1 = pm.sample(tune=0, draws=1, random_seed=1)
    idata2 = pm.sample(tune=0, draws=1, random_seed=1)
    
print(
    idata1.posterior["y"].sel(draw=0).values,
    idata2.posterior["y"].sel(draw=0).values,
    sep="\n"
)
# [-0.39110838 -0.10602868 -0.64066713  0.59270806]
# [ 1.30907711 -0.1391542   0.52679751  0.47189673]

We could pass the rng to the inner compile_pymc. But it's still a bit silly to compile the same function per chain. We could compile the function once and pass it to the trace. After #7578 this should be allowed. We just need to make sure we copy the function with new shared RNGs.

Alternatively, we could defer to compute_deterministics, and move the deterministics computations out of the sampling loop? This would come at a higher memory cost when users choose not to store certain free variables in the trace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions