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