Re: [ojAlgo-user] Markowitz Model Problem
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Anders P. <an...@op...> - 2009-01-20 10:20:31
|
On 16 jan 2009, at 07.25, Anders Peterson wrote: > On 16 jan 2009, at 02.28, Ruan Weichao wrote: > >> Hi, >> >> Yes, that's the expected excess returns of my program. >> >> Is it right? >> >> Can the "expected excess returns" be negative? > > > I have no way of knowing what model/problem you want to solve... > > In a general case excess returns (what you get for taking a risk) > should not be negative. If they are, you do not need to optimise > anything. Just invest everything in the risk free asset. > > I'm not sure exactly why you get those negative (constraint > breaking) portfolio weights. I'm going to look into this, but > later. Don't hold your breath. For you, I think the solution is to > work on your model. This is fixed in CVS now. Your model does not get those constraint breaking portfolio weights anymore. The solution I get is 100% of the last instrument (the one with 0.0 excess return) which seems reasonable. /Anders > >> On Thu, 15 Jan 2009 14:09:43 +0100, Anders Peterson wrote >>> Your expected excess returns matrix looks like this: >>> >>> class org.ojalgo.matrix.BigMatrix >>> {{-0.010638291263564232}, >>> {-0.013500370827906071}, >>> {-0.011390037735101773}, >>> {-0.010385042339767682}, >>> {-0.0003812208389845893}, >>> {-0.002315505853720011}, >>> {0}} >>> >>> Is that correct? >>> >>> Earlier you wrote "And i found that if i change the nagetive >>> ExceptedReturn to positive, the result will be fine." >>> >>> /Anders >>> >>> On 15 jan 2009, at 11.10, Ruan Weichao wrote: >>> >>>> Hi,i am using v25 and here's my code: >>>> >>>> / >>>> ******************************************************************* >>>> *** >>>> **********/ >>>> >>>> import java.math.BigDecimal; >>>> import java.util.List; >>>> >>>> import org.ojalgo.finance.portfolio.MarketEquilibrium; >>>> import org.ojalgo.finance.portfolio.MarkowitzModel; >>>> import org.ojalgo.matrix.*; >>>> import org.ojalgo.matrix.store.BigDenseStore; >>>> import org.ojalgo.matrix.store.MatrixStore; >>>> >>>> >>>> public class TestMarkowitz { >>>> >>>> public double getCovarance(double[] value1, double[] value2){ >>>> >>>> int n = value1.length; >>>> double averOne = 0; >>>> double averTwo = 0; >>>> for(int i=0;i<n;i++) >>>> { >>>> averOne+=value1[i]; >>>> averTwo+=value2[i]; >>>> } >>>> averOne /= n; >>>> averTwo /= n; >>>> >>>> double sum = 0; >>>> for(int i=0;i<n;i++) >>>> sum+=(value1[i]-averOne)*(value2[i]-averTwo); >>>> >>>> return sum / (n-1); >>>> } >>>> >>>> public BasicMatrix getACovariances(double[][] returns){ >>>> >>>> int row = returns.length; >>>> int col = returns[0].length; >>>> MatrixStore<BigDecimal> a = BigDenseStore.FACTORY..makeZero(row, >>>> col); >>>> BasicMatrix covariances = new BigMatrix(a); >>>> >>>> for(int i=1;i<=row;i++){ >>>> for(int j=i;j<=col;j++){ >>>> double tmp = this.getCovarance(returns[i-1], returns[j-1]); >>>> covariances = covariances.set(i-1, j-1,tmp); >>>> covariances = covariances.set(j-1, i-1,tmp); >>>> } >>>> } >>>> return covariances; >>>> } >>>> >>>> public BasicMatrix getAnExpectedExcessReturns(double[][] returns){ >>>> >>>> int row = returns.length; >>>> int col = returns[0].length; >>>> >>>> MatrixStore<BigDecimal> a = BigDenseStore.FACTORY..makeZero >>>> (row, 1); >>>> BasicMatrix expectedExcessReturns = new BigMatrix(a); >>>> >>>> double riskFreeReturn = 0; >>>> for(int i=1;i<=col;i++) >>>> riskFreeReturn+=returns[row-1][i-1]; >>>> riskFreeReturn/=row; >>>> >>>> for(int i=1;i<=row;i++){ >>>> >>>> double tmp = 0; >>>> for(int j=1;j<=col;j++) >>>> tmp+=returns[i-1][j-1]; >>>> tmp/=col; >>>> >>>> expectedExcessReturns = expectedExcessReturns.set(i-1, 0, tmp- >>>> riskFreeReturn); >>>> } >>>> >>>> return expectedExcessReturns; >>>> } >>>> >>>> public static void main(String[] args) >>>> { >>>> int assetNum=7; >>>> double[][] assets_return={ >>>> {-1.5905837442343828E-4, -0.03062360801781757, >>>> -0.029857534032853142, -0.011811692726036832, >>>> -0.017972310602803136, 0.017338003502626997, 0.0}, >>>> {-0.02757158006362653, -0.02562704471101405, >>>> -0.011751538891997735, -0.024915062287655786, -0.01684088269454123, >>>> 0.013585351447135364, 0.0}, >>>> {-0.00699300699300693, -0.033802816901408676, >>>> -0.04675196850393671, -0.021166752710376546, -0.007911392405063583, >>>> 0.03827751196172254, 0.0}, >>>> {-0.007626310772164015, 0.0038424591738713027, >>>> 0.02488038277511978, 0.025210084033613675, -0.02003642987249557, >>>> -0.09758364312267642, 0.0}, >>>> {-0.03965053763440893, 0.021693491952414375, >>>> 0.01643835616438392, -0.007412398921833087, 0.01765105227427014, >>>> -0.010006671114076025, 0.0}, >>>> {-0.017821782178217872, 0.005040322580645311, >>>> 0.006018054162487363, 9.008107296569024E-4, 0.002999999999999824, >>>> -0.01196410767696908, 0.0}, >>>> {2.630552127527583E-4, 2.5867028174649627E-4, >>>> 2.3866431891514327E-4, 1.9564035993080523E-4, 2.351016690966669E-4, >>>> 1.9070675120065465E-4, 0.0} >>>> }; >>>> >>>> TestMarkowitz tm = new TestMarkowitz(); >>>> BasicMatrix covariances = tm.getACovariances(assets_return); >>>> BasicMatrix expectedExcessReturns = tm.getAnExpectedExcessReturns >>>> (assets_return); >>>> BigDecimal riskAversion = new BigDecimal(1.0); >>>> >>>> MarketEquilibrium marketEquilibrium = new MarketEquilibrium >>>> (covariances,riskAversion); >>>> MarkowitzModel markowitzModel = new MarkowitzModel >>>> (marketEquilibrium,expectedExcessReturns); >>>> >>>> for(int i=0;i<assetNum;i++){ >>>> markowitzModel.setLowerLimit(i, new BigDecimal(0.0)); >>>> markowitzModel.setUpperLimit(i, new BigDecimal(1.0)); >>>> } >>>> List<BigDecimal> re = markowitzModel.getWeights(); >>>> >>>> System.out.println("=======result===================="); >>>> for(int i=0;i<re.size();i++) >>>> System.out.println(re.get(i)); >>>> >>>> return; >>>> } >>>> >>>> } >>>> >>>> / >>>> ******************************************************************* >>>> *** >>>> **********/ >>>> >>>> and the result turn out as: >>>> >>>> -0.25 >>>> -0.25 >>>> -0.25 >>>> -0.25 >>>> 0.75 >>>> 0.75 >>>> 0.75 >>>> >>>> / >>>> ******************************************************************* >>>> *** >>>> ******/ >>>> >>>> If you find out any problem, just let me know , thanks a lot! >>>> >>>> On Thu, 15 Jan 2009 09:20:52 +0100, Anders Peterson wrote >>>>> You have to send code (snippets) that I can execute. >>>>> >>>>> Are you using v25 or the latest from cvs? >>>>> >>>>> How do you instantiate the ActiveSetSolver? >>>>> >>>>> What's the correct solution? >>>>> >>>>> You say that if you change the sign of "C" you get an ok result. >>>>> What does this mean? Did you make a mistake with the sign, or do >>>>> you suspect there is a bug in ojAlgo? >>>>> >>>>> I need better info regarding what you do, and what the problem is! >>>>> >>>>> /Anders >>>>> >>>>> On 15 jan 2009, at 06.14, Ruan Weichao wrote: >>>>> >>>>>> >>>>>> Thank you for your response. >>>>>> >>>>>> Here's my model's detail: >>>>>> >>>>>> [AE]={1.0, 1.0, 1.0, 1.0, 1.0, 1.0, >>>> 1.0} >>>>>> >>>>>> >>>>>> [be]={1.0} >>>>>> >>>>>> >>>>>> [Q]={3.048907897157133E-4, 1.6671472561019247E-4, >>>>>> 4.4500080981934345E-4, -5.389129745055723E-4, >>>>>> -2.6090705011393183E-4, -1.2633284900760366E-4, >>>>>> -6.485428846447651E-7}, >>>>>> {1.6671472561019247E-4, 2.341985572849691E-4, >>>>>> 2.9113916450678265E-4, -4.5760873539850514E-4, >>>>>> 1.3078636134987255E-5, -2.354289901013046E-5, >>>> -7.578030042426654E-7}, >>>>>> {4.4500080981934345E-4, 2.9113916450678265E-4, >>>> 7.46023915996829E-4, >>>>>> -0.0010247176498305568, -2.6745504327902895E-4, >>>>>> -1.6563544154823496E-4, -8.293698990696063E-7}, >>>>>> {-5.389129745055723E-4, -4.5760873539850514E-4, >>>>>> -0.0010247176498305568, 0.001754169535149865, >>>>>> 2.0293065310212377E-4, 2.1401092557826588E-4, >>>> 1.0252846778608953E-7}, >>>>>> {-2.6090705011393183E-4, 1.3078636134987255E-5, >>>>>> -2.6745504327902895E-4, 2.0293065310212377E-4, >>>>>> 4.632320892679136E-4, 1.7969731066037214E-4, >>>> 2.4953495129362833E-8}, >>>>>> {-1.2633284900760366E-4, -2.354289901013046E-5, >>>>>> -1.6563544154823496E-4, 2.1401092557826588E-4, >>>>>> 1.7969731066037214E-4, 8..346410612364995E-5, >>>> -7.02099350897589E-8}, >>>>>> {-6.485428846447651E-7, -7.578030042426654E-7, >>>>>> -8.293698990696063E-7, 1.0252846778608953E-7, >>>>>> 2.4953495129362833E-8, -7.02099350897589E-8, >>>> 8.367244992498656E-9}} >>>>>> >>>>>> [c]={{0.010638291263564232}, >>>>>> {0.013500370827906071}, >>>>>> {0.011390037735101773}, >>>>>> {0.010385042339767682}, >>>>>> {3.812208389845893E-4}, >>>>>> {0.002315505853720011}, >>>>>> {0.0}} >>>>>> >>>>>> >>>>>> [AI]={{1.0, 0.0, 0.0, 0.0, 0.0, >>>> 0.0, 0.0}, >>>>>> {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0}, >>>>>> {0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0}, >>>>>> {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0}, >>>>>> {0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0}, >>>>>> {0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0}, >>>>>> {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0}, >>>>>> {-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, >>>>>> {0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0}, >>>>>> {0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0}, >>>>>> {0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0}, >>>>>> {0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0}, >>>>>> {0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0}, >>>>>> {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0}} >>>>>> >>>>>> [bi]={{1.0}, >>>>>> {1.0}, >>>>>> {1.0}, >>>>>> {1.0}, >>>>>> {1.0}, >>>>>> {1.0}, >>>>>> {1.0}, >>>>>> {0.0}, >>>>>> {0.0}, >>>>>> {0.0}, >>>>>> {0.0}, >>>>>> {0.0}, >>>>>> {0.0}, >>>>>> {0.0}} >>>>>> >>>>>> And here's the result: >>>>>> >>>>>> -0.25 >>>>>> -0.25 >>>>>> -0.25 >>>>>> -0.25 >>>>>> 0.75 >>>>>> 0.75 >>>>>> 0.75 >>>>>> >>>>>> And i found that if i change the nagetive ExceptedReturn to >>>>>> positive, the result will be fine. >>>>>> >>>>>> >>>>>> On Wed, 14 Jan 2009 09:27:53 +0100, Anders Peterson wrote >>>>>>> How small are the negative values? >>>>>>> >>>>>>> Give me a unit test (something that I can easily integrate >>>> with the >>>>>>> existing junit tests) that shows what you do, and where/how you >>>>>>> think it goes wrong. >>>>>>> >>>>>>> /Anders >>>>>>> >>>>>>> On 14 jan 2009, at 03.58, Ruan Weichao wrote: >>>>>>> >>>>>>>> >>>>>>>> Hi, >>>>>>>> >>>>>>>> I use the Markowitz model to calculate the optimize some >>>> assets' >>>>>>>> weights, >>>>>>>> >>>>>>>> and set all the assets' lowerlimit with Zero and set all the >>>>>>>> upperlimits with >>>>>>>> >>>>>>>> One, but it turns out with some negative value which are >>>> little >>>>>>>> than Zero, it >>>>>>>> >>>>>>>> seems that the lowerlimit does not work. >>>>>>>> >>>>>>>> So I want to ask what should I do it to confirm the >>>> lowerlimit? >>>>>>>> >>>>>>>> -- >>>>>>>> Best regards >>>>>>>> >>>>>>>> weichao >>>>>>>> >>>>>>>> >>>>>> >>>> ------------------------------------------------------------------- >>>> --- >>>>>>>> -------- >>>>>>>> This SF.net email is sponsored by: >>>>>>>> SourcForge Community >>>>>>>> SourceForge wants to tell your story. >>>>>>>> http://p.sf.net/sfu/sf-spreadtheword >>>>>>>> _______________________________________________ >>>>>>>> ojAlgo-user mailing list >>>>>>>> ojA...@li... >>>>>>>> https://lists.sourceforge.net/lists/listinfo/ojalgo-user >>>>>>>> >>>>>>>> >>>>>>> >>>>>>> >>>>>> >>>> ------------------------------------------------------------------- >>>> --- >>>>>> ------ >>>>>> -- >>>>>>> This SF.net email is sponsored by: >>>>>>> SourcForge Community >>>>>>> SourceForge wants to tell your story. >>>>>>> http://p.sf.net/sfu/sf-spreadtheword >>>>>>> _______________________________________________ >>>>>>> ojAlgo-user mailing list >>>>>>> ojA...@li... >>>>>>> https://lists.sourceforge.net/lists/listinfo/ojalgo-user >>>>>> >>>>>> >>>>>> -- >>>>>> Best regards >>>>>> >>>>>> >>>> ------------------------------------------------------------------- >>>> --- >>>>>> -------- >>>>>> This SF.net email is sponsored by: >>>>>> SourcForge Community >>>>>> SourceForge wants to tell your story. >>>>>> http://p.sf.net/sfu/sf- >>>>>> spreadtheword_______________________________________________ >>>>>> ojAlgo-user mailing list >>>>>> ojA...@li... >>>>>> https://lists.sourceforge.net/lists/listinfo/ojalgo-user >>>>> >>>>> >>>> ------------------------------------------------------------------- >>>> --- >>>> ------ >>>> -- >>>>> This SF.net email is sponsored by: >>>>> SourcForge Community >>>>> SourceForge wants to tell your story. >>>>> http://p.sf.net/sfu/sf-spreadtheword >>>>> _______________________________________________ >>>>> ojAlgo-user mailing list >>>>> ojA...@li... >>>>> https://lists.sourceforge.net/lists/listinfo/ojalgo-user >>>> >>>> >>>> -- >>>> Best regards >>>> >>>> ------------------------------------------------------------------- >>>> --- >>>> -------- >>>> This SF.net email is sponsored by: >>>> SourcForge Community >>>> SourceForge wants to tell your story. >>>> http://p.sf.net/sfu/sf- >>>> spreadtheword_______________________________________________ >>>> ojAlgo-user mailing list >>>> ojA...@li... >>>> https://lists.sourceforge.net/lists/listinfo/ojalgo-user >>> >>> -------------------------------------------------------------------- >>> -------- >> -- >>> This SF.net email is sponsored by: >>> SourcForge Community >>> SourceForge wants to tell your story. >>> http://p.sf.net/sfu/sf-spreadtheword >>> _______________________________________________ >>> ojAlgo-user mailing list >>> ojA...@li... >>> https://lists.sourceforge.net/lists/listinfo/ojalgo-user >> >> >> -- >> Best regards >> >> >> --------------------------------------------------------------------- >> --------- >> This SF.net email is sponsored by: >> SourcForge Community >> SourceForge wants to tell your story. >> http://p.sf.net/sfu/sf-spreadtheword >> _______________________________________________ >> ojAlgo-user mailing list >> ojA...@li... >> https://lists.sourceforge.net/lists/listinfo/ojalgo-user > |