/**************************** * Copyright (c) 2007 ATEJI * * All rights reserved. * ****************************/ package cplex.portfolio; public class RunPortfolio { public static final Investment[] investments = { {"Invest0", 1.00125}, {"Invest1", 1.56359}, {"Invest2", 1.1933}, {"Invest3", 1.80874}, {"Invest4", 1.58501}, {"Invest5", 1.47987}, {"Invest6", 1.35029}, {"Invest7", 1.89596}, {"Invest8", 1.82284}, {"Invest9", 1.7466}, {"Invest10", 1.17411}, {"Invest11", 1.85894}, {"Invest12", 1.7105}, {"Invest13", 1.51353}, {"Invest14", 1.30399}, {"Invest15", 1.01498}, {"Invest16", 1.0914}, {"Invest17", 1.36445}, {"Invest18", 1.14731}, {"Invest19", 1.1659} }; // covarianceValues[i][j]: correlation between Investi and Investj. private final static double [] [] covarianceValues = { { 10.95, -0.119083, -0.0089114, 0.531663, 0.601764, 0.166234, -0.450789, 0.0570391, 0.783319, -0.519883, 0.875973, 0.955901, -0.539354, -0.462081, -0.862239, 0.779656, 0.996796, -0.611499, -0.266213, -0.840144}, { -0.119083, 9.81777, -0.677206, 0.00878933, -0.275887, 0.587909, 0.837611, -0.484939, -0.743736, 0.457961, -0.744438, -0.599048, 0.735008, -0.572405, -0.151555, 0.425153, 0.517106, -0.751549, 0.168981, -0.491897}, { -0.0089114, -0.677206, 9.54527, 0.147496, 0.141575, -0.692892, -0.426557, 0.966613, 0.153233, 0.82168, -0.191351, -0.817194, 0.155553, -0.732017, 0.27958, 0.682241, -0.721915, -0.12302, -0.834681, 0.517014}, { 0.531663, 0.00878933, 0.147496, 11.229, 0.949339, -0.549547, -0.471725, -0.84698, -0.456099, -0.982971, 0.739189, 0.19599, -0.839442, 0.5009, 0.0274667, -0.572588, -0.531327, 0.843043, -0.657613, -0.842158}, { 0.601764, -0.275887, 0.141575, 0.949339, 8.9361, 0.314066, -0.286081, 0.140263, 0.83462, 0.600238, -0.252724, -0.00161748, 0.806238, -0.210578, -0.553209, -0.113773, 0.75222, -0.543443, -0.43672, -0.696219}, { 0.166234, 0.587909, -0.692892, -0.549547, 0.314066, 9.26831, 0.577868, -0.628681, 0.504135, 0.695761, -0.189947, 0.17835, 0.457442, 0.0975066, -0.0943938, -0.931516, -0.89462, 0.227302, -0.410718, 0.628071}, { -0.450789, 0.837611, -0.426557, -0.471725, -0.286081, 0.577868, 11.0518, 0.597827, 0.854793, 0.624775, -0.565752, 0.184271, 0.555132, -0.242866, 0.604724, -0.584613, -0.494461, 0.740745, 0.62038, -0.804529}, { 0.0570391, -0.484939, 0.966613, -0.84698, 0.140263, -0.628681, 0.597827, 9.91122, 0.911557, -0.727683, 0.667776, 0.315012, -0.305826, 0.108554, 0.851222, -0.154881, -0.0793481, 0.64098, -0.545091, -0.408979}, { 0.783319, -0.743736, 0.153233, -0.456099, 0.83462, 0.504135, 0.854793, 0.911557, 11.0092, -0.152654, -0.737999, 0.826685, 0.873348, 0.300058, -0.127232, 0.784967, 0.609638, 0.0722678, -0.653859, 0.104801}, { -0.519883, 0.457961, 0.82168, -0.982971, 0.600238, 0.695761, 0.624775, -0.727683, -0.152654, 11.4757, 0.91998, -0.662801, -0.492538, 0.496811, -0.509262, 0.688162, -0.606281, 0.00589007, 0.10062, -0.863247}, { 0.875973, -0.744438, -0.191351, 0.739189, -0.252724, -0.189947, -0.565752, 0.667776, -0.737999, 0.91998, 9.71151, 0.380108, -0.552812, 0.955718, -0.17658, 0.131626, 0.95172, -0.0278329, 0.0559099, 0.131626}, { 0.955901, -0.599048, -0.817194, 0.19599, -0.00161748, 0.17835, 0.184271, 0.315012, 0.826685, -0.662801, 0.380108, 8.88156, 0.721641, -0.0146794, 0.707907, -0.217566, 0.16892, -0.340983, 0.367565, 0.802393}, { -0.539354, 0.735008, 0.155553, -0.839442, 0.806238, 0.457442, 0.555132, -0.305826, 0.873348, -0.492538, -0.552812, 0.721641, 10.189, 0.798181, 0.14481, -0.402417, 0.13654, -0.0661641, -0.573351, -0.548051}, { -0.462081, -0.572405, -0.732017, 0.5009, -0.210578, 0.0975066, -0.242866, 0.108554, 0.300058, 0.496811, 0.955718, -0.0146794, 0.798181, 9.25395, -0.808039, 0.284249, 0.89523, 0.743797, 0.361126, 0.228492}, { -0.862239, -0.151555, 0.27958, 0.0274667, -0.553209, -0.0943938, 0.604724, 0.851222, -0.127232, -0.509262, -0.17658, 0.707907, 0.14481, -0.808039, 8.40558, 0.542405, -0.0538041, -0.524674, -0.0946684, -0.891537}, { 0.779656, 0.425153, 0.682241, -0.572588, -0.113773, -0.931516, -0.584613, -0.154881, 0.784967, 0.688162, 0.131626, -0.217566, -0.402417, 0.284249, 0.542405, 9.46003, -0.931639, -0.0470901, 0.336406, -0.398602}, { 0.996796, 0.517106, -0.721915, -0.531327, 0.75222, -0.89462, -0.494461, -0.0793481, 0.609638, -0.606281, 0.95172, 0.16892, 0.13654, 0.89523, -0.0538041, -0.931639, 11.3699, 0.534227, -0.693533, -0.259163}, { -0.611499, -0.751549, -0.12302, 0.843043, -0.543443, 0.227302, 0.740745, 0.64098, 0.0722678, 0.00589007, -0.0278329, -0.340983, -0.0661641, 0.743797, -0.524674, -0.0470901, 0.534227, 8.2202, -0.398694, -0.585559}, { -0.266213, 0.168981, -0.834681, -0.657613, -0.43672, -0.410718, 0.62038, -0.545091, -0.653859, 0.10062, 0.0559099, 0.367565, -0.573351, 0.361126, -0.0946684, 0.336406, -0.693533, -0.398694, 8.41221, 0.435499}, { -0.840144, -0.491897, 0.517014, -0.842158, -0.696219, 0.628071, -0.804529, -0.408979, 0.104801, -0.863247, 0.131626, 0.802393, -0.548051, 0.228492, -0.891537, -0.398602, -0.259163, -0.585559, 0.435499, 11.0019} }; public static final double [Investment][Investment] covariance [investments][investments]; // convert an array to an associative array. static { for(int i : investments.length) { for(int j : investments.length) { covariance[investments[i]][investments[j]] = covarianceValues[i][j]; } } } public static void main(String [] args) { // a risk-loving investor might choose k=0, while a risk-averse investor // might choose k=1000. double riskAversion = 10.0; Portfolio myModel = new Portfolio(investments, covariance, riskAversion); // solution: // Invest0 : 0.04118338118258961 // Invest1 : 0.0599965898602571 // Invest2 : 0.05399935154726398 // Invest3 : 0.05937231790585502 // Invest4 : 0.05051909529523475 // Invest5 : 0.05340334803757173 // Invest6 : 0.03116621369196303 // Invest7 : 0.05204371082138232 // Invest8 : 0.03029435303700436 // Invest9 : 0.04785678053242519 // Invest10 : 0.034965692958935715 // Invest11 : 0.04229755520482835 // Invest12 : 0.048709180302246094 // Invest13 : 0.03975787185128657 // Invest14 : 0.06751088927924588 // Invest15 : 0.044378138164937374 // Invest16 : 0.04200999061961791 // Invest17 : 0.06385135583934039 // Invest18 : 0.0733395084730549 // Invest19 : 0.0633446753949596 // totalReturn = 1.4327581704111347 // totalVariance = 0.4779918634667992 try{ myModel.extract(); if (myModel.solve()) { for (Investment i : investments) { System.out.println(i.name+" : "+myModel.allocation(i)); } double totalReturn = sum{Investment i : investments}{i.expectedReturn*myModel.allocation(i)}; double totalVariance = sum{Investment i : investments, Investment j : investments}{covariance[i][j]*myModel.allocation(i)*myModel.allocation(j)}; System.out.println("\ntotalReturn = "+totalReturn+"\ntotalVariance = "+totalVariance); } else { System.out.println("no solution."); } } finally { myModel.dispose(); } } }