Changelog)
PortfolioAllocation (PortfolioAllocation is a JavaScript library designed to help constructing financial portfolios made of several assets: bonds, commodities, cryptocurrencies, currencies, exchange traded funds (ETFs), mutual funds, stocks...
When constructing such portfolios, one of the main problems faced is to determine the proportion of the different assets to hold.
PortfolioAllocation solves this problem using mathematical optimization algorithms.
Do not hesitate to report any bug / request additional features !
Foreword / Project Status
I initially developed the PortfolioAllocation library to allow the optimization of financial portfolios within Google Sheets.
Unfortunately, I quickly noticed two issues:
- The integration of PortfolioAllocation inside a Google Sheet requires stronger IT skills than I anticipated
- The execution of PortfolioAllocation algorithms on Google Sheets servers is 1) slow and 2) severely time constrained
For these reasons, I decided to stop developing the "vanilla" PortfolioAllocation library and replaced it with Portfolio Optimizer, a Web API to analyze and optimize financial portfolios.
As the integration of a Web API inside Google Sheets is much easier than the integration of a third party JavaScript library, this proved a wise choice.
TL;RD: If you need a software library to optimize financial portfolios within Google Sheets, please use the Portfolio Optimizer Web API.
Features
- Compatible with Google Sheets
- Compatible with any browser supporting ECMAScript 5 for front-end development
- Compatible with Node.js for back-end development
- Code continuously tested and integrated by Travis CI
- Code documented for developers using JSDoc
Usage
Usage in Google Sheets
Note: Examples of how to integrate PortfolioAllocation in Google Sheets are provided in this spreadsheet.
First, make the PortfolioAllocation functions available in your spreadsheet script:
- (Recommended) Import PortfolioAllocation as any other external Google Apps Script library, using the Script ID 1cTTAt3VZRZKptyhXtjF3EK-jKagdTUl6t29Pc7YidrC5m5ABR6LUy8sC
- (Alternative) Copy/paste the JavaScript files from the dist/gs directory
Then, you can call these functions your preferred way in your spreadsheet script.
Here is an example through a wrapper function, to which spreadsheet data ranges (e.g. A1:B3) can be provided directly:
function computeERCPortfolioWeights(covarianceMatrix) {
// Note: The input range coming from the spreadsheet is directly usable.
// Compute the ERC portfolio weights
var ercWeights = PortfolioAllocation.equalRiskContributionWeights(covarianceMatrix);
// Return them to the spreadsheet
return ercWeights;
}
Usage in a browser
Note: PortfolioAllocation is delivered through the CDN jsDelivr, at this URL.
Include PortfolioAllocation minified source file in an HTML page, and you are done:
<script src="https://cdn.jsdelivr.net/npm/portfolio-allocation/dist/portfolio_allocation.dist.min.js" type="text/javascript"></script>
<script type="text/javascript">
var w = PortfolioAllocation.riskBudgetingWeights([[0.1,0], [0,0.2]], [0.25, 0.75]);
</script>
Usage with Node.js
Note: PortfolioAllocation is delivered as the npm package portfolio-allocation.
First, declare PortfolioAllocation as a dependency in your project's package.json
file, using the package name portfolio-allocation.
Then, this is standard Node.js:
var PortfolioAllocation = require('portfolio-allocation');
var w = PortfolioAllocation.riskBudgetingWeights([[0.1,0], [0,0.2]], [0.25, 0.75]);
Included algorithms
Portfolio allocation and optimization algorithms
-
Equal weights (EW)
Analyzed by Victor DeMiguel and al. in their research paper Optimal Versus Naive Diversification: How Inefficient is the 1/N Portfolio Strategy?. -
Inverse volatility (IV)
Described by Raul Leote de Carvalho and al. in the research paper Demystifying Equity Risk-Based Strategies: A Simple Alpha Plus Beta Description. -
Equal risk contributions (ERC) and risk budgeting (RB)
Extensively studied by Thierry Roncalli and al in misc. research papers (The properties of equally weighted risk contribution portfolios, Managing Risk Exposures Using the Risk Budgeting Approach, Constrained Risk Budgeting Portfolios...). -
Equal risk bounding (ERB)
Described in the research paper Equal Risk Bounding is better than Risk Parity for portfolio selection by Francesco Cesarone and Fabio Tardella, the ERB portfolio is an ERC portfolio possibly not containing all the assets in the considered universe. -
Cluster risk parity (CRP)
Discovered by David Varadi and Michael Kapler, the CRP portfolio combines the usage of a clustering algorithm (for instance, the Fast Threshold Clustering Algorithm - FTCA - of David Varadi) with the ERC portfolio. -
Most diversified portfolio (MDP)
Introduced in the research paper Toward Maximum Diversification by Yves Choueifaty and al., it maximizes what the authors call the diversification ratio, which is the weighted average of the assets volatilities divided by the portfolio total volatility. -
Minimum correlation algorithm (MCA)
Discovered by David Varadi, the MCA portfolio is meant to be an approximation of the MDP portfolio. -
Mean-variance optimization (MVO)
Based on the modern portfolio theory described by Harry M. Markowitz in numerous articles and books (Portfolio Selection: Efficient Diversification of Investments...), the portfolio obtained through mean-variance optimization is mean-variance efficient, that is, for a given level of return, it possesses the lowest attainable volatility and for a given level of volatility, it possesses the highest attainable return. -
Global minimum variance (GMV)
The leftmost portfolio on the mean-variance efficient frontier, the GMV portfolio possesses the smallest attainable volatility among all the mean-variance efficient portfolios. -
Proportional minimum variance algorithm (MVA)
Discovered by David Varadi, the MVA portfolio is meant to be an approximation of the GMV portfolio. -
Minimax portfolio
Introduced by Martin Young in the research paper A Minimax Portfolio Selection Rule with Linear Programming Solution, the minimax portfolio uses the minimum return as a measure of risk instead of the variance as in the Markowitz framework. -
Random portfolio
Random portfolios are generally used to benchmark the performances of portfolio allocation and optimization algorithms, as pioneered by Ronald J. Surz in the article Portfolio Opportunity Distributions and latter complemented by Patrick Burns in the article Random Portfolios for Performance Measurement. -
Maximum Sharpe ratio (MSR), a.k.a. (Lintner) tangency portfolio
Introduced by John Lintner in the research paper The Valuation of Risk Assets and the Selection of Risky Investments in Stock Portfolios and Capital Budgets, the MSR portfolio possesses the highest Sharpe ratio among all the mean-variance efficient portfolios. -
Random subspace mean-variance optimization (RSO-MVO)
Discovered by David Varadi and formally studied by Benjamin J. Gillen in the research paper Subset Optimization for Asset Allocation, the RSO-MVO portfolio combines the usage of a random subspace method with a mean-variance optimization method. -
Minimum tracking error portfolio, a.k.a. index tracking portfolio
The index tracking portfolio aims at replicating the performances of a given stock market index, or more generally of a given benchmark, with a limited number of its constituents thanks to cardinality constraints. -
Best constantly rebalanced portfolio (BCRP)
The BCRP portfolio is a portfolio determined in hindsight to benchmark the performances of online portfolio selection algorithms.
Misc. other algorithms
-
Post-processing of numerical portfolio weights
The weights obtained through a portfolio optimization algorithm (e.g. w = 0.123456789) need in practice to be either rounded off (e.g. w = 0.12) or converted into an integer number of shares (e.g. q = 10 shares, or q = 2 lots of 100 shares). -
Computation of the mean-variance efficient frontier
The continuous set of all mean-variance efficient portfolios (the mean-variance efficient frontier) as well as its generating discrete set (the set of corner portfolios) can both be efficiently computed thanks to a specialized algorithm developed by Harry M. Markowitz: the critical line method. -
Computation of the nearest portfolio on the mean-variance efficient frontier
Thanks to the Markowitz's critical line method, it is possible to exactly compute the nearest mean-variance efficient portfolio of any given portfolio. -
Generation of perturbed mean vectors, variances and correlation matrices
As demonstrated in The effect of errors in means, variances, and covariances on optimal portfolio choice by Vijay Chopra and William Ziemba, the impact of estimation errors in expected returns, variances and correlation matrices on the output of a portfolio optimization algorithm can be significant, so that it is useful to have an algorithm to perturb these quantities for portfolio weights sensitivity analysis. -
Generic random subspace optimization
A direct extension of the RSO-MVO method, allowing to use the random subspace optimization method with any portfolio optimization method. -
Generic numerical optimization
When no specialized algorithm exist to solve a particular portfolio optimization problem, it is always possible to use a generic numerical optimization algorithm (e.g., grid search on the simplex). -
Random generation of mean vectors, variances and correlation matrices
When implementing portfolio optimization algorithms, the capability to generate random mean vectors, random variances and random correlation matrices can be of great help. -
Computation of shrinkage estimators for mean vectors and covariance matrices
Shrinkage estimators for mean vectors can help to reduce the estimation errors in expected returns as observed in DeMiguel et al. Size Matters: Optimal Calibration of Shrinkage Estimators for Portfolio Selection, and shrinkage estimators for covariance matrices like Ledoit-Wolf's A Well-Conditioned Estimator for Large-Dimensional Covariance Matrices provide an elegant solution to the problem of the ill-conditioning and non-invertibility of sample covariance matrices. -
Repairing indefinite or positive semi-definite correlation matrices
With certain use-cases (e.g. alteration of correlation pairs for stress testing), correlation matrices might lose their positive (semi) definiteness, which is possible to recover thanks for instance to the spectral method described by Rebonato and Jaeckel in their paper The Most General Methodology to Create a Valid Correlation Matrix for Risk Management and Option Pricing Purposes or to other methods like the computation of the nearest correlation matrix addressed by Nicholas J. Higham in his paper Computing the nearest correlation matrix - A problem from Finance.
Documentation
The code is heavily documented, using JSDoc.
That being said, the documentation is rather for developer's usage, so that in case of trouble to use any algorithm, do not hesitate to ask for support !
Contributing
Github...
Fork the project fromGrunt dependencies and command line
Install thenpm install
npm install -g grunt-cli
Develop...
Compile and test
- The following commands generates the files to be used inside a browser or with Node.js in the
dist
directory:
grunt deliver-dev
grunt deliver-dist
- The following command generates the files to be used in Google Sheets in the
dist\gs
directory:
grunt deliver-gs