Minimizing a function

9 vues (au cours des 30 derniers jours)
Jen
Jen le 6 Sep 2011
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

Réponses (3)

Cédric Devivier
Cédric Devivier le 6 Sep 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
Teja Muppirala le 7 Sep 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 commentaires
Jen
Jen le 8 Sep 2011
This method is correct, however when I applied to to my problem where N =25, I run out of memory.
I get the following error:
??? Error using ==> horzcat
Out of memory. Type HELP MEMORY for your options.
Error in ==> GMVPC1Type3 at 6
A = [X1(:) X2(:) X3(:) X4(:) X5(:) X6(:) X7(:) X8(:) X9(:) X10(:) X11(:) X12(:) X13(:) X14(:)
X15(:) X16(:) X17(:) X18(:) X19(:) X20(:) X21(:) X22(:) X23(:) X24(:) X25(:)];
Error in ==> SCM12 at 63
PortfolioWeights = GMVPC1Type3(SCM);
Do you have any other suggestions?
Jen
Jen le 8 Sep 2011
The fmincon code works below for some matrices, but for other, gives equal weights to w1,w2 and w3. Any ideas why this is?

Connectez-vous pour commenter.


Jen
Jen le 7 Sep 2011
Cedric - Will give it a go
Teja - I initially used QUADPROG, but couldnt find a way of expressing the L1 constraint.
I decided to use fmincon with a nonlinear constraint.
Aeq = [1,1,1;0.05,0.06,0];
beq = [1;0.05];
A = [0,1,0];
b = 0.5;
w0 = [0.5 0.5 0.5];
[w,fval] = fmincon(@myobj,w0,A,b,Aeq,beq,[],[],@myconstraint)
------------------------------------------------------------------
function f = myobj(w)
diagmatrix = [0.15,0,0;0,0.20,0;0,0,0.40];
corrmatrix = [1,0.5,-0.7;0.5,1,-0.4;-0.7,-0.4,1];
covmatrix = diagmatrix*corrmatrix*diagmatrix;
w = [w(1);w(2);w(3)];
f = w'*covmatrix*w;
end
-----------------------------------------------------------------
function [c ceq] = myconstraint(w)
c = abs(w(1)) + abs(w(2)) + abs(w(3))-3;
ceq = [];
end
------------------------------------------------------------------
My only concern is how well will this work when dealing with large matrices? Is there code to achieve this I wonder?
  1 commentaire
Jen
Jen le 8 Sep 2011
This code seems to work on some matrices, but on others, it just gives equal weights to w1,w2,w3....anyone have any ideas why?

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by