HI all,
I’ll preface by saying I’m pretty new to MCMC and brand new to Stan.
I’m working on a multicompartment pharmacokinetic model that need to take as input 1) arbitrary, sparse drug doses over time and fit them to 2) arbitrary, sparse drug measurements. It needs to do this simultaneously for (ideally) up to thousands of patients, hundreds of drug administrations, and maybe a dozen serum level measurements. Usually the events span something like 5-50 days.
The kinetics are linear, so it could be represented as an ODE, but my current (working) implementation treats it as discrete, fitting a rate matrix and multiplying to get the concentration at every minute.
The full model is attached (example data here), but the slow step is (no surprise) this:
// an array of rate matrices (one per patient)
array[N_pts] matrix[3, 3] K;
// 2D array (patients x time) of concentrations in each compartment
array[N_pts, T] row_vector[3] y_pred;
// in-coming concentrations to each compartment at each time
array[N_pts, T] row_vector[3] C_in;
for (p in 1:N_pts){
for (t in 2:T) {
y_pred[p, t] = (y_pred[p, t-1] * K[p]) + C_in[p, t-1];
}
}
Some options I thought of:
- trying to vectorize this computation across patients (is there some specialized function for this?)
- skipping times there aren’t doses or measurements with matrix exponentiation
- some other way of inlining/avoiding the copying that happens in the inner loop (looking at the C++, theres a bunch of
deepcopy
statements which seems like it’d be pretty slow. - actually modeling it as an ODE. This makes me nervous as 1) ODE solvers seem complicated/slow 2) I never took diff eq in college and therefore have a complex.
I’m hopeful there some obvious/commonly accepted answer here; guidance very appreciated!