Kelly Criterion & ONS
Relevant source files
The following files were used as context for generating this wiki page:
- universal/algos/kelly.py
- universal/algos/ons.py
- universal/algos/mpt.py
- universal/tools.py
- universal/algo.py (Implicitly utilized via inheritance structures in the provided snippets)
- universal/result.py
The Kelly Criterion and Online Newton Step (ONS) algorithms represent advanced optimization strategies within the universal-portfolios library. While Kelly focus on maximizing the expected logarithm of wealth based on historical mean and variance, ONS utilizes a second-order optimization approach (Newton's method) to adapt portfolio weights online. Both algorithms are integrated into the project's standard Algo framework, allowing for seamless backtesting and performance analysis using unified tools.
These strategies are part of the broader Online Portfolio Selection (OLPS) toolkit, which aims to choose portfolio weights in every period to maximize final wealth.
Sources: README.md:14-17, universal/algos/kelly.py:12-16, universal/algos/ons.py:13-18
Kelly Criterion Algorithm
The Kelly class implements fractioned betting based on the Kelly criterion. It operates by estimating the mean return ($\mu$) and the covariance matrix ($\Sigma$) of assets to determine the optimal allocation that maximizes the logarithmic growth of capital.
Technical Implementation
The algorithm supports several configuration options, including regularization for the covariance matrix and leverage constraints. It pre-calculates correlations and means during the init_step and performs weight optimization during each step.
The diagram above shows the logic flow for determining portfolio weights in the Kelly algorithm.
Sources: universal/algos/kelly.py:49-106
Key Components
| Parameter | Type | Default | Description |
|---|---|---|---|
window | float/int | inf | Window for calculating mean and variance. |
fraction | float | 1.0 | Fraction of Kelly weights (e.g., 0.5 for half-Kelly). |
long_only | bool | False | Restricts weights to positive values only. |
mu_estimate | bool | False | If True, mean is estimated proportional to historical variance. |
gamma | float | 0.0 | Penalty parameter for changing weights. |
Sources: universal/algos/kelly.py:32-42
Online Newton Step (ONS)
The ONS class implements a second-order optimization algorithm for portfolio management. Unlike Kelly, which relies heavily on historical moving statistics, ONS updates its internal state (matrix $A$ and vector $b$) incrementally based on the gradient of the returns.
State Update Logic
At each time step, ONS calculates the gradient of the logarithmic wealth. It maintains a matrix $A$ (related to the Hessian) and a vector $b$ to track the cumulative gradient information. The weights are then projected into a simplex using quadratic programming.
This sequence illustrates the incremental update and projection process used by ONS.
Sources: universal/algos/ons.py:42-56
Projections and Parameters
ONS utilizes the cvxopt library to perform quadratic programming for projections induced by the matrix norm of $A$.
- Delta ($\delta$): Controls the projection scaling.
- Beta ($\beta$): Controls the gradient update weight.
- Eta ($\eta$): A mixing parameter that blends the optimized weights with a uniform portfolio to ensure stability.
Sources: universal/algos/ons.py:25-35, universal/algos/ons.py:58-72
Comparison with Modern Portfolio Theory (MPT)
While Kelly and ONS are specific algorithms, the project also provides a generalized MPT class. There is a technical overlap between Kelly and MPT, particularly when the MPT risk aversion parameter $q$ is set to specific values (e.g., $q=2$ is equivalent to full-Kelly).
| Feature | Kelly Class | ONS Class | MPT Class |
|---|---|---|---|
| Primary Method | Mean-Variance Optimization | Online Gradient Descent | Efficient Frontier Optimization |
| Optimization | Analytic / Markowitz Solver | Quadratic Programming | SLSQP / QP / Sharpe Max |
| Key Metric | Log wealth maximization | Regret minimization | Risk-adjusted return |
| Dependencies | tools.opt_markowitz | cvxopt.solvers | scipy.optimize, sklearn |
Sources: universal/algos/kelly.py:93-100, universal/algos/ons.py:58-72, universal/algos/mpt.py:17-48
Performance Analysis & Tools
Both algorithms return an AlgoResult object which provides comprehensive metrics for evaluation.
Core Metrics
- Total Wealth: The cumulative product of period returns.
- Sharpe Ratio: Annualized risk-adjusted return calculated from log returns.
- Drawdowns: Maximum percentage loss and longest drawdown period.
- Turnover: Annualized measure of weight changes, used to estimate transaction cost impact.
Sources: universal/result.py:136-228
Analytical Visualizations
The library provides built-in methods to visualize the behavior of these algorithms:
- Kelly Fraction Plot: The
Kelly.plot_fractionmethod allows users to see how different fraction values (from 0 to 2) impact total wealth. - Equity Curves: Standardized plotting via
AlgoResult.plotto compare strategy performance against benchmarks like UCRP (Uniform Constant Rebalanced Portfolio).
Sources: universal/algos/kelly.py:108-121, universal/result.py:270-325
Conclusion
The Kelly Criterion and ONS implementations provide the universal-portfolios library with robust, mathematically-grounded strategies for dynamic asset allocation. Kelly offers a traditional statistical approach focused on long-term growth rates, while ONS provides a modern online learning alternative that adapts via first and second-order gradient information. Both benefit from the library's integrated optimization solvers and standardized result reporting.
Sources: universal/algos/kelly.py:12-16, universal/algos/ons.py:13-18