Last updated: 2023-03-16.
tf_quant_finance.math.segment_ops.segment_diff#
Computes difference of successive elements in a segment.
tf_quant_finance.math.segment_ops.segment_diff(
x, segment_ids, order=1, exclusive=False, dtype=None, name=None
)
For a complete description of segment_* ops see documentation of
tf.segment_max. This op extends the diff functionality to segmented
inputs.
The behaviour of this op is the same as that of the op diff within each
segment. The result is effectively a concatenation of the results of diff
applied to each segment.
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])
# First order diff. Expected result: [3, -4, 6, 2, -22, 2, -9, 4, -3]
dx1 = segment_diff(
x, segment_ids=segments, order=1, exclusive=True)
# Non-exclusive, second order diff.
# Expected result: [2, 5, -1, 2, 8, 32, 10, -20, -7, 4, 8, 1]
dx2 = segment_diff(
x, segment_ids=segments, order=2, exclusive=False)
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.order: Positive Python int. The order of the difference to compute.order = 1corresponds to the difference between successive elements. Default value: 1exclusive: 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_diff’.
Returns:#
diffs: 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.