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

1 view (last 30 days)
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 Comments
Image Analyst
Image Analyst on 20 Jul 2013
This should have been continued from your duplicate question. What is the value for W?
Andrew
Andrew on 20 Jul 2013
W =[0.150000000000000 0.150000000000000 0.200000000000000 0.100000000000000 0.100000000000000 0.100000000000000 0.100000000000000 0.100000000000000]
which is exactly equal to 1

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 20 Jul 2013
  1 Comment
Andrew
Andrew on 20 Jul 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing 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!