Clear Filters
Clear Filters

GA Multiobjective, Consider reducing the number of outputs

2 views (last 30 days)
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

Accepted Answer

Matt J
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);
Optimization terminated: average change in the spread of Pareto solutions less than options.FunctionTolerance.
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
T = 70×3 table
Risk Ret Sharpe _________________ _____________________ ____________________ 1.82270580599563 0.0414442638688286 0.0227377691630221 5.5975747702471 0.134827088101126 0.0240866971206485 5.23178853842043 0.120862342189557 0.0231015342653829 0.461935673562308 -0.000674686488551271 -0.00146056372600171 0.528634985441716 0.00767769295854254 0.0145236186971758 5.9032491046392 0.299494602724574 0.0507338581543525 0.704199038638267 0.013492243327366 0.0191597014296645 5.9076423218751 0.306218706756716 0.0518343342525722 5.95204520612309 0.2388704234092 0.0401324948210181 5.99154527935198 0.347974879239879 0.0580776515933322 5.88977561886967 0.313547919299954 0.0532359701947574 6.15988585509584 0.194051150537262 0.0315023938920445 0.767128657407251 0.0155247157962803 0.0202374342900327 0.459904609958335 0.0058126967149091 0.0126389181344273 6.00395224177566 0.230234228469682 0.0383471119020078 0.537608842709213 0.00968018020495565 0.0180059914122201
function f = MultipleMax(w,mu,sigma)
f(1) = -(w * mu');
f(2) = -(f(1)/sqrt(w*sigma*w'));
end
  2 Comments
Santiago
Santiago on 21 Dec 2021
Hi Matt, thank you for your answer.
I have a question, Is it possible to put the percentages (w) of each of the rows of the table you made?
Thanks for your help
Matt J
Matt J on 21 Dec 2021
Sure.
T=table(Risk*100,Ret*100, Ret./Risk,w,'Var',{'Risk', 'Ret','Sharpe','Percentages'})

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!