Minimizing a function in matlab
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|+|w开发者_StackOverflow中文版2|+|w3| < 3, ie. the l1 norm of the weights vector is less that 3.
How can I do this in matlab?
Thanks
You're trying to solve a quadratic minimization problem with linear constraints (also known as quadratic programming).
Do you know anything about your matrix H -- in particular, is it positive semidefinite? I would really expect this to be the case, since this is usual for the problem domains in which quadratic programming problems usually crop up.
If H really is positive semidefinite, and your only constraint is |w1|+|w2|+|w3| < 3, then, as Richie Cotton has already pointed out, the minimum is trivially at w=0. Maybe you have some additional constraints?
If you do have additional constraints, but H is still positive semidefinite, there are existing efficient solvers for this class of problem. In MATLAB, take a look at quadprog.
You'll have to reformulate your single nonlinear constraint |w1|+|w2|+|w3| < 3 as a series of linear constraints.
In the one-dimensional case, the constraint |w1| < 1 turns into two linear constraints:
w1 < 1
-w1 < 1.
In the two-dimensional case, the constraint |w1| + |w2| < 1 turns into four linear constraints:
w1+w2 < 1
w1-w2 < 1
-w1+w2 < 1
-w1-w2 < 1
I'll leave the extension to three dimensions to you.
you need to use the optimization toolbox, specifically fmincon:
use fun to establish w'Hw, and you want c(eq) = (|w1|+|w2|+|w3| - 3)
<0 which you set with nonlcon (in the documentation).
I'd suggest you look at the fminsearch function in the matlab documentation.
Rasman, below is the fmincon code I am using:
function PortfolioWeights = GMVPC1Type2(SCM)
w0 = [0.1 0.1 0.1 0.1 0.1];
n = length(w0);
options = optimset('Display','final-detailed');
PortfolioWeights = fmincon(@myobj2,w0,[],[],ones(1,n),1,[],[],@myconstraint1,options)
function f = myobj2(w)
w = [w(1);w(2);w(3);w(4);w(5)];
f = w'*SCM*w;
end
end
-----------------------------------------------------------------------------
function [c ceq] = myconstraint1(w)
c = abs(w(1))+abs(w(2))+abs(w(3))+abs(w(4))+abs(w(5))-1
ceq = [];
end
------------------------------------------------------------------------------
I added in options = optimset('Display','final-detailed'); as you suggested. I get the following message:
Optimization stopped because the predicted change in the objective function,
6.115031e-009, is less than options.TolFun = 1.000000e-006, and the maximum constraint
violation, 0.000000e+000, is less than options.TolCon = 1.000000e-006.
Optimization Metric Options
abs(steplength*directional derivative) = 6.12e-009 TolFun = 1e-006 (default)
max(constraint violation) = 0.00e+000 TolCon = 1e-006 (default)
Active inequalities (to within options.TolCon = 1e-006):
lower upper ineqlin ineqnonlin
1
PortfolioWeights =
0.2000 0.2000 0.2000 0.2000 0.2000
The matrix I am using is:
0.000257165759136336 8.48196702102889e-05 9.27141501220362e-05 0.000111360154790061 0.000155196440517440
8.48196702102889e-05 0.000277377166669392 0.000101880007672550 0.000107375764193076 0.000117042329431538
9.27141501220362e-05 0.000101880007672550 0.000300697293925817 0.000112004860252653 0.000134354417344316
0.000111360154790061 0.000107375764193076 0.000112004860252653 0.000311028738698100 0.000147296211557256
0.000155196440517440 0.000117042329431538 0.000134354417344316 0.000147296211557256 0.000376418027192374
精彩评论