Skip to content

Running sum

18 messages · Andy Bunn, Sean Davis, Brian Ripley +8 more

#
I have vector X of length N that I want to have a running sum for 
(called Y).  I just need max(Y).  I do this with a "for" loop like so:

     Y <- vector(length=N)
     Y[1] <- X[1]
     for (i in 2:N) {
       Y[i] <- Y[i-1]+X[i]
     }
     return(max(Y))

Is there a faster way to do this?

Thanks,
Sean
#
Sean Davis wrote:

            
max(cumsum(Y))

Kjetil

  
    
#
see ?cumsum
x <- 1:10
cumsum(x)
max(cumsum(x))

HTH, Andy
#
On Fri, 2004-11-19 at 13:08 -0500, Sean Davis wrote:
Something like:
[1]  1  3  6 10 15 21 28 36 45 55
[1] 55

Does that help?

Marc Schwartz
#
?cumsum
On Fri, 19 Nov 2004, Sean Davis wrote:

            

  
    
#
Have you considered "cumsum"? 

      > cumsum(c(1, 2, 3, -9, 2))
      [1]  1  3  6 -3 -1

      hope this helps.  spencer graves
Sean Davis wrote:

            

  
    
#
You could try using embed(), but I doubt it's faster.

-roger
Sean Davis wrote:

  
    
#
?cumsum is not exactly the answer (as I understand it), but a part of it.
I propose:

runSum2 <- function(x)
	cumsum(x)[-1] - c(0, cumsum(x[1:(length(x) - 2)]))

# Example
a <- round(runif(10, 0, 10))
a
runSum2(a)
max(runSum2(a)) # To get only the max

Best,

Philippe

..............................................<??}))><........
 ) ) ) ) )
( ( ( ( (    Prof. Philippe Grosjean
 ) ) ) ) )
( ( ( ( (    Numerical Ecology of Aquatic Systems
 ) ) ) ) )   Mons-Hainaut University, Pentagone
( ( ( ( (    Academie Universitaire Wallonie-Bruxelles
 ) ) ) ) )   6, av du Champ de Mars, 7000 Mons, Belgium  
( ( ( ( (       
 ) ) ) ) )   phone: + 32.65.37.34.97, fax: + 32.65.37.33.12
( ( ( ( (    email: Philippe.Grosjean at umh.ac.be
 ) ) ) ) )      
( ( ( ( (    web:   http://www.umh.ac.be/~econum
 ) ) ) ) )
..............................................................
#
Prof Brian Ripley wrote:

            
My apologies, I am not being entirely serious, but...

Please do read the posting guide! 
(particularly the third point in the "Responding to other posts" section).

Bob
#
On Fri, 2004-11-19 at 21:10 +0100, Philippe Grosjean wrote:
Phillipe, 

If you run Sean's original function for 1:10, you get:

X <- 1:10
Y <- vector(length = 10)
Y[1] <- X[1]

for (i in 2:10)
{
   Y[i] <- Y[i-1] + X[i]
}
[1]  1  3  6 10 15 21 28 36 45 55

which is equivalent to cumsum(1:10)


Your function yields:
[1]  3  5  7  9 11 13 15 17 19

Which is the sum of successive individual pairs of vector elements.

For future reference, the running() function in the gregmisc bundle's
gtools package offers a more general approach to 'moving window'
functions:
1:2  2:3  3:4  4:5  5:6  6:7  7:8  8:9 9:10 
   3    5    7    9   11   13   15   17   19 


Best regards,

Marc Schwartz
#
Perhaps I'm missing something, but isn't the maximum of the cumulative
sum simply the last value, ie. sum(x)?

Hadley
#
On Fri, 2004-11-19 at 16:44 -0600, hadley wickham wrote:
Indeed!

And...going back to Sean's original post he did write:

"I just need max(Y)."

Thus, a whole group of us (independently) managed to over-engineer the
solution. 

Presuming of course, that Sean does not require the entire cumulative
sum vector for other purposes.

I wonder what that says about human cognition....

Thanks,

Marc
<Cleaning his bi-focals...>
#
Marc Schwartz wrote:

            
Please, Please, please, ... was it ever said that the numbers was only 
positive?

Kjetil

  
    
#
Marc Schwartz wrote:
I have forgotten the details of the original question but it seems to me 
that if the elements of x could be both positive and negative then the 
maximum of the cumulative sum doesn't have to be the last sum.
#
On Fri, 2004-11-19 at 18:00 -0600, Douglas Bates wrote:

            

        
On Fri, 2004-11-19 at 20:00 -0400, Kjetil Brinchmann Halvorsen wrote:

            
Thanks to both Doug and Kjetil.

I stand corrected.

Marc
#
Ooops! Sorry. I did not look carefully enough on the code and focused on the
term! My proposition is for a "running sum with a window width of 2
observations" (the title of the question is "Running sum", isn't it?), while
the true question was indeed about a *cumulative sum*, which is something
totally different.
Best,

Philippe Grosjean
#
As many have mentioned, I was forgetting the negative numbers.  Thanks
to those who pointed that out.

Hadley