# Recursive least squares filter

Recursive least squares (RLS) is an adaptive filter algorithm that recursively finds the coefficients that minimize a weighted linear least squares cost function relating to the input signals. This approach is in contrast to other algorithms such as the least mean squares (LMS) that aim to reduce the mean square error. In the derivation of the RLS, the input signals are considered deterministic, while for the LMS and similar algorithms they are considered stochastic. Compared to most of its competitors, the RLS exhibits extremely fast convergence. However, this benefit comes at the cost of high computational complexity.

## Motivation

RLS was discovered by Gauss but lay unused or ignored until 1950 when Plackett rediscovered the original work of Gauss from 1821. In general, the RLS can be used to solve any problem that can be solved by adaptive filters. For example, suppose that a signal $d(n)$ is transmitted over an echoey, noisy channel that causes it to be received as
    $x(n)=\sum _{k=0}^{q}b_{n}(k)d(n-k)+v(n)$
where $v(n)$ represents additive noise. The intent of the RLS filter is to recover the desired signal $d(n)$ by use of a $p+1$-tap FIR filter, $\mathbf {w}$:
    $d(n)\approx \sum _{k=0}^{p}w(k)x(n-k)=\mathbf {w} ^{\mathit {T}}\mathbf {x} _{n}$
where $\mathbf {x} _{n}=[x(n)\quad x(n-1)\quad \ldots \quad x(n-p)]^{T}$ is the column vector containing the $p+1$ most recent samples of $x(n)$. The estimate of the recovered desired signal is
    ${\hat {d}}(n)=\sum _{k=0}^{p}w_{n}(k)x(n-k)=\mathbf {w} _{n}^{\mathit {T}}\mathbf {x} _{n}$
The goal is to estimate the parameters of the filter $\mathbf {w}$, and at each time $n$ we refer to the current estimate as $\mathbf {w} _{n}$ and the adapted least-squares estimate by $\mathbf {w} _{n+1}$. $\mathbf {w} _{n}$ is also a column vector, as shown below, and the transpose, $\mathbf {w} _{n}^{\mathit {T}}$, is a row vector. The matrix product $\mathbf {w} _{n}^{\mathit {T}}\mathbf {x} _{n}$ (which is the dot product of $\mathbf {w} _{n}$ and $\mathbf {x} _{n}$) is ${\hat {d}}(n)$, a scalar. The estimate is "good" if ${\hat {d}}(n)-d(n)$ is small in magnitude in some least squares sense.
As time evolves, it is desired to avoid completely redoing the least squares algorithm to find the new estimate for $\mathbf {w} _{n+1}$, in terms of $\mathbf {w} _{n}$.
The benefit of the RLS algorithm is that there is no need to invert matrices, thereby saving computational cost. Another advantage is that it provides intuition behind such results as the Kalman filter.

## Discussion

The idea behind RLS filters is to minimize a cost function $C$ by appropriately selecting the filter coefficients $\mathbf {w} _{n}$, updating the filter as new data arrives. The error signal $e(n)$ and desired signal $d(n)$ are defined in the negative feedback diagram below:
The error implicitly depends on the filter coefficients through the estimate ${\hat {d}}(n)$:
    $e(n)=d(n)-{\hat {d}}(n)$
The weighted least squares error function $C$—the cost function we desire to minimize—being a function of $e(n)$ is therefore also dependent on the filter coefficients:
    $C(\mathbf {w} _{n})=\sum _{i=0}^{n}\lambda ^{n-i}e^{2}(i)$
where $0<\lambda \leq 1$ is the "forgetting factor" which gives exponentially less weight to older error samples.
The cost function is minimized by taking the partial derivatives for all entries $k$ of the coefficient vector $\mathbf {w} _{n}$ and setting the results to zero
    ${\frac {\partial C(\mathbf {w} _{n})}{\partial w_{n}(k)}}=\sum _{i=0}^{n}2\lambda ^{n-i}e(i)\cdot {\frac {\partial e(i)}{\partial w_{n}(k)}}=-\sum _{i=0}^{n}2\lambda ^{n-i}e(i)\,x(i-k)=0\qquad k=0,1,\ldots ,p$
