Re: [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 13:10:26
|
Indeed you are right! My correlation matrix was wrong. Now I just slightly change the way I set the volatilities of the assets: Double[] vols = new Double[n]; for(int i = 0; i < n; i++) { vols[i] = new Double(i+1)/ new Double(n); } (the volatility of asset i is now (i+1)/n, so there are no zeros anymore). Now the optimization stated is still FAILED. Also when I re-run the same code I get different weights. I will change my code to always check the optimisation state and ignore the weights provided when it's FAILED. Also this time I have no idea what causes the optimization to fail. Samuel -----Original Message----- From: Anders Peterson [mailto:an...@op...] Sent: 27 October 2011 13:59 To: oja...@li... Subject: Re: [ojAlgo-user] Problem with the constraints on the assets with Markowitz model It seems I was lucky that one time. Running the code again I get solutions similar to yours. The problem is your covariance matrix. It has columns and rows with all zeros. That's not allowed! Also you can/should check the optimization state: markowitzModel.getOptimisationState(); I get state "FAILED". /Anders On 27 okt 2011, at 14:24, Anders Peterson wrote: > Just ran your code. I got this solution: > > {{0.05}, > {0.05}, > {0.05}, > {0.05}, > {0.05}, > {0.05}, > {0.05}, > {0.05}, > {0.3}, > {0.3}} > > I am of course using v31, but I don't know what would be the difference. Upgrade to v31 and see if the problem disappears for you as well. > > /Anders > > > On 27 okt 2011, at 13:21, Samuel Le wrote: > >> 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}} >> >> >> __________ Information from ESET NOD32 Antivirus, version of virus signature database 6275 (20110707) __________ >> >> The message was checked by ESET NOD32 Antivirus. >> >> http://www.eset.com >> ------------------------------------------------------------------------------ >> The demand for IT networking professionals continues to grow, and the >> demand for specialized networking skills is growing even more rapidly. >> Take a complimentary Learning@Cisco Self-Assessment and learn >> about Cisco certifications, training, and career opportunities. >> http://p.sf.net/sfu/cisco-dev2dev_______________________________________________ >> ojAlgo-user mailing list >> ojA...@li... >> https://lists.sourceforge.net/lists/listinfo/ojalgo-user > ------------------------------------------------------------------------------ The demand for IT networking professionals continues to grow, and the demand for specialized networking skills is growing even more rapidly. Take a complimentary Learning@Cisco Self-Assessment and learn about Cisco certifications, training, and career opportunities. http://p.sf.net/sfu/cisco-dev2dev _______________________________________________ ojAlgo-user mailing list ojA...@li... https://lists.sourceforge.net/lists/listinfo/ojalgo-user __________ Information from ESET NOD32 Antivirus, version of virus signature database 6275 (20110707) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com __________ Information from ESET NOD32 Antivirus, version of virus signature database 6275 (20110707) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com |