Median Absolute Error (MdAE) measures the middle value of all absolute errors. Unlike MAE which averages errors, MdAE finds the median, providing exceptional robustness against outliers and extreme values.
## Historical Context
MdAE emerged from robust statistics, where the median has long been preferred over the mean for its resistance to outliers. In forecasting and machine learning, MdAE provides a more stable measure of typical prediction accuracy when data contains anomalies or heavy-tailed distributions.
## Architecture & Physics
MdAE maintains a sorted view of errors through a specialized ring buffer. When new errors arrive, they replace the oldest while maintaining sort order, enabling O(1) median retrieval. This makes MdAE both robust and efficient.
### Properties
* **Outlier-robust**: Unaffected by extreme values
* **Non-negative**: MdAE ≥ 0, with 0 indicating perfect prediction
* **Same units**: Results are in the same units as the original data
* **Stable**: Small changes in data produce small changes in output
## Mathematical Foundation
### 1. Absolute Error
For each observation, calculate the absolute difference:
$$e_i = |y_i - \hat{y}_i|$$
Where:
* $y_i$ = actual value
* $\hat{y}_i$ = predicted value
### 2. Median Calculation
Find the middle value of the sorted errors:
$$MdAE = \text{median}(e_1, e_2, ..., e_n)$$
For odd n: middle element
For even n: average of two middle elements
### 3. Running Update (O(1))
QuanTAlib uses a sorted ring buffer for efficient median retrieval:
$$MdAE = \begin{cases}
e_{(n+1)/2} & \text{if } n \text{ is odd} \\
\frac{e_{n/2} + e_{n/2+1}}{2} & \text{if } n \text{ is even}
\end{cases}$$
## Implementation Details
### Usage Patterns
```csharp
// Streaming mode - update with each new observation