Next, replace $e(n)$ with the definition of the error signal
    $\sum _{i=0}^{n}\lambda ^{n-i}\left[d(i)-\sum _{\ell =0}^{p}w_{n}(\ell )x(i-\ell )\right]x(i-k)=0\qquad k=0,1,\ldots ,p$
Rearranging the equation yields
    $\sum _{\ell =0}^{p}w_{n}(\ell )\left[\sum _{i=0}^{n}\lambda ^{n-i}\,x(i-\ell )x(i-k)\right]=\sum _{i=0}^{n}\lambda ^{n-i}d(i)x(i-k)\qquad k=0,1,\ldots ,p$
This form can be expressed in terms of matrices
    $\mathbf {R} _{x}(n)\,\mathbf {w} _{n}=\mathbf {r} _{dx}(n)$
where $\mathbf {R} _{x}(n)$ is the weighted sample covariance matrix for $x(n)$, and $\mathbf {r} _{dx}(n)$ is the equivalent estimate for the cross-covariance between $d(n)$ and $x(n)$. Based on this expression we find the coefficients which minimize the cost function as
    $\mathbf {w} _{n}=\mathbf {R} _{x}^{-1}(n)\,\mathbf {r} _{dx}(n)$
This is the main result of the discussion.

### Choosing λ

The smaller $\lambda$ is, the smaller is the contribution of previous samples to the covariance matrix. This makes the filter more sensitive to recent samples, which means more fluctuations in the filter co-efficients. The $\lambda =1$ case is referred to as the growing window RLS algorithm. In practice, $\lambda$ is usually chosen between 0.98 and 1. By using type-II maximum likelihood estimation the optimal $\lambda$ can be estimated from a set of data.

## Recursive algorithm

The discussion resulted in a single equation to determine a coefficient vector which minimizes the cost function. In this section we want to derive a recursive solution of the form
    $\mathbf {w} _{n}=\mathbf {w} _{n-1}+\Delta \mathbf {w} _{n-1}$
where $\Delta \mathbf {w} _{n-1}$ is a correction factor at time ${n-1}$. We start the derivation of the recursive algorithm by expressing the cross covariance $\mathbf {r} _{dx}(n)$ in terms of $\mathbf {r} _{dx}(n-1)$
    $\mathbf {r} _{dx}(n)$ $=\sum _{i=0}^{n}\lambda ^{n-i}d(i)\mathbf {x} (i)$ $=\sum _{i=0}^{n-1}\lambda ^{n-i}d(i)\mathbf {x} (i)+\lambda ^{0}d(n)\mathbf {x} (n)$ $=\lambda \mathbf {r} _{dx}(n-1)+d(n)\mathbf {x} (n)$
where $\mathbf {x} (i)$ is the ${p+1}$ dimensional data vector
    $\mathbf {x} (i)=[x(i),x(i-1),\dots ,x(i-p)]^{T}$
Similarly we express $\mathbf {R} _{x}(n)$ in terms of $\mathbf {R} _{x}(n-1)$ by
    $\mathbf {R} _{x}(n)$ $=\sum _{i=0}^{n}\lambda ^{n-i}\mathbf {x} (i)\mathbf {x} ^{T}(i)$ $=\lambda \mathbf {R} _{x}(n-1)+\mathbf {x} (n)\mathbf {x} ^{T}(n)$
In order to generate the coefficient vector we are interested in the inverse of the deterministic auto-covariance matrix. For that task the Woodbury matrix identity comes in handy. With
    $A$ $=\lambda \mathbf {R} _{x}(n-1)$ is $(p+1)$-by-$(p+1)$ $U$ $=\mathbf {x} (n)$ is $(p+1)$-by-1 (column vector) $V$ $=\mathbf {x} ^{T}(n)$ is 1-by-$(p+1)$ (row vector) $C$ $=\mathbf {I} _{1}$ is the 1-by-1 identity matrix
