How do I put a series of sum into a function?

Asked by Dennis Göbel

Dennis Göbel (view profile)

on 19 Apr 2019
Latest activity Answered by Alan Weiss

Alan Weiss (view profile)

on 22 Apr 2019
Accepted Answer by Alan Weiss

Alan Weiss (view profile)

Hi! I am trying to solve the optimization problem for a risk parity portfolio. To be precise: w is a nx1 vector of portfolio weigths and C th nxn covariance matrix.
The variable port_size is a scalar that returns the number of assets invested in.
I tried to solve it with fmincon, but the first problem that is appearing is that apparently I cannot do the fun_rp = @(w_rp) with the for loop as MATLAB gives me the error: illegal use of reserved keyword "for".
w_mv0 = [0.25; 0.25; 0.25; 0.25];
Aeq = ones(1, port_size);
beq = 1;
lb = zeros(1, port_size);
ub = ones(1, port_size);
fun_rp = @(w_rp) for i = 1:port_size
for j = 1: port_size
(w(1,i)*(C*w(1,i)')-w(1,j)*(C*w(1,j)'))^2;
end
end
w_rp = fmincon(fun_rp,w_rp0,[],[],Aeq,beq,lb,ub)
Does anybody know where my mistake is? I´d appriciate every help!
Best regards

Answer by Alan Weiss

on 22 Apr 2019