Re: [ojAlgo-user] MarkowitzModel - what is optimal?
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Anthony E. <ant...@al...> - 2013-10-26 08:43:19
|
Ahhh ..... Wise words. Thanks for looking into this. I begin the walk of shame .... thanks for your help -----Original Message----- From: Anders Peterson Sent: Friday, October 25, 2013 7:27 PM To: ojAlgo ojAlgo Subject: Re: [ojAlgo-user] MarkowitzModel - what is optimal? If you change MatrixStore<Double> erms = cvs.builder().build(); into MatrixStore<Double> erms = ers; everything works fine! If you want a BasicMatrix (PrimitiveMatrix) instance, why do you first create a PrimitiveDenseStore? ..and if you want to create a PrimitiveDenseStore, then why set all values in a double[] first? Just call PrimitiveMatrix.getBuilder(int, int) or PrimitiveMatrix.getBuilder(int), set your values, and the call build(); http://code.google.com/p/ojalgo/wiki/GettingStartedLinearAlgebra I've modified ojAlgo to throw an IllegalArgumentException if the dimensions of the covariance matrix and the returns vector don't match. /Anders On 25 okt 2013, at 11:38, Anthony Edwards <ant...@al...> wrote: > Hi Anders > > Test.java attached. > > Thanks - really appreciate your help. > > Tony > > -----Original Message----- >> From: Anders Peterson > Sent: Friday, October 25, 2013 8:15 AM > To: ojAlgo ojAlgo > Subject: Re: [ojAlgo-user] MarkowitzModel - what is optimal? > > Please attach a *.java file that I can use to run the test/example without > any modifications. /Anders > > On 25 okt 2013, at 08:45, Anthony Edwards > <ant...@al...> wrote: > >> Hi Anders >> >> Ive setup a simple example and extracted the data and code that I am >> using >> (see below). Is there any chance you can take a look to see what I am >> doing >> wrong please? >> Thanks - appreciate your help. >> >> Tony >> >> -------------- JAVA Code >> >> public double [] getWeights(double [] er, double [][] cv, double [] >> minWgt, >> double [] maxWgt, boolean shortingAllowed, double targetReturn) { >> int row = er.length; >> int col = er.length; >> >> if (_dbg) System.out.println("AlgoMarkowitz.getWeights() - >> row="+row); >> >> PrimitiveDenseStore cvs = >> PrimitiveDenseStore.FACTORY.makeZero(row,col); >> for (int i = 0; i < row; i++) { >> for (int j = 0; j < col; j++) { >> cvs.set(i, j, cv[i][j]); >> if (_dbg) System.out.println("cv["+i+"]["+j+"] --> >> "+cv[i][j]); >> } >> } >> >> if (_dbg) { >> System.out.println("Covar()"); >> for (int r=0; r<row; r++) { >> StringBuilder sb = new StringBuilder(); >> for (int c=0; c<col; c++) { >> sb.append(Utils.formatp(cv[r][c]*100.0,2)+"% "); >> } >> System.out.println(sb.toString()); >> } >> } >> if (_dbg) System.out.println("AlgoMarkowitz.getWeights() - Covar >> Set"); >> >> PrimitiveDenseStore ers = >> PrimitiveDenseStore.FACTORY.makeZero(1,col); >> >> System.out.println(ers.countColumns()+" cols, "+ers.countRows()+" >> rows. "); >> for (int i = 0; i < row; i++) { >> if (_dbg) System.out.println("er["+i+"] --> "+er[i]); >> ers.set(0,i,er[i]); >> } >> if (_dbg) System.out.println("AlgoMarkowitz.getWeights() - >> Expeected >> Returns set"); >> System.out.println(""+ers.toString()); >> >> MatrixStore<Double> cvms = cvs.builder().build(); >> MatrixStore<Double> erms = cvs.builder().build(); >> >> BasicMatrix cvm = PrimitiveMatrix.FACTORY.copy(cvms); >> BasicMatrix erm = PrimitiveMatrix.FACTORY.copy(erms); >> >> MarkowitzModel markowitzModel = new MarkowitzModel(cvm, erm); >> >> markowitzModel.setTargetReturn(new BigDecimal(targetReturn)); >> >> System.out.println("Target Return = "+targetReturn); >> >> markowitzModel.setShortingAllowed(shortingAllowed); // False >> >> for (int i=0; i<er.length; i++) { >> markowitzModel.setLowerLimit(i, new BigDecimal(minWgt[i])); // >> 0.0 >> markowitzModel.setUpperLimit(i, new BigDecimal(maxWgt[i])); // >> 0.5 >> } >> >> double [] weights = new double[er.length]; >> final List<BigDecimal> re = markowitzModel.getWeights(); >> >> for (int nn = 0; nn < re.size(); nn++) { >> System.out.println("WGT["+nn+"] -->"+re.get(nn)+" --> >> "+re.get(nn).doubleValue() ); >> weights[nn] = re.get(nn).doubleValue(); >> } >> >> if (_dbg) { >> _meanReturn = markowitzModel.getMeanReturn(); >> _sharpeRatio = markowitzModel.getSharpeRatio(); >> _returnVariance = markowitzModel.getReturnVariance(); >> _lossProbability = markowitzModel.getLossProbability(); >> _volatility = markowitzModel.getVolatility(); >> _riskAversion = markowitzModel.getRiskAversion().doubleValue(); >> _resultSuccess = >> markowitzModel.getOptimisationState().isSuccess(); >> _resultFailure = >> markowitzModel.getOptimisationState().isFailure(); >> _resultOptimal = >> markowitzModel.getOptimisationState().isOptimal(); >> _resultValid = markowitzModel.getOptimisationState().isValid(); >> _resultDistinct = >> markowitzModel.getOptimisationState().isDistinct(); >> _resultFeasible = >> markowitzModel.getOptimisationState().isFeasible(); >> _resultUnexplored = >> markowitzModel.getOptimisationState().isUnexplored(); >> System.out.println("Model : Mean Return = "+_meanReturn); >> System.out.println("Model : Sharpe Ratio = "+_sharpeRatio); >> System.out.println("Model : Return Variance = >> "+_returnVariance); >> System.out.println("Model : Loss Probability = >> "+_lossProbability); >> System.out.println("Model : Volatility = "+_volatility); >> System.out.println("Model : Optimal Result = "+_resultOptimal); >> >> } >> return weights; >> } >> >> -------------- Input Data & Debugging Output >> >> AlgoMarkowitz.getWeights() - row=5 >> cv[0][0] --> 0.07843955354726917 >> cv[0][1] --> 0.010917730527180272 >> cv[0][2] --> 0.002617703304568447 >> cv[0][3] --> 0.0015367718256236922 >> cv[0][4] --> -0.007527000582038881 >> cv[1][0] --> 0.010917730527180272 >> cv[1][1] --> 0.010564384563013356 >> cv[1][2] --> 0.009059249805530113 >> cv[1][3] --> 0.007582121464911981 >> cv[1][4] --> -7.085933099370378E-4 >> cv[2][0] --> 0.002617703304568447 >> cv[2][1] --> 0.009059249805530113 >> cv[2][2] --> 0.019431716713640173 >> cv[2][3] --> 0.009759425136794966 >> cv[2][4] --> 0.0018625781574573416 >> cv[3][0] --> 0.0015367718256236922 >> cv[3][1] --> 0.007582121464911981 >> cv[3][2] --> 0.009759425136794966 >> cv[3][3] --> 0.008011558847885328 >> cv[3][4] --> -7.456016323607308E-4 >> cv[4][0] --> -0.007527000582038881 >> cv[4][1] --> -7.085933099370378E-4 >> cv[4][2] --> 0.0018625781574573416 >> cv[4][3] --> -7.456016323607308E-4 >> cv[4][4] --> 0.021224468167287004 >> Covar() >> 7.84% 1.09% 0.26% 0.15% -0.75% >> 1.09% 1.06% 0.91% 0.76% -0.07% >> 0.26% 0.91% 1.94% 0.98% 0.19% >> 0.15% 0.76% 0.98% 0.80% -0.07% >> -0.75% -0.07% 0.19% -0.07% 2.12% >> AlgoMarkowitz.getWeights() - Covar Set >> 5 cols, 1 rows. >> er[0] --> 0.08468993625647991 >> er[1] --> 0.05571192270463743 >> er[2] --> -0.053384899277547415 >> er[3] --> 0.0363486250229359 >> er[4] --> -0.10226497575724715 >> AlgoMarkowitz.getWeights() - Expeected Returns set >> org.ojalgo.matrix.store.PrimitiveDenseStore < 1 x 5 > >> { { 0.08468993625647991, >> 0.05571192270463743, -0.053384899277547415, >> 0.0363486250229359, -0.10226497575724715 } } >> Target Return = 0.05 >> WGT[0] -->0.5814 --> 0.5814 >> WGT[1] -->0.5 --> 0.5 >> WGT[2] -->0.0 --> 0.0 >> WGT[3] -->-0.1848 --> -0.1848 >> WGT[4] -->0.1034 --> 0.1034 >> Model : Mean Return = 0.05000133440241435 >> Model : Sharpe Ratio = 0.2739126173775806 >> Model : Return Variance = 0.03332261792985292 >> Model : Loss Probability = 0.42212046844930723 >> Model : Volatility = 0.18254483813532751 >> Model : Optimal Result = false >> >> >> >> >> >> -------------- HTML Output >> Run Strategy 'Narrow Test Strategy' >> Expected Returns >> 0 AAPL US Equity 0.08469 >> 1 QQQ US Equity 0.05571 >> 2 RWX US Equity -0.05338 >> 3 SPY US Equity 0.03635 >> 4 TLT US Equity -0.10226 >> Efficient Frontier >> Id Freeze Date Calculation Time TARGET_RETURN MEAN_RETURN >> SHARPE_RATIO LOSS_PROBABILITY RETURN_VARIANCE VOLATILITY >> RISK_AVERSION RESULT_SUCCESS RESULT_FAILURE RESULT_OPTIMAL >> RESULT_DISTINCT RESULT_FEASIBLE RESULT_UNEXPLORED WEIGHT_DELTA >> CALCULATION_FAILED >> 1382682291161 2013-09-30 2013-10-25 6:24:51 0.0500 0.0500 >> 0.2739 0.4221 0.0333 0.1825 1.0000 1.0000 0.0000 0.0000 >> 0.0000 0.0000 0.0000 0.0000 0.0000 AAPL US Equity / 0.581 >> QQQ US Equity / 0.500 RWX US Equity / 0.000 SPY US Equity / -0.185 >> TLT US Equity / 0.103 >> (Key point is that the RESULT_OPTIMAL is zero, and clearly the weights >> are >> outside the limits, but as this is also an infeasible result that that is >> fine) >> >> >> -----Original Message----- >>> From: Anders Peterson >> Sent: Thursday, October 24, 2013 8:39 PM >> To: ojAlgo ojAlgo >> Subject: Re: [ojAlgo-user] MarkowitzModel - what is optimal? >> >> Have you read the javadoc for the MarkowitzModel class and its methods? I >> think it does specify how it optimizes. >> >> The results you describe seems "wrong" but without a test case where I >> can >> see exactly what you do I cannot comment. >> >> If you don't like the MarkowitzModel class you can build your own >> optimization models directly using ExpressionsBasedModel >> >> http://ojalgo.org/generated/org/ojalgo/optimisation/ExpressionsBasedModel.html >> >> /Anders >> >> >> On 24 okt 2013, at 11:09, Anthony Edwards >> <ant...@al...> wrote: >> >>> Hi >>> >>> I’m still no nearer in understanding from any of the documentation or >>> javadocs what is optimal. When I specify a target return, I get a set >>> of >>> weights and various stats like ShapreRatio, Variance etc. What is the >>> optimiser maximising/minimising to arrive at this portfolio? Is it >>> maximising the sharp? or maximising mean return / variance? >>> >>> We are trying (with no joy at all) to compare an excel based solver with >>> ojalgo, but cant get anywhere near the same results for the same inputs >>> ..... >>> >>> Below is the covar and expected returns >>> >>> Covar() >>> 7.84% 1.09% 0.26% 0.15% -0.75% >>> 1.09% 1.06% 0.91% 0.76% -0.07% >>> 0.26% 0.91% 1.94% 0.98% 0.19% >>> 0.15% 0.76% 0.98% 0.80% -0.07% >>> -0.75% -0.07% 0.19% -0.07% 2.12% >>> er[0] --> 0.08468993625647991 >>> er[1] --> 0.05571192270463743 >>> er[2] --> -0.053384899277547415 >>> er[3] --> 0.0363486250229359 >>> er[4] --> -0.10226497575724715 >>> >>> The highest mean return solution we get is 4.4% with limits set at 0 to >>> 0.5 for all 5 assets. Clearly that is wrong. There is obviously a >>> solution at the mean of 8.4% and 5.5% with 50% in assets 1 and 2 and the >>> other weights at zero. When I run for a higher target return there is >>> no >>> optimal solution (and weights that outside limits then). The covar >>> looks >>> ok, the expected returns look ok, all assets are limited weights from 0 >>> to >>> 0.5 and the only variable I can control is the targetReturn(), so I’m at >>> a >>> loss here to know what else I can do. >>> >>> Any ideas? Thanks in advance ... >>> >>> Tony >>> ------------------------------------------------------------------------------ >>> October Webinars: Code for Performance >>> Free Intel webinars can help you accelerate application performance. >>> Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most >>> from >>> the latest Intel processors and coprocessors. See abstracts and register >>> > >>> http://pubads.g.doubleclick.net/gampad/clk?id=60135991&iu=/4140/ostg.clktrk_______________________________________________ >>> ojAlgo-user mailing list >>> ojA...@li... >>> https://lists.sourceforge.net/lists/listinfo/ojalgo-user >> >> >> ------------------------------------------------------------------------------ >> October Webinars: Code for Performance >> Free Intel webinars can help you accelerate application performance. >> Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most >> from >> the latest Intel processors and coprocessors. See abstracts and register >> > >> http://pubads.g.doubleclick.net/gampad/clk?id=60135991&iu=/4140/ostg.clktrk >> _______________________________________________ >> ojAlgo-user mailing list >> ojA...@li... >> https://lists.sourceforge.net/lists/listinfo/ojalgo-user >> >> >> ------------------------------------------------------------------------------ >> October Webinars: Code for Performance >> Free Intel webinars can help you accelerate application performance. >> Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most >> from >> the latest Intel processors and coprocessors. See abstracts and register >> > >> http://pubads.g.doubleclick.net/gampad/clk?id=60135991&iu=/4140/ostg.clktrk >> _______________________________________________ >> ojAlgo-user mailing list >> ojA...@li... >> https://lists.sourceforge.net/lists/listinfo/ojalgo-user >> >> > > > ------------------------------------------------------------------------------ > October Webinars: Code for Performance > Free Intel webinars can help you accelerate application performance. > Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most > from > the latest Intel processors and coprocessors. See abstracts and register > > http://pubads.g.doubleclick.net/gampad/clk?id=60135991&iu=/4140/ostg.clktrk > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > <Test.java>------------------------------------------------------------------------------ > October Webinars: Code for Performance > Free Intel webinars can help you accelerate application performance. > Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most > from > the latest Intel processors and coprocessors. See abstracts and register > > http://pubads.g.doubleclick.net/gampad/clk?id=60135991&iu=/4140/ostg.clktrk_______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user ------------------------------------------------------------------------------ October Webinars: Code for Performance Free Intel webinars can help you accelerate application performance. Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from the latest Intel processors and coprocessors. See abstracts and register > http://pubads.g.doubleclick.net/gampad/clk?id=60135991&iu=/4140/ostg.clktrk _______________________________________________ ojAlgo-user mailing list ojA...@li... https://lists.sourceforge.net/lists/listinfo/ojalgo-user |