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

조회 수: 22 (최근 30일)
Andrew
Andrew 2013년 7월 20일
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
Image Analyst
Image Analyst 2013년 7월 20일
This should have been continued from your duplicate question. What is the value for W?
Andrew
Andrew 2013년 7월 20일
W =[0.150000000000000 0.150000000000000 0.200000000000000 0.100000000000000 0.100000000000000 0.100000000000000 0.100000000000000 0.100000000000000]
which is exactly equal to 1

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2013년 7월 20일
  댓글 수: 1
Andrew
Andrew 2013년 7월 20일
Thank you very much. I tried the suggestion and it worked. It was a big help. I could not have guessed it. Thanks again.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Java Package Integration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by