The critical line algorithm is a method to compute the efficient frontier of a portfolio optimization problem.
The algorithm has been introduced by Markowitz in The Optimization of Quadratic Functions Subject to Linear Constraints and subsequently described in his book Portfolio Selection.
The algorithm is based on the observation that the efficient frontier is a piecewise linear function if expected return is plotted over expected variance. The critical line algorithm computes the turning points, e.g. the corners of the efficient frontier.
We are using the following sources
They suggested a method to avoid the expensive inversion of the covariance matrix.
Applying Markowitz's critical line algorithm
It turns out that implementing their method in Python is not significantly faster
than the explicit inversion of the covariance matrix relying on LAPACK via numpy.linalg.inv
.
We have initially started with their code published in An Open-Source Implementation of the Critical-Line Algorithm for Portfolio Optimization. We have updated their original code and covered it in tests. We have made a few noteworthy changes:
- Use a boolean numpy array to indicate whether a weight is free or blocked.
- Rewrote the computation of the first turning point.
- Isolated the computation of
$\lambda$ and the update of weights to make them testable. - Use modern and immutable dataclasses throughout.
The code is not part of the published package though. It is only used for testing purposes. We recommend it for educational purposes only.
In Avoiding the Downside: A Practical Review of the Critical Line Algorithm for Mean-Semivariance Portfolio Optimizatio Markowitz and a team of researchers from Hudson Bay Capital Management and Constantia Capital provide a step-by-step tutorial on how to implement the critical line algorithm.
We address a problem they oversaw: After finding the first starting point all variables might be blocked. We need to enforce that one variable is labeled as free even it sits on a boundary otherwise the matrix needed is singular.
Rather than using their involved construction of the sparse matrix to estimate the weights we bisect the weights into a free and a blocked part. We then use a linear solver to compute the weights only for the free part.
We alter some of their Python code. Our experiments to combine it with Niedermayer's ideas to accelerate the computation of the matrix inverses did not yet justify the additional complexity.
We assume you share already the love for Poetry. Once you have installed poetry you can perform
make install
to replicate the virtual environment we have defined in pyproject.toml and locked in poetry.lock.
We install JupyterLab on fly within the aforementioned virtual environment. Executing
make jupyter
will install and start the jupyter lab.