我尝试使用Mathematica中的矩阵来解决熟悉的均值 - 方差优化问题,并对解决方案向量添加了一些约束" w"。 (均值 - 方差优化问题基本上是根据其均值和协方差选择如何在多个资产上分配给定预算,以便最小化投资组合对所选平均收益水平的风险。)
我的问题:我不确定使用哪个函数来执行目标函数的最小化,这是二次的:
obj = 0.5 * w' * Sig * w
其中w是N个资产中每个资产的Nx1权重向量,Sig是NxN协方差矩阵
从我能够找到的东西(我对Mathematica相当新),似乎FindMinimum,NMinimize等仅用于处理标量输入,而LinearProgramming用于在权向量w中具有线性(非二次)的目标函数。我在这里完全错了 - 非常感谢任何帮助我指向正确功能的帮助!
如果有帮助,我已附上我的示例代码 - 我不确定如何,但如果有上传示例.csv数据的地方,我也可以这样做有人可以指出我。
非常感谢您提供的任何帮助。 -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]