Thread: [ojAlgo-user] Markowitz Model Problem
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Ruan W. <is0...@ma...> - 2009-01-14 02:59:07
|
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 |
From: Anders P. <an...@op...> - 2009-01-14 08:28:07
|
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 > > |
From: <bru...@uk...> - 2009-01-14 08:41:55
|
Hi, even if I am not the expert ni this topic, I think your problem is that actually your model fails to find a solution and then just gives you wrong results that are beyond your constraints. I had this problem to when ysing a sytem with ActiveSetSolver. Before looking at your solution you should check that the optimization actually did not failed. BasicMatrix[] system = new BasicMatrix[6]; //[AE], [be], [Q], [c], [AI], [bi] ..... //create your matrix here ..... ActiveSetSolver as = new ActiveSetSolver(system); OptimisationSolver.Result res = as.solve(); State state = res.getState(); if(state != state.FAILED ) { solution = res.getSolution(); } Let me know if this solve your problem. Regards BF Internet is0...@ma... 14/01/2009 02:58 Please respond to oja...@li... To oja...@li... cc Subject [ojAlgo-user] Markowitz Model Problem 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 communication is confidential, may be privileged and is meant only for the intended recipient. If you are not the intended recipient, please notify the sender by reply and delete the message from your system. Any unauthorised dissemination, distribution or copying hereof is prohibited. BNP Paribas Trust Corporation UK Limited, BNP Paribas UK Limited, BNP Paribas Commodity Futures Limited, BNP Paribas Asset Management UK Limited and Investment Fund Services Limited are authorised and regulated by the Financial Services Authority. BNP Paribas London Branch and BNP Paribas Private Bank London Branch are authorised by the CECEI and supervised by the Commission Bancaire. BNP Paribas London Branch is authorised and subject to limited regulation by the Financial Services Authority. Details about the extent of our authorisation and regulation by the Financial Services Authority are available from us on request. BNP Paribas is also a member of the London Stock Exchange. BNP Paribas Private Bank London Branch is subject to limited regulation by the Financial Services Authority. Details about the extent of our authorisation and regulation by the Financial Services Authority are available from us on request. BNP Paribas Securities Services London Branch is authorised by the CECEI and supervised by the AMF, and subject to limited regulation by the Financial Services Authority. Details on the extent of our regulation by the Financial Services Authority are available from us on request. BNP Paribas Securities Services is also a member of the London Stock Exchange. |
From: Anders P. <an...@op...> - 2009-01-14 09:03:52
|
I believe this is the first time a list member (other than me) tried to help another member! On 14 jan 2009, at 09.26, bru...@uk... wrote: > Hi, even if I am not the expert ni this topic, I think your problem > is that actually your model fails to find a solution and then just > gives you wrong results that are beyond your constraints. Please give me test cases for things you think should work, but currently doesn't. > I had this problem to when ysing a sytem with ActiveSetSolver. > Before looking at your solution you should check that the > optimization actually did not failed. This could be the case, but I'd like to see the actual case. With all lower limits set to 0 and all upper limits set to 1 (and no other constraints) the ActiveSetSolver should be able to find a solution. The quadratic solvers has been much improved since the release of version 25. If it's not to much trouble I'd recommend getting the latest source code from CVS. There's still a few things I'd like to fix before releasing version 26. /Anders > BasicMatrix[] system = new BasicMatrix[6]; > //[AE], [be], [Q], [c], [AI], [bi] > ..... > //create your matrix here > ..... > > ActiveSetSolver as = new ActiveSetSolver(system); > > OptimisationSolver.Result res = as.solve(); > State state = res.getState(); > > if(state != state.FAILED ) { > solution = res.getSolution(); > } > Let me know if this solve your problem. > Regards > BF > > > > > Internet > is0...@ma... > 14/01/2009 02:58 > Please respond to > oja...@li... > > To > oja...@li... > cc > Subject > [ojAlgo-user] Markowitz Model Problem > > > > > > > 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 communication is confidential, may be privileged and is meant > only for the intended recipient. If you are not the intended > recipient, please notify the sender by reply and delete the message > from your system. Any unauthorised dissemination, distribution or > copying hereof is prohibited. BNP Paribas Trust Corporation UK > Limited, BNP Paribas UK Limited, BNP Paribas Commodity Futures > Limited, BNP Paribas Asset Management UK Limited and Investment > Fund Services Limited are authorised and regulated by the Financial > Services Authority. BNP Paribas London Branch and BNP Paribas > Private Bank London Branch are authorised by the CECEI and > supervised by the Commission Bancaire. BNP Paribas London Branch is > authorised and subject to limited regulation by the Financial > Services Authority. Details about the extent of our authorisation > and regulation by the Financial Services Authority are available > from us on request. BNP Paribas is also a member of the London > Stock Exchange. BNP Paribas Private Bank London Branch is subject > to limited regulation by the Financial Services Authority. Details > about the extent of our authorisation and regulation by the > Financial Services Authority are available from us on request. BNP > Paribas Securities Services London Branch is authorised by the > CECEI and supervised by the AMF, and subject to limited regulation > by the Financial Services Authority. Details on the extent of our > regulation by the Financial Services Authority are available from > us on request. BNP Paribas Securities Services is also a member of > the London Stock Exchange. > ---------------------------------------------------------------------- > -------- > 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 |
From: Ruan W. <is0...@ma...> - 2009-01-15 05:14:34
|
<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> <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} </p> <p> <br />[be]={1.0} </p> <p> <br />[Q]={3.048907897157133E-4, 1.6671472561019247E-4, 4.4500080981934345E-4, -5.389129745055723E-4, -2.6090705011393183E-4, -1.2633284900760366E-4, -6.485428846447651E-7}, <br />{1.6671472561019247E-4, 2.341985572849691E-4, 2.9113916450678265E-4, -4.5760873539850514E-4, 1.3078636134987255E-5, -2.354289901013046E-5, -7.578030042426654E-7}, <br />{4.4500080981934345E-4, 2.9113916450678265E-4, 7.46023915996829E-4, -0.0010247176498305568, -2.6745504327902895E-4, -1.6563544154823496E-4, -8.293698990696063E-7}, <br />{-5.389129745055723E-4, -4.5760873539850514E-4, -0.0010247176498305568, 0.001754169535149865, 2.0293065310212377E-4, 2.1401092557826588E-4, 1.0252846778608953E-7}, <br />{-2.6090705011393183E-4, 1.3078636134987255E-5, -2.6745504327902895E-4, 2.0293065310212377E-4, 4.632320892679136E-4, 1.7969731066037214E-4, 2.4953495129362833E-8}, <br />{-1.2633284900760366E-4, -2.354289901013046E-5, -1.6563544154823496E-4, 2.1401092557826588E-4, 1.7969731066037214E-4, 8.346410612364995E-5, -7.02099350897589E-8}, <br />{-6.485428846447651E-7, -7.578030042426654E-7, -8.293698990696063E-7, 1.0252846778608953E-7, 2.4953495129362833E-8, -7.02099350897589E-8, 8.367244992498656E-9}}</p> <p>[c]={{0.010638291263564232}, <br />{0.013500370827906071}, <br />{0.011390037735101773}, <br />{0.010385042339767682}, <br />{3.812208389845893E-4}, <br />{0.002315505853720011}, <br />{0.0}} </p> <p> <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 /></p> <p>[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 /></p> <p>And here's the result:</p> <p>-0.25 <br />-0.25 <br />-0.25 <br />-0.25 <br />0.75 <br />0.75 <br />0.75</p> <p>And i found that if i change the nagetive ExceptedReturn to positive, the result will be fine.</p> <p> <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 />> > 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 />> 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 /></p> </font> </BODY> </HTML> |
From: Anders P. <an...@op...> - 2009-01-15 08:21:00
|
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 |
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> |
From: Anders P. <an...@op...> - 2009-01-15 13:09:57
|
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 |
From: Ruan W. <is0...@ma...> - 2009-01-16 01:28:28
|
Hi, Yes, that's the expected excess returns of my program. Is it right? Can the "expected excess returns" be negative? 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 |
From: Anders P. <an...@op...> - 2009-01-16 06:25:55
|
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. /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 |
From: Ruan W. <is0...@ma...> - 2009-01-16 07:53:10
|
I think i get your point. So I should not use negative excess returns in my model. Thank you very much! On Fri, 16 Jan 2009 07:25:41 +0100, 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. > > /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 > > ---------------------------------------------------------------------------- -- > 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 |
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 > |