The Woodbury matrix identity follows
    $\mathbf {R} _{x}^{-1}(n)$ $=$ $\left[\lambda \mathbf {R} _{x}(n-1)+\mathbf {x} (n)\mathbf {x} ^{T}(n)\right]^{-1}$ $=$ ${\dfrac {1}{\lambda }}\left\lbrace \mathbf {R} _{x}^{-1}(n-1)-{\dfrac {\mathbf {R} _{x}^{-1}(n-1)\mathbf {x} (n)\mathbf {x} ^{T}(n)\mathbf {R} _{x}^{-1}(n-1)}{\lambda +\mathbf {x} ^{T}(n)\mathbf {R} _{x}^{-1}(n-1)\mathbf {x} (n)}}\right\rbrace$ $$
To come in line with the standard literature, we define
    $\mathbf {P} (n)$ $=\mathbf {R} _{x}^{-1}(n)$ $=\lambda ^{-1}\mathbf {P} (n-1)-\mathbf {g} (n)\mathbf {x} ^{T}(n)\lambda ^{-1}\mathbf {P} (n-1)$
where the gain vector $g(n)$ is
    $\mathbf {g} (n)$ $=\lambda ^{-1}\mathbf {P} (n-1)\mathbf {x} (n)\left\{1+\mathbf {x} ^{T}(n)\lambda ^{-1}\mathbf {P} (n-1)\mathbf {x} (n)\right\}^{-1}$ $=\mathbf {P} (n-1)\mathbf {x} (n)\left\{\lambda +\mathbf {x} ^{T}(n)\mathbf {P} (n-1)\mathbf {x} (n)\right\}^{-1}$
Before we move on, it is necessary to bring $\mathbf {g} (n)$ into another form
    $\mathbf {g} (n)\left\{1+\mathbf {x} ^{T}(n)\lambda ^{-1}\mathbf {P} (n-1)\mathbf {x} (n)\right\}$ $=\lambda ^{-1}\mathbf {P} (n-1)\mathbf {x} (n)$ $\mathbf {g} (n)+\mathbf {g} (n)\mathbf {x} ^{T}(n)\lambda ^{-1}\mathbf {P} (n-1)\mathbf {x} (n)$ $=\lambda ^{-1}\mathbf {P} (n-1)\mathbf {x} (n)$
Subtracting the second term on the left side yields
    $\mathbf {g} (n)$ $=\lambda ^{-1}\mathbf {P} (n-1)\mathbf {x} (n)-\mathbf {g} (n)\mathbf {x} ^{T}(n)\lambda ^{-1}\mathbf {P} (n-1)\mathbf {x} (n)$ $=\lambda ^{-1}\left[\mathbf {P} (n-1)-\mathbf {g} (n)\mathbf {x} ^{T}(n)\mathbf {P} (n-1)\right]\mathbf {x} (n)$
With the recursive definition of $\mathbf {P} (n)$ the desired form follows
    $\mathbf {g} (n)=\mathbf {P} (n)\mathbf {x} (n)$
Now we are ready to complete the recursion. As discussed
    $\mathbf {w} _{n}$ $=\mathbf {P} (n)\,\mathbf {r} _{dx}(n)$ $=\lambda \mathbf {P} (n)\,\mathbf {r} _{dx}(n-1)+d(n)\mathbf {P} (n)\,\mathbf {x} (n)$
