Re: [ojAlgo-user] Markowitz optimisation
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Arthur M. <art...@be...> - 2008-02-04 12:02:48
|
Hi Anders, I've been using the new version for a while and it has been working well on= many different constraints - thanks for all the work you've put in here. Unfortunately I've got another issue with NullPointerExceptions. I'd run t= hrough the code myself but I don't understand the maths behind the constrai= nts - hence my use of your library. Could you have another look please regards Arthur public void testojAlgo() { // create expected returns matrix PrimitiveMatrix expectedReturnsMatrix =3D new PrimitiveMatrix(new double[= ][] { {9.994620}, {10.011389}, {10.004353}, {9.998293}, {10.056851}, {9.997= 920}, {9.999011}, {10.050971}, {9.989124}, {9.989912}}); // create covariance matrix PrimitiveMatrix covarianceMatrix =3D new PrimitiveMatrix(new double[][] {= {0.014531344652473037, 4.444675045533674E-4, 0.007234717654072837, -9.45531= 2097865225E-4, 0.0016345464996349748, 1.5256808879495097E-4, 0.002263258187= 49439, 0.003534367267672946, -4.2669306842991344E-5, 6.902267133060073E-5}, {4.444675045533674E-4, 0.008511422662647488, 0.0039821105759899845, 5.54= 3408872612397E-4, -0.0015797828516888929, 1.3505400134130176E-4, -1.5215492= 836142527E-4, 9.381119889780555E-4, -4.5861204247023084E-4, -2.422669450392= 1645E-5}, {0.007234717654072837, 0.0039821105759899845, 0.031037646466036784, -0.0= 022701157440735394, -3.187028053841407E-4, 5.182461519304137E-4, -3.6813402= 42039795E-4, 0.001526984686166616, 1.603885118040309E-4, -1.359858314115312= E-4}, {-9.455312097865225E-4, 5.543408872612397E-4, -0.0022701157440735394, 0.= 005637141895898889, 7.89377521930992E-4, 5.004781934410127E-4, -9.792219671= 72284E-4, -2.912861228906251E-4, 7.842012412867984E-4, 0.001086680880742953= 2}, {0.0016345464996349748, -0.0015797828516888929, -3.187028053841407E-4, 7= .89377521930992E-4, 0.03263062480163135, 6.041130577612135E-5, 6.8834890967= 10362E-4, 0.010830183513887228, 0.0016425608963272292, 0.002481787652249504= }, {1.5256808879495097E-4, 1.3505400134130176E-4, 5.182461519304137E-4, 5.0= 04781934410127E-4, 6.041130577612135E-5, 0.001733612375709255, 2.8742157640= 452992E-5, -3.654534740999083E-4, 9.896178753749563E-5, -1.703972415991329E= -5}, {0.00226325818749439, -1.5215492836142527E-4, -3.681340242039795E-4, -9.= 79221967172284E-4, 6.883489096710362E-4, 2.8742157640452992E-5, 0.008167191= 690212253, -0.0010075092076978207, -4.293010139199468E-4, -6.61564097833129= 2E-4}, {0.003534367267672946, 9.381119889780555E-4, 0.001526984686166616, -2.91= 2861228906251E-4, 0.010830183513887228, -3.654534740999083E-4, -0.001007509= 2076978207, 0.013796198054188104, 0.0013541164478127973, -2.240108672066916= 7E-5}, {-4.2669306842991344E-5, -4.5861204247023084E-4, 1.603885118040309E-4, 7= .842012412867984E-4, 0.0016425608963272292, 9.896178753749563E-5, -4.293010= 139199468E-4, 0.0013541164478127973, 0.004743485149287524, 0.00114642932177= 08277}, {6.902267133060073E-5, -2.4226694503921645E-5, -1.359858314115312E-4, 0.= 0010866808807429532, 0.002481787652249504, -1.703972415991329E-5, -6.615640= 978331292E-4, -2.2401086720669167E-5, 0.0011464293217708277, 0.007398229661= 528494}}); // create asset variables - cost and weighting constraints Variable[] tmpVariables =3D new Variable[expectedReturnsMatrix.getRowDim(= )]; for (int i =3D 0; i < tmpVariables.length; i++) { tmpVariables[i] =3D new Variable(""); tmpVariables[i].setCost(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 =3D new QuadraticModel(tmpVariables); Expression tmpExpr =3D quadModel.addCorrelationExpression("Variance", cov= arianceMatrix.toBigStore().toArray()); BigDecimal riskAversion =3D BigMath.PI.multiply(BigMath.E); tmpExpr.setCost(riskAversion.multiply(BigMath.HALF)); // set balance expression tmpExpr =3D quadModel.addSimpleWeightExpression("Balance"); tmpExpr.setLowerLimit(BigMath.ONE); tmpExpr.setUpperLimit(BigMath.ONE); // exception here... BasicMatrix bm =3D BigMatrix.FACTORY.buildColumnVector(quadModel.getDefau= ltSolver().getSolution()); =09 System.out.print(bm.toString()); } ******************************************************************** This message and any attachment are confidential. If you are not the inten= ded recipient please contact the sender, delete this message and any attach= ment 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 ri= ght to monitor all email messages passing through its network. ******************************************************************** |