Skip to content

pm.sample(random_seed) sets the random_seed of functions from other packages #5714

@benslack19

Description

@benslack19

Description of your problem

The pymc random_seed set within pm.sample is affecting random function output of other. This is true in all pymc versions I've tested (3.11.0, 3.11.5, 4.0.0b6). I would say that this is unexpected since I would think that the model context would confine parameters within the pymc code block.

import pymc as pm
import scipy.stats as stats

for i in range(5):
    print(f" --- loop{i} --- ", end='\n')
    test_vals = stats.norm.rvs(loc=1, scale=1, size=5)
    print(test_vals, end='\n\n')

    with pm.Model() as m:
        dummy = pm.Normal("dummy", 0, 0.5)
        trace_test = pm.sample(
            draws=100, random_seed=0, return_inferencedata=False, progressbar=False
        )

There's no error but you can see that the stats.norm.rvs output repeats itself after the first loop.

# I'm omitting the standard pymc comments for readability

--- loop0 --- 
[1.24009922 0.66126999 1.03865944 1.54606376 1.59789667]

 --- loop1 --- 
[1.97873798 3.2408932  2.86755799 0.02272212 1.95008842]

 --- loop2 --- 
[1.97873798 3.2408932  2.86755799 0.02272212 1.95008842]

 --- loop3 --- 
[1.97873798 3.2408932  2.86755799 0.02272212 1.95008842]

 --- loop4 --- 
[1.97873798 3.2408932  2.86755799 0.02272212 1.95008842]

Removing the random_seed or setting random_seed=None does not show this behavior.

Versions and main components

  • PyMC/PyMC3 Version: 4.0.0b6
  • Aesara/Theano Version:
  • Scipy version: 1.8.0
  • Python Version: 3.9.12
  • Operating system: macOS 11.6.2 (20G314)
  • How did you install PyMC/PyMC3: pip

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