The second step follows from the recursive definition of $\mathbf {r} _{dx}(n)$. Next we incorporate the recursive definition of $\mathbf {P} (n)$ together with the alternate form of $\mathbf {g} (n)$ and get
    $\mathbf {w} _{n}$ $=\lambda \left[\lambda ^{-1}\mathbf {P} (n-1)-\mathbf {g} (n)\mathbf {x} ^{T}(n)\lambda ^{-1}\mathbf {P} (n-1)\right]\mathbf {r} _{dx}(n-1)+d(n)\mathbf {g} (n)$ $=\mathbf {P} (n-1)\mathbf {r} _{dx}(n-1)-\mathbf {g} (n)\mathbf {x} ^{T}(n)\mathbf {P} (n-1)\mathbf {r} _{dx}(n-1)+d(n)\mathbf {g} (n)$ $=\mathbf {P} (n-1)\mathbf {r} _{dx}(n-1)+\mathbf {g} (n)\left[d(n)-\mathbf {x} ^{T}(n)\mathbf {P} (n-1)\mathbf {r} _{dx}(n-1)\right]$
With $\mathbf {w} _{n-1}=\mathbf {P} (n-1)\mathbf {r} _{dx}(n-1)$ we arrive at the update equation
    $\mathbf {w} _{n}$ $=\mathbf {w} _{n-1}+\mathbf {g} (n)\left[d(n)-\mathbf {x} ^{T}(n)\mathbf {w} _{n-1}\right]$ $=\mathbf {w} _{n-1}+\mathbf {g} (n)\alpha (n)$
where $\alpha (n)=d(n)-\mathbf {x} ^{T}(n)\mathbf {w} _{n-1}$ is the a priori error. Compare this with the a posteriori error; the error calculated after the filter is updated:
    $e(n)=d(n)-\mathbf {x} ^{T}(n)\mathbf {w} _{n}$
That means we found the correction factor
    $\Delta \mathbf {w} _{n-1}=\mathbf {g} (n)\alpha (n)$
This intuitively satisfying result indicates that the correction factor is directly proportional to both the error and the gain vector, which controls how much sensitivity is desired, through the weighting factor, $\lambda$.

## RLS algorithm summary

The RLS algorithm for a p-th order RLS filter can be summarized as
| Parameters: | $p=$ filter order |
| | $\lambda =$ forgetting factor |
| | $\delta =$ value to initialize $\mathbf {P} (0)$ |
| Initialization: | $\mathbf {w} (0)=0$, |
| | $x(k)=0,k=-p,\dots ,-1$, |
| | $d(k)=0,k=-p,\dots ,-1$ |
| | $\mathbf {P} (0)=\delta I$ where $I$ is the identity matrix of rank $p+1$ |
| Computation: | For $n=1,2,\dots$ |
| | $\mathbf {x} (n)=\left[{\begin{matrix}x(n)\\x(n-1)\\\vdots \\x(n-p)\end{matrix}}\right]$ |
| | $\alpha (n)=d(n)-\mathbf {x} ^{T}(n)\mathbf {w} (n-1)$ |
| | $\mathbf {g} (n)=\mathbf {P} (n-1)\mathbf {x} (n)\left\{\lambda +\mathbf {x} ^{T}(n)\mathbf {P} (n-1)\mathbf {x} (n)\right\}^{-1}$ |
| | $\mathbf {P} (n)=\lambda ^{-1}\mathbf {P} (n-1)-\mathbf {g} (n)\mathbf {x} ^{T}(n)\lambda ^{-1}\mathbf {P} (n-1)$ |
| | $\mathbf {w} (n)=\mathbf {w} (n-1)+\,\alpha (n)\mathbf {g} (n)$. |
The recursion for $P$ follows an algebraic Riccati equation and thus draws parallels to the Kalman filter.

## Lattice recursive least squares filter (LRLS)

The lattice recursive least squares adaptive filter is related to the standard RLS except that it requires fewer arithmetic operations (order N). It offers additional advantages over conventional LMS algorithms such as faster convergence rates, modular structure, and insensitivity to variations in eigenvalue spread of the input correlation matrix. The LRLS algorithm described is based on a posteriori errors and includes the normalized form. The derivation is similar to the standard RLS algorithm and is based on the definition of $d(k)\,\!$. In the forward prediction case, we have $d(k)=x(k)\,\!$ with the input signal $x(k-1)\,\!$ as the most up to date sample. The backward prediction case is $d(k)=x(k-i-1)\,\!$, where i is the index of the sample in the past we want to predict, and the input signal $x(k)\,\!$ is the most recent sample.

