How can I do a cumulative sum?

Use this forum to post Vensim related questions.
Post Reply
luisangelguzmang
Member
Posts: 42
Joined: Wed Apr 06, 2011 8:20 am

How can I do a cumulative sum?

Post by luisangelguzmang »

I need to do a cumulative sum over a vector according to V=ai+ai-1. For example:
Vector = [2, 4, 6, 3, 8, 1]
I need a new vector: V*=[2, 6, 12, 15, 23, 24]

There are a function for this?
Thks...
Administrator
Super Administrator
Posts: 4838
Joined: Wed Mar 05, 2003 3:10 am

Re: How can I do a cumulative sum?

Post by Administrator »

The following should work (sorry, I don't have time to test it right now).

Code: Select all

subscript i ID = i

v*[i] = sum ( if then else ( subscript i ID[i!] <= i , vector[i!] , 0 ) )
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
tomfid
Administrator
Posts: 3994
Joined: Wed May 24, 2006 4:54 am

Re: How can I do a cumulative sum?

Post by tomfid »

See the sample model in http://www.ventanasystems.co.uk/forum/v ... f=2&t=5202 for an example of an efficient approach to iteration/cumulation with ss mapping.
luisangelguzmang
Member
Posts: 42
Joined: Wed Apr 06, 2011 8:20 am

Re: How can I do a cumulative sum?

Post by luisangelguzmang »

Thks, but I do not quite understand the code. I already got the subindex i, I don´t understand this part:

subscript i ID = i
Administrator
Super Administrator
Posts: 4838
Joined: Wed Mar 05, 2003 3:10 am

Re: How can I do a cumulative sum?

Post by Administrator »

Sorry, I should have written
subscript i ID = i

This is a new parameter. It is basically the value of i so that it can be used in the sum calculation.
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
tomfid
Administrator
Posts: 3994
Joined: Wed May 24, 2006 4:54 am

Re: How can I do a cumulative sum?

Post by tomfid »

Also, it could be wrapped in an INITIAL,

Code: Select all

subscript i ID[i] = INITIAL(i)
The mapping approach in the sample model linked is more efficient, O(i) time vs. O(i^2) here, but more work to set up, so if your model is small, this approach is easier.
luisangelguzmang
Member
Posts: 42
Joined: Wed Apr 06, 2011 8:20 am

Re: How can I do a cumulative sum?

Post by luisangelguzmang »

Sorry if I already have definined my subscript i, I have to redefined? or create a new subscript?
ID is a new variable?
Thanks
tomfid
Administrator
Posts: 3994
Joined: Wed May 24, 2006 4:54 am

Re: How can I do a cumulative sum?

Post by tomfid »

ID is new. i is whatever dimension you already have.
Post Reply