Fmincon inside a for loop, values not changing!
3 views (last 30 days)
Show older comments
I have a task to run an optimization for every row with 100 columns, This is how I have formulated. values of all other parameters are calculated(Code and matrix attached if needed). When i run the loop, I do not get any response (it gives me my initial conditions as final answer)
Here is my script
Aeq = ones(1,n);
Beq = 1;
a0 = rand(1,n);
options = optimoptions(@fmincon,'Display','off');
w0 = 1/n*ones(1,n);
tic
for i = 3:t % trial
[a_opt] = fmincon(@(w) longsharpe(a0, avrcc, avrco, avroc, avroo, avrrvp,...
avrtvl, rcc, rco, roc, roo, tvl, rvp, i, n),w0,[],[],Aeq,Beq,[],[],[],options);
w_strat2(i,:) = a_opt;
end
and Here is the function,
function [y] = longsharpe(a, avrcc, avrco, avroc, avroo, avrrvp,...
avrtvl, rcc, rco, roc, roo, tvl, rvp, i, n)
% find weight vector for each day
w = zeros(1,n);
ret = rcc(i-1:i,:);
mu = mean(ret);
Q = cov(ret);
for j = 1:n
w(1,j) = a(1)*(rcc(i-1,j)-avrcc(i-1))./n...
+ a(2)*(roo(i,j)-avroo(i))./n...
+ a(3)*(roc(i-1,j)-avroc(i-1))./n ...
+ a(4)*(rco(i,j)-avrco(i))./n...
+ a(5)*(tvl(i-1,j)/avrtvl(i-1,j))*(rcc(i-1,j)-avrcc(i-1))./n...
+ a(6)*(tvl(i-1,j)/avrtvl(i-1,j))*(roo(i,j)-avroo(i))./n...
+ a(7)*(tvl(i-1,j)/avrtvl(i-1,j))*(roc(i,j)-avroc(i-1))./n...
+ a(8)*(tvl(i-1,j)/avrtvl(i-1,j))*(rco(i,j)-avrco(i))./n...
+ a(9)*(rvp(i-1,j)/avrrvp(i-1,j))*(rcc(i-1,j)-avrcc(i-1))./n...
+ a(10)*(rvp(i-1,j)/avrrvp(i-1,j))*(roo(i,j)-avroo(i))./n...
+ a(11)*(rvp(i-1,j)/avrrvp(i-1,j))*(roc(i-1,j)-avroc(i-1))./n...
+ a(12)*(rvp(i-1,j)/avrrvp(i-1,j))*(rco(i,j)-avrco(i))./n;
end
%setting up sharpe ratio with risk free rate 4%
y = -(mu*w'-0.04/252)/sqrt(w*(Q*w'));
end
0 Comments
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Linear Least Squares in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!