RuntimeError: Initialization failed in PyStan


#1

I am trying to fit a Binomial Markov Switching Multifractal time series mode in PyStan.

 data {
  int<lower=1> N_STOCKS; // Number of stocks
  int<lower=2> T; // Number of time-steps
  int<lower=2> K; // Number of states
  matrix[N_STOCKS, T] x; // Historical stock returns at (t)
}


parameters {
  
  vector<lower=0, upper=1>[N_STOCKS] gamma_1; //transition probility at k=1
  vector<lower=1>[N_STOCKS] b; //exponatial factor for computing gamma_k
  vector<lower=0>[N_STOCKS] sigma_scale; //scale factor
  vector<lower=0, upper=2>[N_STOCKS] m_0; //Binomial distribution with probability p=1/2
  vector<lower=0>[N_STOCKS] sigma; //std for noise
  }

transformed parameters {
//compute state switching probability gamma_k
  matrix[N_STOCKS, K] gamma_k;
  gamma_k[:, 1] = gamma_1;
  for (k in 2:K)
    for (i in 1:N_STOCKS)
      gamma_k[i, k] = 1-(1-gamma_1[i])^(b[i]^k - 1);
}


model {

  matrix[N_STOCKS,K] M_kt[T]; //matrix of state transition
  real product;
  real err;
  int temp1;
  int temp2;
  
  gamma_1 ~ uniform(0,1);
  b ~ cauchy(0,5);
  sigma_scale ~ cauchy(0,5);
  m_0 ~ uniform(0,2);
  sigma ~ cauchy(0,5);
  
 
  //initialize M_kt for t=1
  for (k in 1:K)
    for (i in 1:N_STOCKS){
      // print(temp1)
      temp1 ~ bernoulli(0.5);
      M_kt[1, i, k] = (2-m_0[i]) + (2*m_0[i] -2)*temp1;
    }

  for (t in 2:T){
    for (i in 1:N_STOCKS){
      product = 1;
      for (k in 1:K){
        temp2 ~ bernoulli(gamma_k[i, k]);
        if (temp2 == 1){
          temp1 ~ bernoulli(0.5);
          M_kt[t, i, k] = (2-m_0[i]) + (2*m_0[i] -2)*temp1; //p = 0.5 for M_kt[t, i, k]=m_0 and p=0.5 for M_kt[t, i, k]=2-m_0
        }
        else{
          M_kt[t, i, k] = M_kt[t-1, i, k];
        }
        product = product*M_kt[t,i,k];
      }
      err = x[i,t]/(sqrt(product)*sigma_scale[i]+2);
      err ~ student_t(1,0,sigma[i]);
    }
}




}

Tried to fit model in Python by:

K=2
fit = sm.sampling(data={‘x’: sample.values, ‘N_STOCKS’: sample.shape[0], ‘T’: sample.shape[1], ‘K’:K},
iter=1000, chains=4, seed=7)]

and I got the run time error:

RemoteTraceback Traceback (most recent call last)
RemoteTraceback:
“”"
Traceback (most recent call last):
File “/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/pool.py”, line 119, in worker
result = (True, func(*args, **kwds))
File “/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/pool.py”, line 44, in mapstar
return list(map(*args))
File “stanfit4anon_model_795a01f84d6fc46ed2373187697a557b_2004836558379422606.pyx”, line 368, in stanfit4anon_model_795a01f84d6fc46ed2373187697a557b_2004836558379422606._call_sampler_star
File “stanfit4anon_model_795a01f84d6fc46ed2373187697a557b_2004836558379422606.pyx”, line 401, in stanfit4anon_model_795a01f84d6fc46ed2373187697a557b_2004836558379422606._call_sampler
RuntimeError: Initialization failed.
“”"

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

RuntimeError Traceback (most recent call last)
in ()
1 fit = sm.sampling(data={‘x’: sample.values, ‘N_STOCKS’: sample.shape[0], ‘T’: sample.shape[1], ‘K’:K},
2 iter=1000, chains=4, seed=7,
----> 3 verbose=True)

~/.virtualenvs/olympus/lib/python3.6/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)
724 call_sampler_args = izip(itertools.repeat(data), args_list, itertools.repeat(pars))
725 call_sampler_star = self.module._call_sampler_star
–> 726 ret_and_samples = _map_parallel(call_sampler_star, call_sampler_args, n_jobs)
727 samples = [smpl for _, smpl in ret_and_samples]
728

~/.virtualenvs/olympus/lib/python3.6/site-packages/pystan/model.py in _map_parallel(function, args, n_jobs)
79 try:
80 pool = multiprocessing.Pool(processes=n_jobs)
—> 81 map_result = pool.map(function, args)
82 finally:
83 pool.close()

/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/pool.py in map(self, func, iterable, chunksize)
264 in a list that is returned.
265 ‘’’
–> 266 return self._map_async(func, iterable, mapstar, chunksize).get()
267
268 def starmap(self, func, iterable, chunksize=None):

/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/pool.py in get(self, timeout)
642 return self._value
643 else:
–> 644 raise self._value
645
646 def _set(self, i, obj):

RuntimeError: Initialization failed.
]

My data is like this:
sample.csv (6.5 KB)

I’m new to STAN. I’ve checked several resources and I still don’t know what’s wrong here. Any help would be appreciated!!


#2

Sorry this didn’t get answered. I don’t use Python much myself, but that’s not a very friendly error message.

Initialization failing means it tried random initialization and either got an infinite or not-a-number result for the target log density. That’s usually because constraints are violated (e.g., negative scale parameters) or matrix arithmetic with non-conforming sizes or indexes out of bounds. That’s why you need the error message.

Some of those messages were getting lost before 2.17, so if you don’t hvae the latest, you might want to try that. There’s also sometimes issues with parallelism in RStan, so maybe running a single chain will make the error message accessible.


#3

You should also be able to insert print() statements to debug. You want to figure out where the target goes wonky.


#4

Thank you for your reply! I’m moving my code to run since the diagnostic is better.