1. Home
  2. Linear algebra

Linear algebra · running products

Cumulative Product (cumprod) of a Vector or Matrix

Eigenfield editorial team ·

The cumulative product of a vector x is a vector p of the same length, where p(k) is the product of the first k entries: p(k) = x(1) · x(2) · … · x(k). Matrix languages usually call the function cumprod. It is the multiplicative twin of the cumulative sum, and it answers questions about total growth after each period in a single call.

Definition and a first example

For x = [1 2 3 4], the running products are 1, then 1 · 2 = 2, then 2 · 3 = 6, then 6 · 4 = 24. So cumprod(x) = [1 2 6 24], which lists the factorials 1!, 2!, 3! and 4!. The last entry equals the ordinary product of all the elements, so cumprod(x)(end) matches prod(x).

Each entry is computed from the previous one, p(k) = p(k − 1) · x(k), so the function takes one pass and n − 1 multiplications. Writing it as an explicit loop is easy, but the built-in is faster and clearer.

Matrices: which direction

Applied to a matrix, the default is to work down each column separately, treating the columns as independent vectors. For A = [1 2; 3 4; 5 6] the result is

cumprod(A) = [1 2; 3 8; 15 48].

Column 1 gives 1, 1 · 3 = 3, 3 · 5 = 15. Column 2 gives 2, 2 · 4 = 8, 8 · 6 = 48. Most languages accept a dimension argument to run along rows instead: along the rows the answer would be [1 2; 3 12; 5 30]. As with other reductions, forgetting which dimension is the default is a frequent source of silent errors. A row vector is the exception worth remembering, since many languages run along its single non-trivial dimension automatically.

Compound growth

A standard use is compounding. If a quantity grows by 5 percent per year, the yearly factor is 1.05, and cumprod([1.05 1.05 1.05]) gives [1.05, 1.1025, 1.157625]. After three years the amount has grown by about 15.8 percent, which is more than three times five. Varying rates work the same way: put each year’s factor in a vector, and the cumulative product is the growth index at each point.

Probabilities chain in the same way. The chance that a system survives k stages in a row, if stage survival events are independent, is the cumulative product of the per-stage survival probabilities.

Overflow, underflow and logarithms

Products grow or shrink fast, and floating point has limits. In IEEE double precision the largest finite value is about 1.8 × 10³⁰⁸. Factorials reach it quickly: 170! is roughly 7.3 × 10³⁰⁶ and still representable, but 171! exceeds the limit and the running product becomes infinity. In the other direction, a long chain of probabilities below one underflows to zero.

When all entries are positive, the logarithm gives a safe route. Compute the cumulative sum of log(x) and exponentiate only at the end, or keep the logs and compare them directly, since log(p(k)) = log x(1) + … + log x(k). Likelihood calculations are normally done this way. The trick fails for zero or negative entries, where the logarithm is undefined or complex, so handle signs and zeros separately.

x = [1 2 3 4];
p = cumprod(x)             % [1 2 6 24]
q = exp(cumsum(log(x)))    % same values up to rounding

Expect q to match p only to rounding error, so compare with a tolerance, never with exact equality.

Neighbors

The operation sits next to element-wise work such as element-by-element multiplication, and its indexing conventions are described in elements of a matrix. A related running quotient is covered under running quotient. The linear algebra hub lists further operations.

Back to top