Git Product home page Git Product logo

portfolio_allocation_js's Introduction

PortfolioAllocation v0.0.7 (Changelog)

Travis Build Status

PortfolioAllocation is a JavaScript library designed to help solving the mathematical problem of portfolio allocation and optimization.

In layman's terms, imagine you are faced with the problem of deciding how to invest your available funds into different financial instruments: stocks, bonds, mutual funds, exchange traded funds, cryptocurrencies...

A library such as PortfolioAllocation allows you to easily determine which proportion of which instrument you need to hold so that your available funds are allocated the best possible way.

Do not hesitate to report any bug / request additional features !

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 internal developers using JSDoc
  • Code documented for users using GitHub Pages

Included algorithms

Portfolio allocation and optimization algorithms

Misc. helper algorithms

  • Portfolio weights rounding
    The portfolio weights obtained through a portfolio optimization algorithm are usually provided with several digits after the decimal point. For any practical usage, these theoretical weights need to be rounded off, which can be done thanks to the algorithm described in the research paper Rounding on the standard simplex: Regular grids for global optimization from Immanuel M. Bomze and al..

  • Mean-variance efficient frontier and corner portfolios computation
    The set of all mean-variance efficient portfolios (the mean-variance efficient frontier), as well its generating discrete set (the set of corner portfolios) can be efficiently computed thanks to a specialized algorithm developped by Harry M. Markowitz: the critical line method.

  • Generic random subspace optimization method
    As a direct extension of the RSO-MVO method, a generic random subspace optimization method has been implemented and is usable with any portfolio optimization method.

  • Generic numerical optimization algorithms
    When no specialized numerical algorithm exist to solve a particular portfolio optimization problem, a slow-but-always-working solution is to use generic numerical optimization algorithms instead (e.g., grid search on the simplex).

Usage

Usage in Google Sheets

If you would like to use PortfolioAllocation in Google Sheets, you can either:

or:

  • Import the JavaScript files from the dist/gs directory into your spreadsheet script

In both cases, calling the PortfolioAllocation functions is then accomplished your preferred way:

  • Using a wrapper function in your spreadsheet script, directly accessible from your spreadsheet, to which you can provide standard data ranges (A1:B3...), e.g.:
function computeERCPortfolioWeights(covarianceMatrix) {
  // Note: The input range coming from the spreadsheet and representing a covariance matrix
  // is directly usable.
    
  // Compute the ERC portfolio weights
  var ercWeights = PortfolioAllocation.equalRiskContributionWeights(covarianceMatrix);
  
  // Return them to the spreadsheet
  return ercWeights;
}
  • Using pure Google Apps Script functions, for which computations are allowed by Google to last longer, e.g.:
function computeERCPortfolioWeights() {
  // Adapted from https://developers.google.com/apps-script/reference/spreadsheet/sheet#getrangerow-column-numrows
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];
 var range = sheet.getRange(1, 1, 3, 3); // A1:B3
 var values = range.getValues();

 // Convert the above range into an array
 var covarianceMatrix = [];
 for (var row in values) {
   for (var col in values[row]) {
     covarianceMatrix.push(values[row][col]);
   }
 }
 
  // Compute the ERC portfolio weights
  var ercWeights = PortfolioAllocation.equalRiskContributionWeights(covarianceMatrix);
  
  // Do something with them (use them in a computation, write them back to the spreadsheet, etc.)
  ...
}

You can find examples of PortfolioAllocation usage in this spreadsheet.

Usage inside a browser

If you would like to use PortfolioAllocation inside a browser you can download its source code and/or its minified source code.

You then just need to include this code in an HTML page to use it, e.g.:

<script src="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]);
  // w = [0.44948974243459944, 0.5505102575654006]
</script>

Usage with Node.js

If you would like to use PortfolioAllocation with Node.js, you simply need to declare it as a dependency of your project in your package.json file.

Then, this is standard Node.js code, e.g.:

var PortfolioAllocation = require('portfolio-allocation');
...
var w = PortfolioAllocation.riskBudgetingWeights([[0.1,0], [0,0.2]], [0.25, 0.75]);
// w = [0.44948974243459944, 0.5505102575654006]

Documentation

A complete documentation, including code examples, can be found on the GitHub Pages associated to this repository.

How to contribute ?

Fork the project from Github...

Instal the Grunt dependencies and command line

npm install
npm install -g grunt-cli

Develop...

Compile

  • The following command generates the files to be used inside a browser or with Node.js in the dist directory:
grunt deliver
  • The following command generates the files to be used in Google Sheets in the dist\gs directory:
grunt deliver-gs

Test

Any of the following two commands run the QUnit unit tests contained in the test directory on the generated file dist\portfolio_allocation.dev.min.js:

npm test
grunt test

Submit a pull-request...

License

MIT License

portfolio_allocation_js's People

Contributors

lequant40 avatar masterjames avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.