Re: [ojAlgo-user] Markowitz optimisation
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Anders P. <an...@op...> - 2008-02-04 16:19:25
|
I've patched the ActiveSetSolver, again... Please verify that that you get correct solutions (at least that they seem reasonable). /Anders On 4 feb 2008, at 12.57, Arthur McGibbon wrote: > 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 through the code myself but I don't understand the maths > behind the constraints - hence my use of your library. > > Could you have another look please > > regards > Arthur > > > > > > public void testojAlgo() > { > // create expected returns matrix > PrimitiveMatrix expectedReturnsMatrix = new PrimitiveMatrix(new > double[][] { {9.994620}, {10.011389}, {10.004353}, {9.998293}, > {10.056851}, {9.997920}, {9.999011}, {10.050971}, {9.989124}, > {9.989912}}); > // create covariance matrix > PrimitiveMatrix covarianceMatrix = new PrimitiveMatrix(new double > [][] {{0.014531344652473037, 4.444675045533674E-4, > 0.007234717654072837, -9.455312097865225E-4, 0.0016345464996349748, > 1.5256808879495097E-4, 0.00226325818749439, > 0.003534367267672946, -4.2669306842991344E-5, 6.902267133060073E-5}, > {4.444675045533674E-4, 0.008511422662647488, > 0.0039821105759899845, > 5.543408872612397E-4, -0.0015797828516888929, > 1.3505400134130176E-4, -1.5215492836142527E-4, > 9.381119889780555E-4, -4.5861204247023084E-4, -2.4226694503921645E-5}, > {0.007234717654072837, 0.0039821105759899845, > 0.031037646466036784, -0.0022701157440735394, -3.187028053841407E-4, 5 > .182461519304137E-4, -3.681340242039795E-4, 0.001526984686166616, > 1.603885118040309E-4, -1.359858314115312E-4}, > {-9.455312097865225E-4, > 5.543408872612397E-4, -0.0022701157440735394, 0.005637141895898889, > 7.89377521930992E-4, > 5.004781934410127E-4, -9.79221967172284E-4, -2.912861228906251E-4, > 7.842012412867984E-4, 0.0010866808807429532}, > > {0.0016345464996349748, -0.0015797828516888929, -3.187028053841407E-4, > 7.89377521930992E-4, 0.03263062480163135, 6.041130577612135E-5, > 6.883489096710362E-4, 0.010830183513887228, 0.0016425608963272292, > 0.002481787652249504}, > {1.5256808879495097E-4, 1.3505400134130176E-4, > 5.182461519304137E-4, 5.004781934410127E-4, 6.041130577612135E-5, > 0.001733612375709255, 2.8742157640452992E-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.008167191690212253, -0.0010075092076978207, -4.293010139199468E-4, - > 6.615640978331292E-4}, > {0.003534367267672946, 9.381119889780555E-4, > 0.001526984686166616, -2.912861228906251E-4, > 0.010830183513887228, -3.654534740999083E-4, -0.0010075092076978207, 0 > .013796198054188104, 0.0013541164478127973, -2.2401086720669167E-5}, > {-4.2669306842991344E-5, -4.5861204247023084E-4, > 1.603885118040309E-4, 7.842012412867984E-4, 0.0016425608963272292, > 9.896178753749563E-5, -4.293010139199468E-4, 0.0013541164478127973, > 0.004743485149287524, 0.0011464293217708277}, > > {6.902267133060073E-5, -2.4226694503921645E-5, -1.359858314115312E-4, > 0.0010866808807429532, > 0.002481787652249504, -1.703972415991329E-5, -6.615640978331292E-4, -2 > .2401086720669167E-5, 0.0011464293217708277, 0.007398229661528494}}); > > // 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].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 = new QuadraticModel(tmpVariables); > > Expression tmpExpr = quadModel.addCorrelationExpression > ("Variance", covarianceMatrix.toBigStore().toArray()); > BigDecimal riskAversion = BigMath.PI.multiply(BigMath.E); > tmpExpr.setCost(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. > ******************************************************************** > > > ---------------------------------------------------------------------- > --- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > ojAlgo-user mailing list > ojA...@li... > https://lists.sourceforge.net/lists/listinfo/ojalgo-user > > |