Reproducibility of pymc calculation results

Hi all,

Changing the order in which the prior distributions are defined changes the calculation results. Is there any way to prevent the calculation results from changing just by changing the order?

Example code:

    with pm.Model() as model1:
      sigma = pm.HalfNormal("sigma", sigma=1)
      Z = pm.Normal("Z", 0, 1)

     ......

      idata = pm.sample(random_seed=rng)
    with pm.Model() as model2:
      Z = pm.Normal("Z", 0, 1)
      sigma = pm.HalfNormal("sigma", sigma=1)
 
     ......

      idata = pm.sample(random_seed=rng)

I would be thankful if you could help me with this question.

You can try to sort the variables in the model. Something like model.free_RVs = sorted(model.free_RVs, key=lambda var: var.name)

3 Likes

Thank you for your prompt reply! I was able to get the same result with this code.
Could you please tell me why this operation is necessary?

It has to do with random seeds. In numpy you also get different results depending on which variables you call first:

import numpy as np

seed = 123
rng = np.random.default_rng(seed)
print(rng.normal(), rng.uniform())

rng = np.random.default_rng(seed)
print(rng.uniform(), rng.normal())
-0.9891213503478509, 0.053821018802222675
0.6823518632481435, -0.3677866514678832

My command was just a trick to make sure the order was the same

I understood. Thank you very much!!