cvxcla
is a Python package that implements the Critical Line Algorithm (CLA)
for portfolio optimization.
The CLA efficiently computes the entire efficient frontier for portfolio optimization
problems with linear constraints and bounds on the weights.
The Critical Line Algorithm was introduced by Harry Markowitz in The Optimization of Quadratic Functions Subject to Linear Constraints and further described in his book Portfolio Selection.
The algorithm is based on the observation that the efficient frontier is a piecewise linear function when expected return is plotted against expected variance. The CLA computes the turning points (corners) of the efficient frontier, allowing for efficient representation of the entire frontier.
I gave the plenary talk at EQD's Singapore conference.
- Efficient computation of the entire efficient frontier
- Support for linear constraints and bounds on portfolio weights
- Multiple implementations based on different approaches from the literature
- Visualization of the efficient frontier using Plotly
- Computation of the maximum Sharpe ratio portfolio
- Fully tested and documented codebase
pip install cvxcla
To set up a development environment:
-
Clone the repository:
git clone https://github.com/cvxgrp/cvxcla.git cd cvxcla
-
Create a virtual environment and install dependencies:
make install
This will:
- Install the uv package manager
- Create a Python 3.12 virtual environment
- Install all dependencies from pyproject.toml
Here's a simple example of how to use cvxcla
to compute the efficient frontier:
import numpy as np
from cvx.cla import CLA
from cvx.cla.plot import plot_frontier
# Define your portfolio problem
n = 10 # Number of assets
mean = np.random.randn(n) # Expected returns
cov = np.random.randn(n, n)
covariance = cov @ cov.T # Covariance matrix
lower_bounds = np.zeros(n) # No short selling
upper_bounds = np.ones(n) # No leverage
# Create a CLA instance
cla = CLA(
mean=mean,
covariance=covariance,
lower_bounds=lower_bounds,
upper_bounds=upper_bounds,
A=np.ones((1, n)), # Fully invested constraint
b=np.ones(1)
)
# Access the efficient frontier
frontier = cla.frontier
# Plot the efficient frontier
fig = plot_frontier(frontier, volatility=True)
fig.show()
# Get the maximum Sharpe ratio portfolio
max_sharpe_ratio, max_sharpe_weights = frontier.max_sharpe
print(f"Maximum Sharpe ratio: {max_sharpe_ratio}")
print(f"Weights for maximum Sharpe ratio portfolio: {max_sharpe_weights}")
The package includes implementations based on several key papers:
They suggested a method to avoid the expensive inversion
of the covariance matrix in Applying Markowitz's critical line algorithm.
Our testing shows that in Python, this approach is not significantly
faster than explicit matrix inversion using LAPACK via numpy.linalg.inv
.
We initially started with their code published in An Open-Source Implementation of the Critical-Line Algorithm for Portfolio Optimization. We've made several improvements:
- Using boolean numpy arrays to indicate whether a weight is free or blocked
- Rewriting the computation of the first turning point
- Isolating the computation of Ξ» and weight updates to make them testable
- Using modern and immutable dataclasses throughout
Our updated implementation is included in the tests but not part of cvxcla package. We use it to verify our results and include it for educational purposes.
In Avoiding the Downside: A Practical Review of the Critical Line Algorithm for Mean-Semivariance Portfolio Optimization, Markowitz and researchers from Hudson Bay Capital Management and Constantia Capital present a step-by-step tutorial.
We address a problem they overlooked: after finding the first starting point, all variables might be blocked. We enforce that one variable labeled as free (even if it sits on a boundary) to avoid a singular matrix.
Rather than using their sparse matrix construction, we bisect the weights into free and blocked parts and use a linear solver for the free part only.
Run the test suite with:
make test
Format and lint the code with:
make fmt
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Run the tests to make sure everything works (
make test
) - Format your code (
make fmt
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.