Confidence Weighted Mean Reversion (CWMR)
Relevant source files
The following files were used as context for generating this wiki page:
Confidence Weighted Mean Reversion (CWMR) is an online portfolio selection algorithm implemented within the universal-portfolios library. It belongs to the category of "Follow the Loser" strategies, which exploit the mean reversion phenomenon in financial markets. The algorithm models the portfolio distribution as a Gaussian distribution, maintaining both a mean (the portfolio weights) and a covariance matrix representing the confidence in those weights.
The CWMR strategy aims to choose portfolio weights in every period to maximize final wealth by adjusting to price relatives. It is based on the work of B. Li, S. C. H. Hoi, P.L. Zhao, and V. Gopalkrishnan (2013). Within this library, CWMR inherits from the base Algo class and provides a structured way to execute mean reversion trading logic alongside other strategies like PAMR and OLMAR.
Sources: universal/algos/cwmr.py:12-20, README.md:23-45
Architecture and Components
The CWMR implementation is structured as a class hierarchy, providing a standard version and a variance-based approximation (CWMR_VAR). It integrates with the library's utility tools for simplex projection and performance analysis.
Class Structure
The following diagram illustrates the relationship between the CWMR classes and the base algorithm framework.
The diagram shows how CWMR extends the base Algo class, implementing specific initialization and update logic, while CWMR_VAR overrides the update step with an approximation.
Sources: universal/algos/cwmr.py:12-132, universal/algo.py
Key Classes and Functions
| Component | Description |
|---|---|
CWMR | The main implementation of the Confidence Weighted Mean Reversion algorithm. |
CWMR_VAR | A variant of CWMR that uses an approximation for the optimization problem. |
init_step | Initializes the covariance matrix $\Sigma$ as a diagonal matrix $\frac{I}{m^2}$. |
step | Executes the daily iteration: calculates intermediate variables and updates the portfolio. |
update | Computes the Lagrange multiplier $\lambda$ and updates the mean $\mu$ and covariance $\Sigma$. |
tools.simplex_proj | Projects updated weights back onto the unit simplex to ensure valid portfolio weights. |
Sources: universal/algos/cwmr.py:38-129, universal/tools.py:270-290
Logic and Data Flow
CWMR operates on "price relatives" (ratios of current price to previous price). The algorithm maintains a Gaussian distribution $\mathcal{N}(\mu, \Sigma)$ over the portfolio weights.
Execution Flow
The sequence of operations during a single time step is as follows:
The flow shows the transition from raw input data to normalized portfolio weights using mean reversion constraints.
Sources: universal/algos/cwmr.py:48-73
The Update Mechanism
The core of CWMR lies in the update method, which solves for a new distribution that satisfies the mean reversion constraint while staying close to the previous distribution.
- Intermediate Variable Calculation: Calculates $M$ (portfolio return) and $V$ (return variance).
- Lagrange Multiplier ($\lambda$): The algorithm solves a quadratic equation for $\lambda$ based on the threshold
epsand confidencetheta. - Portfolio Mean ($\mu$) Update: $\mu_{t+1} = \mu_t - \lambda \Sigma_t (x_t - \bar{x}_t) / M_t$.
- Covariance ($\Sigma$) Update: The confidence matrix is updated using the inverse of the sum of the previous inverse and a term involving the squared price relatives.
Sources: universal/algos/cwmr.py:75-103
Configuration Parameters
CWMR requires specific parameters to control the aggressiveness of the mean reversion and the confidence level.
| Parameter | Type | Default | Description |
|---|---|---|---|
eps | float | -0.5 | Mean reversion threshold. Expected return must be lower than this. |
confidence | float | 0.95 | Confidence parameter (0 to 1) for the profitable portfolio. |
PRICE_TYPE | string | "ratio" | The type of price data the algorithm expects (price relatives). |
Sources: universal/algos/cwmr.py:25-36
Integration and Usage
CWMR results are returned as AlgoResult objects, which provide metrics such as Sharpe ratio, turnover, and total wealth.
from universal import tools
from universal.algos import CWMR
# Run CWMR with default parameters
# eps=-0.5, confidence=0.95
algo = CWMR()
result = algo.run(data)
# Print performance summary
print(result.summary())
Sources: universal/algos/cwmr.py:131-132, universal/result.py:245-275
Conclusion
The CWMR implementation provides a robust framework for testing confidence-weighted strategies. By maintaining a covariance matrix, it accounts for the uncertainty in asset performance, distinguishing it from simpler mean reversion strategies like PAMR. It is a critical component of the library's "Follow the Loser" algorithm suite.
Sources: README.md:40-45, universal/algos/cwmr.py:12-20