我有一个权重向量w
,我希望为其设置一个值范围。
尤其是以下内容:
当w(i)> 0
0.0001 <= w(i)<= 0.01
当w(i)<= 0
-0.01 <= w(i)<= -0.0001
如何在fmincon中翻译?我尝试了以下方法:
% problem
fnhandle = @(x)in_objectfun(x, ymu);
nonlcon = @(x)in_nonlconstr(x);
[A, b, Aeq, beq, lb, ub] = in_linconstr(imu);
x0 = in_startvalue(imu); % equal weighted with sign respected
% optimization
[woptimal,fval,exitflag,output,lambda,grad,hessian] = fmincon(fnhandle, x0, A, b, Aeq, beq, lb, ub, nonlcon, options);
% the core script where I have difficulties with
function [A, b, Aeq, beq, lb, ub] = in_linconstr(imu)
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
% the sign of the weights is determined by imu
temp = zeros(1,nbAssets);
temp(imu >= 0) = 1;
temp(imu < 0) = -1;
tempPos = zeros(1,nbAssets);
tempPos(imu>=0) = 1;
tempNeg = zeros(1,nbAssets);
tempNeg(imu<0) = -1;
% -----------------------------------------------------------------
% A: Sign of the Weight
Asign = diag(-temp);
bsign = zeros(nbAssets,1);
A = [A; Asign];
b = [b; bsign];
% -----------------------------------------------------------------
% B: Single exposures
% A x >= 0.0001
AposMin = -diag(tempPos);
bposMin = zeros(nbAssets,1);
bposMin(imu>=0) = -0.0001;
% A x < 0.01
AposMax = diag(tempPos);
bposMax = zeros(nbAssets,1);
bposMax(imu>=0) = 0.01;
% A x < -0.0001
AnegMin = diag(tempNeg);
bnegMin = zeros(nbAssets,1);
bnegMin(imu<0) = 0.0001;
% A x >= -0.01
AnegMax = -diag(tempNeg);
bnegMax = zeros(nbAssets,1);
bnegMax(imu<0) = -0.01;
A = [A; AposMin];
b = [b; bposMin];
A = [A; AposMax];
b = [b; bposMax];
A = [A; AnegMin];
b = [b; bnegMin];
A = [A; AnegMax];
b = [b; bnegMax];
end
运行以下命令时,我发现其中一个约束未得到满足,因此我猜想我的公式出了点问题:
[wraw,fval,exitflag,output,lambda,grad,hessian] = fmincon(fnhandle, x0, A, b, Aeq, beq, lb, ub, nonlcon, options); %#ok<*ASGLU>