[ojAlgo-user] Problem with the constraints on the assets with Markowitz model
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Samuel Le <Sam...@sr...> - 2011-10-27 11:34:32
|
Dear all, I am new to ojalgo and am trying to use it for portfolio optimisation. I think I found a bug: when trying to find some optimal weights with constraints, I can obtain inconsistent weights. I include below an example. I am using the ojalgo version 30.6. I would be grateful if someone could tell me what I am doing wrong. Many thanks, Samuel Here is my code: import java.math.BigDecimal; import org.ojalgo.finance.portfolio.MarketEquilibrium; import org.ojalgo.finance.portfolio.MarkowitzModel; import org.ojalgo.matrix.BasicMatrix; import org.ojalgo.matrix.PrimitiveMatrix; import org.ojalgo.matrix.store.PhysicalStore; import org.ojalgo.matrix.store.PrimitiveDenseStore; public class OjalgoTest { public static void main(String[] args) { int n = 10; PhysicalStore<Double> resStore = PrimitiveDenseStore.FACTORY.makeZero(n, n); Double[] vols = new Double[n]; for(int i = 0; i < n; i++) { vols[i] = new Double(i)/ new Double(n); } Double corr = 0.1; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j ++) { if(i == j) { resStore.set(i, j, vols[i]*vols[i]); } else { resStore.set(i,j,corr*vols[i]*vols[j]); } } } double[] expectedRets = new double[]{0.2,0.19,0.18,0.17,0.16,0.15,0.14,0.13,0.12,0.11}; PhysicalStore<Double> physicalStorDbl = PrimitiveDenseStore.FACTORY.makeZero(n, 1); for(int i = 0; i < n ; i++) { physicalStorDbl.set(i, 0, expectedRets[i]); } PrimitiveMatrix expectedReturnsMatrix = new PrimitiveMatrix(physicalStorDbl); PrimitiveMatrix covMat = new PrimitiveMatrix(resStore); MarketEquilibrium me = new MarketEquilibrium(covMat); MarkowitzModel markowitzModel = new MarkowitzModel(me, expectedReturnsMatrix); for(int i = 0; i < n; i++) { markowitzModel.setUpperLimit(i, new BigDecimal("0.05")); markowitzModel.setLowerLimit(i, new BigDecimal("0.3")); } markowitzModel.setRiskAversion(new BigDecimal("0.0001")); BasicMatrix tmpWgts = markowitzModel.getAssetWeights(); System.out.println(tmpWgts); } } And this is what I am getting: {{0.05}, {0.05}, {0.05}, {0.05}, {0.05}, {0.05}, {0.05}, {0.05}, {76.9098}, {-76.3098}} |