Simulation
simulate()
Generate simulated observations from a model with random effects and residual error.
#![allow(unused)] fn main() { pub fn simulate( model: &CompiledModel, population: &Population, params: &ModelParameters, n_sim: usize, ) -> Vec<SimulationResult> }
Parameters:
model: Compiled modelpopulation: Template population (dose events and observation times are used; DV values are ignored)params: True parameter values for simulationn_sim: Number of simulation replicates
Returns: Vector of SimulationResult, one per observation per subject per replicate.
Example:
#![allow(unused)] fn main() { let model = parse_model_file(Path::new("model.ferx"))?; let population = read_nonmem_csv(Path::new("data.csv"), None)?; // Simulate 1000 replicates let sims = simulate(&model, &population, &model.default_params, 1000); for sim in &sims[..5] { println!("Sim {}, ID {}, TIME {}, IPRED {:.3}, DV {:.3}", sim.sim, sim.id, sim.time, sim.ipred, sim.dv_sim); } }
simulate_with_seed()
Same as simulate() but with a fixed random seed for reproducibility.
#![allow(unused)] fn main() { pub fn simulate_with_seed( model: &CompiledModel, population: &Population, params: &ModelParameters, n_sim: usize, seed: u64, ) -> Vec<SimulationResult> }
predict()
Population predictions without random effects (eta = 0). No simulation noise is added.
#![allow(unused)] fn main() { pub fn predict( model: &CompiledModel, population: &Population, params: &ModelParameters, ) -> Vec<PredictionResult> }
Returns: Vector of PredictionResult with population-level predictions.
Example:
#![allow(unused)] fn main() { let preds = predict(&model, &population, &model.default_params); for p in &preds { println!("ID {}, TIME {}, PRED {:.3}", p.id, p.time, p.pred); } }
Result Types
#![allow(unused)] fn main() { pub struct SimulationResult { pub sim: usize, // Replicate number (1-indexed) pub id: String, // Subject ID pub time: f64, // Observation time pub ipred: f64, // Individual prediction (no residual error) pub dv_sim: f64, // Simulated observation (with residual error) } pub struct PredictionResult { pub id: String, pub time: f64, pub pred: f64, // Population prediction (eta = 0) } }
Simulation Process
For each replicate and each subject:
- Sample random effects: \( \eta_i \sim N(0, \Omega) \) using the Cholesky factor \( L \): \( \eta = L \cdot z \), where \( z \sim N(0, I) \)
- Compute individual PK parameters via
pk_param_fn(theta, eta, covariates) - Generate predictions using the structural model
- Add residual error: \( DV = IPRED + \sqrt{V} \cdot \epsilon \), where \( \epsilon \sim N(0, 1) \) and \( V \) is the residual variance from the error model