Last updated: 2023-03-16.
tf_quant_finance.math.interpolation.linear.interpolate#
Performs linear interpolation for supplied points.
tf_quant_finance.math.interpolation.linear.interpolate(
x, x_data, y_data, left_slope=None, right_slope=None, validate_args=False,
optimize_for_tpu=False, dtype=None, name=None
)
Given a set of knots whose x- and y- coordinates are in x_data and y_data,
this function returns y-values for x-coordinates in x via piecewise
linear interpolation.
x_data must be non decreasing, but y_data don’t need to be because we do
not require the function approximated by these knots to be monotonic.
Examples#
import tf_quant_finance as tff
x = [-10, -1, 1, 3, 6, 7, 8, 15, 18, 25, 30, 35]
x_data = [-1, 2, 6, 8, 18, 30.0]
y_data = [10, -1, -5, 7, 9, 20]
tff.math.interpolation.linear.interpolate(x, x_data, y_data,
dtype=tf.float64)
# Expected: [ 10, 10, 2.66666667, -2, -5, 1, 7, 8.4, 9, 15.41666667, 20, 20]
Args:#
x: x-coordinates for which we need to get interpolation. A N-DTensorof real dtype. First N-1 dimensions represent batching dimensions.x_data: x coordinates. A N-DTensorof real dtype. Should be sorted in non decreasing order. First N-1 dimensions represent batching dimensions.y_data: y coordinates. A N-DTensorof real dtype. Should have the compatible shape asx_data. First N-1 dimensions represent batching dimensions.left_slope: The slope to use for extrapolation with x-coordinate smaller than the minx_data. It’s a 0-D or N-DTensor. Default value:None, which maps to0.0meaning constant extrapolation, i.e. extrapolated value will be the leftmosty_data.right_slope: The slope to use for extrapolation with x-coordinate greater than the maxx_data. It’s a 0-D or N-DTensor. Default value:Nonewhich maps to0.0meaning constant extrapolation, i.e. extrapolated value will be the rightmosty_data.validate_args: Pythonboolthat indicates whether the function performs the check if the shapes ofx_dataandy_dataare equal and that the elements inx_dataare non decreasing. If this value is set toFalseand the elements inx_dataare not increasing, the result of linear interpolation may be wrong. Default value:False.optimize_for_tpu: A Python bool. IfTrue, the algorithm uses one-hot encoding to lookup indices ofxinx_data. This significantly improves performance of the algorithm on a TPU device but may slow down performance on the CPU. Default value:False.dtype: Optional tf.dtype forx, x_data,y_data,left_slopeandright_slope. Default value:Nonewhich means that thedtypeinferred fromx`.name: Python str. The name prefixed to the ops created by this function. Default value:Nonewhich maps to ‘linear_interpolation’.
Returns:#
A N-D Tensor of real dtype corresponding to the x-values in x.