Minimizing a function
Mostrar comentarios más antiguos
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
Respuestas (3)
Cédric Devivier
el 6 de Sept. de 2011
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
el 7 de Sept. de 2011
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 comentarios
Walter Roberson
el 7 de Sept. de 2011
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
el 7 de Sept. de 2011
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
el 8 de Sept. de 2011
Jen
el 8 de Sept. de 2011
Categorías
Más información sobre Linear Least Squares en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!