GA Multiobjective, Consider reducing the number of outputs
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Matt J
2021-12-21
编辑:Matt J
2021-12-21
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 个评论
Matt J
2021-12-21
Sure.
T=table(Risk*100,Ret*100, Ret./Risk,w,'Var',{'Risk', 'Ret','Sharpe','Percentages'})
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!