Hi All,

I am trying to perform parameter estimation on an SIB (susceptible, infected, bacteria) model based on this paper:

The model runs OK, and produces good estimations with healthy looking diagnostics, however it takes longer than I feel it should. When I run the model in the Anaconda Prompt, I get the same warning repeatedly:

‘Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:

Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in ‘unknown file name’ at line 49)’

I am relatively new to STAN, and I’m not really sure what this means. Given that it produces decent results should I just ignore the warnings? Or is that dangerous? I am planning to scale up this model quite a lot so would like it be running as smooth as possible in its simplified state. Does anybody have any advice?

```
import pystan
import numpy as np
import time
import matplotlib.pyplot as plt
import pandas as pd
from scipy.integrate import odeint
######################### STAN Model ########################################
model = """
functions{
real f(real B) {
real force;
force = B/(B+1000000);
return force;
}
real[] dz_dt(real t, //time
real[] z, // state
real[] theta, // parameters
real[] x_r, // data (real)
int[] x_i) { // data (integer)
real S = z[1]; //Unpack state variables
real I = z[2];
real B = z[3];
real N = 10000; //Known parameters
real mu = 0.0001;
real net_b = -0.33;
real r = 0.2;
real a = theta[1]; //Unknown parameters
real e = theta[2];
real dS_dt = mu * (N - S) - a * f(B) * S;
real dI_dt = a * f(B) * S - r * I;
real dB_dt = B * net_b + e * I;
return {dS_dt, dI_dt, dB_dt};
}
}
data{
int<lower=0> M; //Number of measurements
real<lower = 0> ts[M]; //Measurement times > 0
real<lower = 0> y_init[3]; //Measured initial conditions
real<lower = 0> z_init[3]; //True initial conditions
real<lower = 0> y[M,3]; //Data
}
parameters{
real<lower=0> theta[2]; //theta = [a,e]
real<lower=0> sigma; //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{
//Priors
theta[1] ~ gamma(3,0.6); //a
theta[2] ~ gamma(10,1); //e
for (k in 1:3) {
y_init ~ normal(z_init[k], sigma);
y[,k] ~ normal(z[,k], sigma);
}
}
"""
sm = pystan.StanModel(model_code = model)
######################## Simulated data ##############################
N = 10000
I0, B0 = 10,0
S0 = N - I0
a, e = 1, 10
num_days = 500
M = num_days - 1
t = np.arange(0,num_days)
ts = np.arange(1,num_days)
def f(B):
f = B /(B+ (10**6))
return f
#SIR Model Differential Equations
def deriv(y ,t, N , a, e):
"""Derivative for SIB model """
net_b = -0.33
mu = 0.0001
r = 0.2
S, I, B = y
dSdt = mu * (N - S) - a * f(B) * S
dIdt = a * f(B) * S - r * I
dBdt = B * net_b + e * I
return dSdt, dIdt, dBdt
y0 = S0, I0, B0
y0_ls = [S0, I0, B0]
sim = odeint(deriv, y0, t, args=(N, a, e))
S,I,B = sim.T
def add_noise(sim, sigma):
"Adds random noise to deterministic simulation"
dims = sim.shape
rows = dims[0]
cols = dims[1]
sim_noise = np.zeros((rows, cols))
for x in np.arange(rows):
for y in np.arange(cols):
sim_noise[x,y] = max(0,sim[x,y] + np.random.normal(0,sigma))
return sim_noise
noisy_sim = add_noise(sim, 50)
S_noise, I_noise, B_noise = noisy_sim.T
data_smooth = sim
data_noise = noisy_sim[1:len(noisy_sim)]
stan_data = {'M': M, 'ts':ts, 'y_init': y0_ls, 'z_init': y0_ls, 'y':data_noise}
################################## Run MCMC #######################################
fit = sm.sampling(data = stan_data, chains = 4, iter = 2000, n_jobs=1)
# ##################################### Post Analysis ##################################
summary_dict = fit.summary()
df = pd.DataFrame(summary_dict['summary'],
columns=summary_dict['summary_colnames'],
index=summary_dict['summary_rownames'])
a_mean, e_mean,sigma_mean = df['mean']['theta[1]'], df['mean']['theta[2]'],df['mean']['sigma']
print("a: Mean =", round(a_mean,2), ' N_eff=', round(df['n_eff']['theta[1]'],1),
' Rhat=',round(df['Rhat']['theta[1]'],4))
print("e: Mean =", round(e_mean,2), ' N_eff=', round(df['n_eff']['theta[2]'],1),
' Rhat=',round(df['Rhat']['theta[2]'],4))
print("Sigma: Mean =", round(sigma_mean,2), ' N_eff=', round(df['n_eff']['sigma'],1),
' Rhat=',round(df['Rhat']['sigma'],4))
stan_theta = [a_mean, e_mean]
stan_sim = odeint(deriv, y0, t, args = (N, a_mean, e_mean))
stan_S = pd.concat([pd.Series(y0[0]),df['mean']['z[1,1]':'z[499,1]']])
stan_I = pd.concat([pd.Series(y0[1]),df['mean']['z[1,2]':'z[499,2]']])
stan_B = pd.concat([pd.Series(y0[2]),df['mean']['z[1,3]':'z[499,3]']])
#Extract trace
a_trace = fit['theta[1]']
e_trace = fit['theta[2]']
sigma_trace = fit['sigma']
lp_trace = fit['lp__']
fig, (ax1, ax2, ax3) = plt.subplots(3,1, dpi = 300)
fig.suptitle('Trace plots', fontsize = 10)
fig.tight_layout(pad = 2)
ax1.plot(a_trace, linewidth = 0.5)
ax1.set_title('a', fontsize = 10)
ax2.plot(e_trace, linewidth = 0.5)
ax2.set_title('e', fontsize = 10)
ax3.plot(sigma_trace, linewidth = 0.5)
ax3.set_title('Sigma', fontsize = 10)
########################Visualise Results###################################
tplus = np.arange(0,num_days)
fig, ax1 = plt.subplots(1,1, dpi = 300)
#Plot Simulated Data
ax1.plot(tplus, S_noise, label='S simulated', color = 'r')
ax1.plot(tplus, I_noise, label='I simulated', c = 'g')
ax1.plot(tplus, B_noise, label = 'B simulated', c = 'b')
ax1.set_ylim(0,10000)
ax1.plot(tplus, stan_S, label = 'S STAN modelled', c = 'lightcoral')
ax1.plot(tplus, stan_I, label = 'I STAN modelled', c = 'lightgreen')
ax1.plot(tplus, stan_B, label = 'B STAN modelled', c = 'lightblue')
ax1.plot(tplus, stan_S, label = 'S STAN simulated', c = 'darkred', ls = '--')
ax1.plot(tplus, stan_I, label = 'I STAN simulated', c = 'darkgreen', ls = '--')
ax1.plot(tplus, stan_B, label = 'B STAN simulated', c = 'darkblue', ls = '--')
# # ax1.set_title('Simulated Data')
# ax1.set(xlabel = 'time', ylabel = 'Population')
ax1.legend()
```