### Parameter summary

    $\kappa _{f}(k,i)\,\!$ is the forward reflection coefficient
    $\kappa _{b}(k,i)\,\!$ is the backward reflection coefficient
    $e_{f}(k,i)\,\!$ represents the instantaneous a posteriori forward prediction error
    $e_{b}(k,i)\,\!$ represents the instantaneous a posteriori backward prediction error
    $\xi _{b_{\min }}^{d}(k,i)\,\!$ is the minimum least-squares backward prediction error
    $\xi _{f_{\min }}^{d}(k,i)\,\!$ is the minimum least-squares forward prediction error
    $\gamma (k,i)\,\!$ is a conversion factor between a priori and a posteriori errors
    $v_{i}(k)\,\!$ are the feedforward multiplier coefficients.
    $\varepsilon \,\!$ is a small positive constant that can be 0.01

### LRLS algorithm summary

The algorithm for a LRLS filter can be summarized as
| Initialization: |
| | For i=0, 1, …, N{\textstyle i=0, 1, \ldots , N} |
| | $\delta (-1,i)=\delta _{D}(-1,i)=0\,\!$ (if x(k)=0{\textstyle x(k)=0} for k<0{\textstyle k<0}) |
| | $\xi _{b_{\min }}^{d}(-1,i)=\xi _{f_{\min }}^{d}(-1,i)=\varepsilon$ |
| | $\gamma (-1,i)=1\,\!$ |
| | $e_{b}(-1,i)=0\,\!$ |
| | End |
| Computation: |
| | For k≥0{\textstyle k\geq 0} |
| | $\gamma (k,0)=1\,\!$ |
| | $e_{b}(k,0)=e_{f}(k,0)=x(k)\,\!$ |
| | $\xi _{b_{\min }}^{d}(k,0)=\xi _{f_{\min }}^{d}(k,0)=x^{2}(k)+\lambda \xi _{f_{\min }}^{d}(k-1,0)\,\!$ |
| | $e(k,0)=d(k)\,\!$ |
| | For i=0, 1, …, N{\textstyle i=0, 1, \ldots , N} |
| | $\delta (k,i)=\lambda \delta (k-1,i)+{\frac {e_{b}(k-1,i)e_{f}(k,i)}{\gamma (k-1,i)}}$ |
| | $\gamma (k,i+1)=\gamma (k,i)-{\frac {e_{b}^{2}(k,i)}{\xi _{b_{\min }}^{d}(k,i)}}$ |
| | $\kappa _{b}(k,i)={\frac {\delta (k,i)}{\xi _{f_{\min }}^{d}(k,i)}}$ |
| | $\kappa _{f}(k,i)={\frac {\delta (k,i)}{\xi _{b_{\min }}^{d}(k-1,i)}}$ |
| | $e_{b}(k,i+1)=e_{b}(k-1,i)-\kappa _{b}(k,i)e_{f}(k,i)\,\!$ |
| | $e_{f}(k,i+1)=e_{f}(k,i)-\kappa _{f}(k,i)e_{b}(k-1,i)\,\!$ |
| | $\xi _{b_{\min }}^{d}(k,i+1)=\xi _{b_{\min }}^{d}(k-1,i)-\delta (k,i)\kappa _{b}(k,i)$ |
| | $\xi _{f_{\min }}^{d}(k,i+1)=\xi _{f_{\min }}^{d}(k,i)-\delta (k,i)\kappa _{f}(k,i)$ |
| | Feedforward filtering |
| | $\delta _{D}(k,i)=\lambda \delta _{D}(k-1,i)+{\frac {e(k,i)e_{b}(k,i)}{\gamma (k,i)}}$ |
| | $v_{i}(k)={\frac {\delta _{D}(k,i)}{\xi _{b_{\min }}^{d}(k,i)}}$ |
| | $e(k,i+1)=e(k,i)-v_{i}(k)e_{b}(k,i)\,\!$ |
| | End |
| | End |

