Skip to content

Optimization

synfit fits dose-response curves by maximum likelihood estimation — finding the parameter values that make the observed data most probable under the chosen error model.

Objective Function

The optimizer minimizes the negative log-posterior:

\[ \text{objective}(\theta) = -\left[\log p(y \mid \theta) + \log p(\theta)\right] \]

where:

  • \(\log p(y \mid \theta)\) is the log-likelihood from the error model
  • \(\log p(\theta)\) is a soft boundary penalty (log-prior)

Soft Boundary Penalty (log_wall)

Rather than using hard parameter bounds (which can cause numerical issues at boundaries), synfit applies a soft penalty that ramps up smoothly when a parameter leaves its allowed range. In code, log_wall centers the bound interval, measures normalized distance from that center, and applies a linear ramp steep * (excess) outside the box (see synfit.hill.log_wall). The result is multiplied by a large constant (1000) when folded into the objective so boundary violations are costly.

scipy.optimize.minimize

The actual optimization uses scipy.optimize.minimize with box bounds. For single-drug fits, the parameter vector is typically 4-dimensional: [log_c50, hill, effect_0, effect_inf] (or 5-dimensional if asymmetry is included in fitting_parameters).

Initial guesses are derived automatically from the data (see Fit Configuration for how to override).

Goodness of Fit

After optimization, several metrics are computed:

Metric Formula What it tells you
R² \(1 - \text{RSS} / \text{SS}_{\text{tot}}\) Fraction of variance explained (closer to 1 = better)
AIC \(-2\log\mathcal{L} + 2k\) Model quality penalized for complexity (lower = better)
BIC \(-2\log\mathcal{L} + k\log n\) Like AIC but penalizes complexity more heavily
RSS \(\sum(y_i - \hat{y}_i)^2\) Total squared residual error

AIC and BIC are especially useful for comparing the 4-parameter vs 5-parameter Hill model — a lower value means the extra parameter is justified by the data.

Covariance Estimation

After optimization, the parameter covariance matrix is estimated from the numerical Hessian of the negative log-likelihood (data term only, no prior penalty) at the optimum. This covariance drives the confidence intervals on both individual parameters and the fitted curve.

See the Confidence Intervals page for the full methodology.

Further Reading

Implementation details

Base fitter (synfit.fitting.FitBase):

  • _objective(x, **kwargs) — returns -(log_prob_data + log_prior_prob). This is what scipy minimizes.
  • _log_prior_prob(x) — iterates over fitting parameters, gets bounds from self.config.bounds, applies log_wall() with a ×1000 multiplier.
  • _run_minimize(x0, bounds, **kwargs) — calls scipy.optimize.minimize(objective, x0, bounds=bounds), then estimates the parameter covariance via _estimate_covariance(). Returns (x_opt, success, message, pcov).
  • _estimate_covariance(x_opt, **kwargs) — computes the Hessian of the negative log-likelihood via central differences and inverts it. Returns None if the Hessian is singular or not positive-definite.
  • _x_to_kwargs(x) — converts the parameter array back to keyword arguments, un-logging any log_ prefixed parameters (e.g., log_c50 → ic50 = 10^x).

Soft boundary (synfit.hill):

  • log_wall(x, boundaries, steep=1) — bound penalty. boundaries is a (lo, hi) tuple. Returns 0 inside the interval (in the normalised sense described in the source), linearly increasing outside.

Initial guess derivation (synfit.single):

  • _init_config_from_data(data, direction) — finds the concentration closest to the midpoint response to initialize log_c50, sets effect_0 / effect_inf and their bounds from the observed response range (inhibition vs activation uses different asymptote heuristics).

Goodness-of-fit (synfit.single.SingleDrugFit.fit()):

  • R² is computed from 1 - RSS/SS_tot on valid (non-excluded) points.
  • AIC = -2 * log_likelihood + 2 * k where k = len(fitting_parameters).
  • BIC = -2 * log_likelihood + k * log(n).
  • All metrics are stored in the FitResult dataclass.