MATLAB: Minimising the variance of a portfolio using weights of assets and covariance matrix

fminconGlobal Optimization ToolboxMATLABoptimizationOptimization Toolbox

I have the formula for the variance of a portfolio
variance = transpose(weight)* covariance * weights
where the covariance(covR) is a 10*10 matrix and the weights(w) are a 10*1 matrix
I am trying to minimize the weights while having the constraints that the weights fall between 0 and 0.1
var = @ (w) w'*corR*w
I have tried to use the quadprog function, but I still can't seem to get the right answer.
Could anyone help ?

Best Answer

If you have the additional constrain that the weights must sum to 1 than you must also increase the lower bound. Otherwise the only feasible solution is that all of them equal 0.1, so the sum is 1. Saying this you have an equality constrain that the sum of all weightings equal ones, this can be done using the Aeq and Beq options, a full example:
rng(42)
convR = randn(10,10);
convR = convR+eye(10)-diag(convR);
convR = 1/2*(convR+convR');
lb = zeros(10,1);
up = ones(10,1)*0.15;% changing this may be helpful depending of your matrix
f = zeros(10,1);
% Equality constrain
Aeq = zeros(10);
Aeq(1,1:10) = 1;
beq = zeros(10,1);
beq(1) = 1;
[x,fval,IsGlobalMin] = quadprog(convR,f,[],[],Aeq,beq,lb,up)
sum(x) % Sum of x
Aeq*beq % first element is sum of x
Another option in order to retain your initial constrain of 0.1 is just to normalize the result:
x = x/sum(x);