Skip to main content

Module cost

Module cost 

Source
Expand description

Cost function calculation for BestDose optimization

Implements the hybrid cost function that trades off hitting the target on average against the spread of outcomes across the parameter distribution. Also enforces dose-range constraints through penalty-based bounds checking.

§Cost Function

Everything is computed from a single distribution over parameters — the support points and their probability weights w (see BestDoseObjective). Let p[i,j] be the prediction for support point i at observation j, and t[j] the target.

Cost = {
  (1-λ) × Variance + λ × Bias²,  if doses within bounds
  1e12 + violation² × 1e6,        if any dose violates bounds
}

§Variance term — expected squared error

Variance = Σᵢ wᵢ Σⱼ (t[j] - p[i,j])²   =   E_w[(t - p)²]

§Bias term — squared error of the mean prediction

Bias² = Σⱼ (t[j] - ȳ[j])²,   where ȳ[j] = Σᵢ wᵢ p[i,j]   (the weighted mean)

§Bias weight parameter (λ)

Using the decomposition E_w[(t-p)²] = (t - E_w[p])² + Var_w(p), the cost simplifies to:

Cost = (t - E_w[p])² + (1-λ) · Var_w(p)

So λ controls how strongly the spread of predicted outcomes across the distribution is penalized:

  • λ = 0.0: minimize the full expected squared error — hit the target on average and keep the prediction spread small (robust across all plausible parameter values).
  • λ = 1.0: only the weighted-mean prediction has to hit the target; the spread is ignored.
  • 0 < λ < 1: interpolates the variance penalty.

Note: λ is independent of whether w is a population distribution or a patient-specific posterior — that choice is made upstream by the caller.

§Implementation Notes

The cost function handles both concentration and AUC targets:

  • Concentration: Simulates model at observation times directly
  • AUC: Generates dense time grid and calculates AUC via trapezoidal rule

See evaluate for the main implementation.