Hello,

I am trying to use PyStan to perform parameter estimation on an SIR model. When I try to sample, I get

Runtime Error: Exception: Max number of iterations exceeded (1000)

I think the program is taking issue with my beta priors. Apparently the ‘random variable’ is not between 1 and 0. But I don’t understand what random variable it is talking about. Where is it getting these numbers from? And why are they not between 1 and 0? Any advice would be greatly appreciated.

Here is my code:

```
import pystan
import numpy as np
model = """
functions{
real[] dz_dt(real t, real[] z, real[] theta,
real[] x_r, int[] x_i){
real N = 1000;
real s = z[1];
real i = z[2];
real r = z[3];
real alpha = theta[1];
real beta = theta[2];
real ds_dt = -alpha * s * i * N;
real di_dt = alpha * s * i * N- beta * i;
real dr_dt = beta * i;
return {ds_dt, di_dt, dr_dt};
}
}
data{
int<lower = 0> M; //Number of measurements
real ts[M]; //Measurement times > 0
real y_init[3]; //Initial measured population proportions
real<lower = 0> y[M, 3]; //Measured population proportion at measurement times
}
parameters{
real<lower = 0> theta[2]; //theta = {alpha, beta}
real<lower = 0> z_init[3]; //True initial population proportion
real<lower = 0> sigma[3]; //error scale
}
transformed parameters{
real z[M, 3]
= integrate_ode_rk45(dz_dt, z_init, 0.0, ts, theta,
rep_array(0.0,0), rep_array(0,0),
1e-6, 1e-5, 1e3);
}
model{
theta[{1,2}] ~ normal(0.005, 1);
sigma ~ normal(0.01, 0.5);
z_init[1] ~ beta(5, 1.2);
z_init[2] ~ beta(1.5, 3);
z_init[3] ~ beta(1.5, 5);
for (k in 1:3) {
y_init[k] ~ normal(z_init[k], sigma[k]);
y[ ,k] ~ normal(z[, k], sigma[k]);
}
}
"""
sm = pystan.StanModel(model_code = model)
#######Data Creation##############
num_meas = 100 #Number of measurements
M = num_meas - 1 #Number of measurements minus initial condition
t = np.arange(1,M+1)
alpha_true = 0.001
beta_true = 0.09
initial_inf = 1
N = 1000
y0 = [(N - initial_inf)/N ,initial_inf/N,0]
s,i,r= np.zeros(num_meas), np.zeros(num_meas), np.zeros(num_meas)
s_noise = np.zeros(num_meas)
i_noise = np.zeros(num_meas)
r_noise = np.zeros(num_meas)
s[0], i[0], r[0] = y0
for x in range(M):
s[x+1] = s[x] - alpha_true * s[x] * i[x] * N
i[x+1] = i[x] + alpha_true * s[x] * i[x] * N - beta_true * i[x]
r[x+1] = r[x] + beta_true * i[x]
for x in range(M+1):
s_noise[x] = min(max(0.0, s[x]+np.random.normal(0,0.01)),1)
i_noise[x] = max(0.0, i[x]+np.random.normal(0,0.02))
if s_noise[x] + i_noise[x] > 1:
i_noise[x] = 1 - s_noise[x]
r_noise[x] = 1 - s_noise[x] - i_noise[x]
data = np.array([s_noise[1:num_meas],i_noise[1:num_meas],r_noise[1:num_meas]])
data_tr = data.transpose()
stan_data = {'M': M, 'ts':t, 'y_init': y0, 'y':data_tr}
######Run MCMC###########
fit = sm.sampling(data = stan_data, chains = 4, iter = 1000, n_jobs=1)
```

And here is my Error Traceback:

If this warning occurs sporadically, such as for highly constrained variable typ

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.00135, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)If this warning occurs sporadically, such as for highly constrained variable typ

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.02215, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)If this warning occurs sporadically, such as for highly constrained variable typ

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.03049, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.01304, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.0421, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.0984, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.11171, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: Max number of iterations exceeded (1000). (in ‘unknown file name’ at

line 35)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.01083, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.10168, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.00218, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.01742, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.0002, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.08993, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: Max number of iterations exceeded (1000). (in ‘unknown file name’ at

line 35)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 3.02119, but must be less than or equal

to 1 (in ‘unknown file name’ at line 45)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.01776, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.00703, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.00346, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: Max number of iterations exceeded (1000). (in ‘unknown file name’ at

line 35)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Iteration: 300 / 1000 [ 30%] (Warmup)

Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.00189, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.00208, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.00331, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Informational Message: The current Metropolis proposal is about to be rejected b

ecause of the following issue:

Exception: beta_lpdf: Random variable is 1.00026, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)

es like covariance matrices, then the sampler is fine,

but if this warning occurs often then your model may be either severely ill-cond

itioned or misspecified.Iteration: 400 / 1000 [ 40%] (Warmup)

Iteration: 500 / 1000 [ 50%] (Warmup)

Iteration: 501 / 1000 [ 50%] (Sampling)

Iteration: 600 / 1000 [ 60%] (Sampling)

Iteration: 700 / 1000 [ 70%] (Sampling)

Iteration: 800 / 1000 [ 80%] (Sampling)

Iteration: 900 / 1000 [ 90%] (Sampling)

Iteration: 1000 / 1000 [100%] (Sampling)Elapsed Time: 580.341 seconds (Warm-up)

103.124 seconds (Sampling)

683.465 seconds (Total)Rejecting initial value:

Error evaluating the log probability at the initial value.

Exception: beta_lpdf: Random variable is 3.19315, but must be less than or equal

to 1 (in ‘unknown file name’ at line 44)Rejecting initial value:

Error evaluating the log probability at the initial value.

Exception: beta_lpdf: Random variable is 4.03353, but must be less than or equal

to 1 (in ‘unknown file name’ at line 45)Rejecting initial value:

Error evaluating the log probability at the initial value.

Exception: beta_lpdf: Random variable is 5.94499, but must be less than or equal

to 1 (in ‘unknown file name’ at line 46)Unrecoverable error evaluating the log probability at the initial value.

Exception: Max number of iterations exceeded (1000). (in ‘unknown file name’ at

line 35)Traceback (most recent call last):

File “C:\Users\dms228\SIR_norm_txt.py”, line 101, in

fit = sm.sampling(data = stan_data, chains = 4, iter = 1000, n_jobs=1)

File “C:\Users\dms228\AppData\Local\Continuum\anaconda3\envs\stan_env\lib\site

-packages\pystan\model.py”, line 955, in sampling

ret_and_samples = _map_parallel(call_sampler_star, call_sampler_args, n_jobs

)

File “C:\Users\dms228\AppData\Local\Continuum\anaconda3\envs\stan_env\lib\site

-packages\pystan\model.py”, line 151, in _map_parallel

map_result = list(map(function, args))

File “stanfit4anon_model_3d2c4c1db96e4b77eec6e73ad0ea2184_587230830382530664.p

yx”, line 373, in stanfit4anon_model_3d2c4c1db96e4b77eec6e73ad0ea2184_5872308303

82530664._call_sampler_star

File “stanfit4anon_model_3d2c4c1db96e4b77eec6e73ad0ea2184_587230830382530664.p

yx”, line 406, in stanfit4anon_model_3d2c4c1db96e4b77eec6e73ad0ea2184_5872308303

82530664._call_sampler

RuntimeError: Exception: Max number of iterations exceeded (1000). (in ‘unknown

file name’ at line 35)