Last updated: 2023-03-16.
tf_quant_finance.math.segment_ops.segment_cumsum#
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 1Tensorof any dtype for which arithmetic operations are permitted.segment_ids: ATensor. Must be one of the following types: int32, int64. A 1-D tensor whose size is equal to the size ofx. Values should be sorted and can be repeated. Values must range from0tonum segments - 1.exclusive: Python bool. See description above. Default value: Falsedtype: Optionaltf.Dtype. If supplied, the dtype forxto use when converting toTensor. Default value: None which maps to the default dtype inferred by TF.name: Pythonstrname prefixed to Ops created by this class. Default value: None which is mapped to the default name ‘segment_cumsum’.
Returns:#
cumsums: ATensorof the same dtype asx. Assuming that each segment is of length greater than or equal to order, ifexclusiveis True, then the size isn-order*kwherenis the size of x,kis the number of different segment ids supplied ifsegment_idsis not None or 1 ifsegment_idsis 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. Ifexclusiveis False, then the size isn.