← All algorithms

Regression

Recursive Least Squares (RLS)

2023-12-14

Advanced process control is essential in semiconductor manufacturing, chemical plants, and other industries, where reducing process variation helps improve yield. When process characteristics change over time, adapting the model can help account for these changes. Recursive least squares (RLS) can be used to track changes in the underlying relationships.

Previously, when solving linear regression equations, the Matrix Inversion Method was introduced. For the measurements Y as a function of source parameter X, the predicted value (y) was expressed as y = Xw, and the coefficient w which best represents the real measurements was derived by the following matrix equation.

\(w=(X^TX)^{-1}X^TY\)

The sizes of the matrix associated with the equation for the n number of samples and d number of source variables are as follows:​

[dx1] = ([dxn][nxd])[dxn][nx1].

​

The problem statement is how to update the new coefficient wnew from the old coefficient wold when a new dataset (U, v) is added to n samples. If we express this in matrix form,

\(w=(X|U^TX|U)^{-1}(X|U)^T(Y|v)=(X^TX+U^TU)^{-1}(X^TY+U^Tv)\)

and the size of the matrix becomes​

[dx1] = ([dxn][nxd]+[dxd])([dxn][nx1]+[dx1][1x1]).

​

The two-dimensional representation of the above matrix can be shown in the picture below.​

​

The term of the inverse matrix can be expanded by using Sherman Morrison's inverse matrix lemma as shown below:

\((A+uv^T)^{-1}=A^{-1}-\dfrac{A^{-1}uv^TA^{-1}}{1+v^TA^{-1}u}\)

\((X^TX+U^TU)^{-1}=(X^TX)^{-1}-\dfrac{(X^TX)^{-1}U^TU(X^TX)^{-1}}{1+U(X^TX)^{-1}U^T}\)

By introducing the following P and δP​ definitions,

\(P_n=(X^TX)^{-1},\)

\(\delta P_n=- \dfrac{ (X^TX)^{-1}U^TU(X^TX)^{-1}}{1+U(X^TX)^{-1}U^T}\)

\(P_{n+1}=P_n-\dfrac{(P_nU)^T(P_nU)}{1+P_nU^2}=P_n+\delta P_n\)

​​

The new gradient, wnew, for the n+1 number of the data points can be formulated using the old gradient, wold, and new dataset (U,v)

\(w^{new}=(X^TX+U^TU)^{-1}(X^TY+U^Tv)=w^{old}+P_n(U^Tv)-\delta P_n(X^TY)-\delta P_n(U^Tv)\)

Let's consider the simplest case which there is only one source parameter (i.e. d=1) to help visualizing how it works.

(1) When there is only one sample data point (x1, y1), the slope β1 becomes

\(y_1=x_1\beta _1\)

\(\beta _1=\dfrac{x_1y_1}{x_1^2}=p_1x_1y_1\)

where \(p_1 = \dfrac{1}{x_1^2}\)

(2) If a new sample (x2, y2) added, the new slope β2 can be expressed by β1, p1, and (x2, y2).

\(\begin{pmatrix}y_1\\y_2\end{pmatrix}=\begin{pmatrix}x_1\\x_2\end{pmatrix}\beta _2\)

\(\beta _2=\dfrac{x_1y_1+x_2y_2}{x_1^2+x_2^2}=\dfrac{x_1y_1+\beta _1x_2^2-\beta _1x_2^2+x_2y_2}{x_1^2+x_2^2}=\dfrac{\beta _1x_1^2+\beta _1x_2^2-\beta _1x_2^2+x_2y_2}{x_1^2+x_2^2}\)

\(=\beta _1+\dfrac{x_2y_2-\beta _1x_2^2}{x_1^2+x_2^2}=\beta _1+x_2\dfrac{y_2-\beta _1x_2}{x_1^2+x_2^2}\equiv \beta _1+p_2x_2(y_2-\beta _1x_2)\)

\(\text{where }p_2=\dfrac{1}{x_1^2+x_2^2}=\dfrac{1}{\dfrac{1}{p_1}+x_2^2}=\dfrac{p_1+p_1x_2^2p_1-p_1x_2^2p_1}{1+p_1x_2^2}=p_1-p_1x_2(1+p_1x_2^2)^{-1}x_2p_1\)

(3) If next new sample (x3, y3) added, the new slope β3 can be expressed by β2, p2, and (x3, y3) same as before.

\(\begin{pmatrix}y_1\\y_2\\y_3\end{pmatrix}=\begin{pmatrix}x_1\\x_2\\x_3\end{pmatrix}\beta _3,\)

\(\beta _3=\dfrac{x_1y_1+x_2y_2+x_3y_3}{x_1^2+x_2^2+x_3^2}=\dfrac{x_1y_1+x_2y_2+\beta _2x_3^2-\beta _2x_3^2+x_3y_3}{x_1^2+x_2^2+x_3^2}=\equiv \beta _2+p_3x_3(y_3-\beta _2x_3)\)

\(\text{where }p_3=\dfrac{1}{x_1^2+x_2^2+x_3^2}=\dfrac{1}{\frac{1}{p_2}+x_3^2}=p_2-p_2x_3(1+p_2x_3^2)^{-1}x_3p_2\)

Therefore, the gradient update after the new sample added is completely described by previous gradient, the inverse of the previous covariance matrix (XTX)-1, and added new sample data.

\(\beta_k=\beta_{k-1}+p_kx_k(y_k-\beta_{k-1}x_k)\)

here

\(p_k=p_{k-1}-p_{k-1}x_k(1+p_{k-1}x_k^2)^{-1}x_kp_{k-1}\).

Below is an example that demonstrates how to obtain the coefficients of a second-order polynomial approximation, the inverse of the covariance matrix from the first five data points, and how to update only the slope using the sixth data point.

​

Until now, it has been assumed that all past data is used, which means that the equal amount of weight is assigned to the all past data. However, with the introduction of a forgetting factor, λ, it is possible to assign higher weights in recent data.

When there is only one source parameter, simply replacing the pk-1 term in calculating the gradient above with λpk-1 will produce new slope with the forgetting factor.

\(p_k=\lambda p_{k-1}-\lambda^2 p_{k-1}x_k(1+\lambda p_{k-1}x_k^2)^{-1}x_kp_{k-1}\)

​