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 !
Features
 Compatible with Google Sheets
 Compatible with any browser supporting ECMAScript 5 for frontend development
 Compatible with Node.js for backend 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 1cTTAt3VZRZKptyhXtjF3EKjKagdTUl6t29Pc7YidrC5m5ABR6LUy8sC
 (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:
{ // Note: The input range coming from the spreadsheet is directly usable. // Compute the ERC portfolio weights var ercWeights = PortfolioAllocation; // 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:
Usage with Node.js
Note: PortfolioAllocation is delivered as the npm package portfolioallocation.
First, declare PortfolioAllocation as a dependency in your project's package.json
file, using the package name portfolioallocation.
Then, this is standard Node.js:
var PortfolioAllocation = ;var w = PortfolioAllocation;
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 RiskBased 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. 
Meanvariance 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 meanvariance optimization is meanvariance 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 meanvariance efficient frontier, the GMV portfolio possesses the smallest attainable volatility among all the meanvariance 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 meanvariance efficient portfolios. 
Random subspace meanvariance optimization (RSOMVO)
Discovered by David Varadi and formally studied by Benjamin J. Gillen in the research paper Subset Optimization for Asset Allocation, the RSOMVO portfolio combines the usage of a random subspace method with a meanvariance 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

Postprocessing 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 meanvariance efficient frontier
The continuous set of all meanvariance efficient portfolios (the meanvariance 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 meanvariance efficient frontier
Thanks to the Markowitz's critical line method, it is possible to exactly compute the nearest meanvariance 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 RSOMVO 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 LedoitWolf's A WellConditioned Estimator for LargeDimensional Covariance Matrices provide an elegant solution to the problem of the illconditioning and noninvertibility of sample covariance matrices. 
Repairing indefinite or positive semidefinite correlation matrices
With certain usecases (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 gruntcli
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 deliverdev
grunt deliverdist
 The following command generates the files to be used in Google Sheets in the
dist\gs
directory:
grunt delivergs