pymc-devs / pymc-resources

PyMC educational resources
MIT License
1.96k stars 745 forks source link

Type error - Code 15.12 - pymc v4 #219

Closed EAly closed 1 year ago

EAly commented 2 years ago

I get the following error when I attempt to run code 15.12 (the Pymc v4 version):

---------------------------------------------------------------------------
RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/home/eaunix/miniconda3/envs/StatsReth_Py39/lib/python3.9/site-packages/pymc/parallel_sampling.py", line 129, in run
    self._start_loop()
  File "/home/eaunix/miniconda3/envs/StatsReth_Py39/lib/python3.9/site-packages/pymc/parallel_sampling.py", line 163, in _start_loop
    np.random.seed(self._seed)
TypeError: 'int' object is not callable
"""

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
TypeError: 'int' object is not callable

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
/home/eaunix/xFiles/statsRethinking/ch015.ipynb Cell 26 in <cell line: 5>()
      [9](vscode-notebook-cell://wsl%2Bubuntu/home/eaunix/xFiles/statsRethinking/ch015.ipynb#X40sdnNjb2RlLXJlbW90ZQ%3D%3D?line=8) p= pm.invlogit(a + bS * S) # Model includes study S but not X
     [11](vscode-notebook-cell://wsl%2Bubuntu/home/eaunix/xFiles/statsRethinking/ch015.ipynb#X40sdnNjb2RlLXJlbW90ZQ%3D%3D?line=10) Hi= pm.Binomial('Hi', n=10, p=p, observed= H)
---> [13](vscode-notebook-cell://wsl%2Bubuntu/home/eaunix/xFiles/statsRethinking/ch015.ipynb#X40sdnNjb2RlLXJlbW90ZQ%3D%3D?line=12) trace_m15_3= pm.sample(random_seed=RANDOM_SEED)
...
--> 353     raise error from old_error
    354 elif msg[0] == "writing_done":
    355     proc._readable = True

RuntimeError: Chain 0 failed.

The code I'm running is:

from scipy.special import expit as invlogit
from numpy.random import default_rng
RANDOM_SEED= 1234
np.random.seed= RANDOM_SEED
rng= default_rng(RANDOM_SEED)

N= 1000
X= rng.normal(size=N) 
S= rng.normal(size=N)
H= rng.binomial(n=10, p=invlogit(2 + S -2 * X), size= N) 
D= X > 1 

with pm.Model() as m15_3:
    bS= pm.Normal('bS', 0, 0.5)
    a= pm.Normal('a', 0, 1)

    p= pm.invlogit(a + bS * S) # Model includes study S but not X

    Hi= pm.Binomial('Hi', n=10, p=p, observed= H)

    trace_m15_3= pm.sample(random_seed=RANDOM_SEED)

Could you please help me with this error?

EAly commented 2 years ago

@yahrMason would you please help with this error?

fonnesbeck commented 2 years ago

You've clobbered the np.random.seed function with an integer:

np.random.seed= RANDOM_SEED
ColCarroll commented 1 year ago

More specifically, you'd want to replace the line fonnesbeck noted with just np.random.seed(RANDOM_SEED)

EAly commented 1 year ago

Thanks, that solved the problem.