pub fn calculate_cost(
problem: &BestDoseProblem,
candidate_doses: &[f64],
) -> Result<f64>Expand description
Calculate cost function for a candidate dose regimen
This is the core objective function minimized by the Nelder-Mead optimizer during Stage 2 of the BestDose algorithm.
§Arguments
problem- TheBestDoseProblemcontaining all necessary datacandidate_doses- Dose amounts to evaluate (only for optimizable doses)
§Returns
The cost value (1-λ) × Variance + λ × Bias² for the candidate doses.
Lower cost indicates better match to targets.
§Dose Masking
Only doses with amount == 0.0 in the target subject are considered optimizable.
Doses with non-zero amounts remain fixed at their specified values.
The candidate_doses parameter contains only the optimizable doses, which are
substituted into the target subject before simulation
§Cost Function Details
§Variance Term
Expected squared prediction error using posterior weights:
Variance = Σᵢ P(θᵢ|data) × Σⱼ (target[j] - pred[i,j])²For each support point θᵢ:
- Simulate model with candidate doses
- Calculate squared error at each observation time j
- Weight by posterior probability P(θᵢ|data)
§Bias Term
Squared deviation from population mean:
Bias² = Σⱼ (target[j] - E[pred[j]])²
where E[pred[j]] = Σᵢ P(θᵢ) × pred[i,j] (prior weights)The population mean uses prior weights, not posterior weights, to represent population-typical behavior independent of patient-specific data.
§Target Types
-
Concentration (
Target::Concentration): Predictions are concentrations at observation times -
AUC ([
Target::AUC]): Predictions are cumulative AUC values calculated via trapezoidal rule on a dense time grid (controlled bysettings.predictions().idelta)
§Example
// Internal use by optimizer
let cost = calculate_cost(&problem, &[100.0, 150.0])?;§Errors
Returns error if:
- Model simulation fails
- Prediction length doesn’t match observation count
- AUC calculation fails (for AUC targets)