Re: [ojAlgo-user] Markowitz optimisation
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Arthur M. <art...@be...> - 2008-01-24 12:11:08
|
Hi Anders, I've run some more tests and I get a different NullPointerException on diff= erent data. If I set the variable upper bounds to "1.00" then it works but if I set the= m to "0.35" as in this example then I get an error. Could you have a look = please. The underlying code seems to set the upper bounds to "0.30" when p= assing in "1.00" but leaves as is with "0.35" which I think is the max poss= ible upper bounds. regards Arthur public void testojAlgo3() { // create expected returns matrix PrimitiveMatrix expectedReturnsMatrix =3D new PrimitiveMatrix(new double[= ][] { {10.012158}, {9.996046}, {10.000744}, {9.990585}, {9.998392}, {9.9966= 14}, {10.010531}, {10.001401}, {9.997447}, {9.993817}, {9.998537}, {9.99574= 1}, {9.987224}, {9.992392}}); // create covariance matrix PrimitiveMatrix covarianceMatrix =3D new PrimitiveMatrix(new double[][] {= {0.0013191354374342357, 7.786471466322114E-5, -3.810886655309235E-5, -2.281= 02405899103E-4, -1.2589115740653127E-4, -1.3247692268411991E-5, 1.422624656= 557158E-4, -2.7176361887359125E-5, 8.675127894495302E-5, -8.116577287090551= E-5, -8.468380774247271E-6, 4.930080166695193E-5, -2.774138231533918E-4, -3= .148322898570031E-5}, {7.786471466322114E-5, 0.001028250547816086, 8.986425197170406E-4, -1.0= 341435238579975E-5, 6.472902968147139E-4, 2.9014435841747375E-4, 1.06404144= 44602855E-4, 5.638694128451113E-4, 6.024515366195699E-4, -1.094867665517237= E-4, 6.177221606260711E-6, -5.682215091954099E-5, 2.7178074500896235E-4, 0.= 0010146062950574643}, {-3.810886655309235E-5, 8.986425197170406E-4, 0.0012477403456464075, -1= .8104847201530489E-4, 9.299199981666304E-4, 3.486383951982303E-4, 1.0246402= 606579107E-4, 7.009722990366382E-4, 6.545695073447614E-4, -1.16809691715001= 55E-4, 7.123493385355658E-5, 1.559414390174896E-5, 1.972605480880284E-4, 9.= 368808845809186E-4}, {-2.28102405899103E-4, -1.0341435238579975E-5, -1.8104847201530489E-4, = 6.250793590180099E-4, -5.4721911720097E-6, 1.3081826023829458E-4, -5.644046= 856412501E-5, -1.1282043806099452E-5, -6.729835202722053E-5, 1.392968154273= 7307E-4, 3.698155248637573E-6, 5.0269944317023966E-5, 5.344931460074395E-4,= -1.1654882792112444E-4}, {-1.2589115740653127E-4, 6.472902968147139E-4, 9.299199981666304E-4, -5= .4721911720097E-6, 0.001181357476541527, 3.0334522038028824E-4, 2.698384049= 7611894E-4, 6.983493701701867E-4, 5.68816790613126E-4, -7.899505299987754E-= 5, 1.05074262063586E-5, 1.137295188785598E-4, 1.9732025136606058E-4, 6.6313= 30613471645E-4}, {-1.3247692268411991E-5, 2.9014435841747375E-4, 3.486383951982303E-4, 1= .3081826023829458E-4, 3.0334522038028824E-4, 3.372068413122505E-4, 1.106746= 8759384309E-4, 2.6589126866881173E-4, 2.1364931019670806E-4, -4.20123947252= 0589E-5, 2.32769639721745E-5, 5.847559594073046E-6, 1.9925897592339058E-4, = 1.9671375386540353E-4}, {1.422624656557158E-4, 1.0640414444602855E-4, 1.0246402606579107E-4, -5= .644046856412501E-5, 2.6983840497611894E-4, 1.1067468759384309E-4, 0.001484= 755064835215, 1.2295961703024863E-4, 1.0843198781689372E-4, -2.129232829431= 3923E-5, -4.152686600769749E-6, 1.163599038579726E-4, -3.14739599261259E-4,= 2.4519847977412686E-4}, {-2.7176361887359125E-5, 5.638694128451113E-4, 7.009722990366382E-4, -1= .1282043806099452E-5, 6.983493701701867E-4, 2.6589126866881173E-4, 1.229596= 1703024863E-4, 5.563328439145604E-4, 4.4816730200338125E-4, -3.472983281400= 7256E-5, -6.028818604193519E-7, 3.192976987126335E-5, 1.7402262469809026E-4= , 5.182632389125651E-4}, {8.675127894495302E-5, 6.024515366195699E-4, 6.545695073447614E-4, -6.7= 29835202722053E-5, 5.68816790613126E-4, 2.1364931019670806E-4, 1.0843198781= 689372E-4, 4.4816730200338125E-4, 6.277134808325468E-4, -4.988229718603287E= -5, -5.5018781802344255E-6, -1.3231260300518203E-5, 8.214207901880769E-5, 5= .841470978796527E-4}, {-8.116577287090551E-5, -1.094867665517237E-4, -1.1680969171500155E-4, = 1.3929681542737307E-4, -7.899505299987754E-5, -4.201239472520589E-5, -2.129= 2328294313923E-5, -3.4729832814007256E-5, -4.988229718603287E-5, 3.51526926= 12068785E-4, -9.358092257358399E-6, 4.962216896551324E-6, 1.291957229930161= E-4, -1.5046975508620905E-4}, {-8.468380774247271E-6, 6.177221606260711E-6, 7.123493385355658E-5, 3.6= 98155248637573E-6, 1.05074262063586E-5, 2.32769639721745E-5, -4.15268660076= 9749E-6, -6.028818604193519E-7, -5.5018781802344255E-6, -9.358092257358399E= -6, 4.8495980378967104E-5, 1.1704645004909169E-5, 1.814918597253607E-5, 1.2= 448218299234062E-5}, {4.930080166695193E-5, -5.682215091954099E-5, 1.559414390174896E-5, 5.0= 269944317023966E-5, 1.137295188785598E-4, 5.847559594073046E-6, 1.163599038= 579726E-4, 3.192976987126335E-5, -1.3231260300518203E-5, 4.962216896551324E= -6, 1.1704645004909169E-5, 1.802684481609152E-4, 1.0475986793792914E-5, -4.= 113641419540392E-5}, {-2.774138231533918E-4, 2.7178074500896235E-4, 1.972605480880284E-4, 5.= 344931460074395E-4, 1.9732025136606058E-4, 1.9925897592339058E-4, -3.147395= 99261259E-4, 1.7402262469809026E-4, 8.214207901880769E-5, 1.291957229930161= E-4, 1.814918597253607E-5, 1.0475986793792914E-5, 7.843917688960864E-4, 1.2= 31995848356005E-4}, {-3.148322898570031E-5, 0.0010146062950574643, 9.368808845809186E-4, -1= .1654882792112444E-4, 6.631330613471645E-4, 1.9671375386540353E-4, 2.451984= 7977412686E-4, 5.182632389125651E-4, 5.841470978796527E-4, -1.5046975508620= 905E-4, 1.2448218299234062E-5, -4.113641419540392E-5, 1.231995848356005E-4,= 0.0011885193322126312}}); // create asset variables - cost and weighting constraints Variable[] tmpVariables =3D new Variable[expectedReturnsMatrix.getRowDim(= )]; for (int i =3D 0; i < tmpVariables.length; i++) { tmpVariables[i] =3D new Variable(""); tmpVariables[i].setCost(expectedReturnsMatrix.toBigDecimal(i, 0).negate(= )); // set the constraints on the asset weights // require at least a 2% allocation to each asset tmpVariables[i].setLowerLimit(new BigDecimal("0.05")); // require no more than 80% allocation to each asset tmpVariables[i].setUpperLimit(new BigDecimal("0.35")); // tmpVariables[i].setUpperLimit(new BigDecimal("1.00")); } QuadraticModel quadModel =3D new QuadraticModel(tmpVariables); Expression tmpExpr =3D quadModel.addCorrelationExpression("Variance", cov= arianceMatrix.toBigStore().toArray()); BigDecimal riskAversion =3D BigMath.PI.multiply(BigMath.E); tmpExpr.setCost(riskAversion.multiply(BigMath.HALF)); // set balance expression tmpExpr =3D quadModel.addSimpleWeightExpression("Balance"); tmpExpr.setLowerLimit(BigMath.ONE); tmpExpr.setUpperLimit(BigMath.ONE); // exception here... BasicMatrix bm =3D BigMatrix.FACTORY.buildColumnVector(quadModel.getDefau= ltSolver().getSolution()); =09 System.out.print(bm.toString()); } ******************************************************************** This message and any attachment are confidential. If you are not the inten= ded recipient please contact the sender, delete this message and any attach= ment from your system and do not disclose, copy or distribute the contents = to any other person. Beach Horizon LLP (Beach Horizon) is not responsible = for any information contained in this email. Beach Horizon reserves the ri= ght to monitor all email messages passing through its network. ******************************************************************** |