Hi all,

This is my first time using Stan so forgive any dumb errors. I am trying to fit a hierarchical/random effects model consisting of a system of first order differential equation. One of the parameters (epsilon) in the ODE

comes from a gamma distribution and is unique for each individual. So (r,epsilon_i,K) for i = 1,…,J are ODE model parameters, alpha and beta are hyperparameters and sigma is the observance variance. The data is one observation for individual. Here’s my attempt in pystan:

obdata = {‘J’: ncats,‘ts’: daypool, ‘y1_data’ : countpool}

withinhostcode = “”"

functions {

real[] whmodel(real t, // time

real[] y, // state

real[] theta, // parameters

real[] x_r, // data (real)

int[] x_i) { // data (integer)

real dydt[2];

dydt[1] = theta[1] * y[1] * (1 - (y[1]/theta[2])) - theta[3] * y[1] * y[2];

dydt[2] = -theta[3] * y[1] * y[2];

return dydt;

}

}

data {

int<lower=1> J;

real ts[J,3];

real y1_data[J];

}

transformed data {

real y0[2];

real t0;

real x_r[0];

int x_i[0];

t0 = 0;

y0[1] = 10;

y0[2] = 100;

}

parameters {

real<lower=0> r;

real<lower=0> K;

real<lower=0> eps[J];

real<lower=0> alpha;

real<lower=0> beta;

real<lower=0> sigma;

}

transformed parameters{

real y[3,2];

real y_hat[J];

real theta[3];

for (i in 1:J)

{

theta[1] = r;

theta[2] = K;

theta[3] = eps[i];

```
y = integrate_ode_bdf(whmodel, y0, t0, ts[i], theta, x_r, x_i);
y_hat[i] = y[3,1]; //solution corresponding to observed data time
```

}

}

model{

eps ~ gamma(alpha, beta); //alpha, beta have uniform priors

r ~ gamma(100,0.00292);

K ~ normal(2000,2000);

sigma ~ normal(0,1000);

y1_data ~ normal(y_hat, sigma); //Gaussian error assumption

}

"""

However, when I run the model using

sm = StanModel(model_code = withinhostcode, verbose=True)

fit = sm.sampling(data = obdata, iter =100, chains = 1)

It successfully compiles but once it gets to fit, doesn’t really print anything or exit. It gets stuck even when I do a few iterations. Not quite sure if I’m doing anything wrong or it’s really that slow to sample from. Any suggestions would be highly appreciated!