(Translated by https://www.hiragana.jp/)
GitHub - lequant40/portfolio_allocation_js: A JavaScript library to allocate and optimize financial portfolios.
Skip to content

lequant40/portfolio_allocation_js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PortfolioAllocation (Changelog)

npm Travis Build Status Feature Requests

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:

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

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

Fork the project from Github...

Install the Grunt dependencies and command line

npm 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

Submit a pull-request...

License

MIT License