Last active
June 15, 2021 09:33
-
-
Save lequant40/f1c2eca6625ec8a18fa947ab7c8a9330 to your computer and use it in GitHub Desktop.
Exemple of call to Portfolio Optimizer API, to compute the mean-variance minimum variance frontier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Compute the mean-variance minimum variance frontier, as a list of (portfolio volatility, portfolio return) pairs. | |
* | |
* @param {Array.<number>} returns, the assets returns, e.g. [0.01, 0.05] | |
* @param {Array.<Array.<number>>} covarianceMatrix, the covariance matrix of the assets, e.g. [[1, 0], [0, 1]] | |
* @param {number} portfolios, the number of portfolios to compute on the mean-variance minimum variance frontier, e.g. 25 | |
* | |
* @return {Array.<Array.<number>>}, the list of (portfolio volatility, portfolio return) pairs to be inserted into a Google Spreadsheet | |
**/ | |
function computeMinimumVarianceFrontier(returns, covarianceMatrix, portfolios) { | |
// Define the content of the request, with a JSON payload. | |
var data = { | |
"assets": covarianceMatrix.length, | |
"assetsReturns": returns.map(x => x[0]), // Arrays are provided by Google Sheets as 2 dimensional, so, the last dimension must be removed | |
"assetsCovarianceMatrix": covarianceMatrix, | |
"portfolios": portfolios | |
}; | |
// Request to Portfolio Optimizer | |
var response = portfolioOptimizerAPICall("/v1/portfolio/analysis/mean-variance/minimum-variance-frontier", data); | |
// Decode the response from Portfolio Optimizer | |
var out = []; | |
for (var i = 0; i < response.portfolios.length; ++i) { | |
// | |
var portfolio = response.portfolios[i]; | |
// | |
var portfolioReturn = portfolio.portfolioReturn; | |
var portfolioVolatility = portfolio.portfolioVolatility; | |
var assetsWeights = portfolio.assetsWeights; | |
// | |
out.push([portfolioVolatility, portfolioReturn]); | |
} | |
// Return the decoded response to the sheet | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment