Re: [ojAlgo-user] Markowitz optimisation
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Arthur M. <art...@be...> - 2008-01-17 14:59:03
|
Hi Anders, Here's an example that gives an exception. Hopefully this code will retain= a semblance of formatting on emailing. public class TestStats extends TestCase { public void testEfficientFrontier() { // create expected returns matrix PrimitiveMatrix expectedReturnsMatrix =3D new PrimitiveMatrix(new double[= ][] { { -0.007155942261937039 }, { -0.003665887902733331 }, { -0.004130184341000032 }, { -0.005639860515= 211043 }, { 0.0007211966666666817 }, { 0.0003258225000000077 }, { -0.005754291666= 666666 }, { -0.004264291666666667 }, { -0.0017500000000000003 } }); // create covariance matrix PrimitiveMatrix covarianceMatrix =3D new PrimitiveMatrix(new double[][] { { 0.001561410465201063, 0.00006366128201274021, -0.0001323096896= 759724, 0.0000909074052724909, 0.00003172000033558704, 0.00001955483= 223848944, -0.00013771504482647386, -0.00004858457275314645, -0.0000129= 54723060403266 }, { 0.00006366128201274021, 0.00016419786524761803, -0.0000156628891= 1558343, -0.00008688646089751923, 0.0000027349925543017186, 0.00000123561= 59598500247, -0.000024367796639005863, -0.000017576048221096555, -0.0000070= 052245518771815 }, { -0.0001323096896759724, -0.00001566288911558343, 0.0001430155985= 985913, 0.00007675339168559199, -0.00007600590426518823, 0.00003297653= 8909267937, 0.00009520305608240259, 0.00007373075639042642, -0.0000074= 77057858706954 }, { 0.0000909074052724909, -0.00008688646089751923, 0.0000767533916= 8559199, 0.000967519991100896, -0.0000533460293834595, 0.00008665760= 416026126, 0.00014591175388747613, 0.0001232364989586903, 0.0001109= 7998789484925 }, { 0.00003172000033558704, 0.0000027349925543017186, -0.0000760059042= 6518823, -0.0000533460293834595, 0.000025267064307337795, -0.00003089584= 520279407, -0.00005593123237578969, -0.000017013960349712132, 0.0000013= 056146551724419 }, { 0.00001955483223848944, 0.0000012356159598500247, 0.0000329765389= 09267937, 0.00008665760416026126, -0.00003089584520279407, 0.00016254994= 47274783, 0.00008242949058588471, 0.00010276895784859992, 0.0000005= 898510775862205 }, { -0.00013771504482647386, -0.000024367796639005863, 0.0000952030560= 8240259, 0.00014591175388747613, -0.00005593123237578969, 0.00008242949= 058588471, 0.000560956958802083, 0.0002838794236862429, 0.0000914= 3821659482758 }, { -0.00004858457275314645, -0.000017576048221096555, 0.0000737307563= 9042642, 0.0001232364989586903, -0.000017013960349712132, 0.00010276895= 784859992, 0.0002838794236862429, 0.00021068964250359204, 0.0000446= 1044181034483 }, { -0.000012954723060403266, -0.0000070052245518771815, -0.0000074770578= 58706954, 0.00011097998789484925, 0.0000013056146551724419, 0.00000058985= 10775862205, 0.00009143821659482758, 0.00004461044181034483, 0.0000676= 1920797413792 } }); MarketEquilibrium myMarket =3D new MarketEquilibrium(covarianceMatrix); // 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 2% allocation to each asset tmpVariables[i].setLowerLimit(new BigDecimal("0.02")); // require no more than 80% allocation to each asset tmpVariables[i].setUpperLimit(new BigDecimal("0.80")); } QuadraticModel quadModel =3D new QuadraticModel(tmpVariables); Expression tmpExpr =3D quadModel.addCorrelationExpression("Variance", cov= arianceMatrix.toBigStore().toArray()); BigDecimal tmpRiskAversion =3D myMarket.getRiskAversion(); tmpExpr.setCost(tmpRiskAversion.multiply(BigMath.HALF)); // set balance expression tmpExpr =3D quadModel.addSimpleWeightExpression("Balance"); tmpExpr.setLowerLimit(BigMath.ONE); tmpExpr.setUpperLimit(BigMath.ONE); // exception here... BigMatrix.FACTORY.buildColumnVector(quadModel.getDefaultSolver().getSolut= ion()); } } ******************************************************************** 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. ******************************************************************** |