sum(W)==1 used in a function does not work with the exact number

6 visualizzazioni (ultimi 30 giorni)
I have used "if sum(W)==1" in a function and the function does not work as it should. If I change it to "sum(W)<=1" it starts to work.
I have checked multiple times sum(W) is in fact equal to 1. I have tried several iterations e.g. increasing the value of W to more than 1 OR using "sum(W)>=1" In all iterations the conclusion is that something is getting lost in summation process and MATLAB thinks the sum is smaller than 1.
So my question what do I need to do for MATLAB to see that the sum of W is in fact 1 (and not less than 1).
The code (that works)is as follows:
function RARV=RARvariance1(A,B,C,D,E,F,G,H,W)
T=length(A); %Time period, the same for all assets
SW=sum(W);
if SW<=1
DD=[A-mean(A), B-mean(B), C-mean(C), D-mean(D), E-mean(E), F-mean(F), G-mean(G), H-mean(H)]; %Temporal, needed for covariances
Cov=DD'*DD/T;
V=W(1,:)*Cov*W(1,:)';
%Portfolio expected returns
ER=[mean(A), mean(B), mean(C), mean(D), mean(E), mean(F), mean(G), mean(H)]*W(1,:)';
%(Negative) risk adjusted return
RARV=-(ER)/sqrt(V); %Change V to sqrt(V) for semi-standard deviation
else
end
*I need the code to work with SW==1 *
Thanks
  2 Commenti
Image Analyst
Image Analyst il 20 Lug 2013
This should have been continued from your duplicate question. What is the value for W?
Andrew
Andrew il 20 Lug 2013
W =[0.150000000000000 0.150000000000000 0.200000000000000 0.100000000000000 0.100000000000000 0.100000000000000 0.100000000000000 0.100000000000000]
which is exactly equal to 1

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 20 Lug 2013
  1 Commento
Andrew
Andrew il 20 Lug 2013
Thank you very much. I tried the suggestion and it worked. It was a big help. I could not have guessed it. Thanks again.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Java Package Integration in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by