Asked by Andrew
on 19 Jul 2013

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?

*No products are associated with this question.*

Answer by nl2605
on 19 Jul 2013

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.

Image Analyst
on 20 Jul 2013

Andrew
on 20 Jul 2013

Muthu Annamalai
on 22 Jul 2013

**RARvariance1** we I cannot speculate on that.

Log in to comment.

Answer by Image Analyst
on 20 Jul 2013

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.

Image Analyst
on 20 Jul 2013

Log in to comment.

Answer by Muthu Annamalai
on 22 Jul 2013

@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

Log in to comment.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 4 Comments

## Muthu Annamalai (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/82629-using-sum-w-1-as-a-condition-in-a-function#comment_160735

You don't seem to be doing anything wrong, @Andrew. It would help if you showed us some code and error message from terminal.

## Andrew (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/82629-using-sum-w-1-as-a-condition-in-a-function#comment_160738

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

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

## Azzi Abdelmalek (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/82629-using-sum-w-1-as-a-condition-in-a-function#comment_160739

What is the error message?

## Andrew (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/82629-using-sum-w-1-as-a-condition-in-a-function#comment_160756

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

Log in to comment.