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