How Curve Fitting Works¶
This guide explains the mathematical foundation of nonlinear least squares curve fitting - what it means to “fit” a model to data and how the algorithm finds optimal parameters.
The Fitting Problem¶
Given: - Data points: (x₁, y₁), (x₂, y₂), …, (xₙ, yₙ) - A model function: f(x; θ) with parameters θ = (θ₁, θ₂, …, θₘ)
Find the parameters θ that minimize the sum of squared residuals:
This is called the least squares objective because we’re minimizing the sum of squared differences (residuals) between data and model.
Why Squared Residuals?¶
Mathematical convenience: The squared function is smooth and differentiable everywhere, making optimization easier.
Statistical interpretation: If errors are normally distributed, minimizing squared residuals gives the maximum likelihood estimate.
Equal treatment: Positive and negative residuals contribute equally.
Linear vs Nonlinear Least Squares¶
Linear least squares: The model is linear in parameters:
This has a direct algebraic solution (normal equations).
Nonlinear least squares: The model is nonlinear in parameters:
No direct solution exists - we need iterative optimization.
The Optimization Process¶
NLSQ uses the Trust Region Reflective (TRF) algorithm:
Initialize: Start with initial guess θ₀
Evaluate: Compute residuals r = y - f(x; θ)
Linearize: Compute Jacobian J = ∂f/∂θ (automatic with JAX)
Solve subproblem: Find step direction δ within a “trust region”
Update: θ ← θ + δ
Check convergence: Stop if change is small enough
Repeat: Go to step 2
θ₀ → [compute residuals] → [compute Jacobian] → [solve for δ]
↑ ↓
└──────────────── [update θ ← θ + δ] ←──────────────┘
The Jacobian Matrix¶
The Jacobian J is an n × m matrix of partial derivatives:
It tells us how sensitive the model output is to each parameter.
In NLSQ, the Jacobian is computed automatically using JAX’s automatic differentiation - no manual derivatives needed!
# You just write the model
def model(x, a, b):
return a * jnp.exp(-b * x)
# NLSQ automatically computes ∂f/∂a and ∂f/∂b
Trust Region Method¶
Instead of taking the full Newton step, TRF restricts the step to a “trust region” - a ball of radius Δ around the current point.
This ensures stability:
Step too large? Reduce trust region
Good step? Expand trust region
Near bounds? Use reflected steps
See Trust Region Reflective Algorithm for more details.
Convergence Criteria¶
Optimization stops when any of these are satisfied:
Gradient tolerance (gtol): Gradient is nearly zero
\[\|J^T r\|_\infty < \text{gtol}\]Function tolerance (ftol): Cost isn’t decreasing
\[\frac{|S_{k+1} - S_k|}{S_k} < \text{ftol}\]Step tolerance (xtol): Parameters aren’t changing
\[\frac{\|δ\|}{\|θ\|} < \text{xtol}\]Maximum iterations: Safety limit reached
Parameter Uncertainties¶
After finding optimal θ*, we estimate uncertainties from the covariance matrix:
where s² is the residual variance:
The standard error of each parameter is:
Goodness of Fit¶
R-squared (coefficient of determination):
where Sₜₒₜ = Σ(yᵢ - ȳ)² is the total variance.
R² = 1: Perfect fit
R² = 0: Model no better than mean
R² < 0: Model worse than mean (rare, indicates problems)
Reduced chi-squared:
Should be approximately 1 for a good fit with correctly estimated errors.
When Fitting Fails¶
Common issues:
Local minimum: Found a suboptimal solution
Use better initial guesses
Try global optimization (preset=’global’)
Ill-conditioned Jacobian: Parameters are poorly determined
Simplify model
Fix some parameters
Divergence: Cost keeps increasing
Check data quality
Adjust bounds
Oscillation: Parameters alternate without converging
Check for parameter correlations
Reparameterize model
See How to Debug Bad Fits for troubleshooting.
Summary¶
Concept |
Meaning |
|---|---|
Residuals |
Difference between data and model |
Least squares |
Minimize sum of squared residuals |
Jacobian |
How model changes with parameters |
Trust region |
Safe step size limit |
Covariance |
Parameter uncertainties |
R² |
Fraction of variance explained |
See Also¶
Trust Region Reflective Algorithm - Trust Region Reflective algorithm details
JAX and Automatic Differentiation - How automatic differentiation works
Understanding Results - Practical result interpretation