lequant40 / portfolio_allocation_js

A JavaScript library to allocate and optimize financial portfolios.
https://lequant40.github.io/portfolio_allocation_js/
MIT License
172 stars 32 forks source link

lambda_e is not defined when calling meanVarianceEfficientFrontierPortfolios #15

Open amoghkulkarnifr opened 8 months ago

amoghkulkarnifr commented 8 months ago

Hello @lequant40, I'm trying to use this library for a personal project and it's proving a very valuable learning exercise for understanding the relevant concepts in Portfolio Optimization space. Thanks a lot for making this resource open source!

I was hoping to get some clarification on a bug I'm running into while using method meanVarianceEfficientFrontierPortfolios from lib/allocation/mean-variance.js. Here are the details -

Describe the bug Getting the following error while using the method meanVarianceEfficientFrontierPortfolios -

ReferenceError: lambda_e is not defined
    at computeCornerPortfolios (portfolio_allocation.dist.js:15132:4)
    at new MeanVarianceEfficientFrontierCla (portfolio_allocation.dist.js:14596:26)
    at new MeanVarianceEfficientFrontierWrapper (portfolio_allocation.dist.js:16471:28)
    at self.meanVarianceEfficientFrontierPortfolios (portfolio_allocation.dist.js:17353:23)
    at getOptimalPortfolio (mvt-utils.ts?t=1699625436099:16:49)
    at App.tsx?t=1699626061247:25:17
    at commitHookEffectListMount (react-dom.development.js:23150:26)
    at commitPassiveMountOnFiber (react-dom.development.js:24926:13)
    at commitPassiveMountEffects_complete (react-dom.development.js:24891:9)
    at commitPassiveMountEffects_begin (react-dom.development.js:24878:7)

To Reproduce I am trying to run this method on dummy data, like so -

let stockData = [[0.05, 0.01, 0.01], [0.1, -0.03, 0.05],]

let covMat = PortfolioAllocation.covarianceMatrix(stockData, {
  assumeZeroMean: false,
}).toRowArray();

let returns = [];

for (let s_i = 0; s_i < stockData.length; s_i += 1) {
  let R_i = PortfolioAllocation.returns(stockData[s_i]);
  let returns_i = 1;
  for (let s_i_t = 0; s_i_t < stockData[s_i].length - 1; s_i_t += 1) {
    returns_i *= 1 + R_i[s_i_t];
  }

  returns.push(returns_i - 1);
}

let mvEfficientFrontier =
  PortfolioAllocation.meanVarianceEfficientFrontierPortfolios(
    returns,
    covMat,
    { nbPortfolios: 10 }
  );

and got the aforementioned error.

Expected behavior meanVarianceEfficientFrontierPortfolios method should return n portfolios (n=10 in this case) when called.

I would really appreciate it if you could look into this and provide some information. Thanks!

lequant40 commented 8 months ago

Hello @amoghkulkarnifr ,

Thanks for reporting this bug.

For reference, the covariance matrix and the asset returns are:

[0.0003555555555555556, 0.0008] [0.0008, 0.0028666666666666667]

[-0.7999999999999999, -0.4999999999999999]


Unfortunately, I put the public version of this lib on hiatus on my side, because I am focusing on its big brother > https://portfoliooptimizer.io/

So, if your personal project is compatible with the use of a Web API, I would encourage you to have a look to the API doc -> https://docs.portfoliooptimizer.io/index.html

(I confirmed that Portfolio Optimizer Web API is working with your example, the endpoint is -> https://docs.portfoliooptimizer.io/index.html#post-/portfolio/analysis/mean-variance/efficient-frontier)

Otherwise, please understand that I will need some time to have a look at this bug.

Cheers,

Roman

amoghkulkarnifr commented 8 months ago

Thank you for your reply, @lequant40.

I completely understand if this takes some time to get resolved, as I saw that the repo was not under active development recently. Just thought of reporting the bug regardless.

I also looked into https://portfoliooptimizer.io briefly, although wasn't sure if the free tier would be sufficient for my use case. (Sorry - would have loved to go for the premium tier, but in a bit of a cash crunch lately :(( )

Nonetheless, thanks again for your attention to this. Phenomenal work on this library overall. Hoping to see it being usable in the future.

amoghkulkarnifr commented 8 months ago

For reference, the covariance matrix and the asset returns are:

[0.0003555555555555556, 0.0008] [0.0008, 0.0028666666666666667]

[-0.7999999999999999, -0.4999999999999999]

By the way, I did get the same values for the covariance matrix and asset returns on my side, so that part of the library is working as expected.