Skip to article frontmatterSkip to article content

Nonlinear least squares

After the solution of square linear systems, we generalized to the case of having more constraints to satisfy than available variables. Our next step is to do the same for nonlinear equations, thus filling out this table:

linearnonlinear
squareAx=b\mathbf{A}\mathbf{x}=\mathbf{b}f(x)=0\mathbf{f}(\mathbf{x})=\boldsymbol{0}
overdeterminedminAxb2\min\, \bigl|\mathbf{A}\mathbf{x} - \mathbf{b}\bigr|_2minf(x)2\min\, \bigl|\mathbf{f}(\mathbf{x}) \bigr|_2

As in the linear case, we consider only overdetermined problems, where m>nm>n. Minimizing a positive quantity is equivalent to minimizing its square, so we could also define the result as minimizing ϕ(x)=f(x)Tf(x)\phi(\mathbf{x})=\mathbf{f}(\mathbf{x})^T\mathbf{f}(\mathbf{x}).

4.7.1Gauss–Newton method

You should not be surprised to learn that we can formulate an algorithm by substituting a linear model function for f\mathbf{f}. At a current estimate xk\mathbf{x}_k we define

q(x)=f(xk)+Ak(xxk), \mathbf{q}(\mathbf{x}) = \mathbf{f}(\mathbf{x}_k) + \mathbf{A}_k(\mathbf{x}-\mathbf{x}_k),

where Ak\mathbf{A}_k is the exact m×nm\times n Jacobian matrix, J(xk)\mathbf{J}(\mathbf{x}_k), or an approximation of it as described in Quasi-Newton methods.

In the square case, we solved q=0\mathbf{q}=\boldsymbol{0} to define the new value for x\mathbf{x}, leading to the condition Aksk=fk\mathbf{A}_k\mathbf{s}_k=-\mathbf{f}_k, where sk=xk+1xk\mathbf{s}_k=\mathbf{x}_{k+1}-\mathbf{x}_k. Now, with m>nm>n, we cannot expect to solve q=0\mathbf{q}=\boldsymbol{0}, so instead we define xk+1\mathbf{x}_{k+1} as the value that minimizes q2\| \mathbf{q} \|_2.

In brief, Gauss–Newton solves a series of linear least-squares problems in order to solve a nonlinear least-squares problem.

Surprisingly, Function 4.5.2 and Function 4.6.3, which were introduced for the case of m=nm=n nonlinear equations, work without modification as the Gauss–Newton method for the overdetermined case! The reason is that the backslash operator applies equally well to the linear system and linear least-squares problems, and nothing else in those functions was written with explicit reference to nn.

4.7.2Convergence

In the multidimensional Newton method for a nonlinear system, we expect quadratic convergence to a solution in the typical case. For the Gauss–Newton method, the picture is more complicated.

As always in least-squares problems, the residual f(x)\mathbf{f}(\mathbf{x}) will not necessarily be zero when f\|\mathbf{f}\| is minimized. Suppose that the minimum value of f\|\mathbf{f}\| is R>0R>0. In general, we might observe quadratic-like convergence until the iterate xk\|\mathbf{x}_k\| is within distance RR of a true minimizer, and linear convergence thereafter. When RR is not sufficiently small, the convergence can be quite slow.

4.7.3Nonlinear data fitting

In Fitting functions to data we saw how to fit functions to data values, provided that the set of candidate fitting functions depends linearly on the undetermined coefficients. We now have a tool to generalize that process to fitting functions that depend nonlinearly on unknown parameters.

Suppose that (ti,yi)(t_i,y_i) for i=1,,mi=1,\ldots,m are given points. We wish to model the data by a function g(t,x)g(t,\mathbf{x}) that depends on unknown parameters x1,,xnx_1,\ldots,x_n in an arbitrary way. A standard approach is to minimize the discrepancy between the model and the observations, in a least-squares sense. Define

f(x)=[g(ti,x)yi]i=1,,m.\mathbf{f}(\mathbf{x}) = \left[\, g(t_i,\mathbf{x})-y_i \, \right]_{\,i=1,\ldots,m}.

We call f\mathbf{f} a misfit function. By minimizing f(c)2\bigl\| \mathbf{f}(\mathbf{c}) \bigr\|^2, we get the best possible fit to the data. If an explicit Jacobian matrix is desired for the minimization, we can compute

f(x)=[xjg(ti,x)]i=1,,m;j=1,,n.\mathbf{f}{\,}'(\mathbf{x}) = \left[ \frac{\partial}{\partial x_j} g(t_i,\mathbf{x}) \right]_{\,i=1,\ldots,m;\,j=1,\ldots,n.}

The form of gg is up to the modeler. There may be compelling theoretical choices, or you may just be looking for enough algebraic power to express the data well. Naturally, in the special case where the dependence on c\mathbf{c} is linear, i.e.,

g(t,c)=c1g1(t)+c2g2(t)++cmgm(t), g(t,\mathbf{c}) = c_1 g_1(t) + c_2 g_2(t) + \cdots + c_m g_m(t),

then the misfit function is also linear in c\mathbf{c} and the fitting problem reduces to linear least squares.

