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

7 visualizaciones (últimos 30 días)
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 comentarios
Image Analyst
Image Analyst el 20 de Jul. de 2013
This should have been continued from your duplicate question. What is the value for W?
Andrew
Andrew el 20 de Jul. de 2013
W =[0.150000000000000 0.150000000000000 0.200000000000000 0.100000000000000 0.100000000000000 0.100000000000000 0.100000000000000 0.100000000000000]
which is exactly equal to 1

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 20 de Jul. de 2013
  1 comentario
Andrew
Andrew el 20 de Jul. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Java Package Integration en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by