0

I have been trying to use the MarkowitzModel of ojalgo to determine the optimal weights of a portfolio. The documentation refers to a BasixMatrix for the constructor, but I believe this has been deprecated. I have managed to get something working using the constructor requiring a MarketEquilibrium and expectedExcessReturns as per code below. However, I am not sure what the MarketEquilibrium is and whether this is the correct approach. A simple example given an array of returns for a three stock portfolio would be great.

final PrimitiveMatrix cov = matrixFactory.rows(new double[][] { { 0.01, 0.0018, 0.0011 }, { 0.0018, 0.0109, 0.0026 }, { 0.0011, 0.0026, 0.0199 } });
    final PrimitiveMatrix ret = matrixFactory.columns(new double[] { 0.0427, 0.0015, 0.0285 });

    final MarketEquilibrium marketEquilibrium = new MarketEquilibrium(cov);
    final MarkowitzModel markowitz = new MarkowitzModel(marketEquilibrium, ret);
makasu
  • 65
  • 1
  • 3
  • 9
  • Looks ok. Have a look at the tests to see some examples: https://github.com/optimatika/ojAlgo-finance/tree/develop/src/test/java/org/ojalgo/finance/portfolio – apete Jun 18 '19 at 15:40
  • At least it seems that I am on the right track. I will look into the examples. I am currently using Apache Commons Math for portfolio statistics and should be able to generate the required data. Many thanks for your help. – makasu Jun 18 '19 at 16:33
  • I am looking at the examples. In particular the getCovariances method in the P20090115 class. I am not sure how I can make it work if the objective is to calculate the covariances of say 5 assets with monthly returns going back five years. The nested loop seems to assume a square matrix. – makasu Jun 21 '19 at 09:53
  • Yes, that code unfortunately had a bug (I would say) but it happened to work in the case where the number of assets and the number of samples were equal. I fixed that. – apete Jun 23 '19 at 19:14
  • Hi apete, many thanks. It works and I get the same results as in excel. The only thing to note is that trailing zeros are added to each element of the covariance matrix. For example, the first element of the covariance matrix shows the following [3.052, -2.91, -0.851, 0.0, 0.0] when I use a 3x5 array of returns. The relavant figures are exactly the same as in excel. I suppose I could deal with it programmatically. In any event the example you provided are of great help. Thank you. – makasu Jun 23 '19 at 20:15
  • Hi apete, I was wondering if I could ask another question. There is a class in the docs indicating that you can get different efficient portfolios by altering the risk aversion. I have been trying that using different loops but I have not been able to get a proper efficient frontier. Is there an example that you could point me to calculate the efficient frontier? – makasu Jul 20 '19 at 15:54
  • If you think you found a problem then construct a test case and open an issue at GitHub. – apete Jul 20 '19 at 20:53

0 Answers0