Robust Median Reversion (RMR)
Relevant source files
The following files were used as context for generating this wiki page:
Robust Median Reversion (RMR) is an online portfolio selection strategy that exploits the mean-reversion property of financial assets using a robust L1-median estimator. It is designed to be more resistant to outliers in price data compared to standard moving average methods. Within the universal-portfolios library, RMR is implemented as a specialized extension of the OLMAR (On-Line Portfolio Selection with Moving Average Reversion) algorithm.
Sources: universal/algos/rmr.py:16-22, universal/algos/olmar.py:8-13
Architecture and Inheritance
RMR is built upon the Algo base class, which provides the standard framework for sequential portfolio weight calculation. It specifically inherits from OLMAR, reusing its weight update logic while overriding the price prediction mechanism to use a robust median instead of a simple or exponential moving average.
Sources: universal/algos/rmr.py:25, universal/algo.py:15-27
Class Hierarchy
The following diagram illustrates the relationship between RMR and the core algorithm classes.
A diagram showing the inheritance structure where RMR extends OLMAR, which in turn extends the base Algo class.
Sources: universal/algos/rmr.py:25, universal/algos/olmar.py:15, universal/algo.py:15
Logic and Logic Flow
The RMR algorithm operates by predicting next-period price relatives using a geometric L1-median of historical prices. Once the prediction is made, it uses a passive-aggressive update scheme to adjust the portfolio weights.
Prediction Logic (L1-Median)
Unlike standard averages, the L1-median in RMR is calculated iteratively. The process continues until the relative change in the estimated median falls below a precision threshold, tau.
- Initialize the median estimate $y$ as the mean of the history.
- Iteratively update $y$ using a weighted average where weights are inversely proportional to the distance from the current estimate.
- Calculate the predicted price relative as $y / x$ (where $x$ is the current price).
Sources: universal/algos/rmr.py:48-56
Update Logic (Passive-Aggressive)
The weight update logic is inherited from OLMAR. It aims to satisfy the constraint that the expected return based on the prediction must meet a threshold eps, while minimizing the deviation from the previous weights.
The flow of the RMR step function, highlighting the iterative prediction and the subsequent weight update inherited from OLMAR.
Sources: universal/algos/rmr.py:48-56, universal/algos/olmar.py:65-80
Implementation Details
Key Parameters
The RMR algorithm uses specific parameters to control its behavior and computational cost.
| Parameter | Type | Default | Description |
|---|---|---|---|
window | int | 5 | The lookback window for historical prices. |
eps | float | 10.0 | The return constraint for new weights ($x \cdot w \geq eps$). |
tau | float | 0.001 | Precision threshold for the iterative L1-median calculation. |
Sources: universal/algos/rmr.py:38-46
Core Functions
| Function | File | Description |
|---|---|---|
predict | rmr.py | Implements the robust L1-median estimation to predict future price relatives. |
update | olmar.py | Adjusts weights based on prediction and eps constraint. |
simplex_proj | tools.py | Projects updated weights back onto a valid simplex (weights sum to 1, all $\geq 0$). |
run | algo.py | Orchestrates the sequential execution of the algorithm over a dataset. |
Sources: universal/algos/rmr.py:48, universal/algos/olmar.py:65, universal/tools.py:284, universal/algo.py:99
Execution and Results Analysis
RMR, like all algorithms in the project, produces an AlgoResult object upon completion. This object contains the weights calculated at each step and enables the calculation of performance metrics.
# RMR Step Implementation Detail
def predict(self, x, history):
y = history.mean()
y_last = None
while y_last is None or norm(y - y_last) / norm(y_last) > self.tau:
y_last = y
d = norm(history - y)
y = history.div(d, axis=0).sum() / (1.0 / d).sum()
return y / x
Sources: universal/algos/rmr.py:48-56
The performance of RMR can be evaluated using standard metrics provided by universal.result, such as the Sharpe Ratio, Annualized Return, and Maximum Drawdown.
Sources: universal/result.py:129, universal/result.py:168, universal/result.py:183
Summary
Robust Median Reversion (RMR) serves as a robust alternative to moving-average-based mean reversion strategies. By utilizing an L1-median estimator, it provides a prediction mechanism that is less sensitive to extreme price movements, while leveraging the established passive-aggressive update framework of the OLMAR class to maintain portfolio feasibility and stability.