ojalgo-user Mailing List for oj! Algorithms (Page 17)
Mathematics, linear algebra and optimisation
Brought to you by:
apete
You can subscribe to this list here.
2003 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
(1) |
Jul
(1) |
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
2004 |
Jan
|
Feb
(1) |
Mar
|
Apr
(14) |
May
(1) |
Jun
(1) |
Jul
|
Aug
|
Sep
|
Oct
(1) |
Nov
|
Dec
(1) |
2005 |
Jan
(3) |
Feb
|
Mar
|
Apr
|
May
|
Jun
(1) |
Jul
|
Aug
(1) |
Sep
(3) |
Oct
|
Nov
|
Dec
(1) |
2006 |
Jan
(1) |
Feb
(1) |
Mar
(1) |
Apr
|
May
(1) |
Jun
(2) |
Jul
|
Aug
(5) |
Sep
|
Oct
(3) |
Nov
(4) |
Dec
(2) |
2007 |
Jan
(8) |
Feb
(1) |
Mar
(2) |
Apr
(11) |
May
(6) |
Jun
|
Jul
(10) |
Aug
(2) |
Sep
|
Oct
(4) |
Nov
|
Dec
(11) |
2008 |
Jan
(22) |
Feb
(4) |
Mar
(2) |
Apr
(4) |
May
(2) |
Jun
|
Jul
|
Aug
(1) |
Sep
|
Oct
(6) |
Nov
(17) |
Dec
(5) |
2009 |
Jan
(15) |
Feb
(2) |
Mar
(2) |
Apr
(3) |
May
(4) |
Jun
(5) |
Jul
(9) |
Aug
(2) |
Sep
|
Oct
(5) |
Nov
(14) |
Dec
|
2010 |
Jan
(3) |
Feb
(5) |
Mar
(2) |
Apr
(19) |
May
|
Jun
|
Jul
|
Aug
(2) |
Sep
|
Oct
(5) |
Nov
(3) |
Dec
|
2011 |
Jan
|
Feb
(1) |
Mar
(7) |
Apr
(10) |
May
(1) |
Jun
(15) |
Jul
(1) |
Aug
|
Sep
|
Oct
(13) |
Nov
(9) |
Dec
(11) |
2012 |
Jan
|
Feb
(1) |
Mar
(9) |
Apr
(23) |
May
(16) |
Jun
(9) |
Jul
(13) |
Aug
|
Sep
(4) |
Oct
(1) |
Nov
(1) |
Dec
|
2013 |
Jan
|
Feb
(12) |
Mar
(7) |
Apr
(24) |
May
|
Jun
(7) |
Jul
(4) |
Aug
|
Sep
(3) |
Oct
(16) |
Nov
(7) |
Dec
(3) |
2014 |
Jan
(22) |
Feb
(1) |
Mar
(1) |
Apr
|
May
(2) |
Jun
(3) |
Jul
(4) |
Aug
(4) |
Sep
(1) |
Oct
(2) |
Nov
(6) |
Dec
(2) |
2015 |
Jan
(11) |
Feb
(10) |
Mar
(1) |
Apr
(9) |
May
|
Jun
|
Jul
(1) |
Aug
(4) |
Sep
(6) |
Oct
(3) |
Nov
(15) |
Dec
(4) |
2016 |
Jan
(6) |
Feb
|
Mar
(18) |
Apr
(5) |
May
(9) |
Jun
(3) |
Jul
(5) |
Aug
(2) |
Sep
(5) |
Oct
(2) |
Nov
|
Dec
(5) |
2017 |
Jan
(4) |
Feb
(3) |
Mar
|
Apr
|
May
(12) |
Jun
(1) |
Jul
(4) |
Aug
|
Sep
(3) |
Oct
(1) |
Nov
|
Dec
(1) |
2018 |
Jan
(5) |
Feb
|
Mar
(1) |
Apr
(1) |
May
(1) |
Jun
|
Jul
(4) |
Aug
(1) |
Sep
(8) |
Oct
|
Nov
|
Dec
(1) |
2019 |
Jan
|
Feb
(2) |
Mar
(1) |
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
2022 |
Jan
|
Feb
|
Mar
|
Apr
|
May
(1) |
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: Samuel Le <Sam...@sr...> - 2011-10-27 14:05:47
|
Yes, I made this mistake. Now it's fixed. Thanks a lot for the quick help Anders, much appreciated. Samuel -----Original Message----- From: Anders Peterson [mailto:an...@op...] Sent: 27 October 2011 14:56 To: oja...@li... Subject: Re: [ojAlgo-user] Problem with the constraints on the assets with Markowitz model Your model is very artificial/symmetric... The problem is that you've mixed up the lower and upper limits on the variables (asset weight constraints). /Anders On 27 okt 2011, at 15:10, Samuel Le wrote: > 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 > > > ------------------------------------------------------------------------------ > 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 |
From: Anders P. <an...@op...> - 2011-10-27 13:56:21
|
Your model is very artificial/symmetric… The problem is that you've mixed up the lower and upper limits on the variables (asset weight constraints). /Anders On 27 okt 2011, at 15:10, Samuel Le wrote: > 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 > > > ------------------------------------------------------------------------------ > 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 > > |
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 |
From: Anders P. <an...@op...> - 2011-10-27 13:00:12
|
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 > |
From: Samuel Le <Sam...@sr...> - 2011-10-27 12:59:08
|
Hi Anders, Thank you for your quick reply. I just upgraded my jars and re ran my code. This time I get a plausible result but for some reason different of yours: {{0.05}, {0.05}, {0.05}, {0.05}, {0.05}, {0.05}, {0.05}, {0.05}, {0.05}, {0.55}} I will keep investigating, I must have something else wrong. Cheers, Samuel -----Original Message----- From: Anders Peterson [mailto:an...@op...] Sent: 27 October 2011 13:24 To: oja...@li... Subject: Re: [ojAlgo-user] Problem with the constraints on the assets with Markowitz model 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 |
From: Anders P. <an...@op...> - 2011-10-27 12:24:23
|
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 |
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}} |
From: Anders P. <an...@op...> - 2011-10-26 17:59:23
|
http://ojalgo.org/change_log.html http://sourceforge.net/projects/ojalgo/files/ojAlgo/ /Anders |
From: Anders P. <an...@op...> - 2011-10-13 08:20:12
|
http://en.wikipedia.org/wiki/Quadratic_programming#Solution_methods It's a plain Lagrange solver. If there are inequality constraints it uses an active set strategy, and if there are any integer constraints on the variables everything is wrapped in a branch-and-bound algorithm. There are plenty of books and on-line resources covering this. Just google for it. /Anders On 13 okt 2011, at 06:00, Tin Nguyen Chi wrote: > Hi, > > I'd like to ask about the method ojalgo use to solve the quadratic programming. It would be great if a reference (paper or book) so I can look for more details > > Thanks a lot > ------------------------------------------------------------------------------ > All the data continuously generated in your IT infrastructure contains a > definitive record of customers, application performance, security > threats, fraudulent activity and more. Splunk takes this data and makes > sense of it. Business sense. IT sense. Common sense. > http://p.sf.net/sfu/splunk-d2d-oct_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user |
From: Tin N. C. <tin...@gm...> - 2011-10-13 04:00:44
|
Hi, I'd like to ask about the method ojalgo use to solve the quadratic programming. It would be great if a reference (paper or book) so I can look for more details Thanks a lot |
From: Anders P. <an...@op...> - 2011-10-10 21:43:34
|
I uploaded a new snapshot https://sourceforge.net/projects/ojalgo/files/ojAlgo/snapshot/ Could you test if that works better for you? /Anders On 10 okt 2011, at 14:45, Anders Peterson wrote: > Haven't looked at this in great detail, but if you change: > > objective[1].setInteger(true); > to > objective[1].setInteger(false); > > and > > model.minimise(); > to > model.setMinimisation(true); > final OptimisationSolver tmpSolver = model.getDefaultSolver(); > final Result tmpResult = tmpSolver.solve(); > > Then you'll see that ojAlgo reports the relaxed problem to be unbounded. If that's true it's a (big) problem for the integer solver. The relaxed problems need to be able to be solved by the linear or quadratic solvers. > > I'm not confident that ojAlgo reports INFEASIBLE and/or UNBOUNDED problems right all the time, but you should definitely not get a NullPointerException! > > X=0, Y=0 & Z=0 is a valid solution to your problem, right? In that case there is a bug with the LinearSolver. > > /Anders > > > On 10 okt 2011, at 12:18, Julien.Cojan wrote: > >> Hello, >> >> I am using OjAlgo to solve mixed linear programming problems. >> However I often get a NullPointerException when running on my examples. >> I tried version 30.0 and 30.6, I get the same error. >> >> This is the smallest example I could find where I get this exception : >> >> >> import static org.ojalgo.constant.BigMath.ONE; >> import static org.ojalgo.constant.BigMath.ZERO; >> >> import java.math.BigDecimal; >> >> import org.ojalgo.optimisation.Expression; >> import org.ojalgo.optimisation.Variable; >> import org.ojalgo.optimisation.linear.LinearExpressionsModel; >> >> >> public class TestOjAlgo { >> >> public static void testBug1(){ >> Variable[] objective = new Variable[] { >> new Variable("X").weight(ONE), >> new Variable("Y").weight(ZERO), >> new Variable("Z").weight(ZERO)}; >> >> objective[1].setInteger(true); >> >> LinearExpressionsModel model = new LinearExpressionsModel(objective); >> >> // c1: X =0 >> Expression c1 = model.addEmptyLinearExpression("c1"); >> c1.level(ZERO); >> c1.setLinearFactor(0, ONE); >> >> // c2: -X +5Y =0 >> Expression c2 = model.addEmptyLinearExpression("c2"); >> c2.level(ZERO); >> >> c2.setLinearFactor(0, new BigDecimal(-1)); >> c2.setLinearFactor(1, ONE); >> >> // c3: X -Z =0 >> Expression c3 = model.addEmptyLinearExpression("c3"); >> c3.level(ZERO); >> // bugs with this constraint >> c3.setLinearFactor(0, ONE); >> c3.setLinearFactor(2, new BigDecimal(-1)); >> // but not with this one ??? >> // c3.setLinearFactor(0, new BigDecimal(-1)); >> // c3.setLinearFactor(2, ONE); >> >> model.minimise(); >> } >> >> public static void main(String[] args){ >> testBug1(); >> } >> >> } >> >> >> >> >> >> I get the following message >> Exception in thread "main" java.lang.NullPointerException >> at org.ojalgo.optimisation.OptimisationSolver$Result.<init>(OptimisationSolver.java:99) >> at org.ojalgo.optimisation.integer.IntegerSolver.solve(IntegerSolver.java:318) >> at org.ojalgo.optimisation.ExpressionsBasedModel.solve(ExpressionsBasedModel.java:741) >> at org.ojalgo.optimisation.ExpressionsBasedModel.minimise(ExpressionsBasedModel.java:458) >> at TestOjAlgo.testBug1(TestOjAlgo.java:57) >> at TestOjAlgo.main(TestOjAlgo.java:61) >> >> >> Is it a bug or am I not using ojAlgo properly ? >> >> >> -- >> The Open University is incorporated by Royal Charter (RC 000391), an exempt charity in England & Wales and a charity registered in Scotland (SC 038302). >> >> >> ------------------------------------------------------------------------------ >> All the data continuously generated in your IT infrastructure contains a >> definitive record of customers, application performance, security >> threats, fraudulent activity and more. Splunk takes this data and makes >> sense of it. Business sense. IT sense. Common sense. >> http://p.sf.net/sfu/splunk-d2dcopy1 >> _______________________________________________ >> ojAlgo-user mailing list >> ojA...@li... >> https://lists.sourceforge.net/lists/listinfo/ojalgo-user >> >> > > > ------------------------------------------------------------------------------ > All the data continuously generated in your IT infrastructure contains a > definitive record of customers, application performance, security > threats, fraudulent activity and more. Splunk takes this data and makes > sense of it. Business sense. IT sense. Common sense. > http://p.sf.net/sfu/splunk-d2dcopy1 > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > |
From: Anders P. <an...@op...> - 2011-10-10 13:01:16
|
Haven't looked at this in great detail, but if you change: objective[1].setInteger(true); to objective[1].setInteger(false); and model.minimise(); to model.setMinimisation(true); final OptimisationSolver tmpSolver = model.getDefaultSolver(); final Result tmpResult = tmpSolver.solve(); Then you'll see that ojAlgo reports the relaxed problem to be unbounded. If that's true it's a (big) problem for the integer solver. The relaxed problems need to be able to be solved by the linear or quadratic solvers. I'm not confident that ojAlgo reports INFEASIBLE and/or UNBOUNDED problems right all the time, but you should definitely not get a NullPointerException! X=0, Y=0 & Z=0 is a valid solution to your problem, right? In that case there is a bug with the LinearSolver. /Anders On 10 okt 2011, at 12:18, Julien.Cojan wrote: > Hello, > > I am using OjAlgo to solve mixed linear programming problems. > However I often get a NullPointerException when running on my examples. > I tried version 30.0 and 30.6, I get the same error. > > This is the smallest example I could find where I get this exception : > > > import static org.ojalgo.constant.BigMath.ONE; > import static org.ojalgo.constant.BigMath.ZERO; > > import java.math.BigDecimal; > > import org.ojalgo.optimisation.Expression; > import org.ojalgo.optimisation.Variable; > import org.ojalgo.optimisation.linear.LinearExpressionsModel; > > > public class TestOjAlgo { > > public static void testBug1(){ > Variable[] objective = new Variable[] { > new Variable("X").weight(ONE), > new Variable("Y").weight(ZERO), > new Variable("Z").weight(ZERO)}; > > objective[1].setInteger(true); > > LinearExpressionsModel model = new LinearExpressionsModel(objective); > > // c1: X =0 > Expression c1 = model.addEmptyLinearExpression("c1"); > c1.level(ZERO); > c1.setLinearFactor(0, ONE); > > // c2: -X +5Y =0 > Expression c2 = model.addEmptyLinearExpression("c2"); > c2.level(ZERO); > > c2.setLinearFactor(0, new BigDecimal(-1)); > c2.setLinearFactor(1, ONE); > > // c3: X -Z =0 > Expression c3 = model.addEmptyLinearExpression("c3"); > c3.level(ZERO); > // bugs with this constraint > c3.setLinearFactor(0, ONE); > c3.setLinearFactor(2, new BigDecimal(-1)); > // but not with this one ??? > // c3.setLinearFactor(0, new BigDecimal(-1)); > // c3.setLinearFactor(2, ONE); > > model.minimise(); > } > > public static void main(String[] args){ > testBug1(); > } > > } > > > > > > I get the following message > Exception in thread "main" java.lang.NullPointerException > at org.ojalgo.optimisation.OptimisationSolver$Result.<init>(OptimisationSolver.java:99) > at org.ojalgo.optimisation.integer.IntegerSolver.solve(IntegerSolver.java:318) > at org.ojalgo.optimisation.ExpressionsBasedModel.solve(ExpressionsBasedModel.java:741) > at org.ojalgo.optimisation.ExpressionsBasedModel.minimise(ExpressionsBasedModel.java:458) > at TestOjAlgo.testBug1(TestOjAlgo.java:57) > at TestOjAlgo.main(TestOjAlgo.java:61) > > > Is it a bug or am I not using ojAlgo properly ? > > > -- > The Open University is incorporated by Royal Charter (RC 000391), an exempt charity in England & Wales and a charity registered in Scotland (SC 038302). > > > ------------------------------------------------------------------------------ > All the data continuously generated in your IT infrastructure contains a > definitive record of customers, application performance, security > threats, fraudulent activity and more. Splunk takes this data and makes > sense of it. Business sense. IT sense. Common sense. > http://p.sf.net/sfu/splunk-d2dcopy1 > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > |
From: Julien.Cojan <Jul...@op...> - 2011-10-10 10:18:39
|
Hello, I am using OjAlgo to solve mixed linear programming problems. However I often get a NullPointerException when running on my examples. I tried version 30.0 and 30.6, I get the same error. This is the smallest example I could find where I get this exception : import static org.ojalgo.constant.BigMath.ONE; import static org.ojalgo.constant.BigMath.ZERO; import java.math.BigDecimal; import org.ojalgo.optimisation.Expression; import org.ojalgo.optimisation.Variable; import org.ojalgo.optimisation.linear.LinearExpressionsModel; public class TestOjAlgo { public static void testBug1(){ Variable[] objective = new Variable[] { new Variable("X").weight(ONE), new Variable("Y").weight(ZERO), new Variable("Z").weight(ZERO)}; objective[1].setInteger(true); LinearExpressionsModel model = new LinearExpressionsModel(objective); // c1: X =0 Expression c1 = model.addEmptyLinearExpression("c1"); c1.level(ZERO); c1.setLinearFactor(0, ONE); // c2: -X +5Y =0 Expression c2 = model.addEmptyLinearExpression("c2"); c2.level(ZERO); c2.setLinearFactor(0, new BigDecimal(-1)); c2.setLinearFactor(1, ONE); // c3: X -Z =0 Expression c3 = model.addEmptyLinearExpression("c3"); c3.level(ZERO); // bugs with this constraint c3.setLinearFactor(0, ONE); c3.setLinearFactor(2, new BigDecimal(-1)); // but not with this one ??? // c3.setLinearFactor(0, new BigDecimal(-1)); // c3.setLinearFactor(2, ONE); model.minimise(); } public static void main(String[] args){ testBug1(); } } I get the following message Exception in thread "main" java.lang.NullPointerException at org.ojalgo.optimisation.OptimisationSolver$Result.<init>(OptimisationSolver.java:99) at org.ojalgo.optimisation.integer.IntegerSolver.solve(IntegerSolver.java:318) at org.ojalgo.optimisation.ExpressionsBasedModel.solve(ExpressionsBasedModel.java:741) at org.ojalgo.optimisation.ExpressionsBasedModel.minimise(ExpressionsBasedModel.java:458) at TestOjAlgo.testBug1(TestOjAlgo.java:57) at TestOjAlgo.main(TestOjAlgo.java:61) Is it a bug or am I not using ojAlgo properly ? -- The Open University is incorporated by Royal Charter (RC 000391), an exempt charity in England & Wales and a charity registered in Scotland (SC 038302). |
From: Anders P. <an...@op...> - 2011-07-04 21:01:54
|
I ran these tests again with v30 and updated the results page. http://ojalgo.org/performance_ejml.html /Anders On 4 apr 2011, at 22.58, Anders Peterson wrote: > I ran the "Java Matrix Benchmark" > > http://ojalgo.org/performance_ejml.html > > ojAlgo performs ok considering I didn't write the benchmark ;-) > > /Anders |
From: Anders P. <an...@op...> - 2011-06-30 15:06:15
|
Just use the MarkowitzModel class http://ojalgo.org/generated/org/ojalgo/finance/portfolio/MarkowitzModel.html and/or browse through the list archive. I believe this question (similar questions at least) has been asked before. /Anders On 30 jun 2011, at 02.31, Charley Swift wrote: > I'm working on a portfolio optimization problem, using QP to find the optimal weight (allocation) of each asset in the portfolio. My objective is to minimize variance: find the optimal portfolio weights for the Minimum Variance Portfolio (MVP). Here's an example in R of what I'm trying to do in Java: > > http://quantivity.wordpress.com/2011/04/17/minimum-variance-portfolios/ > > My inputs are an n x n covariance matrix (where n is the number of assets in the portfolio), which I've calculated from the known asset returns. > > The two constraints are: sum of the portfolio weights must add up to 1.0 (allocate all available capital), and all weights must be >= 0 (no shorting allowed). > > Could someone please provide (or point me to) some Java code how to do this in ojAlgo (using its quadratic solver)? > > Thank you, > Charley > > > ------------------------------------------------------------------------------ > All of the data generated in your IT infrastructure is seriously valuable. > Why? It contains a definitive record of application performance, security > threats, fraudulent activity, and more. Splunk takes this data and makes > sense of it. IT sense. And common sense. > http://p.sf.net/sfu/splunk-d2d-c2 > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > |
From: Charley S. <cha...@ya...> - 2011-06-30 00:31:29
|
I'm working on a portfolio optimization problem, using QP to find the optimal weight (allocation) of each asset in the portfolio. My objective is to minimize variance: find the optimal portfolio weights for the Minimum Variance Portfolio (MVP). Here's an example in R of what I'm trying to do in Java: http://quantivity.wordpress.com/2011/04/17/minimum-variance-portfolios/ My inputs are an n x n covariance matrix (where n is the number of assets in the portfolio), which I've calculated from the known asset returns. The two constraints are: sum of the portfolio weights must add up to 1.0 (allocate all available capital), and all weights must be >= 0 (no shorting allowed). Could someone please provide (or point me to) some Java code how to do this in ojAlgo (using its quadratic solver)? Thank you, Charley |
From: Anders P. <an...@op...> - 2011-06-26 21:35:13
|
I have now released v30.1 that should fix that problem. /Anders On 26 jun 2011, at 10.23, Anders Peterson wrote: > That looks like a bug. I'll have to think about which is the correct fix. > > Until I release a fixed version, creating an empty linear expression should work: > > Expression tmpLinExpr = addEmptyLinearExpression("LinearExpression"); > tmpLinExpr.setLinearFactor(0, ...); > tmpLinExpr.setLinearFactor(1, ...); > tmpLinExpr.setLinearFactor(2, ...); > tmpLinExpr.setLinearFactor(3, ...); > > > /Anders > > > > On 24 jun 2011, at 13.56, Luke Lindsay wrote: > >> What is the correct way to create a row vector for >> ExpressionsBasedModel.addLinearExpression? The code below throws an >> excecption: >> >> LinearExpressionsModel model = new LinearExpressionsModel(vars); >> Access1D a1d = Array1D.makePrimitive(2); >> model.addLinearExpression("LinearExpression", a1d); >> >> Must be a row vector! >> java.lang.IllegalArgumentException: Must be a row vector! >> at org.ojalgo.function.multiary.LinearFunction.<init>(LinearFunction.java:91) >> at org.ojalgo.function.multiary.LinearFunction.makeBig(LinearFunction.java:39) >> at org.ojalgo.optimisation.Expression.makeLinear(Expression.java:70) >> at org.ojalgo.optimisation.ExpressionsBasedModel.addLinearExpression(ExpressionsBasedModel.java:160) >> >> ------------------------------------------------------------------------------ >> All the data continuously generated in your IT infrastructure contains a >> definitive record of customers, application performance, security >> threats, fraudulent activity and more. Splunk takes this data and makes >> sense of it. Business sense. IT sense. Common sense.. >> http://p.sf.net/sfu/splunk-d2d-c1 >> _______________________________________________ >> ojAlgo-user mailing list >> ojA...@li... >> https://lists.sourceforge.net/lists/listinfo/ojalgo-user >> >> > > > ------------------------------------------------------------------------------ > All of the data generated in your IT infrastructure is seriously valuable. > Why? It contains a definitive record of application performance, security > threats, fraudulent activity, and more. Splunk takes this data and makes > sense of it. IT sense. And common sense. > http://p.sf.net/sfu/splunk-d2d-c2 > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > |
From: Anders P. <an...@op...> - 2011-06-26 08:23:41
|
That looks like a bug. I'll have to think about which is the correct fix. Until I release a fixed version, creating an empty linear expression should work: Expression tmpLinExpr = addEmptyLinearExpression("LinearExpression"); tmpLinExpr.setLinearFactor(0, ...); tmpLinExpr.setLinearFactor(1, ...); tmpLinExpr.setLinearFactor(2, ...); tmpLinExpr.setLinearFactor(3, ...); /Anders On 24 jun 2011, at 13.56, Luke Lindsay wrote: > What is the correct way to create a row vector for > ExpressionsBasedModel.addLinearExpression? The code below throws an > excecption: > > LinearExpressionsModel model = new LinearExpressionsModel(vars); > Access1D a1d = Array1D.makePrimitive(2); > model.addLinearExpression("LinearExpression", a1d); > > Must be a row vector! > java.lang.IllegalArgumentException: Must be a row vector! > at org.ojalgo.function.multiary.LinearFunction.<init>(LinearFunction.java:91) > at org.ojalgo.function.multiary.LinearFunction.makeBig(LinearFunction.java:39) > at org.ojalgo.optimisation.Expression.makeLinear(Expression.java:70) > at org.ojalgo.optimisation.ExpressionsBasedModel.addLinearExpression(ExpressionsBasedModel.java:160) > > ------------------------------------------------------------------------------ > All the data continuously generated in your IT infrastructure contains a > definitive record of customers, application performance, security > threats, fraudulent activity and more. Splunk takes this data and makes > sense of it. Business sense. IT sense. Common sense.. > http://p.sf.net/sfu/splunk-d2d-c1 > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > |
From: Luke L. <luk...@gm...> - 2011-06-24 11:56:54
|
What is the correct way to create a row vector for ExpressionsBasedModel.addLinearExpression? The code below throws an excecption: LinearExpressionsModel model = new LinearExpressionsModel(vars); Access1D a1d = Array1D.makePrimitive(2); model.addLinearExpression("LinearExpression", a1d); Must be a row vector! java.lang.IllegalArgumentException: Must be a row vector! at org.ojalgo.function.multiary.LinearFunction.<init>(LinearFunction.java:91) at org.ojalgo.function.multiary.LinearFunction.makeBig(LinearFunction.java:39) at org.ojalgo.optimisation.Expression.makeLinear(Expression.java:70) at org.ojalgo.optimisation.ExpressionsBasedModel.addLinearExpression(ExpressionsBasedModel.java:160) |
From: Simon G. <si...@ya...> - 2011-06-15 20:59:25
|
Thank you for your help, it works now. SG ________________________________ From: Anders Peterson <an...@op...> To: oja...@li... Sent: Wed, June 15, 2011 4:10:03 AM Subject: Re: [ojAlgo-user] QuadraticSolver issue Here's my code. testP20110614 is your *problem* http://ojalgo.cvs.sourceforge.net/viewvc/ojalgo/TestProj/src/org/ojalgo/finance/portfolio/ReportedProblems.java?view=markup /Anders On 14 jun 2011, at 23.33, Simon Greaves wrote: > Yes, I have to finish the lower bounds, that part I'm not worried about. > But I don't understand, I must be doing something different from how you did >it, because I get: > > X class org.ojalgo.matrix.PrimitiveMatrix > {{1.000000000000013}, > {3.2333333333333143}, > {-3.233333333333325}} > > This is how I get X: > > Result rslt = qdrtc_slvr.solve(); > BasicMatrix rslt_mtrx = rslt.getSolution(); > PhysicalStore<Double> result = rslt_mtrx.toPrimitiveStore(); > System.out.println("\nX " + rslt_mtrx.toString()); > > I this correct? > Thanks again for all your help by the way! > > SG > > From: Anders Peterson <an...@op...> > To: oja...@li... > Sent: Tue, June 14, 2011 4:34:59 PM > Subject: Re: [ojAlgo-user] QuadraticSolver issue > > Here's what I get: > > [AE] = class org.ojalgo.matrix.PrimitiveMatrix > {{1.0, 1.0, 1.0}} > > [BE] = class org.ojalgo.matrix.PrimitiveMatrix > {{1.0}} > > [Q] = class org.ojalgo.matrix.PrimitiveMatrix > {{4.0, 1.0, 2.0}, > {1.0, 9.0, 1.0}, > {2.0, 1.0, 16.0}} > > [C] = class org.ojalgo.matrix.PrimitiveMatrix > {{10.0}, > {15.0}, > {18.0}} > > [AI] = class org.ojalgo.matrix.PrimitiveMatrix > {{1.0, 0.0, 0.0}, > {0.0, 1.0, 0.0}, > {0.0, 0.0, 1.0}} > > [BI] = class org.ojalgo.matrix.PrimitiveMatrix > {{1.0}, > {1.0}, > {1.0}} > > [X] = class org.ojalgo.matrix.PrimitiveMatrix > {{-0.17441860465116266}, > {0.6279069767441859}, > {0.5465116279069767}} > > [LE] = class org.ojalgo.matrix.PrimitiveMatrix > {{8.976744186046512}} > > [LI] = class org.ojalgo.matrix.PrimitiveMatrix > {{0.0}, > {0.0}, > {0.0}} > > [SE] = class org.ojalgo.matrix.PrimitiveMatrix > {{0.0}} > > [SI] = class org.ojalgo.matrix.PrimitiveMatrix > {{1.1744186046511627}, > {0.37209302325581406}, > {0.4534883720930233}} > > I do not get any weights larger than 1.0, but I do get one weight smaller than >0.0. There's no constraint to restrict that! Add constraints for that and you >should get the solutions you expect. > > Here's how I would find the efficient frontier: > > final BasicMatrix tmpCovars = ...; > final BasicMatrix tmpReturs = ...; > > final MarketEquilibrium tmpME = new MarketEquilibrium(tmpCovars); > > final MarkowitzModel tmpMarkowitz = new MarkowitzModel(tmpME, >tmpReturs); > > for (int i = 1; i < 10; i++) { // Some different risk aversion factors > |
From: Anders P. <an...@op...> - 2011-06-15 08:40:11
|
Here's my code. testP20110614 is your *problem* http://ojalgo.cvs.sourceforge.net/viewvc/ojalgo/TestProj/src/org/ojalgo/finance/portfolio/ReportedProblems.java?view=markup /Anders On 14 jun 2011, at 23.33, Simon Greaves wrote: > Yes, I have to finish the lower bounds, that part I'm not worried about. > But I don't understand, I must be doing something different from how you did it, because I get: > > X class org.ojalgo.matrix.PrimitiveMatrix > {{1.000000000000013}, > {3.2333333333333143}, > {-3.233333333333325}} > > This is how I get X: > > Result rslt = qdrtc_slvr.solve(); > BasicMatrix rslt_mtrx = rslt.getSolution(); > PhysicalStore<Double> result = rslt_mtrx.toPrimitiveStore(); > System.out.println("\nX " + rslt_mtrx.toString()); > > I this correct? > Thanks again for all your help by the way! > > SG > > From: Anders Peterson <an...@op...> > To: oja...@li... > Sent: Tue, June 14, 2011 4:34:59 PM > Subject: Re: [ojAlgo-user] QuadraticSolver issue > > Here's what I get: > > [AE] = class org.ojalgo.matrix.PrimitiveMatrix > {{1.0, 1.0, 1.0}} > > [BE] = class org.ojalgo.matrix.PrimitiveMatrix > {{1.0}} > > [Q] = class org.ojalgo.matrix.PrimitiveMatrix > {{4.0, 1.0, 2.0}, > {1.0, 9.0, 1.0}, > {2.0, 1.0, 16.0}} > > [C] = class org.ojalgo.matrix.PrimitiveMatrix > {{10.0}, > {15.0}, > {18.0}} > > [AI] = class org.ojalgo.matrix.PrimitiveMatrix > {{1.0, 0.0, 0.0}, > {0.0, 1.0, 0.0}, > {0.0, 0.0, 1.0}} > > [BI] = class org.ojalgo.matrix.PrimitiveMatrix > {{1.0}, > {1.0}, > {1.0}} > > [X] = class org.ojalgo.matrix.PrimitiveMatrix > {{-0.17441860465116266}, > {0.6279069767441859}, > {0.5465116279069767}} > > [LE] = class org.ojalgo.matrix.PrimitiveMatrix > {{8.976744186046512}} > > [LI] = class org.ojalgo.matrix.PrimitiveMatrix > {{0.0}, > {0.0}, > {0.0}} > > [SE] = class org.ojalgo.matrix.PrimitiveMatrix > {{0.0}} > > [SI] = class org.ojalgo.matrix.PrimitiveMatrix > {{1.1744186046511627}, > {0.37209302325581406}, > {0.4534883720930233}} > > I do not get any weights larger than 1.0, but I do get one weight smaller than 0.0. There's no constraint to restrict that! Add constraints for that and you should get the solutions you expect. > > Here's how I would find the efficient frontier: > > final BasicMatrix tmpCovars = ...; > final BasicMatrix tmpReturs = ...; > > final MarketEquilibrium tmpME = new MarketEquilibrium(tmpCovars); > > final MarkowitzModel tmpMarkowitz = new MarkowitzModel(tmpME, tmpReturs); > > for (int i = 1; i < 10; i++) { // Some different risk aversion factors > > tmpMarkowitz.setRiskAversion(new BigDecimal(i)); > > final List<BigDecimal> tmpWeights = tmpMarkowitz.getWeights(); > } > > ------------------------------------------------------------------------------ > EditLive Enterprise is the world's most technically advanced content > authoring tool. Experience the power of Track Changes, Inline Image > Editing and ensure content is compliant with Accessibility Checking. > http://p.sf.net/sfu/ephox-dev2dev_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user |
From: Anders P. <an...@op...> - 2011-06-15 08:35:21
|
2011-06-15: Released v30 http://ojalgo.org/change_log.html http://sourceforge.net/projects/ojalgo/files/ojAlgo/v30/ |
From: Simon G. <si...@ya...> - 2011-06-14 21:33:20
|
Yes, I have to finish the lower bounds, that part I'm not worried about. But I don't understand, I must be doing something different from how you did it, because I get: X class org.ojalgo.matrix.PrimitiveMatrix {{1.000000000000013}, {3.2333333333333143}, {-3.233333333333325}} This is how I get X: Result rslt = qdrtc_slvr.solve(); BasicMatrix rslt_mtrx = rslt.getSolution(); PhysicalStore<Double> result = rslt_mtrx.toPrimitiveStore(); System.out.println("\nX " + rslt_mtrx.toString()); I this correct? Thanks again for all your help by the way! SG ________________________________ From: Anders Peterson <an...@op...> To: oja...@li... Sent: Tue, June 14, 2011 4:34:59 PM Subject: Re: [ojAlgo-user] QuadraticSolver issue Here's what I get: [AE] = class org.ojalgo.matrix.PrimitiveMatrix {{1.0, 1.0, 1.0}} [BE] = class org.ojalgo.matrix.PrimitiveMatrix {{1.0}} [Q] = class org.ojalgo.matrix.PrimitiveMatrix {{4.0, 1.0, 2.0}, {1.0, 9.0, 1.0}, {2.0, 1.0, 16.0}} [C] = class org.ojalgo.matrix.PrimitiveMatrix {{10.0}, {15.0}, {18.0}} [AI] = class org.ojalgo.matrix.PrimitiveMatrix {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}} [BI] = class org.ojalgo.matrix.PrimitiveMatrix {{1.0}, {1.0}, {1.0}} [X] = class org.ojalgo.matrix.PrimitiveMatrix {{-0.17441860465116266}, {0.6279069767441859}, {0.5465116279069767}} [LE] = class org.ojalgo.matrix.PrimitiveMatrix {{8.976744186046512}} [LI] = class org.ojalgo.matrix.PrimitiveMatrix {{0.0}, {0.0}, {0.0}} [SE] = class org.ojalgo.matrix.PrimitiveMatrix {{0.0}} [SI] = class org.ojalgo.matrix.PrimitiveMatrix {{1.1744186046511627}, {0.37209302325581406}, {0.4534883720930233}} I do not get any weights larger than 1.0, but I do get one weight smaller than 0.0. There's no constraint to restrict that! Add constraints for that and you should get the solutions you expect. Here's how I would find the efficient frontier: final BasicMatrix tmpCovars = ...; final BasicMatrix tmpReturs = ...; final MarketEquilibrium tmpME = new MarketEquilibrium(tmpCovars); final MarkowitzModel tmpMarkowitz = new MarkowitzModel(tmpME, tmpReturs); for (int i = 1; i < 10; i++) { // Some different risk aversion factors tmpMarkowitz.setRiskAversion(new BigDecimal(i)); final List<BigDecimal> tmpWeights = tmpMarkowitz.getWeights(); } |
From: Anders P. <an...@op...> - 2011-06-14 21:05:09
|
Here's what I get: [AE] = class org.ojalgo.matrix.PrimitiveMatrix {{1.0, 1.0, 1.0}} [BE] = class org.ojalgo.matrix.PrimitiveMatrix {{1.0}} [Q] = class org.ojalgo.matrix.PrimitiveMatrix {{4.0, 1.0, 2.0}, {1.0, 9.0, 1.0}, {2.0, 1.0, 16.0}} [C] = class org.ojalgo.matrix.PrimitiveMatrix {{10.0}, {15.0}, {18.0}} [AI] = class org.ojalgo.matrix.PrimitiveMatrix {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}} [BI] = class org.ojalgo.matrix.PrimitiveMatrix {{1.0}, {1.0}, {1.0}} [X] = class org.ojalgo.matrix.PrimitiveMatrix {{-0.17441860465116266}, {0.6279069767441859}, {0.5465116279069767}} [LE] = class org.ojalgo.matrix.PrimitiveMatrix {{8.976744186046512}} [LI] = class org.ojalgo.matrix.PrimitiveMatrix {{0.0}, {0.0}, {0.0}} [SE] = class org.ojalgo.matrix.PrimitiveMatrix {{0.0}} [SI] = class org.ojalgo.matrix.PrimitiveMatrix {{1.1744186046511627}, {0.37209302325581406}, {0.4534883720930233}} I do not get any weights larger than 1.0, but I do get one weight smaller than 0.0. There's no constraint to restrict that! Add constraints for that and you should get the solutions you expect. Here's how I would find the efficient frontier: final BasicMatrix tmpCovars = ...; final BasicMatrix tmpReturs = ...; final MarketEquilibrium tmpME = new MarketEquilibrium(tmpCovars); final MarkowitzModel tmpMarkowitz = new MarkowitzModel(tmpME, tmpReturs); for (int i = 1; i < 10; i++) { // Some different risk aversion factors tmpMarkowitz.setRiskAversion(new BigDecimal(i)); final List<BigDecimal> tmpWeights = tmpMarkowitz.getWeights(); } On 14 jun 2011, at 17.59, Simon Greaves wrote: > Well I made this test case, but it requires a utilities library to load the files that contain the matrices. > You can see what I'm doing but it can't run. > I'm trying to to Efficient Frontier analysis. I tried using the ojAlgo.finance.porfolio.MarkowitzModel class, but I had a lot of trouble understanding the doc file. So I decided to to it myself using the QuadraticSolver, which I had less trouble with... > > I'm solving (as in the doc file): > min 1/2 [X]T[Q][X] - [C]T[X] > when [AE][X] == [BE] > and [AI][X] <= [BI] > > where (in the test case) > Q (Covariance Matrix): > 0.04 0.01 0.02 > 0.01 0.09 0.01 > 0.02 0.01 0.16 > > C (Expected Return Vector): > 0.10 > 0.15 > 0.18 > > The sum of X should be 1 since it's portfolio analysis, and I should use 100% of the investment on all activities. > And every Xi should be between 0 and 1. > So I made AE a vector of 1's and BE=1, so that takes care of sum(X)=1. And > AI is an identity matrix, and BI a 1 vector. So all Xi's should be <=1. > > But as a result I get, for example: [-2.4, 2.4, 1] > > So, I guess I'm not doing things right, and the inequality constraints aren't being respected... > From: Anders Peterson <an...@op...> > To: oja...@li... > Sent: Mon, June 13, 2011 4:55:38 PM > Subject: Re: [ojAlgo-user] QuadraticSolver issue > > What org.ojalgo.optimisation.State do you get? > > Why don't you construct a junit test case to show me exactly what you're doing. > > /Anders > <TempClass.java><COVAR_MATRIX.txt><RETURN.txt><VARS.txt>------------------------------------------------------------------------------ > EditLive Enterprise is the world's most technically advanced content > authoring tool. Experience the power of Track Changes, Inline Image > Editing and ensure content is compliant with Accessibility Checking. > http://p.sf.net/sfu/ephox-dev2dev_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user |
From: Simon G. <si...@ya...> - 2011-06-14 16:02:09
|
package temporaryapp; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author simon */ import ms2Utils.FileUtils; import org.ojalgo.optimisation.quadratic.*; import org.ojalgo.matrix.BasicMatrix; import org.ojalgo.optimisation.OptimisationSolver.Result; import org.ojalgo.optimisation.*; import org.ojalgo.matrix.store.*; public class TempClass { private double var, ret, fxd_ret; private double[] C; private double[][] Q; private String[] var_names; public static void TempClass() { } public static void main(String[] args) { double[][] cov_mat = strng2Dbl(FileUtils.fileToArray("/home/simon/Documents/Frontera Eficiente de Markowitz/COVAR_MATRIX.txt", "\t")); double[] r = strng2Dbl(FileUtils.fileToList("/home/simon/Documents/Frontera Eficiente de Markowitz/RETURN.txt")); String[] vars = FileUtils.fileToList("/home/simon/Documents/Frontera Eficiente de Markowitz/VARS.txt"); TempClass FE = new TempClass(); FE.setQ(cov_mat); FE.setC(r); FE.setVar_names(vars); // FE.minimizeVarMinusReturn(); FE.minimizeVarGivenReturn(0.3); } private void minimizeVarMinusReturn() { /* CREACION DE VARIABLES COMPATIBLES CON LA LIBRERIA OjAlgo */ double[][] c_temp = new double[Q[0].length][1]; for (int i = 0; i < c_temp.length; i++) { c_temp[i][0] = C[i]; } double[] ae = new double[Q.length]; double[][] ai = new double[Q[0].length][Q.length]; double[][] be = new double[1][1]; be[0][0] = 1; double[][] bi = new double[Q[0].length][1]; for (int i = 0; i < Q.length; i++) { ae[i] = 1; bi[i][0] = 1; } for (int i = 0; i < ai.length; i++) { for (int j = 0; j < ai[0].length; j++) { if (i == j) { ai[i][j] = 1; } else { ai[i][j] = 0; } } } final PhysicalStore<Double> Q_aux = PrimitiveDenseStore.FACTORY.copy(Q); final PhysicalStore<Double> C_aux = PrimitiveDenseStore.FACTORY.copy(c_temp); final PhysicalStore<Double> aux_AE = PrimitiveDenseStore.FACTORY.makeColumn(ae); final PhysicalStore<Double> aux_BE = PrimitiveDenseStore.FACTORY.copy(be); final PhysicalStore<Double> aux_AI = PrimitiveDenseStore.FACTORY.copy(ai); final PhysicalStore<Double> aux_BI = PrimitiveDenseStore.FACTORY.copy(bi); MatrixStore Q_ms = Q_aux.copy(); MatrixStore C_ms = C_aux.copy(); MatrixStore AE_ms = aux_AE.copy().transpose(); MatrixStore BE_ms = aux_BE.copy(); MatrixStore AI_ms = aux_AI.copy().transpose(); MatrixStore BI_ms = aux_BI.copy(); Variable[] VARIABLE = new Variable[var_names.length]; for (int i = 0; i < var_names.length; i++) { VARIABLE[i] = new Variable(var_names[i]); System.out.println(VARIABLE[i].toString()); } /* FIN CREACION DE VARIABLES COMPATIBLES CON LA LIBRERIA OjAlgo */ /* INICIALIZACION DE OBJETO QUADRATIC SOLVER */ QuadraticExpressionsModel qem = new QuadraticExpressionsModel(VARIABLE); QuadraticSolver.Builder qdrtc_slvr_bldr = new QuadraticSolver.Builder(qem).objective(Q_ms, C_ms).equalities(AE_ms, BE_ms).inequalities(AI_ms, BI_ms); QuadraticSolver qdrtc_slvr = qdrtc_slvr_bldr.build(); /* FIN INICIALIZACION DE OBJETO QUADRATIC SOLVER */ /* RESULTADOS */ Result rslt = qdrtc_slvr.solve(); BasicMatrix rslt_mtrx = rslt.getSolution(); PhysicalStore<Double> result = rslt_mtrx.toPrimitiveStore(); var = result.transpose().multiplyRight(Q_ms).multiplyRight(result).get(0, 0); ret = C_aux.transpose().multiplyRight(result).get(0, 0); System.out.println("\nRESULT"); for (int i = 0; i < rslt_mtrx.getRowDim(); i++) { for (int j = 0; j < rslt_mtrx.getColDim(); j++) { System.out.println(rslt_mtrx.doubleValue(i, j) + " "); } System.out.println(); } System.out.println("\nVARIANCE " + var); System.out.println("\nRETORNO " + ret); /* FIN RESULTADOS */ } private void minimizeVarGivenReturn(double given_return) { setFxd_ret(given_return); /* CREACION DE VARIABLES COMPATIBLES CON LA LIBRERIA OjAlgo * Aqui haremos que los retornos entren en las restricciones. Esto significa * que en vez de introducir los retornos en la matriz C, esta sera zeros * y los retornos entraran en AE. De esta manera se obtienen retornos * iguales a fxd_ret. */ double[][] c_temp = new double[Q[0].length][1]; for (int i = 0; i < c_temp.length; i++) { c_temp[i][0] = 0; } double[][] ae = new double[2][Q.length]; double[][] ai = new double[Q[0].length][Q.length]; double[][] be = new double[2][1]; be[0][0] = 1; be[1][0] = fxd_ret; double[][] bi = new double[Q[0].length][1]; for (int i = 0; i < Q.length; i++) { ae[0][i] = 1; ae[1][i] = C[i]; bi[i][0] = 1; } for (int i = 0; i < ai.length; i++) { for (int j = 0; j < ai[0].length; j++) { if (i == j) { ai[i][j] = 1; } else { ai[i][j] = 0; } } } final PhysicalStore<Double> Q_aux = PrimitiveDenseStore.FACTORY.copy(Q); final PhysicalStore<Double> C_aux = PrimitiveDenseStore.FACTORY.copy(c_temp); final PhysicalStore<Double> aux_AE = PrimitiveDenseStore.FACTORY.copy(ae); final PhysicalStore<Double> aux_BE = PrimitiveDenseStore.FACTORY.copy(be); final PhysicalStore<Double> aux_AI = PrimitiveDenseStore.FACTORY.copy(ai); final PhysicalStore<Double> aux_BI = PrimitiveDenseStore.FACTORY.copy(bi); MatrixStore Q_ms = Q_aux.copy(); MatrixStore C_ms = C_aux.copy(); MatrixStore AE_ms = aux_AE.copy(); MatrixStore BE_ms = aux_BE.copy(); MatrixStore AI_ms = aux_AI.copy().transpose(); MatrixStore BI_ms = aux_BI.copy(); Variable[] VARIABLE = new Variable[var_names.length]; for (int i = 0; i < var_names.length; i++) { VARIABLE[i] = new Variable(var_names[i]); System.out.println(VARIABLE[i].toString()); } /* FIN CREACION DE VARIABLES COMPATIBLES CON LA LIBRERIA OjAlgo */ /* INICIALIZACION DE OBJETO QUADRATIC SOLVER */ QuadraticExpressionsModel qem = new QuadraticExpressionsModel(VARIABLE); QuadraticSolver.Builder qdrtc_slvr_bldr = new QuadraticSolver.Builder(qem).objective(Q_ms, C_ms).equalities(AE_ms, BE_ms).inequalities(AI_ms, BI_ms); QuadraticSolver qdrtc_slvr = qdrtc_slvr_bldr.build(); /* FIN INICIALIZACION DE OBJETO QUADRATIC SOLVER */ /* RESULTADOS */ Result rslt = qdrtc_slvr.solve(); BasicMatrix rslt_mtrx = rslt.getSolution(); PhysicalStore<Double> result = rslt_mtrx.toPrimitiveStore(); var = result.transpose().multiplyRight(Q_ms).multiplyRight(result).get(0, 0); System.out.println("\nRESULT"); for (int i = 0; i < rslt_mtrx.getRowDim(); i++) { for (int j = 0; j < rslt_mtrx.getColDim(); j++) { System.out.println(rslt_mtrx.doubleValue(i, j) + " "); } System.out.println(); } System.out.println("\nVARIANCE " + var); /* FIN RESULTADOS */ } private static double[][] strng2Dbl(String[][] strng_mat) { double[][] mat = new double[strng_mat.length][strng_mat[0].length]; for (int i = 0; i < strng_mat.length; i++) { for (int j = 0; j < strng_mat[0].length; j++) { mat[i][j] = Double.parseDouble(strng_mat[i][j]); } } return mat; } private static double[] strng2Dbl(String[] strng_vec) { double[] vec = new double[strng_vec.length]; for (int i = 0; i < strng_vec.length; i++) { vec[i] = Double.parseDouble(strng_vec[i]); } return vec; } /** * @param R the R to set */ public void setC(double[] C) { this.C = C; } /** * @param W the W to set */ public void setQ(double[][] Q) { this.Q = Q; } /** * @return the var */ public double getVar() { return var; } /** * @return the ret */ public double getRet() { return ret; } /** * @param var_names the var_names to set */ public void setVar_names(String[] var_names) { this.var_names = var_names; } /** * @param fxd_ret the fxd_ret to set */ public void setFxd_ret(double fxd_ret) { this.fxd_ret = fxd_ret; } } |