How to apply fmincon when we have long function creation
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Fmincon doesnt use the function f That I created so my result doesnt make sense can anyone help me please
x0 = w_maxRet;
[x,fval] = fmincon(@objfun,x0,[],[],[1 1 1 1],[1],[0 0 0 0],[]);
function f = objfun(w1, w2, w3, w4)
clc
w_maxRet = [0.3382 0.3382 0.1414 0.1821];
initial_position = 1000000 * w_maxRet;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% You can insert your code here %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global four_data_prices;
for i = 1:size(four_data_prices,2)
for j = 1:size(four_data_prices,1)-1
cumul_Return(j,i) = ((four_data_prices(j+1,i)-four_data_prices(1,i))/four_data_prices(1,i)+1);
cumul_Value(j,i) = cumul_Return(j,i) * initial_position(1,i);
end
end
% 2. Calculate the cumulative return and cumulative value of your position in each equity
% all the way from the first to the last day.
cumReturn = [100*ones(1,size(four_data_prices,2)) ; cumul_Return];
cumValue = [initial_position ; cumul_Value];
%3. Calculate the cumulative return and total value (total portfolio value is the sum of daily
% values of all portfolio components) of your fund all the way from the first to the last
% day.
fund_totalValue = transpose(sum(cumValue'));
%4. Calculate daily rate of return (daily return of your portfolio) of your your fund all the way from the first to the last day.
for i = 1:size(fund_totalValue,1)-1
fund_cumulRet(i,1) = (1+(fund_totalValue(i+1,1)-fund_totalValue(i,1))/fund_totalValue(i,1))*100;
fund_dailyRet(i,1) = ((fund_totalValue(i+1,1)-fund_totalValue(i,1))/fund_totalValue(i,1))*100;
end
fund_cumulRet = [100 ; fund_cumulRet];
%Calculate annual rate of return of your portfolio
annual_rate = (fund_totalValue(end/1)/fund_totalValue(1,1))-1;
%6,7 calculate mean and st_dev
average_daily_return = mean(fund_dailyRet);
st_dev_daily_return = std(fund_dailyRet);
% 8. Calculate annualized Sharpe ratio of your portfolio
annualized_SR = sqrt(252)*(average_daily_return/st_dev_daily_return);
f = sqrt(252)*(average_daily_return/st_dev_daily_return);
end
0 commentaires
Réponses (1)
Walter Roberson
le 18 Fév 2013
You wrote your objfun to expect 4 parameters, but fmincon will only pass 1 parameter, a vector the same length as your x0.
Please read http://www.mathworks.com/help/optim/ug/passing-extra-parameters.html if you need extra parameters.
0 commentaires
Voir également
Catégories
En savoir plus sur Portfolio Optimization and Asset Allocation 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!