Minimizing a function
이전 댓글 표시
Hi,
I would like to minimize w'Hw, with respect to w, where w is a vector, and H is matrix.
And with the following constraint, (|w1|+|w2|+|w3| < 3), ie. the l1 norm of the weights vector is less that 3.
How can I do this in matlab?
Thanks
답변 (3개)
Cédric Devivier
2011년 9월 6일
Hi,
Maybe the fminsearch function is enough ?
Something like that should work. Copy these two routines in a single m file.
function [w_min value] = minimize(w0,H)
[w_min value] = fminsearch(@(w) funtomini(w,H,w0) , w0);
end
function value = funtomini(w,H,w0)
if sum(abs(w))<3;
value = w'*H*w;
else
value = w0'*H*w0;
end
end
Find the minimum using this command
[w_min value] = minimize(w0,H);
Cheers,
Cédric
Teja Muppirala
2011년 9월 7일
This is a quadratic programming problem, and can be solved very easily using QUADPROG. The only thing is correctly expressing the L1 constraint.
% Make some random H
H = rand(3,3);
H =H+H';
% Express the L1 constraint using matrices:
[X1,X2,X3] = ndgrid([-1 1]);
A = [X1(:) X2(:) X3(:)];
b = 3*ones(size(A,1),1);
% solve for x
x = quadprog(H,[],A,b)
댓글 수: 4
Walter Roberson
2011년 9월 7일
It is not immediately clear to me that the L1 constraint is correct there. It appears to me that you have effectively limited the range of values to [-1,1], but that is not the range limit of the original problem. In the original problem, it is the _sum_ of the absolute values that must be less than 3, which is a condition reachable with values up to [-3,3] (for which the weights of the other two would have to be near 0.)
Teja Muppirala
2011년 9월 7일
I believe the method outlined above is correct.
The L1 constraint is defined by 8 planes using the following inequalities:
x1+x2+x3 <= 3
x1+x2-x3 <= 3
x1-x2+x3 <= 3
.
.
.
.
-x1-x2-x3 <= 3
If you have access to QUADPROG, you can copy and paste the code above and confirm that norm(x,1) = 3. (Unless H is positive definite, and the solution is x = [0 0 0])
This method will scale well until you run out of memory for your A matrix, since its size is very large: [(2^N) x N] where N is the length of x. And there are other ways to generate the A matrix that may be easier that using ndgrid:
N = 3;
A = sign(dec2bin(0:(2^N-1),N) - 48 - 0.5)
For this problem, if your problem size lets you use QUADPROG, it will work significanly faster than FMINCON, and more importantly, as N gets larger, say 10~20, it will find a better value for x than FMINCON will.
Jen
2011년 9월 8일
Jen
2011년 9월 8일
카테고리
도움말 센터 및 File Exchange에서 Linear Least Squares에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!