next up previous contents
Next: OSiL representation for problem Up: Appendix Previous: Appendix   Contents

Building a Model in MATLAB

We illustrate how to build a simple Markowitz portfolio optimization problem (a quadratic programming problem) from template.m. First copy template.m to markowitz.m.

The problem consists in investing in a number of stocks. The expected returns and risks (covariances) of the stocks are known. Assume that the decision variables $ x_i$ represent the fraction of wealth invested in stock $ i$ and that no stock can have more than 75% of the total wealth. The problem then is to minimize the total risk subject to a budget constraint and a lower bound on the expected portfolio return.

Assume that there are three stocks (variables) and two constraints (do not count the upper limit investment of .75 on the variables.).

% the number of constraints
numCon = 2;
% the number of variables
numVar = 3;

All the variables are continuous

VarType='CCC';

Next define the constraint upper and lower bounds. There are two constraints. A equality constraint (an $ =$) and a lower bound on portfolio return of .15 (a $ \ge$). These two constraints are expressed as

BU = [1  inf];
BL = [1   .15];

The variables are nonnegative and have upper limits of .75 (no stock can comprise more than 75% of the portfolio). This is written as

VL = [];
VU = [.75 .75 .75];

There are no nonzero linear coefficients in the objective function, but the objective function vector must always be defined and the number of components of this vector is the number of variables.

OBJ = [0 0 0 ]

Now the linear constraints. In the model the two linear constraints are

$\displaystyle x_{1} + x_{2} + x_{3}$ $\displaystyle =$ $\displaystyle 1$  
$\displaystyle 0.3221 x_{1} + 0.0963x_{2} + 0.1187x_{3}$ $\displaystyle \ge$ $\displaystyle .15$  

These are expressed as

 A = [ 1 1 1  ;
  0.3221   0.0963   0.1187 ];

Now for the quadratic terms. The only quadratic terms are in the objective function. The objective function is


$\displaystyle \min 0.4253 x_{1}^{2} + 0.4458 x_{2}^{2} + 0.2314 x_{3}^{2} + 2 \times 0.1852 x_{1} x_{2}$      
$\displaystyle + 2 \times 0.1393 x_{1} x_{3} + 2 \times
0.1388 x_{2} x_{3}$      

The quadratic matrix $ Q$ has 4 rows and a column for each quadratic term. In this example there are six quadratic terms. The first row of $ Q$ is the row index where the terms appear. By convention, the objective function has index -1 and we count constraints starting at 0. The first row of $ Q$ is

 -1 -1 -1 -1 -1 -1

The second row of $ Q$ is the index of the first variable in the quadratic term. We use zero based counting. Variable $ x_{1}$ has index 0, variable $ x_{2}$ has index 1, and variable $ x_{3}$ has index 2. Therefore, the second row of $ Q$ is

0 1 2 0 0 1

The third row of $ Q$ is the index of the second variable in the quadratic term. Therefore, the third row of $ Q$ is

0 1 2  1 2 2

The last (fourth) row is the coefficient. Therefore, the fourth row is

 .425349654  .445784443   0.231430983
 .370437388  .27862509 .27763384

The quadratic matrix is

Q = [ -1 -1 -1 -1 -1 -1;
0 1 2 0 0 1 ;
0 1 2 1 2 2;
.425349654  .445784443   0.231430983   ...
.370437388  .27862509 .27763384];

Finally, name the problem, specify the solver (in this case ipopt), the service address (and password if required by the service), and call the solver.

prob_name = 'Markowitz Example from Anderson, Sweeney, Williams, and Martin'
password = 'chicagoesmuyFRIO';
%
%the solver
solverName = 'ipopt';
%the remote service service address
%if left empty we solve locally
serviceAddress='http://gsbkip.chicagogsb.edu/os/OSSolverService.jws';
% now solve
callMatlabSolver( numVar, numCon, A, BL, BU, OBJ, VL, VU, ObjType, VarType, ...
     Q, prob_name, password, solverName, serviceAddress)


next up previous contents
Next: OSiL representation for problem Up: Appendix Previous: Appendix   Contents
Kipp Martin 2008-01-16