tf_quant_finance.math.segment_ops.segment_cumsum

Last updated: 2023-03-16.

tf_quant_finance.math.segment_ops.segment_cumsum#

View source

Computes cumulative sum of elements in a segment.

tf_quant_finance.math.segment_ops.segment_cumsum(
    x, segment_ids, exclusive=False, dtype=None, name=None
)

For a complete description of segment_* ops see documentation of tf.segment_sum. This op extends the tf.math.cumsum functionality to segmented inputs.

The behaviour of this op is the same as that of the op tf.math.cumsum within each segment. The result is effectively a concatenation of the results of tf.math.cumsum applied to each segment with the same interpretation for the argument exclusive.

Example#

  x = tf.constant([2, 5, 1, 7, 9] + [32, 10, 12, 3] + [4, 8, 5])
  segments = tf.constant([0, 0, 0, 0, 0] + [1, 1, 1, 1] + [2, 2, 2])
  # Inclusive cumulative sum.
  # Expected result: [2, 7, 8, 15, 24, 32, 42, 54, 57, 4, 12, 17]
  cumsum1 = segment_cumsum(
      x, segment_ids=segments, exclusive=False)
  # Exclusive cumsum.
  # Expected result: [0, 2, 7, 8, 15, 0, 32, 42, 54, 0, 4, 12]
  cumsum2 = segment_cumsum(
      x, segment_ids=segments, exclusive=True)

Args:#

  • x: A rank 1 Tensor of any dtype for which arithmetic operations are permitted.

  • segment_ids: A Tensor. Must be one of the following types: int32, int64. A 1-D tensor whose size is equal to the size of x. Values should be sorted and can be repeated. Values must range from 0 to num segments - 1.

  • exclusive: Python bool. See description above. Default value: False

  • dtype: Optional tf.Dtype. If supplied, the dtype for x to use when converting to Tensor. Default value: None which maps to the default dtype inferred by TF.

  • name: Python str name prefixed to Ops created by this class. Default value: None which is mapped to the default name ‘segment_cumsum’.

Returns:#

  • cumsums: A Tensor of the same dtype as x. Assuming that each segment is of length greater than or equal to order, if exclusive is True, then the size is n-order*k where n is the size of x, k is the number of different segment ids supplied if segment_ids is not None or 1 if segment_ids is None. If any of the segments is of length less than the order, then the size is: n-sum(min(order, length(segment_j)), j) where the sum is over segments. If exclusive is False, then the size is n.