Last updated: 2023-03-16.
tf_quant_finance.math.diff#
Computes the difference between elements of an array at a regular interval.
tf_quant_finance.math.diff(
x, order=1, exclusive=False, axis=-1, dtype=None, name=None
)
For a difference along the final axis, if exclusive is True, then computes:
result[..., i] = x[..., i+order] - x[..., i] for i < size(x) - order
This is the same as doing x[..., order:] - x[..., :-order]. Note that in
this case the result Tensor is smaller in size than the input Tensor.
If exclusive is False, then computes:
result[..., i] = x[..., i] - x[..., i-order] for i >= order
result[..., i] = x[..., i] for 0 <= i < order
Example#
x = tf.constant([1, 2, 3, 4, 5])
dx = diff(x, order=1, exclusive=False) # Returns [1, 1, 1, 1, 1]
dx1 = diff(x, order=1, exclusive=True) # Returns [1, 1, 1, 1]
dx2 = diff(x, order=2, exclusive=False) # Returns [1, 2, 2, 2, 2]
Args:#
x: ATensorof shapebatch_shape + [n]and of any dtype for which arithmetic operations are permitted.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: Falseaxis: Python int. The axis ofxalong which to difference. Default value: -1 (the final axis).dtype: 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 ‘diff’.
Returns:#
diffs: ATensorof the same dtype asx. Ifexclusiveis True, then the shape isbatch_shape + [n-min(order, n)], otherwise it isbatch_shape + [n]. The final dimension of which contains the differences of the requested order.