Optimisation, with 20 variable program?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
So I am creating a portfolio of 20 assets and their returns, and then I am trying to minimize them with the optimisation app. I have managed to get it to work with 3 assets (variables) but I don't have a clue how to get the right line of code for 20.
The is the code that I have so far.
function Ret = PRet(x)
Return = xlsread('Data.xlsx','Sheet1', 'V3:AO254'); % I change the amount of data coming in depending on the formula, this is bringing in 20 assets.
Ret = x(1)* Return(:,1) + x(2)*Return(:,2) + (1-x(1)-x(2))*Return(:,3); % This is the line I have to change to 20 variables and I cannot work it out.
end
The other code is in a function GetVar, which works fine, by sorting the data then getting the correct percentile and I apply this to the Optimisation app.
@(x)GetVar(PRet(x),0.05,-1) %where the 0.05 is the percentile, the -1 is just the left tail, and it is the x value that I am trying to work out.
I have tried using
Ret = x(1)*Return(:,1) + x(2)*Return(:,2) + x(3)*Return(:,3) +x(4)*Return(:,4) +x(5)*Return(:,5) +x(6)*Return(:,6)+x(7)*Return(:,7)+x(8)*Return(:,8)+x(9)*Return(:,9)+x(10)*Return(:,10)+x(11)*Return(:,11)+x(12)*Return(:,12)+x(13)*Return(:,13)+x(14)*Return(:,14)+x(15)*Return(:,15)+x(16)*Return(:,16)+x(17)*Return(:,17)+x(18)*Return(:,18)+x(19)*Return(:,19)+x(20)*Return(:,20);
but that doesn't really help unless i give an x of [0.05....0.05] but then i don't need the optimisation tool.
the x matrix is the weightings of each individual asset returns, and i am trying to find the x values that add up to one that will give me the lowest value of VaR.
there error i get whenever I try to change the code to add more data in is:
Index exceeds matrix dimensions.
Any help would be great thanks.
Edit:
So I think by changing the line of code to
Ret = x(1)*Return(:,1) + x(2)*Return(:,2) + x(3)*Return(:,3) +(1-x(2)-x(3)-x(1))*Return(:,4);
for 4 sets of data, then changing the optimisation tool start points, linear inequalities and bounds to a 3 column matrix would work for the 4 variables? it seems too anyway, would changing this to 20 variables in a a similar way work too? or would that just take forever :S
0 commentaires
Réponses (1)
Walter Roberson
le 1 Mai 2018
Ret = Return * [x(:); 1-sum(x)];
Here x would be a vector with one fewer entries than the number of columns in Return.
Note you should avoid doing the xlsread there. See https://www.mathworks.com/help/matlab/math/parameterizing-functions.html
0 commentaires
Voir également
Catégories
En savoir plus sur Financial Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!