I'm beginner in optimization and welcome any guide in this field.
I have 15 matrices (i.e., Di
of size (n*m)
) and want to find best weights (i.e., wi
) for weighted averaging them and make a better matrix that is more similar to one given matrix (i.e., Dt
).
In fact my objective function is like it:
min [norm2(sum(wi * Di) - Dt) + norm2(W)]
for i=1 ... 15 s.t. sum(wi) = 1 , wi >= 0
How can I optimize this function in Matlab?
You are describing a simple Quadratic programming, that can be easily optimized using Matlab's quadprog
.
Here how it goes:
You objective function is [norm2(sum(wi * Di) - Dt) + norm2(W)]
subject to some linear constraints on w
. Let's re-write it using some simplified notations. Let w
be a 15-by-1 vector of unknowns. Let D
be an n*m
-by-15 matrix (each column is one of the Di
matrices you have - written as a single column), and Dt
is a n*m
-by-1 vector (same as your Dt
but written as a column vector). Now some linear algebra (using the fact that ||x||^2 = x'*x and that argmin x is equivalent to argmin x^2)
[norm2(sum(wi * Di) - Dt)^2 + norm2(W)^2] =
(D*w-Dt)'*(D*w-Dt) + w'*w =
w'D'Dw - 2w'D'Dt + Dt'Dt + w'w =
w'(D'D+I)w - 2w'D'Dt + Dt'Dt
The last term Dt'Dt
is constant w.r.t w
and therefore can be discarded during minimization, leaving you with
H = 2*(D'*D+eye(15));
f = -2*Dt'*D;
As for the constraint sum(w)=1
, this can easily be defined by
Aeq = ones(1,15);
beq = 1;
And a lower bound lb = zeros(15,1)
will ensure that all w_i>=0
.
And the quadratic optimization:
w = quadprog( H, f, [], [], Aeq, beq, lb );
Should do the trick for you!