Calculates the cumulative sum of a series.
CUMSUM(series, dim, "naflag", "method")
series |
- |
A series or array, the input series. |
||||||
dim |
- |
Optional. An integer or string, the computation dimension.
|
||||||
"naflag" |
- |
Optional. A string, the NA handling method.
|
||||||
"method" |
- |
Optional. A string, the summation method.
|
A series or table.
cumsum({20, 15, 30, 10, 25})
returns {20, 35, 65, 75, 100}.
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.
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.
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.
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.
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.
The nth value of the output series is equal to the sum of the first n points of the input series:
or by the equivalent difference equation:
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.