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

8 views (last 30 days)
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 ?
  2 Comments
Thiago Henrique Gomes Lobato
Edited: Thiago Henrique Gomes Lobato on 5 Apr 2020
Why you believe you don't get the right answer? Quadprog should actually work, here is a minimalist example:
rng(42)
corR = randn(10,10);
corR = convR+eye(10)-diag(corR); % 1 diagonal
corR = 1/2*(corR+corR'); % Covariance is symmetric
lb = zeros(10,1);
up = ones(10,1)*0.1; % changing this to 0.08, 0.09 or other value may be helpful depending of your matrix
f = zeros(10,1); % no f
[x,fval,IsGlobalMin] = quadprog(corR,f,[],[],[],[],lb,up)
Shaun King
Shaun King on 5 Apr 2020
Thanks Thiago, That seems to be on the right track. There is another constraint that I am only realising now, that all the weights(x) need to sum up to 1. do you know how I could add this to your code ?
thanks for your help !

Sign in to comment.

Accepted Answer

Thiago Henrique Gomes Lobato
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);

More Answers (1)

Ameer Hamza
Ameer Hamza on 5 Apr 2020
Edited: Ameer Hamza on 5 Apr 2020
As Thiago pointed out, you can use Quadprog, and in this case, it will work, but the portfolio optimization model can become quite complex, which cannot be handled by quadprog. So this shows you a general way to handle your optimization problem, and implement the constraint that the sum of all portfolios should be one. Remember, in this case, the upper limit of 0.1 does not make sense because if there are 10 portfolios, then each one will take the value of 0.1, so I placed the upper limit of 1 on each portfolio.
A = ones(1,10); % A and B define A*x = B constraint
B = 1;
lb = zeros(size(corR,1), 1);
ub = ones(size(corR,1), 1);
obj_fun = @(w) w*corR*w'; % objective function
q = fmincon(obj_fun, rand(1,10), [], [], A, B, lb, ub);

Categories

Find more on Portfolio Optimization and Asset Allocation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!