GA Multiobjective, Consider reducing the number of outputs
2 views (last 30 days)
Show older comments
I'm trying to make maximize the portfolio return and the sharpe ratio at the same time using a genetic algorithm.
So, I'm using gamultiobj function, but my code don't archive a result.
Could someone help me fix my code or help me find where the error is.
Thanks
format long g
filename = 'Returns.xlsx';
data = readmatrix(filename);
nAssets = size(data, 2);
%Returns and covariance
mu = mean(data);
mu.'
sigma = cov(data);
%formulate the problem/optimization
f = zeros(nAssets, 1); %there is no constant
A = -eye(nAssets) %besides the returns we forbid short selling
b = 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
fcn = @(w)MultipleMax(w,mu,sigma);
[w, fval, flag, output] = gamultiobj(fcn, nAssets, A, b, Aeq, beq)
if isempty(w)
warning('could not find any solution')
else
%print the solution
fprintf(2, 'Risk: %.3f%%\n', sqrt(w*sigma*w')*100);
fprintf(2, 'Ret: %.3f%%\n', w*mu'*100);
fprintf(2, 'Sharpe: %.3f%%\n', (w * mu')/sqrt(w*sigma*w'));
w.'
end
function f = MultipleMax(w,mu,sigma)
f(1) = -(w * mu');
f(2) = -((w * mu')/sqrt(w*sigma*w'))
end
2 Comments
Accepted Answer
Matt J
on 21 Dec 2021
Edited: Matt J
on 21 Dec 2021
format long g
filename = 'Returns.xlsx';
data = readmatrix(filename);
nAssets = size(data, 2);
%Returns and covariance
mu = mean(data);
sigma = cov(data);
%formulate the problem/optimization
f = zeros(nAssets, 1); %there is no constant
A = -eye(nAssets); %besides the returns we forbid short selling
b = 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
fcn = @(w)MultipleMax(w,mu,sigma);
[w, fval, flag, output] = gamultiobj(fcn, nAssets, A, b, Aeq, beq);
if isempty(w)
warning('could not find any solution')
else
Risk=sqrt(sum( (w*sigma).*w ,2));
Ret=w*mu';
%print the solution
T=table(Risk*100,Ret*100, Ret./Risk,'Var',{'Risk', 'Ret','Sharpe'})
end
function f = MultipleMax(w,mu,sigma)
f(1) = -(w * mu');
f(2) = -(f(1)/sqrt(w*sigma*w'));
end
2 Comments
Matt J
on 21 Dec 2021
Sure.
T=table(Risk*100,Ret*100, Ret./Risk,w,'Var',{'Risk', 'Ret','Sharpe','Percentages'})
More Answers (0)
See Also
Categories
Find more on Genetic Algorithm 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!