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!