CUMSUM

Purpose:

Calculates the cumulative sum of a series.

Syntax:

CUMSUM(series, dim, "naflag", "method")

series

-

A series or array, the input series.

dim

-

Optional. An integer or string, the computation dimension.

1

:

calculate column-wise (default)

2

:

calculate row-wise

"naflag"

-

Optional. A string, the NA handling method.

"includenan"

:

include NA values (default)

"omitnan"

:

ignore NA values

"method"

-

Optional. A string, the summation method.

"neumaier" or empty

:

high precision compensated summation (default)

"direct"

:

direct floating point summation

Returns:

A series or table.

Example:

cumsum({20, 15, 30, 10, 25})

 

returns {20, 35, 65, 75, 100}.

Example:

W1: integ(gnorm(1000, 1))

W2: cumsum(W1)

 

sum(W1) == W2[end]

 

The last point of CUMSUM is the overall sum of the input data.

Example:

W1: {{1, 2, 3},

     {4, 5, 6},

     {7, 8, 9}}

 

W2: cumsum(w1)

W3: cumsum(w1, 1)

W4: cumsum(w1, 2)

 

W2 == {{ 1,  2,  3},

       { 5,  7,  9},

       {12, 15, 18}}

 

W3 == {{ 1,  2,  3},

       { 5,  7,  9},

       {12, 15, 18}}

 

W4 == {{1,  3,  6},

       {4,  9, 15},

       {7, 15, 24}}

 

Both W2 and W3 compute the cumulative sum along the columns.

 

W4 computes the cumulative sum along the rows.

Example:

W1: {{1,   2, 3},

     {nan, 5, 6},

     {7,   8, 9}}

 

W2: cumsum(w1)

W3: cumsum(w1, 1)

W4: cumsum(w1, 2)

 

W2 == {{  1,  2,  3},

       {nan,  7,  9},

       {nan, 15, 18}}

 

W3 == {{  1,  2,  3},

       {nan,  7,  9},

       {nan, 15, 18}}

 

W4 == {{  1,   3,  6},

       {nan, nan, nan},

       {  7,  15, 24}}

 

The NaN value is included in the computations by default.

Example:

W1: {{1,   2, 3},

     {nan, 5, 6},

     {7,   8, 9}}

 

W2: cumsum(w1, "omitnan")

W3: cumsum(w1, 1, "omitnan")

W4: cumsum(w1, 2, "omitnan")

 

W2 == {{1,  2,  3},

       {1,  7,  9},

       {8, 15, 18}}

 

W3 == {{1,  2,  3},

       {1,  7,  9},

       {8, 15, 18}}

 

W4 == {{1,   3,  6},

       {0,   5, 11},

       {7,  15, 24}}

 

Same as above except the NaN value is omitted from the computations.

Example:

W1: {1e16, 1, -1e16, -1}

W2: cumsum(w1)

W3: cumsum(w1, "neumaier")

W4: cumsum(w1, "direct")

 

 

W2[end] == 0

W3[end] == 0

W4[end] == -1

 

The series in W1 contains numbers that differ by many orders of magnitude. Mathematically, the exact cumulative sum of W1 should end with the total sum of 0.

The Neumaier compensation summation used in W2 and W3 track precision loss during intermediate steps and accurately return a total sum of 0.

The direct summation method in W4 uses standard floating point addition and suffers from catastrophic cancellation leading to an incorrect result of -1.

Remarks:

The nth value of the output series is equal to the sum of the first n points of the input series:

 

image\cumsum01.svg

 

or by the equivalent difference equation:

 

image\cumsum02.svg

 

Unlike SUM, the default for CUMSUM is to include NaN values. Set naflag to "omitnan" to skip NaN values.

 

The default Neumaier method is a compensated summation algorithm that extends Kahan summation to accurately add floating-point numbers of vastly different magnitudes, minimizing catastrophic cancellation and rounding errors.

 

The less accurate "direct" method may be faster for very large series or arrays.

 

CUMSUM calculates the cumulative sum of a series.

 

CUMSUM differs from integration in that the DELTAX information is not incorporated into the calculation.

 

CUMSUM is produces the same results as PARTSUM.

 

See CUMAVG to compute the cumulative average.

 

See CUMPROD to calculate the cumulative product.

 

See SUM to compute the total sum of a series.

See Also:

CUMAVG

CUMMAX

CUMMIN

CUMPROD

CUMTRAPZ

INTEG

PARTSUM

SUM