필터 지우기
필터 지우기

Minimizing a function

조회 수: 8 (최근 30일)
Jen
Jen 2011년 9월 6일
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
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
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
Jen
Jen 2011년 9월 8일
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 2011년 9월 8일
The fmincon code works below for some matrices, but for other, gives equal weights to w1,w2 and w3. Any ideas why this is?

댓글을 달려면 로그인하십시오.


Jen
Jen 2011년 9월 7일
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
Jen
Jen 2011년 9월 8일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by