- **Similar:** [Mode](../mode/Mode.md), [Percentile](../percentile/Percentile.md) | **Trading note:** Rolling median; robust central tendency resistant to outliers. Good for support/resistance identification.
The Rolling Median is a robust statistic that represents the middle value of a dataset within a moving window. Unlike the Simple Moving Average (SMA), which can be skewed by extreme values, the Median provides a more stable measure of central tendency, making it particularly useful for filtering noise in volatile markets.
## Historical Context
The concept of the median dates back to Edward Wright in 1599, but its application in time-series analysis became prominent with the rise of robust statistics in the 20th century. In technical analysis, it is often used as a replacement for moving averages to identify trends without the lag induced by averaging large deviations.
## Architecture & Physics
The Median calculation requires maintaining a sorted view of the data window.
* **Inertia**: High. A single new data point rarely shifts the median significantly unless it crosses the middle threshold.
* **Stability**: Extremely robust against outliers. A price spike of 1000% has the same effect on the median as a spike of 1%.
* **Complexity**: $O(N \log N)$ per update due to sorting, where $N$ is the period. For typical trading periods ($N < 200$), this is negligible on modern CPUs.
## Mathematical Foundation
For a window of $N$ values $X = \{x_1, x_2, ..., x_N\}$ sorted in ascending order:
### 1. Odd Period
If $N$ is odd, the median is the middle element:
$$ \text{Median} = X_{(N+1)/2} $$
### 2. Even Period
If $N$ is even, the median is the average of the two middle elements:
O(N) per update. For large N, a dual-heap (min-heap + max-heap) O(log N) structure would be faster, but for typical periods (≤200) the sorted-array approach is cache-friendly.