Page 1 of 1

Conditional sum (i.e.: sum if) over a vector

Posted: Mon Jun 15, 2015 3:12 am
by grabodan
Dear Members,

I am having a presumably very common problem:
Simulating a time series many times, then conducting a test, and then check how often the Null hypothesis was rejected.
That way I can test the size and power of the test. At the end I store the p-values in a vector (not a series).

I need the sum of entries in that vector that are less then 5%.
And I dont find a command for this. Like the simple Excel "sumif".

The question might have been answered a million times, but I didnt find any of these.
The Problem can be solved by a for loop such as:

Code: Select all

count=0
for !i=1 to !mcmax
    if pvalue(!i) <0.05 then
       count= count+1
    endif
next


Question: Is there a command for this?

Maybe I can use @sumsby. But then it seems to me that I have to (a) use a series instead of a vector which is not so nice and (b) first have to specify a new series containing a 1 if pvalue<0.05 and a zero otherwise. Doing (b) would again require a loop such as the one above, doesn't it?


To add some naivety to my question: How can I, generally, find commands like the sumsby? Or find a command that does what I am looking for in the above question? I often only get anywhere by searching the forum and I am having a hard time finding what I am looking for in the Help files. Am I doing somewhing wrong? ;)

Thanks 2 everyone!
Daniel

Re: Conditional sum (i.e.: sum if) over a vector

Posted: Mon Jun 15, 2015 6:35 am
by trubador

Code: Select all

vector reject = @elt(pvalue,0.05*@ones(!mcmax))
scalar count = @sum(reject)

Re: Conditional sum (i.e.: sum if) over a vector

Posted: Mon Jun 15, 2015 7:13 am
by grabodan
Thanks a lot trubador!
That does the job.

(I combined it to save space :) scalar count = @sum(@elt(pwerte_05,0.05*@ones(!mcmax)))