Using Sum(W) ==1 as a condition in a function

I have to write [sum(W) should equal 1] within a function. W is an 8 element vector.
When I try to write: if sum(W)==1
the function stops working. I have tried many variations of this but none of them work. Currently the sum of W is equal to 1.
How can I put this condition in the function?

댓글 수: 4

You don't seem to be doing anything wrong, @Andrew. It would help if you showed us some code and error message from terminal.
Thanks for asking for code. The following is the code.
function RARV=RARvariance1(A,B,C,D,E,F,G,H,W)
T=length(A); %Time period, the same for all assets
if sum(W)==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
disp error
end
The error message I get is: Error in RARvariance1 (line 19) T=length(A); %Time period, the same for all assets
The variable A is in the workspace. This error message only comes after I put the "if sum(W)==1" argument in the function.
Thanks again
What is the error message?
Andrew
Andrew 2013년 7월 20일
The following is the error message:
Error in RARvariance1 (line 19) T=length(A); %Time period, the same for all assets
Output argument "RARV" (and maybe others) not assigned during call to "C:\Users

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

답변 (3개)

nl2605
nl2605 2013년 7월 19일

0 개 추천

I think all you need to do is to pass the value A,B,C,D,E,F,G,H,W when you call this function. It has got nothing to do with sum. Also, the code that you have posted here has a semi-colon after if statement which is wrong. If its just a typo then its ok.

댓글 수: 6

Andrew
Andrew 2013년 7월 20일
Thanks for pointing out the semi-colon typo. I fixed it. The results are still the same.
I have all the variables (A,B,C,D,E,F,G,H,W)in the workspace. I just removed the if statement (along with the else and end) and the function worked and gave an answer.
this is W in workspace [0.150000000000000,0.150000000000000,0.150000000000000,0.150000000000000,0.100000000000000,0.100000000000000,0.100000000000000,0.100000000000000]
Please help me. Thanks.
Andy
Image Analyst
Image Analyst 2013년 7월 20일
You say "the function worked and gave an answer." - What help do you need? Are you still getting the output not assigned error (in contradiction to you saying it worked)? What did you discover when you used the debugger?
I changed the function a little and it worked with the change. The change I made is: from SW==1 to SW<=1
As you can see SW= sum(W), which is equal to 1.
SW<=1 does not serve my purpose.
My question:
Why is SW==1 not working when SW is equal to 1.
The following is the new function with the change:
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.0000
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
Image Analyst
Image Analyst 2013년 7월 20일
You seem to be avoiding the question. Are you using the debugger like everyone else would in this situation? Yes or no. If no, why not?
Andrew
Andrew 2013년 7월 20일
I saw the video. I don't have the ability to use this debugger. I am barely able to write a function. It is not a bug. I am just not doing things right.
sum(W) == 1 may involve some floating point comparison issues depending on the type of variable W. Without the full invocation code of function RARvariance1 we I cannot speculate on that.

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

Image Analyst
Image Analyst 2013년 7월 20일

0 개 추천

For some reason your function is returning before it ever gets to the "RARV=-(ER)/sqrt(V);" line so RARV (which is required to be defined because it needs to be returned) never gets defined.
I have an idea - why don't you use the debugger to step through your code? http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ You will quickly see which lines of code get executed and which don't.

댓글 수: 1

Image Analyst
Image Analyst 2013년 7월 20일
편집: Image Analyst 2013년 7월 20일
That's too bad. Debugging is a skill you should be able to learn. If you click in the margin and get the red spot on the first line in the function, the click F5 to run your program, when is stops at the red spot, you'll see that you don't have SW less than 1 so you go into the "else" block. Unfortunately you have no code in there, thus RARV never gets assigned to any value. What do you want to happen if SW > 1? Whatever it is, put that code after the else and make sure you assign some value for RARV.

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

Muthu Annamalai
Muthu Annamalai 2013년 7월 22일

0 개 추천

@Andrew on the similar lines as @Image Analyst - I think your return value is not assigned because you don't enter the if-condition branch.
So why don't you replace the code, using an assertion, and eliminating the if-else-end statement.
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);
assert( abs( SW - 1.0000) < 1e-3, 'sum(W) does not add to 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
end

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2013년 7월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by