# pystan.StanModel RuntimeError: Initialization failed when trying to fit

**URL:** https://discourse.mc-stan.org/t/pystan-stanmodel-runtimeerror-initialization-failed-when-trying-to-fit/11780
**Category:** Modeling
**Created:** [November 6, 2019, 2:08pm UTC](https://discourse.mc-stan.org/t/pystan-stanmodel-runtimeerror-initialization-failed-when-trying-to-fit/11780 "2019-11-06T14:08:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![brewedhumor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/brewedhumor/32/8102_2.png) [@brewedhumor](https://discourse.mc-stan.org/u/brewedhumor)
#### Post date: [November 6, 2019, 2:08pm UTC](https://discourse.mc-stan.org/t/pystan-stanmodel-runtimeerror-initialization-failed-when-trying-to-fit/11780/1 "2019-11-06T14:08:03Z")

</div>

For data part you can assume any random data which satisfies dimensions constraints  
All i want to do for now is model should sample properly, which it isn’t.

(PS: Code is shown written below)

It is showing error :

RemoteTraceback Traceback (most recent call last)  
RemoteTraceback:  
“”"  
Traceback (most recent call last):  
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py”, line 121, in worker  
result = (True, func(\*args, \*\*kwds))  
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py”, line 44, in mapstar  
return list(map(\*args))  
File “stanfit4dummy\_model\_84553e8202127efa9b9f2f6c6961ebe6\_8664948722932070801.pyx”, line 371, in stanfit4dummy\_model\_84553e8202127efa9b9f2f6c6961ebe6\_8664948722932070801.\_call\_sampler\_star  
File “stanfit4dummy\_model\_84553e8202127efa9b9f2f6c6961ebe6\_8664948722932070801.pyx”, line 404, in stanfit4dummy\_model\_84553e8202127efa9b9f2f6c6961ebe6\_8664948722932070801.\_call\_sampler  
RuntimeError: Initialization failed.  
“”"

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

RuntimeError Traceback (most recent call last)  
 in   
8 ‘demand’:demand\_data[1]  
9 }  
—\> 10 model\_fit = model.sampling(data=data\_dict, iter=1000, chains=4, warmup=750, n\_jobs=-1, seed=42)

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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)  
776 call\_sampler\_args = izip(itertools.repeat(data), args\_list, itertools.repeat(pars))  
777 call\_sampler\_star = self.module.\_call\_sampler\_star  
–\> 778 ret\_and\_samples = \_map\_parallel(call\_sampler\_star, call\_sampler\_args, n\_jobs)  
779 samples = [smpl for \_, smpl in ret\_and\_samples]  
780

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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()

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py in map(self, func, iterable, chunksize)  
288 in a list that is returned.  
289 ‘’’  
–\> 290 return self.\_map\_async(func, iterable, mapstar, chunksize).get()  
291  
292 def starmap(self, func, iterable, chunksize=None):

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py in get(self, timeout)  
681 return self.\_value  
682 else:  
–\> 683 raise self.\_value  
684  
685 def \_set(self, i, obj):

RuntimeError: Initialization failed.

```
data {
int<lower=1> N; // N = 288
row_vector[N] cdd; // vector of 288 values
row_vector[N] gdp;
row_vector[N] prices;
row_vector[N] demand_1m_lagged;
row_vector[N] demand_1yr_lagged;
row_vector[N] demand; // true value which i want to model
}
parameters {
real alpha;

vector[5] b;
vector[5] mu_beta;
cov_matrix[5] sigma_beta;

vector[12] e;
cov_matrix[12] sigma_e;

}

transformed parameters {
    row_vector[N] demand_hat; //transformed model parameter
    row_vector[N] temp;
    for(i in 1:N) {
        temp[i] = cdd[i] * b[1] + gdp[i] * b[2] + prices[i] * b[3] + demand_1m_lagged[i] * b[4] + demand_1yr_lagged[i] * b[5];
        demand_hat[i] <- alpha + temp[i] + e[i%12+1];
    }
}
model {
    alpha ~ normal(0, 100);
    for (j in 1:5)
      mu_beta[j] ~ normal(0, 100);
    
    sigma_beta ~ inv_wishart(6, diag_matrix(rep_vector(0.0001,5)));
    
    sigma_e ~ inv_wishart(13, diag_matrix(rep_vector(0.0001,12)));
    e ~ multi_normal(rep_vector(0.0, 12), sigma_e);
    
    b ~ multi_normal(mu_beta, sigma_beta);
    
    demand ~ normal(demand_hat, 0); //how to incorporate error term, like sigma here already added up
}

model = pystan.model.StanModel(model_code=model_code, model_name='dummy_model')
data_dict = {
    'N':288,
    'cdd':cdd_data[1],
    'gdp':gdp_data[1],
    'prices':prices_data[1],
    'demand_1m_lagged':demand_1m_lagged_data[1],
    'demand_1yr_lagged':demand_1yr_lagged_data[1],
    'demand':demand_data[1]
}
model_fit = model.sampling(data=data_dict, iter=1000, chains=4, warmup=750, n_jobs=-1, seed=42)

```

---

<div class="post-metadata">

### Author: ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)
#### Post date: [November 6, 2019, 3:42pm UTC](https://discourse.mc-stan.org/t/pystan-stanmodel-runtimeerror-initialization-failed-when-trying-to-fit/11780/2 "2019-11-06T15:42:17Z")

</div>

I think it fails because of the last line where it’s `normal(demand_hat, 0)`. The normal distribution needs a nonzero standard deviation.

Those noninformative priors for sigmas are going to be difficult to estimate. Can’t you just have independent gamma priors for `e` and `b`?

---

<div class="post-metadata">

### Author: ![brewedhumor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/brewedhumor/32/8102_2.png) [@brewedhumor](https://discourse.mc-stan.org/u/brewedhumor)
#### Post date: [November 6, 2019, 7:05pm UTC](https://discourse.mc-stan.org/t/pystan-stanmodel-runtimeerror-initialization-failed-when-trying-to-fit/11780/3 "2019-11-06T19:05:19Z")

</div>

thank you for your observation, code actually ran after i changed standard deviation to a non zero value.

Earlier I had made the standard deviation 0, because I already added the error term here:

```
demand_hat[i] <- alpha + temp[i] + e[i%12+1];

```

(in transformed parameters block).

---

<div class="post-metadata">

### Author: ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)
#### Post date: [November 6, 2019, 7:28pm UTC](https://discourse.mc-stan.org/t/pystan-stanmodel-runtimeerror-initialization-failed-when-trying-to-fit/11780/4 "2019-11-06T19:28:08Z")

</div>

`e` repeats the same value exactly every year. You have 24 years and only 5 `b`s. If you ignore all data except the first month of each year, `e` would be constant (and therefore indistinguishable from `alpha`) and a model without additional error terms is overdetermined.

---

<div class="post-metadata">

### Author: ![brewedhumor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/brewedhumor/32/8102_2.png) [@brewedhumor](https://discourse.mc-stan.org/u/brewedhumor)
#### Post date: [November 6, 2019, 8:37pm UTC](https://discourse.mc-stan.org/t/pystan-stanmodel-runtimeerror-initialization-failed-when-trying-to-fit/11780/5 "2019-11-06T20:37:58Z")

</div>

ohh! i get it now, i was trying to implement this paper, each of the variables described these distribution, i got the idea after going through the paper. i missed this subtle point, i am glad you pointed it out.
