Re: [ojAlgo-user] Markowitz optimisation
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Arthur M. <art...@be...> - 2008-02-08 15:23:38
|
Hi Anders, Sorry - but I've got another example of a NullPointerException. Thanks for patching all of these - it's taken a while to get another one. I'd give you all the examples at once but they're dependent on previous optimisation results. regards Arthur public void testojAlgo6() { // create expected returns matrix PrimitiveMatrix expectedReturnsMatrix = new PrimitiveMatrix(new double[][] { {9.997829}, {10.008909}, {10.010849}, {9.998919}, {10.055549}, {9.999127}, {9.999720}, {10.049002}, {9.988769}, {9.990095}}); // create covariance matrix PrimitiveMatrix covarianceMatrix = new PrimitiveMatrix(new double[][] {{0.014661954677318977, 3.459112088561122E-4, 0.007798752920910871, 0.0020921425081866503, 0.001846944297640248, 1.0531906931335766E-4, -2.7515614291198E-4, 0.0034083900074454894, 1.1859491261103433E-4, -0.0027421673864628264}, {3.459112088561122E-4, 0.008695862475003915, 0.004154360841751649, -2.661685231819661E-4, -0.0015999007544258263, 3.590680217774603E-4, -0.00186976624370318, 0.0010975416828213752, -5.512038393911129E-4, -0.0010605923775744853}, {0.007798752920910871, 0.004154360841751649, 0.032945930970836965, 0.0037716078815399324, -2.2919474365382624E-4, 3.3938035033219876E-4, -0.0015613122026082874, 0.0010975697179894332, 2.296422665244149E-4, -0.001709517941787044}, {0.0020921425081866503, -2.661685231819661E-4, 0.0037716078815399324, 0.0057162979859706736, 5.573137056500744E-4, 4.91132887765294E-4, -9.94830474250937E-4, 8.331708084069932E-4, -6.595917138470072E-4, -0.0018693519327569541}, {0.001846944297640248, -0.0015999007544258263, -2.2919474365382624E-4, 5.573137056500744E-4, 0.03230071314144326, -2.2320789666419312E-4, -2.2639506820057415E-4, 0.010695663287043154, 0.0014569847730040847, 0.002160537177809949}, {1.0531906931335766E-4, 3.590680217774603E-4, 3.3938035033219876E-4, 4.91132887765294E-4, -2.2320789666419312E-4, 0.0017540170708301957, 5.153195618913916E-5, 7.339825618468765E-4, -9.309096233432093E-6, -1.814362059740286E-4}, {-2.7515614291198E-4, -0.00186976624370318, -0.0015613122026082874, -9.94830474250937E-4, -2.2639506820057415E-4, 5.153195618913916E-5, 0.00809348822665732, -0.0017672866424053742, 3.058672988166145E-4, 0.001201578905822851}, {0.0034083900074454894, 0.0010975416828213752, 0.0010975697179894332, 8.331708084069932E-4, 0.010695663287043154, 7.339825618468765E-4, -0.0017672866424053742, 0.013913761913235494, 0.0012785124957521252, 5.298368056593439E-4}, {1.1859491261103433E-4, -5.512038393911129E-4, 2.296422665244149E-4, -6.595917138470072E-4, 0.0014569847730040847, -9.309096233432093E-6, 3.058672988166145E-4, 0.0012785124957521252, 0.004650801896027841, 5.437156659657787E-4}, {-0.0027421673864628264, -0.0010605923775744853, -0.001709517941787044, -0.0018693519327569541, 0.002160537177809949, -1.814362059740286E-4, 0.001201578905822851, 5.298368056593439E-4, 5.437156659657787E-4, 0.007359495478781133}}); // create asset variables - cost and weighting constraints Variable[] tmpVariables = new Variable[expectedReturnsMatrix.getRowDim()]; for (int i = 0; i < tmpVariables.length; i++) { tmpVariables[i] = new Variable(""); tmpVariables[i].setContribution(expectedReturnsMatrix.toBigDecimal(i, 0).negate()); // set the constraints on the asset weights // require at least a 8% allocation to each asset tmpVariables[i].setLowerLimit(new BigDecimal("0.08")); // require no more than 12% allocation to each asset tmpVariables[i].setUpperLimit(new BigDecimal("0.12")); } QuadraticModel quadModel = new QuadraticModel(tmpVariables); Expression tmpExpr = quadModel.addCorrelationExpression("Variance", covarianceMatrix.toBigStore().toArray()); BigDecimal riskAversion = BigMath.PI.multiply(BigMath.E); tmpExpr.setContribution(riskAversion.multiply(BigMath.HALF)); // set balance expression tmpExpr = quadModel.addSimpleWeightExpression("Balance"); tmpExpr.setLowerLimit(BigMath.ONE); tmpExpr.setUpperLimit(BigMath.ONE); // exception here... BasicMatrix bm = BigMatrix.FACTORY.buildColumnVector(quadModel.getDefaultSolver().getSolution()); System.out.print(bm.toString()); } ******************************************************************** This message and any attachment are confidential. If you are not the intended recipient please contact the sender, delete this message and any attachment from your system and do not disclose, copy or distribute the contents to any other person. Beach Horizon LLP (Beach Horizon) is not responsible for any information contained in this email. Beach Horizon reserves the right to monitor all email messages passing through its network. ******************************************************************** |