npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

portfolio-allocation

v0.0.11

Published

A JavaScript library to allocate and optimize financial portfolios.

Downloads

96

Readme

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 !

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