現在、ベイズ推論とMCMCのフリーソフトという書籍のPyMCのチュートリアル的な章を学んでいるのですが、  
以下のコードの`ts=[pm.sample(itenum, step, chains=i,progressbar=False) for i in
range(chainnum)]`という部分で、`IndexError: list index out of range`というエラーが出てしまいます。  
このエラーを回避して正常に動作させるのは、該当箇所をどのように修正すれば良いでしょうか。

環境は、google colabで、python3.6です。

```

    import pymc3 as pm
    import time
    from pymc3.backends.base import merge_traces
    import numpy as np
    
    N=40
    X=np.random.uniform(10,size=N)
    Y=X*30+4+np.random.normal(0,16,size=N)
    
    multicore=False
    saveimage=False
    
    itenum=1000
    t0=time.clock()
    chainnum=3
    
    with pm.Model() as model:
      alpha = pm.Normal('alpha', mu=0, sd=20)
      beta = pm.Normal('beta', mu=0, sd=20)
      sigma = pm.Uniform('sigma', lower=0)
      y = pm.Normal('y', mu=beta*X + alpha, sd=sigma, observed=Y)
      start = pm.find_MAP()
      step = pm.NUTS(scaling=start)
    
    with model:
      if(multicore):
        trace = pm.sample(itenum, step, start=start, njobs=chainnum, random_seed=range(chainnum), progress_bar=False)
      else:
        ts=[pm.sample(itenum, step, chains=i,progressbar=False) for i in range(chainnum)]
        trace=merge_traces(ts)
      if(saveimage):
        pm.traceplot(trace).savefig("simple_linear_trace.png")
      print("Rhat="+str(pm.gelman_rubin(trace)))
    
    t1=time.clock()
    print("elapsed time="+str(t1-t0))
    
```

具体的なエラー内容は以下です。

```

    <ipython-input-8-b9c1eab04925> in <listcomp>(.0)
         22     trace = pm.sample(itenum, step, start=start, njobs=chainnum, random_seed=list(range(chainnum)), progress_bar=False)
         23   else:
    ---> 24     ts=[pm.sample(itenum, step, chains=i,progressbar=False) for i in list(range(chainnum))]
         25     trace=merge_traces(ts)
         26   if(saveimage):
    
    /usr/local/lib/python3.6/dist-packages/pymc3/sampling.py in sample(draws, step, init, n_init, start, trace, chain_idx, chains, cores, tune, progressbar, model, random_seed, discard_tuned_samples, compute_convergence_checks, **kwargs)
        458 
        459         discard = tune if discard_tuned_samples else 0
    --> 460         trace = trace[discard:]
        461 
        462         if compute_convergence_checks:
    
    /usr/local/lib/python3.6/dist-packages/pymc3/backends/base.py in __getitem__(self, idx)
        290     def __getitem__(self, idx):
        291         if isinstance(idx, slice):
    --> 292             return self._slice(idx)
        293 
        294         try:
    
    /usr/local/lib/python3.6/dist-packages/pymc3/backends/base.py in _slice(self, slice)
        496         new_traces = [trace._slice(slice) for trace in self._straces.values()]
        497         trace = MultiTrace(new_traces)
    --> 498         idxs = slice.indices(len(self))
        499         trace._report = self._report._slice(*idxs)
        500         return trace
    
    /usr/local/lib/python3.6/dist-packages/pymc3/backends/base.py in __len__(self)
        341 
        342     def __len__(self):
    --> 343         chain = self.chains[-1]
        344         return len(self._straces[chain])
        345 
    
    IndexError: list index out of range
    
```

よろしくお願いします。
CC BY-SA 4.0
2020-10-03T06:59:35.290
70895
2020-10-03T06:59:35.290
37125
question
python python3
PyMCのsample関数でlist index out of rangeエラーが発生した時の修正方法について