|
| 1 | +# Recursive Least Squares (RLS) |
| 2 | + |
| 3 | +## Overview & Motivation |
| 4 | + |
| 5 | +Recursive Least Squares (RLS) is an online parameter estimation algorithm that incrementally updates a least-squares fit as new data arrives, without reprocessing past observations. It converges faster than gradient-descent methods (e.g., LMS) and is well-suited for real-time system identification and adaptive filtering on embedded systems. |
| 6 | + |
| 7 | +## Mathematical Theory |
| 8 | + |
| 9 | +### Problem Setting |
| 10 | + |
| 11 | +Given a linear model $y = \mathbf{x}^T \boldsymbol{\theta} + \epsilon$, where $\mathbf{x} \in \mathbb{R}^p$ is the regressor, $\boldsymbol{\theta} \in \mathbb{R}^p$ are the unknown parameters, and $\epsilon$ is noise, RLS minimizes the exponentially weighted cost: |
| 12 | + |
| 13 | +$$J(\boldsymbol{\theta}) = \sum_{k=1}^{n} \lambda^{n-k} (y_k - \mathbf{x}_k^T \boldsymbol{\theta})^2$$ |
| 14 | + |
| 15 | +where $\lambda \in (0, 1]$ is the forgetting factor. |
| 16 | + |
| 17 | +### Update Equations |
| 18 | + |
| 19 | +Given a new observation $(\mathbf{x}, y)$: |
| 20 | + |
| 21 | +1. **Gain vector**: $\mathbf{g} = P \mathbf{x}$ |
| 22 | +2. **Denominator**: $d = \lambda + \mathbf{x}^T P \mathbf{x}$ |
| 23 | +3. **Innovation (pre-update error)**: $e = y - \mathbf{x}^T \hat{\boldsymbol{\theta}}$ |
| 24 | +4. **Parameter update**: $\hat{\boldsymbol{\theta}} \leftarrow \hat{\boldsymbol{\theta}} + \frac{\mathbf{g} \cdot e}{d}$ |
| 25 | +5. **Covariance update**: $P \leftarrow \frac{1}{\lambda}\left(P - \frac{\mathbf{g} \mathbf{g}^T}{d}\right)$ |
| 26 | + |
| 27 | +### Forgetting Factor |
| 28 | + |
| 29 | +- $\lambda = 1$: Full memory — equivalent to batch least squares with all past data equally weighted. |
| 30 | +- $\lambda < 1$: Exponential forgetting — recent data is weighted more heavily, enabling tracking of time-varying systems. |
| 31 | +- Typical values: $0.95 \leq \lambda \leq 1.0$. |
| 32 | + |
| 33 | +### Convergence Evaluation |
| 34 | + |
| 35 | +The implementation provides convergence diagnostics via `EstimationMetrics`: |
| 36 | +- **Innovation**: Pre-update prediction error magnitude |
| 37 | +- **Residual**: Post-update prediction error magnitude |
| 38 | +- **Uncertainty**: Trace of the covariance matrix $P$ (overall parameter uncertainty) |
| 39 | + |
| 40 | +## Complexity Analysis |
| 41 | + |
| 42 | +| Case | Time | Space | Notes | |
| 43 | +|---------|----------|----------|----------------------------------------| |
| 44 | +| Average | $O(p^2)$ | $O(p^2)$ | Matrix-vector products + outer product | |
| 45 | + |
| 46 | +Per-update cost is $O(p^2)$ for $p$ parameters, dominated by the covariance update. This is significantly cheaper than the $O(p^3)$ batch solution via matrix inversion. |
| 47 | + |
| 48 | +## Step-by-Step Walkthrough |
| 49 | + |
| 50 | +Estimating the slope of $y = 2x$ with RLS ($p = 2$: bias + slope, $\lambda = 1$): |
| 51 | + |
| 52 | +| Step | $\mathbf{x}$ | $y$ | $\hat{\theta}_0$ (bias) | $\hat{\theta}_1$ (slope) | Innovation | |
| 53 | +|------|--------------|-----|-------------------------|--------------------------|------------| |
| 54 | +| Init | — | — | 0.0 | 0.0 | — | |
| 55 | +| 1 | [1, 1] | 2 | 1.0 | 1.0 | 2.0 | |
| 56 | +| 2 | [1, 2] | 4 | 0.0 | 2.0 | 1.0 | |
| 57 | +| 3 | [1, 3] | 6 | 0.0 | 2.0 | 0.0 | |
| 58 | + |
| 59 | +Convergence is achieved in 2 steps for this noiseless system. |
| 60 | + |
| 61 | +## Pitfalls & Edge Cases |
| 62 | + |
| 63 | +- **Covariance blow-up**: With $\lambda < 1$ and insufficient excitation, the covariance matrix $P$ can grow unbounded. Monitor the uncertainty metric and consider covariance resetting. |
| 64 | +- **Numerical conditioning**: The covariance update can lose symmetry and positive-definiteness due to floating-point errors. For long-running applications, consider using the Joseph form: $P \leftarrow (I - K\mathbf{x}^T)P(I - K\mathbf{x}^T)^T + K K^T / \lambda$. |
| 65 | +- **Initial covariance**: A large initial $P$ (e.g., $\alpha I$ with $\alpha = 100$) gives fast initial convergence but higher initial sensitivity. A small $P$ gives conservative updates. |
| 66 | +- **Regressor scaling**: Large differences in regressor magnitudes can cause numerical issues. Normalize inputs when possible. |
| 67 | + |
| 68 | +## Variants & Generalizations |
| 69 | + |
| 70 | +- **Weighted RLS**: Use a general weighting matrix instead of scalar forgetting. |
| 71 | +- **QR-decomposition RLS**: Better numerical stability by avoiding explicit covariance updates. |
| 72 | +- **Kalman filter**: RLS is equivalent to a steady-state Kalman filter for the linear regression problem. |
| 73 | +- **Regularized RLS**: Add a regularization term $\mu \|\boldsymbol{\theta}\|^2$ to prevent overfitting. |
| 74 | + |
| 75 | +## Applications |
| 76 | + |
| 77 | +- Online system identification for adaptive control |
| 78 | +- Adaptive noise cancellation |
| 79 | +- Channel equalization in communications |
| 80 | +- Real-time parameter tracking in engine/motor control |
| 81 | +- Sensor calibration |
| 82 | + |
| 83 | +## Connections to Other Algorithms |
| 84 | + |
| 85 | +- **Linear Regression**: Batch (offline) version of the same problem. Linear regression solves via matrix inversion; RLS updates incrementally. |
| 86 | +- **Yule-Walker**: Both estimate AR model parameters, but Yule-Walker uses autocorrelation statistics (offline) while RLS processes samples sequentially. |
| 87 | +- **Kalman Filter**: RLS is a special case of the Kalman filter where the state transition is identity and the process noise is zero. |
| 88 | +- **Gradient Descent**: LMS/gradient descent is a simpler adaptive algorithm with $O(p)$ cost but slower convergence compared to RLS's $O(p^2)$. |
| 89 | + |
| 90 | +## References & Further Reading |
| 91 | + |
| 92 | +- Haykin, S., *Adaptive Filter Theory*, 5th ed., Pearson, 2014 — Chapter 9. |
| 93 | +- Ljung, L., *System Identification: Theory for the User*, 2nd ed., Prentice Hall, 1999 — Chapter 11. |
| 94 | +- Åström, K.J. and Wittenmark, B., *Adaptive Control*, 2nd ed., Dover, 2008 — Chapter 2. |
0 commit comments