Do a few classic portfolio optimizations using:
- CVXPY (paper), a modeling environment for convex optimization, supporting many back-end solvers.
- Data (mostly) from Prof. Aswath Damodaran and FRED
- Load asset return data from Damodaran website using
pd.read_excel
. - Load gold and GDP data from FRED using
pandas_datareader
module. - Manually fill in some missing data back to 1928.
- Compute covariance matrix, long-only efficient frontier, and transition map using historical data (see above). Also compute same outputs for 1972-present (post-gold standard) and 1983-present (post-inflation era).
- Compute long-short efficient frontier and transition map, adding a random short asset with 5% annualized negative return and 90% correlation to S&P, and adding a 150% gross exposure constraint.
- Compute an efficient frontier using a factor model, using a random set of returns for 1000 stocks and 10 random factor exposures and a random factor covariance matrix.
This follows the cvxpy tutorial but uses real historical data, and visualizes the full efficient frontier and transition map.
- Gold adds some value for most portfolios, except in most disinflationary environment at higher risk tolerances. (Of course TIPS may be a better inflation hedge but we don't have data back very far).
- If you can find good shorts and use leverage, you can supercharge returns.
You should be able to run directly in Google Colab.
Or, to run locally
-
git clone this repo, cd to repo directory
-
Install Anaconda
-
Create virtual environment
conda create -n portfolio_opt
conda activate portfolio_opt
pip install -r requirements.txt
or
conda env create -f environment.yaml
conda activate portfolio_opt
-
jupyter notebook
-
Run
Portfolio optimization.ipynb