Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

 Open In Colab

There is one more important unconstrained minimization that we should discuss: non-linear least-squares, which is the extension of the linear least-square regression we did for curve fitting.

Now, we have several measurements in pairs (xi,yi)(x_i, y_i) and we wish to fit a function of xx which depends in some complex, nonlinear fashion on a set of parameters β\beta. E.g.: f(x,β)=sin⁡(β1x)eβ2x3f(x,\beta) = \sin(\beta_1 x) e^{\beta_2 x^3}

For each data point we have, we can define a residual, ri=f(xi,β)−yir_i = f(x_i,\beta) - y_i. Summing the square of these residuals, we get F=r⋅rF = \mathbf{r} \cdot \mathbf{r}. Our goal is to choose the set of parameters β\beta that minimizes FF:

min⁡βF=min⁡β∑iri2=min⁡β∑i[f(xi,β)−yi]2\min_\beta F = \min_\beta \sum_i r_i^2 = \min_\beta \sum_i [f(x_i,\beta) - y_i]^2

Note: FF is necessarily symmetric and positive semidefinite, therefore any extremum is a minimum.

Gauss-Newton regression

Let’s consider Newton’s optimization:

∂2F∂β2Δβ=−∂F∂β\frac{\partial^2 F}{\partial \beta^2} \Delta \beta = - \frac{\partial F}{\partial \beta}

Since FF is the sum of squared rir_i we can expand:

∂F∂β=2r∂r∂β=2rJ\frac{\partial F}{\partial \beta} = 2 r \frac{\partial r}{\partial \beta} = 2 r J

where J=∂r∂βJ = \frac{\partial r}{\partial \beta} is the Jacobian of the residuals with respect to the parameters. Since we typically have more data points (residuals) than parameters, this is rectangular.

The Hessian is expanded:

∂2F∂β2=2∑i∂ri∂βj∂ri∂βk+r∂2ri∂βj∂βk=2[JTJ+r∂2ri∂βj∂βk]\begin{aligned} \frac{\partial^2 F}{\partial \beta^2} &= 2 \sum_i \frac{\partial r_i}{\partial \beta_j} \frac{\partial r_i}{\partial \beta_k} + r \frac{\partial^2 r_i}{\partial \beta_j \partial \beta_k} \\ &= 2\left[ J^T J + r \frac{\partial^2 r_i}{\partial \beta_j \partial \beta_k} \right] \end{aligned}

The second derivative in the Hessian is troublesome for computation. Luckily, near the root r→0r\rightarrow 0 and we can disregard the term entirely:

∂2F∂β2≈2JTJ\frac{\partial^2 F}{\partial \beta^2} \approx 2 J^T J

Our minimization now becomes:

JTJΔβ=−JTrΔβ=−[JTJ]−1JTr=−J†r\begin{aligned} J^T J \Delta \beta &= -J^T r \\ \Delta \beta &= -[J^T J]^{-1} J^T r \\ &= -J^\dagger r \end{aligned}

where J†J^\dagger is the pseudoinverse of the Jacobian!

This is an interesting result since it suggests we could write the original problem as solving JΔβ=−rJ \Delta \beta = -r which is the Newton-Raphson method for root finding, not minimization... Remember however, that this is the pseudoinverse, not the true inverse. Indeed, if we had exactly as many data points as parameters, JJ would be square, and we would be able to find the exact solution which is the root! The fact that this is analogously true for least squares regression comes from 1) approximating the Hessian as the product of Jacobians, and 2) properties of the pseudo-inverse in performing linear least-squares.

The Levenberg-Marquardt algorithm

The problem with Gauss-Newton is that the Hessian approximation is only good near the root, and the method fails unless the initial guess is good.

Levenberg-Marquardt blends Gauss-Newton with Gradient descent:

[JTJ+λI]Δβ=−JTr[J^T J + \lambda I ] \Delta \beta = -J^T r

When λ\lambda is small, the method reduces to Gauss-Newton. When it is large, the JTJJ^T J term can be disregarded and the method is simply gradient descent. Determination of λ\lambda is a matter of heuristics and implementation.

The Levenberg-Marquardt algorithm is the go-to for common least-squares solvers including SciPy, NumPy, and tools like Excel.

Example: Nonlinear curve fit

We fit a damped sinusoidal model to noisy observations.

The original (data-generating) function is:

forig(x)=2.5sin⁡(1.3x)e−0.5xf_{\text{orig}}(x) = 2.5 \sin(1.3 x) e^{-0.5 x}

We fit the parameterized model function:

f(x;a,b,c)=asin⁡(bx)e−cxf(x; a, b, c) = a \sin(b x) e^{-c x}

Nonlinear least-squares determines the optimal parameters a^,b^,c^\hat{a}, \hat{b}, \hat{c} for the fit function:

ffit(x)=a^sin⁡(b^x)e−c^xf_{\text{fit}}(x) = \hat{a} \sin(\hat{b} x) e^{-\hat{c} x}

Example: Nonlinear 2D curve fit

We extend nonlinear regression to higher dimensions by fitting a 2D surface.

The original (data-generating) function is a standard 2D Gaussian centered at the origin:

forig(x,y)=exp⁡(−x2+y22)f_{\text{orig}}(x, y) = \exp\left( -\frac{x^2 + y^2}{2} \right)

We fit an elliptical 2D Gaussian model with a vertical baseline offset:

f(x,y;a,b,c,d,e,f)=aexp⁡(−[(x−b)22c2+(y−d)22e2])+ff(x, y; a, b, c, d, e, f) = a \exp\left( -\left[ \frac{(x - b)^2}{2 c^2} + \frac{(y - d)^2}{2 e^2} \right] \right) + f

Nonlinear least-squares estimates β^=[a^,b^,c^,d^,e^,f^]T\hat{\boldsymbol{\beta}} = [\hat{a}, \hat{b}, \hat{c}, \hat{d}, \hat{e}, \hat{f}]^T to obtain the fit function:

ffit(x,y)=a^exp⁡(−[(x−b^)22c^2+(y−d^)22e^2])+f^f_{\text{fit}}(x, y) = \hat{a} \exp\left( -\left[ \frac{(x - \hat{b})^2}{2 \hat{c}^2} + \frac{(y - \hat{d})^2}{2 \hat{e}^2} \right] \right) + \hat{f}