1

I'm attempting to solve the familiar mean-variance optimization problem using matrices in Mathematica, with a few added constraints on the solution vector "w". (The mean-variance optimization problem is basically choosing how to allocate a given budget over a number of assets according to their means and covariances in order to minimize the risk of the portfolio for a chosen level of mean return.)

My question: I'm not sure which function to use to perform the minimization of the objective function, which is quadratic:

obj = 0.5*w'* Sig * w

where w is the Nx1 vector of weights for each of the N assets and Sig is the NxN covariance matrix

From what I've been able to find (I'm fairly new to Mathematica), it seems that FindMinimum, NMinimize, etc. are meant to deal only with scalar inputs, while LinearProgramming is meant for an objective function that's linear (not quadratic) in the weight vector w. I could very well be wrong here--any help in steering me toward the correct function would be much appreciated!

If it helps, I've attached my sample code--I'm not sure how, but if there's a place to upload the sample .csv data, I can do that as well if someone could point me to it.

Thank you very much for any help you can provide. -Dan


CODE

(* Goal: find an Nx1 vector of weights w that minimizes total \
portfolio risk w'*Sig*w (where Sig is the covariance matrix) subject to:
-The portfolio expected return is equal to the desired level d: w'*M \
= d, where M is the Nx1 vector of means
-There are exactly two assets chosen from each of the refining, \
construction, hitech, and utility sectors, and exactly one asset \
chosen from the "other" sector
^ The above two constraints are represented together as w'*R = k, \
where R is the matrix [M SEC] and k is the vector [d 2 2 2 2 1]
-Each weight in w takes an integer value of either 0 or 1, \
representing buying or not buying that physical asset (ex. a plant) -- \
this constraint is achieved as a combination of an integer constraint \
and a boundary constraint

**Note that for the T=41 days of observations in the data, not every \
asset generates a value for every day; by leaving the days when the \
asset is "off" as blanks, this shouldn't affect the mean or \
covariance matrices. 
*)
Clear["Global`*"]

(* (1) Import the data for today *)
X = Import["X:\\testassets.csv", "Data"];
Dimensions[X]; 

(* (2) Create required vectors and matrices *)
P = Take[X, {2, 42}, {4}];
Dimensions[P];    (* Should be N assets x 1) *)
r = Take[X, {2, 42}, {10, 50}];
Dimensions[r]; (* Should be N x T *)
Sig = Covariance[
   r]; (* When there's more time, add block diagonal restriction here \
*)
Dimensions[Sig]; (* Should be N x N *)
M = Mean[r\[Transpose]];
Dimensions[M]; (* Should be N x 1 *)
SEC = Take[X, {2, 42}, {5, 9}];
Dimensions[SEC]; (* Should be N x 5 *)

(* (3) Set up constrained optimization *) 

d = 200; (* desired level of return *)
b = 60000;(* budget constraint *)
R = Join[M, POS];
Dimensions[R]; (* Should be N x 6 *)
k = {d, 2, 2, 2, 2, 1};
obj = w*Sig*w\[Transpose];
constr = w*R;
budgetcap = w*P;
lb = ConstantArray[0, 41];
ub = ConstantArray[1, 41];
FindMinimum[{obj, constr = k, budgetcap <= b, Element[w, Integers], 
  lb <= w <= ub}, w]
DanMcK
  • 37
  • 9
  • you can not work with an abstract vector (I don't see `w` defined anywhere). You need to define it as a list, eq `w=Array[ wc , {n} ]`. Also take care to check that `*` is giving you the type of matrix multiplication you want. ( you might want `Outer` ) – agentp Feb 19 '15 at 19:50
  • Thanks @agentp -- am I correct in assuming that w=Array[wc,{n}] will generate an N-vector of identical entries "wc"? Thanks again for your help – DanMcK Feb 19 '15 at 19:57
  • right, you'll get `{wc[1],wc[2], .. }`. In some cases your code remains the same, eg `Element[w,Integers]` works. Other things like your constraint will need to be written component wise, `lb – agentp Feb 19 '15 at 20:06
  • Thanks again--the code I posted above has quite a few other issues in addition to the ones you've identified--once I get it fixed I'll post the corrected version. – DanMcK Feb 20 '15 at 19:03

0 Answers0