Function Repository Resource:

ReverseAccumulate

Source Notebook

Replace each element of a list with the sum of the elements in the original list that are not to the left of that element

Contributed by: Seth J. Chandler

ResourceFunction["ReverseAccumulate"][list]

computes the successive accumulated totals of list, proceding from right to left.

Details and Options

ResourceFunction["ReverseAccumulate"] is efficiently computed by reversing the original list, running Accumulate on it and then reversing the result.
ResourceFunction["ReverseAccumulate"] is useful in computing actuarial commutation functions.

Examples

Basic Examples (2) 

Get the sum of elements not to the left of each element in a list:

In[1]:=
ResourceFunction["ReverseAccumulate"][{1, 2, 3, 4}]
Out[1]=

Apply ReverseAccumulate to a symbolic array:

In[2]:=
ResourceFunction["ReverseAccumulate"][Array[a, {5}]]
Out[2]=

Scope (3) 

Actuarial computations sometimes depend on nested ReverseAccumulate operations:

In[3]:=
Nest[ResourceFunction["ReverseAccumulate"], {1, 3, 5, 7, 2}, 2]
Out[3]=

The Head of an expression does not need to be List for ReverseAccumulate to work:

In[4]:=
ResourceFunction["ReverseAccumulate"][f[1, 2, 3, 4]]
Out[4]=

Where the argument is a nested list, X, ReverseAccumulate creates a nested list Y of similar dimension in which Y[[i,j]]=ReverseAccumulate[X[[All,j]]][[i]]:

In[5]:=
Y = ResourceFunction["ReverseAccumulate"][X = Array[x, {4, 3}]]
Out[5]=
In[6]:=
Table[Y[[i, j]] === ResourceFunction["ReverseAccumulate"][X[[All, j]]][[i]], {i, 1, 4}, {j, 1, 3}]
Out[6]=

Applications (1) 

Compute the values of the actuarial commutation function S for a survival list and an annual discount value of 0.8:

In[7]:=
Nest[ResourceFunction[
 "ReverseAccumulate"], {100, 97, 92, 86, 50, 20, 9, 2, 0}*0.8^
  Range[0, 8], 2]
Out[7]=

Properties and Relations (2) 

The ReverseAccumulate of a list is equivalent to the Reverse of the Accumulate of the Reverse of the list:

In[8]:=
ResourceFunction["ReverseAccumulate"][{x, y, z}] === Reverse[Accumulate[Reverse[{x, y, z}]]]
Out[8]=

One can generalize the idea of ReverseAccumulate to include operations that do not involve addition:

In[9]:=
RightFoldList[f_, x_, list_] := Reverse[Rest@FoldList[f, x, Reverse[list]]]
In[10]:=
RightFoldList[Times, 1, {3, 4, 5}]
Out[10]=

Indeed, ReverseAccumulate is a special case of the following RightFoldList:

In[11]:=
RightFoldList[Plus, 0, {1, 2, 3, 4}]
Out[11]=

Publisher

Seth J. Chandler

Version History

  • 1.0.0 – 10 September 2019

Related Resources

License Information