Filter löschen
Filter löschen

How do i get a scalar value so i can get rid of this error using fmincon function?

1 Ansicht (letzte 30 Tage)
trying to find optimal weights for portfolio and want to use fmincon to maximise sharp ratio
Maximize Sharpe Ratio If Put a negative sign - on the compute_sharpe objective function: maximization problem into a minimization one:
1 %%Maximize Sharpe ratio
2 Aeq = ones(1,N);
3 beq = 1;
4
5 w0 = ones(N,1)*(1/N);
6 % NOTE: use - to convert max optimization to a min ...
optimization problem
7 w4 = fmincon(@(w)-compute_sharpe(w, avg_ret, ...
cov_mat), w0, [], [], Aeq, beq, [], [], [], ...
options);
8 [w4,fval4] = fmincon(@(w)-compute_sharpe(w, avg_ret, ...
cov_mat), w0, [], [], Aeq, beq, [], [], [], ...
options);
9 % fval4 = -sr1 as the fun = -compute_sharp
10 sr = - fval4;
11 disp(['The maximised portfolio Sharpe ratio is ', ...
num2str(sr)]);
i keep getting this error in the screen shot how do i make it a scalar value so i can get rid of it? thanks
  4 Kommentare
Cam Salzberger
Cam Salzberger am 20 Okt. 2017
As I said, we can't really tell you why your objective function is giving a non-scalar result without seeing your objective function. Can you edit your question and add the definition for "compute_sharpe"?
LM
LM am 20 Okt. 2017
Bearbeitet: LM am 20 Okt. 2017
This line here is giving me the error
w4 = fmincon(@(w)-compute_sharpe(w,avg_daily_ret,cov_mat),w0, [], [],Aeq ,beq)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Okt. 2017
According to the comments in that code, w is a column vector, which would be N x 1 . w' would then be 1 x N, and so if the w' * cov_mat * w is not going to fail on inconsistent size basis, pvar is going to have to end up a scalar.
You have pret = w * avg_daily_ret; with both defined as column vectors. That would have to fail. Perhaps avg_daily_ret is a row vector or scalar? If the * is to work, then the N x 1 * something is going to have to end up with an N x whatever result, not a scalar.
You then have p_sharpe = (pret - 3) / sqrt(pvar); . We have determined that pret cannot be a scalar (unless w was a scalar, I guess). Therefore p_sharpe cannot be a scalar.
  2 Kommentare
LM
LM am 20 Okt. 2017
What if i try useing avg_daily_ret’ as input, transposed. As it is a row vector now, but if it's a col vector would the calculation go ahead?
Walter Roberson
Walter Roberson am 20 Okt. 2017
With w on the left side of w * avg_daily_ret then you are going to get N x 1 size for pret, and then you have a problem. Your pret calculation needs to calculate a scalar. Possibly
pret = w' * avg_daily_ret
with avg_daily_ret still a column vector.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by