pystan.StanModel RuntimeError:尝试拟合时初始化失败

时间:2019-10-18 16:17:26

标签: stan pystan

我试图拟合一个Lotka-volterra stan模型,定义为:

functions{
    real[] dz_dt(real t, real[] z, real[] theta,real[] x_r, int[] x_i){

        real u = z[1];
        real v = z[2];

        real alpha = theta[1];
        real beta = theta[2];
        real gamma = theta[3];
        real delta = theta[4];

        real du_dt = (alpha - beta * v) * u;
        real dv_dt = (-gamma + delta * u) * v;
        return { du_dt, dv_dt };
    }
}

data {
    int<lower = 0> N;           // num measurements
    real ts[N];                 // measurement times > 0
    real y_init[2];             // initial measured population
    real<lower = 0> y[N, 2];    // measured population at measurement times
}

parameters {
    real<lower = 0> theta[4];   // theta = { alpha, beta, gamma, delta }
    real<lower = 0> z_init[2];  // initial population
    real<lower = 0> sigma[2];   // error scale
}

transformed parameters {
    real z[N, 2] = integrate_ode_rk45(dz_dt, z_init, 0, ts, theta,rep_array(0.0, 0), rep_array(0, 0),1e-6, 1e-5, 1e3);
}

model {
    theta[{1, 3}] ~ normal(1, 0.5);
    theta[{2, 4}] ~ normal(0.05, 0.05);
    sigma ~ lognormal(-1, 1);
    z_init ~ lognormal(log(10), 1);
    for (k in 1:2) {
        y_init[k] ~ lognormal(log(z_init[k]), sigma[k]);
        y[ , k] ~ lognormal(log(z[, k]), sigma[k]);
    }
}

我用于拟合模型的数据是numpy数组(如果它们是矢量或矩阵):

datos={"N":N,"ts":ts,"y_init":y[0,:],"y":y}
# where N=100
# ts.shape=(100,)
# y[0,:].shape=(2,)
# y.shape=(100,2)

代码正确编译:

sm = pystan.StanModel(file='lotka-volterra.stan')

但是当我尝试拟合模型时

fit=sm.sampling(data=datos)

返回大错误:

---------------------------------------------------------------------------
RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
    return list(map(*args))
  File "stanfit4anon_model_97aa305000da3f2b4cb00e52fda1eeba_134896954346517730.pyx", line 371, in stanfit4anon_model_97aa305000da3f2b4cb00e52fda1eeba_134896954346517730._call_sampler_star
  File "stanfit4anon_model_97aa305000da3f2b4cb00e52fda1eeba_134896954346517730.pyx", line 404, in stanfit4anon_model_97aa305000da3f2b4cb00e52fda1eeba_134896954346517730._call_sampler
RuntimeError: Initialization failed.
"""

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

RuntimeError                              Traceback (most recent call last)
<ipython-input-14-93e36dfa9d3e> in <module>
----> 1 fit=sm.sampling(data=datos)

/usr/local/lib/python3.6/dist-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 

/usr/local/lib/python3.6/dist-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()

/usr/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/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.
​
0
1
Python 3 | Idle
infiriendo_con_stan.ipynb
Ln 1, Col 28

0 个答案:

没有答案