Exponential Gradient (EG)
Relevant source files
The following files were used as context for generating this wiki page:
The Exponential Gradient (EG) algorithm is an Online Portfolio Selection (OLPS) strategy implemented within the universal-portfolios library. It is based on the work of Helmbold et al. (1998) and belongs to the "Follow the Winner" category of algorithms. The primary goal of EG is to choose portfolio weights in every period to maximize final wealth by performing multiplicative updates to the asset weights based on their relative performance.
Sources: universal/algos/eg.py:10-16, README.md:20-22, README.md:38
In this project, EG is implemented as a subclass of the base Algo class, which provides a unified framework for executing online portfolio strategies. It uses a learning rate parameter, $\eta$ (eta), to control the volatility of weight updates.
Sources: universal/algos/eg.py:19, universal/algo.py:19
Core Logic and Implementation
Initialization
The EG class initializes with a specific learning rate and sets up an initial uniform distribution across all assets in the portfolio.
- Learning Rate ($\eta$): Defaults to 0.05. It controls how aggressively the weights shift in response to price changes.
- Initial Weights: The algorithm starts with a uniform portfolio where each asset has a weight of $1/m$, where $m$ is the number of assets.
Sources: universal/algos/eg.py:24-30, universal/algos/eg.py:32-34
Step Logic
The core update logic occurs in the step method, which is called at each time interval to calculate the next set of portfolio weights. The update formula is multiplicative:
$b_{new} = b_{last} \cdot \exp\left(\eta \cdot \frac{x}{\sum(x \cdot b_{last})}\right)$
where $x$ represents the price relatives and $b_{last}$ represents the previous weights. The resulting vector is then normalized so the sum of weights equals 1.
Sources: universal/algos/eg.py:36-38
Class Architecture
The following diagram illustrates the relationship between the EG algorithm and the base portfolio management framework.
The EG class implements the specific step and init_weights methods required by the Algo base class.
Sources: universal/algos/eg.py:19, universal/algo.py:19, universal/result.py:27
Data Flow and Execution
The execution of the EG algorithm follows a sequential process managed by the run method in the base class.
The algorithm processes absolute stock prices ($S$), converts them to price relatives based on the PRICE_TYPE (which defaults to "ratio" for many algos), and iteratively updates the weights stored in a weights DataFrame ($B$).
Sources: universal/algo.py:59-90, universal/algos/eg.py:36-38
Configuration and Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
eta | float | 0.05 | Learning rate. Higher values increase the volatility of weight updates. |
min_history | int | 0 | Minimum historical periods required before the algorithm begins trading. |
frequency | int | 1 | Trading frequency (e.g., trade every $n$ periods). |
Sources: universal/algos/eg.py:24-27, universal/algo.py:32-35
Performance Analysis
The algorithm's performance is encapsulated in the AlgoResult class. Key metrics derived from EG's output include:
- Total Wealth: The final wealth generated by the strategy.
- Sharpe Ratio: Annualized risk-adjusted return.
- Turnover: The rate of portfolio rebalancing.
- Drawdown: The maximum peak-to-trough decline.
Sources: universal/result.py:145-200, universal/result.py:270-280
Implementation Example
The algorithm can be executed using the quickrun tool provided in the project.
from universal import tools
from universal.algos import EG
# Load dataset and run EG with a learning rate of 0.5
data = tools.dataset("nyse_o")
tools.quickrun(EG(eta=0.5), data)
Sources: universal/algos/eg.py:41-43, universal/tools.py:73-82
Conclusion
Exponential Gradient (EG) is a robust "Follow the Winner" strategy that leverages multiplicative updates and a configurable learning rate to optimize portfolio weights. By inheriting from the standard Algo class, it integrates seamlessly with the library's performance measurement and visualization tools, allowing for easy comparison against benchmarks like Buy-and-Hold (BAH) or Constant Rebalanced Portfolios (CRP).
Sources: universal/algos/eg.py:10-16, README.md:38, universal/algo.py:19