## Normalized lattice recursive least squares filter (NLRLS)

The normalized form of the LRLS has fewer recursions and variables. It can be calculated by applying a normalization to the internal variables of the algorithm which will keep their magnitude bounded by one. This is generally not used in real-time applications because of the number of division and square-root operations which comes with a high computational load.

### NLRLS algorithm summary

The algorithm for a NLRLS filter can be summarized as
| Initialization: |
| | For i=0, 1, …, N.{\textstyle i=0, 1, \ldots , N.} |
| | ${\overline {\delta }}(-1,i)=0\,\!$ (if x(k)=d(k)=0{\textstyle x(k)=d(k)=0} for k<0{\textstyle k<0}) |
| | ${\overline {\delta }}_{D}(-1,i)=0\,\!$ |
| | ${\overline {e}}_{b}(-1,i)=0\,\!$ |
| | End |
| | $\sigma _{x}^{2}(-1)=\lambda \sigma _{d}^{2}(-1)=\varepsilon \,\!$ |
| Computation: |
| | For k≥0{\textstyle k\geq 0} |
| | $\sigma _{x}^{2}(k)=\lambda \sigma _{x}^{2}(k-1)+x^{2}(k)\,\!$ (Input signal energy) |
| | $\sigma _{d}^{2}(k)=\lambda \sigma _{d}^{2}(k-1)+d^{2}(k)\,\!$ (Reference signal energy) |
| | ${\overline {e}}_{b}(k,0)={\overline {e}}_{f}(k,0)={\frac {x(k)}{\sigma _{x}(k)}}\,\!$ |
| | ${\overline {e}}(k,0)={\frac {d(k)}{\sigma _{d}(k)}}\,\!$ |
| | For i=0, 1, …, N{\textstyle i=0, 1, \ldots , N} |
| | ${\overline {\delta }}(k,i)=\delta (k-1,i){\sqrt {(1-{\overline {e}}_{b}^{2}(k-1,i))(1-{\overline {e}}_{f}^{2}(k,i))}}+{\overline {e}}_{b}(k-1,i){\overline {e}}_{f}(k,i)$ |
| | ${\overline {e}}_{b}(k,i+1)={\frac {{\overline {e}}_{b}(k-1,i)-{\overline {\delta }}(k,i){\overline {e}}_{f}(k,i)}{\sqrt {(1-{\overline {\delta }}^{2}(k,i))(1-{\overline {e}}_{f}^{2}(k,i))}}}$ |
| | ${\overline {e}}_{f}(k,i+1)={\frac {{\overline {e}}_{f}(k,i)-{\overline {\delta }}(k,i){\overline {e}}_{b}(k-1,i)}{\sqrt {(1-{\overline {\delta }}^{2}(k,i))(1-{\overline {e}}_{b}^{2}(k-1,i))}}}$ |
| | Feedforward filter |
| | ${\overline {\delta }}_{D}(k,i)={\overline {\delta }}_{D}(k-1,i){\sqrt {(1-{\overline {e}}_{b}^{2}(k,i))(1-{\overline {e}}^{2}(k,i))}}+{\overline {e}}(k,i){\overline {e}}_{b}(k,i)$ |
| | ${\overline {e}}(k,i+1)={\frac {1}{\sqrt {(1-{\overline {e}}_{b}^{2}(k,i))(1-{\overline {\delta }}_{D}^{2}(k,i))}}}[{\overline {e}}(k,i)-{\overline {\delta }}_{D}(k,i){\overline {e}}_{b}(k,i)]$ |
| | End |
| | End |