And here’s a full traceback of a run:

(stan_env) C:>python C:\Users\dms228\codeco_simpler.py

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_362d950e48e6dbef7b45b26eebb499b4 NOW.

```
Gradient evaluation took 0.001 seconds
1000 transitions using 10 leapfrog steps per transition would take 10 seconds.
Adjust your expectations accordingly!
Iteration: 1 / 2000 [ 0%] (Warmup)
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: Max number of iterations exceeded (1000). (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Iteration: 200 / 2000 [ 10%] (Warmup)
Iteration: 400 / 2000 [ 20%] (Warmup)
Iteration: 600 / 2000 [ 30%] (Warmup)
Iteration: 800 / 2000 [ 40%] (Warmup)
Iteration: 1000 / 2000 [ 50%] (Warmup)
Iteration: 1001 / 2000 [ 50%] (Sampling)
Iteration: 1200 / 2000 [ 60%] (Sampling)
Iteration: 1400 / 2000 [ 70%] (Sampling)
Iteration: 1600 / 2000 [ 80%] (Sampling)
Iteration: 1800 / 2000 [ 90%] (Sampling)
Iteration: 2000 / 2000 [100%] (Sampling)
Elapsed Time: 57.262 seconds (Warm-up)
74.873 seconds (Sampling)
132.135 seconds (Total)
Gradient evaluation took 0.002 seconds
1000 transitions using 10 leapfrog steps per transition would take 20 seconds.
Adjust your expectations accordingly!
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: normal_lpdf: Location parameter[1] is nan, but must be finite! (in 'unknown file name' at line 61)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: Max number of iterations exceeded (1000). (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: Max number of iterations exceeded (1000). (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: Max number of iterations exceeded (1000). (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Iteration: 1 / 2000 [ 0%] (Warmup)
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: normal_lpdf: Location parameter[1] is nan, but must be finite! (in 'unknown file name' at line 61)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Iteration: 200 / 2000 [ 10%] (Warmup)
Iteration: 400 / 2000 [ 20%] (Warmup)
Iteration: 600 / 2000 [ 30%] (Warmup)
Iteration: 800 / 2000 [ 40%] (Warmup)
Iteration: 1000 / 2000 [ 50%] (Warmup)
Iteration: 1001 / 2000 [ 50%] (Sampling)
Iteration: 1200 / 2000 [ 60%] (Sampling)
Iteration: 1400 / 2000 [ 70%] (Sampling)
Iteration: 1600 / 2000 [ 80%] (Sampling)
Iteration: 1800 / 2000 [ 90%] (Sampling)
Iteration: 2000 / 2000 [100%] (Sampling)
Elapsed Time: 65.566 seconds (Warm-up)
56.692 seconds (Sampling)
122.258 seconds (Total)
Gradient evaluation took 0.001 seconds
1000 transitions using 10 leapfrog steps per transition would take 10 seconds.
Adjust your expectations accordingly!
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: normal_lpdf: Location parameter[1] is nan, but must be finite! (in 'unknown file name' at line 61)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: Max number of iterations exceeded (1000). (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: Max number of iterations exceeded (1000). (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Iteration: 1 / 2000 [ 0%] (Warmup)
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: integrate_ode_rk45: parameter vector[1] is inf, but must be finite! (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception: Max number of iterations exceeded (1000). (in 'unknown file name' at line 49)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Iteration: 200 / 2000 [ 10%] (Warmup)
Iteration: 400 / 2000 [ 20%] (Warmup)
Iteration: 600 / 2000 [ 30%] (Warmup)
Iteration: 800 / 2000 [ 40%] (Warmup)
Iteration: 1000 / 2000 [ 50%] (Warmup)
Iteration: 1001 / 2000 [ 50%] (Sampling)
Iteration: 1200 / 2000 [ 60%] (Sampling)
Iteration: 1400 / 2000 [ 70%] (Sampling)
Iteration: 1600 / 2000 [ 80%] (Sampling)
Iteration: 1800 / 2000 [ 90%] (Sampling)
Iteration: 2000 / 2000 [100%] (Sampling)
Elapsed Time: 58.073 seconds (Warm-up)
64.961 seconds (Sampling)
123.034 seconds (Total)
WARNING:pystan:Maximum (flat) parameter count (1000) exceeded: skipping diagnostic tests for n_eff and Rhat.
To run all diagnostics call pystan.check_hmc_diagnostics(fit)
a: Mean = 1.0 N_eff= 1274.3 Rhat= 1.0037
e: Mean = 9.99 N_eff= 1277.4 Rhat= 1.0036
Sigma: Mean = 517.25 N_eff= 1539.5 Rhat= 1.0014
```