Error in "A student's guide to bayesian statistics" example

Hi,

I’m learning Bayesian statistics using “A Student’s Guide to Bayesian Statistics by Ben Lambert”. I’m working through an example in the later chapters of the book where the aim is to infer how many times a coin was flipped N given that 10 experiments were performed and the number of heads in each experiment is given by the data in Y below.

The code below is copied from the book and compiles without errors but when I try and fit the code I get a runtime Initialization error (see separate Python and Stan(terminal) traces at the bottom of post). I think I understand why the error appears but I’m not sure how to fix it. I’ve tried changing things the “transformed parameters” section of the Stan model code, particularly playing with the (Y | N[s], theta) portion of the binomial_lpmf by removing and adding [s] from the code but the error is pretty much always the same even though some of the numbers may change. Any help with this would be greatly appreciated.

Many thanks,
Jaf

p.s. apologies if I’ve missed any standards for this forum - it’s my first post.


Y = [9,7,11,19,19,9,8,11,9,11]
nStudy = 10

model_code = """
    data {
        int nStudy;
        int Y[nStudy];
    }
    
    transformed data{
        int N[10];
        for (s in 1:10)
            N[s] = 10 + s;
    }
    
    parameters {
      real<lower=0,upper=1> theta;
    }
    
    transformed parameters {
        vector[10] lp;
        for (s in 1:10)
            lp[s] = log(0.1) + binomial_lpmf(Y | N[s], theta);
    }
    
    model {
        target += log_sum_exp(lp);      
        theta ~ beta(7,2);
    }
   
    generated quantities {
        simplex[10] pState;
        pState = exp(lp - log_sum_exp(lp));
    }

"""
sm = StanModel(model_code=model_code)
fit = sm.sampling(
    data=dict(nStudy=nStudy,Y=Y),
    warmup=500,
    iter=1000,
    chains=4,
    seed=1
)

PYTHON ERROR TRACE:


---------------------------------------------------------------------------
RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/home/jaf/.conda/envs/pystan-env/lib/python3.8/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/home/jaf/.conda/envs/pystan-env/lib/python3.8/multiprocessing/pool.py", line 48, in mapstar
    return list(map(*args))
  File "stanfit4anon_model_39368d80adc693f1938ef1ee0051a427_1056900776692445796.pyx", line 373, in stanfit4anon_model_39368d80adc693f1938ef1ee0051a427_1056900776692445796._call_sampler_star
  File "stanfit4anon_model_39368d80adc693f1938ef1ee0051a427_1056900776692445796.pyx", line 406, in stanfit4anon_model_39368d80adc693f1938ef1ee0051a427_1056900776692445796._call_sampler
RuntimeError: Initialization failed.
"""

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

RuntimeError                              Traceback (most recent call last)
<ipython-input-93-c99474c6b026> in <module>
----> 1 fit = sm.sampling(
      2     data=dict(nStudy=nStudy,Y=Y),
      3     warmup=500,
      4     iter=1000,
      5     chains=4,

~/.conda/envs/pystan-env/lib/python3.8/site-packages/pystan/model.py in sampling(self, data, pars, chains, iter, warmup, thin, seed, init, sample_file, diagnostic_file, verbose, algorithm, control, n_jobs, **kwargs)
    811         call_sampler_args = izip(itertools.repeat(data), args_list, itertools.repeat(pars))
    812         call_sampler_star = self.module._call_sampler_star
--> 813         ret_and_samples = _map_parallel(call_sampler_star, call_sampler_args, n_jobs)
    814         samples = [smpl for _, smpl in ret_and_samples]
    815 

~/.conda/envs/pystan-env/lib/python3.8/site-packages/pystan/model.py in _map_parallel(function, args, n_jobs)
     83         try:
     84             pool = multiprocessing.Pool(processes=n_jobs)
---> 85             map_result = pool.map(function, args)
     86         finally:
     87             pool.close()

~/.conda/envs/pystan-env/lib/python3.8/multiprocessing/pool.py in map(self, func, iterable, chunksize)
    362         in a list that is returned.
    363         '''
--> 364         return self._map_async(func, iterable, mapstar, chunksize).get()
    365 
    366     def starmap(self, func, iterable, chunksize=None):

~/.conda/envs/pystan-env/lib/python3.8/multiprocessing/pool.py in get(self, timeout)
    769             return self._value
    770         else:
--> 771             raise self._value
    772 
    773     def _set(self, i, obj):

RuntimeError: Initialization failed.

STAN ERROR from TERMINAL (this is the end of the terminal output but it pretty much repeats this A LOT):


...
...
...

Rejecting initial value:
Initialization between (-2, 2) failed after 100 attempts.
Initialization between (-2, 2) failed after 100 attempts.
 Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
Rejecting initial value:
  Error evaluating the log probability at the initial value.
 Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
  Error evaluating the log probability at the initial value.
Exception: binomial_lpmf: Successes variable[4] is 19, but must be in the interval [0, 11]  (in 'unknown file name' at line 20)
Exception: binomial_lpmf: Successes variable[4] is 19, but must be in the interval [0, 11]  (in 'unknown file name' at line 20)



Initialization between (-2, 2) failed after 100 attempts.
 Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: binomial_lpmf: Successes variable[4] is 19, but must be in the interval [0, 11]  (in 'unknown file name' at line 20)
***

The problem is that the int array N, encoding the candidate number of times the coin was flipped in all experiments, starts with a value of 11 but some of the experiments had more than 11 heads observed, which isn’t possible. Are these numbers direct copies of what’s in the book? Maybe the 19s were actually 9s?

2 Likes

Hi, thanks for your response and sorry about the late reply!!! You’re absolutely right!!! I had copied them from the book incorrectly facepalm - it serves me right for trying to learn Stan with little sleep. It all works as it should have now.

2 Likes