PortfolioAllocation JavaScript Library

repository·master·Indexed 41 days ago

https://github.com/lequant40/portfolio_allocation_js

A JavaScript library (v0.0.11) for constructing and optimizing financial portfolios using mathematical optimization algorithms. It supports a wide range of strategies including Mean-Variance Optimization (MVO), Equal Risk Contributions (ERC), and Maximum Sharpe Ratio (MSR), as well as utilities for shrinkage estimators and matrix repairing. Compatible with Node.js, web browsers via CDN, and Google Sheets.

Tokens
1.6K
Snippets
4
Records
7
Agent score
2%

What's inside PortfolioAllocation

  1. Miscellaneous portfolio algorithms and utilities

    master

    Beyond standard allocation, the library provides several mathematical utilities for portfolio management:

    Mathematical Utilities

    • Post-processing: Rounding numerical weights or converting them into integer share counts.
    • Efficient Frontier: Computation of the mean-variance efficient frontier and its corner portfolios using the critical line method.
    • Nearest Portfolio: Computing the nearest mean-variance efficient portfolio to a given portfolio.
    • Sensitivity Analysis: Generation of perturbed mean vectors, variances, and correlation matrices to test estimation error impacts.
    • Optimization Extensions: Generic random subspace optimization and generic numerical optimization (e.g., grid search on the simplex).
    • Data Generation: Random generation of mean vectors, variances, and correlation matrices.
    • Shrinkage Estimators: Computation of shrinkage estimators for mean vectors and covariance matrices (e.g., Ledoit-Wolf) to reduce estimation errors.
    • Matrix Repairing: Repairing indefinite or positive semi-definite correlation matrices using spectral methods or nearest correlation matrix algorithms.
  2. Portfolio allocation and optimization algorithms

    master

    The library includes a wide range of algorithms for portfolio construction and optimization, categorized into two main groups:

    Portfolio allocation and optimization

    • Equal weights (EW): Naive diversification strategy (1/N).
    • Inverse volatility (IV): Risk-based strategy based on asset volatility.
    • Equal risk contributions (ERC) & Risk budgeting (RB): Strategies to ensure each asset contributes equally to the total risk.
    • Equal risk bounding (ERB): An ERC-based approach that may exclude some assets from the universe.
    • Cluster risk parity (CRP): Combines clustering algorithms (like FTCA) with ERC.
    • Most diversified portfolio (MDP): Maximizes the diversification ratio.
    • Minimum correlation algorithm (MCA): An approximation of the MDP portfolio.
    • Mean-variance optimization (MVO): Finds the efficient frontier (lowest volatility for a given return or highest return for a given volatility).
    • Global minimum variance (GMV): The portfolio with the lowest possible volatility on the efficient frontier.
    • Proportional minimum variance algorithm (MVA): An approximation of the GMV portfolio.
    • Minimax portfolio: Uses minimum return as the risk measure instead of variance.
    • Random portfolio: Used for benchmarking performance.
    • Maximum Sharpe ratio (MSR): Also known as the Lintner tangency portfolio; possesses the highest Sharpe ratio.
    • Random subspace mean-variance optimization (RSO-MVO): Combines random subspace methods with MVO.
    • Minimum tracking error portfolio: Also known as index tracking; aims to replicate a benchmark with cardinality constraints.
    • Best constantly rebalanced portfolio (BCRP): A hindsight-based benchmark for online selection algorithms.
  3. Use PortfolioAllocation in a browser

    master

    You can include PortfolioAllocation in a web application by loading the minified source file via the jsDelivr CDN. This makes the PortfolioAllocation object available globally in your scripts.

    <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>
  4. Setup and development environment

    master

    To contribute to the project or run the development pipeline, you need to install the Grunt dependencies and the Grunt CLI.

    Installation

    npm install
    npm install -g grunt-cli

    Build Commands

    Use Grunt to generate the distribution files for different environments:

    • Browser or Node.js: Generates files in the dist directory.
    • Google Sheets: Generates files in the dist\gs directory.
    grant deliver-dev
    grant deliver-dist
    grant deliver-gs
    npm install
    npm install -g grunt-cli
    
    # For Browser/Node.js
    grant deliver-dev
    grant deliver-dist
    
    # For Google Sheets
    grant deliver-gs
  5. Use PortfolioAllocation with Node.js

    master

    To use the library in a Node.js environment, install the portfolio-allocation package via npm and require it in your project.

    var PortfolioAllocation = require('portfolio-allocation');
    var w = PortfolioAllocation.riskBudgetingWeights([[0.1,0], [0,0.2]], [0.25, 0.75]);
  6. Integrate PortfolioAllocation in Google Sheets

    master

    To use PortfolioAllocation within Google Sheets, you must make its functions available in your spreadsheet script.

    There are two methods:

    1. Recommended: Import PortfolioAllocation as an external Google Apps Script library using the Script ID: 1cTTAt3VZRZKptyhXtjF3EK-jKagdTUl6t29Pc7YidrC5m5ABR6LUy8sC.
    2. Alternative: Copy and paste the JavaScript files from the dist/gs directory of the repository into your script.

    Once integrated, you can call functions directly. For example, you can create a wrapper function that accepts a spreadsheet data range (like A1:B3) as a covariance matrix and returns the calculated weights.

    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;
    }