Re: [ojAlgo-user] Markowitz optimisation
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Anders P. <an...@op...> - 2008-01-24 12:59:19
|
Sorry for all the problems you're having. There is a fix in CVS. It's the ActiveSetSolver's strategy for constraint (de)activation that is problematic. Maybe I should read a book on the subject. ;-) /Anders ############################################ 0.050 <= : 0.317 (-10.012) <= 0.350 0.050 <= : 0.050 (-9.996) <= 0.350 0.050 <= : 0.050 (-10.001) <= 0.350 0.050 <= : 0.050 (-9.991) <= 0.350 0.050 <= : 0.050 (-9.998) <= 0.350 0.050 <= : 0.050 (-9.997) <= 0.350 0.050 <= : 0.083 (-10.011) <= 0.350 0.050 <= : 0.050 (-10.001) <= 0.350 0.050 <= : 0.050 (-9.997) <= 0.350 0.050 <= : 0.050 (-9.994) <= 0.350 0.050 <= : 0.050 (-9.999) <= 0.350 0.050 <= : 0.050 (-9.996) <= 0.350 0.050 <= : 0.050 (-9.987) <= 0.350 0.050 <= : 0.050 (-9.992) <= 0.350 Variance: 0.001 (4.270) 1.000 <= Balance: 1.000 <= 1.000 ############################################ On 24 jan 2008, at 13.10, Arthur McGibbon wrote: > Hi Anders, > > I've run some more tests and I get a different NullPointerException > on different data. > > If I set the variable upper bounds to "1.00" then it works but if I > set them 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 passing in "1.00" but leaves as is with > "0.35" which I think is the max possible upper bounds. > > regards > Arthur > > > public void testojAlgo3() > { > // create expected returns matrix > PrimitiveMatrix expectedReturnsMatrix = new PrimitiveMatrix(new > double[][] { {10.012158}, {9.996046}, {10.000744}, {9.990585}, > {9.998392}, {9.996614}, {10.010531}, {10.001401}, {9.997447}, > {9.993817}, {9.998537}, {9.995741}, {9.987224}, {9.992392}}); > // create covariance matrix > PrimitiveMatrix covarianceMatrix = new PrimitiveMatrix(new double > [][] {{0.0013191354374342357, > 7.786471466322114E-5, -3.810886655309235E-5, -2.28102405899103E-4, -1. > 2589115740653127E-4, -1.3247692268411991E-5, > 1.422624656557158E-4, -2.7176361887359125E-5, > 8.675127894495302E-5, -8.116577287090551E-5, -8.468380774247271E-6, > 4.930080166695193E-5, -2.774138231533918E-4, -3.148322898570031E-5}, > {7.786471466322114E-5, 0.001028250547816086, > 8.986425197170406E-4, -1.0341435238579975E-5, 6.472902968147139E-4, > 2.9014435841747375E-4, 1.0640414444602855E-4, 5.638694128451113E-4, > 6.024515366195699E-4, -1.094867665517237E-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.0246402606579107E-4, > 7.009722990366382E-4, 6.545695073447614E-4, -1.1680969171500155E-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.644046856412501E-5, -1.1282043806099452E-5, > -6.729835202722053E-5, 1.3929681542737307E-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.6983840497611894E-4, 6.983493701701867E-4, > 5.68816790613126E-4, -7.899505299987754E-5, 1.05074262063586E-5, > 1.137295188785598E-4, 1.9732025136606058E-4, 6.631330613471645E-4}, > {-1.3247692268411991E-5, 2.9014435841747375E-4, > 3.486383951982303E-4, 1.3081826023829458E-4, 3.0334522038028824E-4, > 3.372068413122505E-4, 1.1067468759384309E-4, 2.6589126866881173E-4, > 2.1364931019670806E-4, -4.201239472520589E-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.001484755064835215, > 1.2295961703024863E-4, > 1.0843198781689372E-4, -2.1292328294313923E-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.2295961703024863E-4, 5.563328439145604E-4, > 4.4816730200338125E-4, -3.4729832814007256E-5, -6.028818604193519E-7, > 3.192976987126335E-5, 1.7402262469809026E-4, 5.182632389125651E-4}, > {8.675127894495302E-5, 6.024515366195699E-4, > 6.545695073447614E-4, -6.729835202722053E-5, 5.68816790613126E-4, > 2.1364931019670806E-4, 1.0843198781689372E-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.1292328294313923E-5, -3.4729832814007256E-5, -4.988229718603287E-5, > 3.5152692612068785E-4, -9.358092257358399E-6, 4.962216896551324E-6, > 1.291957229930161E-4, -1.5046975508620905E-4}, > {-8.468380774247271E-6, 6.177221606260711E-6, > 7.123493385355658E-5, 3.698155248637573E-6, 1.05074262063586E-5, > 2.32769639721745E-5, -4.152686600769749E-6, -6.028818604193519E-7, -5. > 5018781802344255E-6, -9.358092257358399E-6, 4.8495980378967104E-5, > 1.1704645004909169E-5, 1.814918597253607E-5, 1.2448218299234062E-5}, > {4.930080166695193E-5, -5.682215091954099E-5, > 1.559414390174896E-5, 5.0269944317023966E-5, 1.137295188785598E-4, > 5.847559594073046E-6, 1.163599038579726E-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.14739599261259E-4, 1.7402262469809026E-4, > 8.214207901880769E-5, 1.291957229930161E-4, 1.814918597253607E-5, > 1.0475986793792914E-5, 7.843917688960864E-4, 1.231995848356005E-4}, > {-3.148322898570031E-5, 0.0010146062950574643, > 9.368808845809186E-4, -1.1654882792112444E-4, 6.631330613471645E-4, > 1.9671375386540353E-4, 2.4519847977412686E-4, 5.182632389125651E-4, > 5.841470978796527E-4, -1.5046975508620905E-4, > 1.2448218299234062E-5, -4.113641419540392E-5, 1.231995848356005E-4, > 0.0011885193322126312}}); > > // create asset variables - cost and weighting constraints > Variable[] tmpVariables = new Variable > [expectedReturnsMatrix.getRowDim()]; > for (int i = 0; i < tmpVariables.length; i++) > { > tmpVariables[i] = 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 = new QuadraticModel(tmpVariables); > > Expression tmpExpr = quadModel.addCorrelationExpression > ("Variance", covarianceMatrix.toBigStore().toArray()); > BigDecimal riskAversion = BigMath.PI.multiply(BigMath.E); > tmpExpr.setCost(riskAversion.multiply(BigMath.HALF)); > > // set balance expression > tmpExpr = quadModel.addSimpleWeightExpression("Balance"); > tmpExpr.setLowerLimit(BigMath.ONE); > tmpExpr.setUpperLimit(BigMath.ONE); > > // exception here... > BasicMatrix bm = BigMatrix.FACTORY.buildColumnVector > (quadModel.getDefaultSolver().getSolution()); > > System.out.print(bm.toString()); > } > > > ******************************************************************** > This message and any attachment are confidential. If you are not > the intended recipient please contact the sender, delete this > message and any attachment 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 right to > monitor all email messages passing through its network. > ******************************************************************** > > > ---------------------------------------------------------------------- > --- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > |