[ojAlgo-user] Markowitz Problem 2 :)
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Matteo B. <mat...@gm...> - 2013-04-04 14:16:30
|
Hi we have a second problem with MarkowitzModel. I have produced a little source that explain the problem. The problem is similar with the previous: 3 different TargetReturn 2 ok and 1 ko- This is the source that reproduce the problem ---------8<--------------8<--------------8<--------------8<--------------8<----- //package org.ojalgo.finance.portfolio; import java.math.BigDecimal; import java.util.List; import org.ojalgo.matrix.BasicMatrix; import org.ojalgo.matrix.BigMatrix; import org.ojalgo.matrix.store.BigDenseStore; import org.ojalgo.matrix.store.MatrixStore; import org.ojalgo.optimisation.Optimisation.State; import org.ojalgo.finance.portfolio.MarketEquilibrium; import org.ojalgo.finance.portfolio.MarkowitzModel; public class MarkowitzProblems2 { public static void main(final String[] args) { //BigDecimal nTarget = new BigDecimal("0.07"); // OK //0.0401 //0.0 //0.0 //0.0 //0.0239 //0.0 //0.0051 //0.0 //0.5305 //0.1669 //0.0 //0.0335 //0.2 BigDecimal nTarget = new BigDecimal("0.08"); // ERROR // 0.0828 // 0.0164 // 0.0 // 0.0 // 0.0501 // 0.0 // -0.0396 -> Out of Weight Range // 0.0 // 0.5876 // 0.1908 // -0.0049 -> Out of Weight Range // 0.0 // 0.1169 //BigDecimal nTarget = new BigDecimal("0.09"); // OK //0.0788 //0.0045 //0.0 //0.0 //0.0 //0.0 //0.0711 //0.0 //0.6272 //0.1628 //0.0057 //0.0 //0.05 final double[][] cm= {{0.04206601, 0.044550845724, 0.048217369200, 0.054350966740, 0.022460320512, 0.055208551370, 0.034165558000, 0.029414658518, -0.000907567500, -0.003266560017, 0.011408652633, 0.007174996892, 0}, {0.044550845724, 0.06796449, 0.063983606214, 0.072022499274, 0.032393205504, 0.085397746665, 0.046597518000, 0.040498055664, -0.001203130500, -0.003712871151, 0.016252486404, 0.008933472075, 0}, {0.048217369200, 0.063983606214, 0.09084196, 0.096768635348, 0.043186328064, 0.083969449256, 0.054541344000, 0.049330020454, -0.001261359000, -0.004770776208, 0.023684614800, 0.016492134802, 0}, {0.054350966740, 0.072022499274, 0.096768635348, 0.11874916, 0.039110390784, 0.091360338216, 0.058037532000, 0.051565647644, -0.001011401000, -0.004914519792, 0.024900113692, 0.014709412962, 0}, {0.022460320512, 0.032393205504, 0.043186328064, 0.039110390784, 0.05308416, 0.064832995584, 0.040674816000, 0.041904582912, -0.001357056000, -0.003786856704, 0.018876923136, 0.017478379008, 0}, {0.055208551370, 0.085397746665, 0.083969449256, 0.091360338216, 0.064832995584, 0.14661241, 0.060023404000, 0.054428675966, -0.000761971000, -0.004890659172, 0.020088021436, 0.009159105844, 0}, {0.034165558000, 0.046597518000, 0.054541344000, 0.058037532000, 0.040674816000, 0.060023404000, 0.04000000, 0.030711460000, -0.001110000000, -0.003517058000, 0.013415070000, 0.007178534000, 0}, {0.029414658518, 0.040498055664, 0.049330020454, 0.051565647644, 0.041904582912, 0.054428675966, 0.030711460000, 0.08300161, 0.000404780500, -0.001591573878, 0.020639653979, 0.010983694379, 0}, {-0.000907567500, -0.001203130500, -0.001261359000, -0.001011401000, -0.001357056000, -0.000761971000, -0.001110000000, 0.000404780500, 0.00250000, 0.002084702500, 0.000854111500, 0.000385734000, 0}, {-0.003266560017, -0.003712871151, -0.004770776208, -0.004914519792, -0.003786856704, -0.004890659172, -0.003517058000, -0.001591573878, 0.002084702500, 0.00426409, 0.000608437974, 0.001034468234, 0}, {0.011408652633, 0.016252486404, 0.023684614800, 0.024900113692, 0.018876923136, 0.020088021436, 0.013415070000, 0.020639653979, 0.000854111500, 0.000608437974, 0.03500641, 0.003756060565, 0}, {0.007174996892, 0.008933472075, 0.016492134802, 0.014709412962, 0.017478379008, 0.009159105844, 0.007178534000, 0.010983694379, 0.000385734000, 0.001034468234, 0.003756060565, 0.01471369, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}} ; final int assetNum = cm.length; final int row = cm.length; if (row > 0) { final int col = cm[0].length; final MatrixStore<BigDecimal> a = BigDenseStore.FACTORY.makeZero(row, col); BasicMatrix covariances = BigMatrix.FACTORY.copy(a); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { covariances = covariances.replace(i, j, cm[i][j]); } } final double nRisk = 1000.0; final BigDecimal riskAversion = new BigDecimal(nRisk); final MarketEquilibrium marketEquilibrium = new MarketEquilibrium(covariances, riskAversion); final MatrixStore<BigDecimal> ret_matrix = BigDenseStore.FACTORY.makeZero(assetNum, 1); BasicMatrix expectedExcessReturns1 = BigMatrix.FACTORY.copy(ret_matrix); expectedExcessReturns1 = expectedExcessReturns1.replace(0, 0, 0.1389); expectedExcessReturns1 = expectedExcessReturns1.replace(1, 0, 0.1545); expectedExcessReturns1 = expectedExcessReturns1.replace(2, 0, 0.0736); expectedExcessReturns1 = expectedExcessReturns1.replace(3, 0, 0.0648); expectedExcessReturns1 = expectedExcessReturns1.replace(4, 0, 0.0915); expectedExcessReturns1 = expectedExcessReturns1.replace(5, 0, 0.1802); expectedExcessReturns1 = expectedExcessReturns1.replace(6, 0, 0.1331); expectedExcessReturns1 = expectedExcessReturns1.replace(7, 0, 0.0816); expectedExcessReturns1 = expectedExcessReturns1.replace(8, 0, 0.0874); expectedExcessReturns1 = expectedExcessReturns1.replace(9, 0, 0.0829); expectedExcessReturns1 = expectedExcessReturns1.replace(10, 0, 0.1038); expectedExcessReturns1 = expectedExcessReturns1.replace(11, 0, 0.0759); expectedExcessReturns1 = expectedExcessReturns1.replace(12, 0, 0.0000); final MarkowitzModel markowitzModel = new MarkowitzModel(marketEquilibrium, expectedExcessReturns1); markowitzModel.setLowerLimit(0, new BigDecimal(0.0000)); markowitzModel.setUpperLimit(0, new BigDecimal(1.0000)); markowitzModel.setLowerLimit(1, new BigDecimal(0.0000)); markowitzModel.setUpperLimit(1, new BigDecimal(1.0000)); markowitzModel.setLowerLimit(2, new BigDecimal(0.0000)); markowitzModel.setUpperLimit(2, new BigDecimal(1.0000)); markowitzModel.setLowerLimit(3, new BigDecimal(0.0000)); markowitzModel.setUpperLimit(3, new BigDecimal(1.0000)); markowitzModel.setLowerLimit(4, new BigDecimal(0.0000)); markowitzModel.setUpperLimit(4, new BigDecimal(1.0000)); markowitzModel.setLowerLimit(5, new BigDecimal(0.0000)); markowitzModel.setUpperLimit(5, new BigDecimal(1.0000)); markowitzModel.setLowerLimit(6, new BigDecimal(0.0000)); markowitzModel.setUpperLimit(6, new BigDecimal(1.0000)); markowitzModel.setLowerLimit(7, new BigDecimal(0.0000)); markowitzModel.setUpperLimit(7, new BigDecimal(1.0000)); markowitzModel.setLowerLimit(8, new BigDecimal(0.0000)); markowitzModel.setUpperLimit(8, new BigDecimal(1.0000)); markowitzModel.setLowerLimit(9, new BigDecimal(0.0000)); markowitzModel.setUpperLimit(9, new BigDecimal(1.0000)); markowitzModel.setLowerLimit(10, new BigDecimal(0.0000)); markowitzModel.setUpperLimit(10, new BigDecimal(1.0000)); markowitzModel.setLowerLimit(11, new BigDecimal(0.0000)); markowitzModel.setUpperLimit(11, new BigDecimal(1.0000)); markowitzModel.setLowerLimit(12, new BigDecimal(0.0500)); markowitzModel.setUpperLimit(12, new BigDecimal(0.2000)); final boolean bShort = false; markowitzModel.setShortingAllowed(bShort); markowitzModel.setTargetReturn(nTarget); final List<BigDecimal> re = markowitzModel.getWeights(); final State tmpOptState = markowitzModel.getOptimisationState(); for (int nn = 0; nn < re.size(); nn++) { System.out.println(re.get(nn)); } } } } ---------8<--------------8<--------------8<--------------8<--------------8<----- All the best Matteo Baccan -- Matteo Baccan http://www.baccan.it http://www.moonlab.it http://www.jobcrawler.it http://www.paginelibere.it http://www.cvcrawler.it http://www.newscrawler.it http://casa.newscrawler.it http://www.autocrawler.it http://www.annunciveloci.it |