GA, Could not find a feasible initial point
Show older comments
I'm triying to make a portfolio optimization using genetic algorithm.
This is the model that I made, with some constraints, but when I run it, I can't reach a solution, just appear "Could not find a feasible initial point".
Someone could help me fixing my code or helping me to find the mistake.
Thanks.
data = xlsread('Returns.xlsx');
nAssets = size(data, 2);
%Returns and covariance
mu = mean(data);
sigma = cov(data);
%formulate the problem/optimization
r_target = 0.10; %r_target is the required return
f = zeros(nAssets, 1); %there is no constant
A = [-mu; -eye(nAssets)]; %besides the returns we forbid short selling
b = [-r_target; zeros(nAssets, 1)]; % required return and weights greater/eqauls 0
Aeq = ones(1, nAssets); %All weights should sum up...
beq = 1; %... to one (1)
%solve the optimization
w = ga(@MaxReturn,nAssets,A,b,Aeq,beq);
w
%print the solution
fprintf(2, 'Risk: %.3f%%\n', sqrt(w'*sigma*w)*100);
fprintf(2, 'Ret: %.3f%%\n', w'*mu'*100);
function f = MaxReturn(w,mu);
f = mtimes(w'*mu');
end
4 Comments
John D'Errico
on 15 Dec 2021
Edited: John D'Errico
on 15 Dec 2021
I'm not sure what you think this does?
f = mtimes(w'*mu');
Well, besides generate an error, since mtimes is not a monadic operator.
w'*mu' is a valid operation. But then to try to pass that to mtimes, it will fail.
Anyway, without your data, we cannot even test your code.
Santiago
on 16 Dec 2021
John D'Errico
on 16 Dec 2021
Edited: John D'Errico
on 16 Dec 2021
My point is, IF you want to multiply w' with mu', then just write
f = w'*mu';
The mtimes there as you wrote it is redundant, thus mtimes(w'*mu'), and in fact, will generate a runtime error.
Walter Roberson
on 16 Dec 2021
ga() always passes in row vectors for the unknowns. If you transpose that you would get a column vector, and matrix multiply of a column vector on the left is not going to result in a scalar as required for ga.
The corrected code is in my Answer: f = w*mu'
Accepted Answer
More Answers (0)
Categories
Find more on Problem-Based Optimization Setup 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!