4.7.4Exercises

  1. ✍ Define f(x)=[x8,  x24]\mathbf{f}(x)=[ x-8, \; x^2-4 ].

    (a) Write out the linear model of f\mathbf{f} at x=2x=2.

    (b) Find the estimate produced by one step of the Gauss–Newton method, starting at x=2x=2.

  2. ✍ (Continuation of Exercise 1.) The Gauss–Newton method replaces f(x)\mathbf{f}(\mathbf{x}) by a linear model and minimizes the norm of its residual. An alternative is to replace f(x)22\| \mathbf{f}(\mathbf{x}) \|_2^2 by a scalar quadratic model q(x)q(\mathbf{x}) and minimize that.

    (a) Using f(x)=[x8,  x24]\mathbf{f}(x) = [ x-8, \; x^2-4 ], let q(x)q(x) be defined by the first three terms in the Taylor series for f(x)22\| \mathbf{f}(x) \|_2^2 at x=2x=2.

    (b) Find the unique xx that minimizes q(x)q(x). Is the result the same as the estimate produced by Gauss–Newton?

  3. ⌨ A famous result by Kermack and McKendrick in 1927 Kermack et al. (1927) suggests that in epidemics that kill only a small fraction of a susceptible population, the death rate as a function of time is well modeled by

    w(t)=Asech2[B(tC)]w'(t) = A \operatorname{sech}^2[B(t-C)]

    for constant values of the parameters A,B,CA,B,C. Since the maximum of sech is sech(0)=1\operatorname{sech}(0)=1, AA is the maximum death rate and CC is the time of peak deaths. You will use this model to fit the deaths per week from plague recorded in Mumbai during 1906:

    5, 10, 17, 22, 30, 50, 51, 90, 120, 180, 292, 395, 445, 775, 780,
    700, 698, 880, 925, 800, 578, 400, 350, 202, 105, 65, 55, 40, 30, 20

    (a) Use Function 4.6.3 to find the best least-squares fit to the data using the sech2\operatorname{sech}^2 model. Make a plot of the model fit superimposed on the data.

    (b) Repeat part (a) using only the first 15 data values.

  4. ⌨ (Variation on Exercise 4.5.6.) Suppose the points (xi,yi)(x_i,y_i) for i=1,,mi=1,\ldots,m are given, and the goal is to find the circle with center (a,b)(a,b) and radius rr that best fits the points. Define

    fi(a,b,r)=(axi)2+(byi)2r2,i=1,,m.f_i(a,b,r) = (a-x_i)^2 + (b-y_i)^2 - r^2, \qquad i=1,\ldots,m.

    Then we can define the best circle as the one that minimizes f\|\mathbf{f}\|.

    Define data points as follows:

    m = 30; t = 2π*rand(m);
    x = @. -2 + 5*cos(t); y = @. 1 + 5*sin(t);
    x += 0.2*randn(m); y += 0.2*randn(m);

    Use Function 4.6.3 to find the best-fit circle, and make a plot of the circle superimposed on the points.

  5. ⌨ The position of the upper lid during an eye blink can be measured from high-speed video Wu et al. (2014), and it may be possible to classify blinks based in part on fits to the lid position Brosch et al. (2017). The lid position functions proposed to fit blinks is a product of a monomial or polynomial multiplying a decaying exponential Berke & Mueller (1998). In this problem, you will generate representative data, add a small amount of noise to it, and then perform nonlinear least-squares fits to the data.

    (a) Consider the function y(a)=a1t2exp(a2ta3)y(\mathbf{a}) = a_1 t^2 \exp \left( -a_2 t^{a_3} \right), using the vector of coefficients a=[a1,a2,a3]\mathbf{a} = [a_1,a_2,a_3], and create synthetic eyelid position data as follows:

    N = 20;                            # number of time values
    t = (1:N)/N;                       # equally spaced to t=1
    a = [10, 10, 2];                   # baseline values
    y = @. a(1)*t^2*exp(-a(2)*t^a(3)); # ideal data
    ym = copy(y);                      # vector for data
    ir = 1:N-1;                        # range to add noise
    noise = 0.03;                      # amplitude of noise
    ym[ir] += noise*rand(N-1);         # add noise

    (b) Using the data (t,ym), find the nonlinear least-squares fit using Function 4.6.3.

    (c) Plot the fits using np = 100 points over t=(1:np)/np together with symbols for the N measured data points ym.

    (d) Increase the noise to 5% and 10%. You may have to increase the number of measured points N and/or the maximum number of iterations. How close are the coefficients? Plot the data and the resulting fit for each case.

  6. ⌨ Repeat the previous problem using the fitting function y(a)=(a1+a2t+a3t2)t2exp(a4ta5)y(\mathbf{a}) = (a_1+a_2 t + a_3 t^2) t^2 \exp \left( -a_4 t^{a_5} \right), using the vector of coefficients a=[a1,,a5]\mathbf{a} = [a_1,\ldots,a_5]. (This was the choice used in Brosch et al Brosch et al. (2017).) Use a = [20, -10, -8, 7, 2] to create the data and as an initial guess for the coefficients for the fit to the noisy data.

References
  1. Kermack, W. O., McKendrick, A. G., & Walker, G. T. (1927). A Contribution to the Mathematical Theory of Epidemics. Proceedings of the Royal Society of London. Series A, Containing Papers of a Mathematical and Physical Character, 115(772), 700–721. 10.1098/rspa.1927.0118
  2. Wu, Z., Begley, C. G., Situ, P., Simpson, T., & Liu, H. (2014). The Effects of Mild Ocular Surface Stimulation and Concentration on Spontaneous Blink Parameters. Current Eye Research, 39(1), 9–20. 10.3109/02713683.2013.822896
  3. Brosch, J. K., Wu, Z., Begley, C. G., Driscoll, T. A., & Braun, R. J. (2017). Blink Characterization Using Curve Fitting and Clustering Algorithms. Journal for Modeling in Ophthalmology, 1(3), 60–81. 10.35119/maio.v1i3.38
  4. Berke, A., & Mueller, S. (1998). The Kinetics of Lid Motion and Its Effects on the Tear Film. In D. A. Sullivan, D. A. Dartt, & M. A. Meneray (Eds.), Lacrimal Gland, Tear Film, and Dry Eye Syndromes 2 (Vol. 438, pp. 417–424). Springer US. 10.1007/978-1-4615-5359-5_58