tf_quant_finance.math.interpolation.linear.interpolate

Last updated: 2023-03-16.

tf_quant_finance.math.interpolation.linear.interpolate#

View source

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-D Tensor of real dtype. First N-1 dimensions represent batching dimensions.

  • x_data: x coordinates. A N-D Tensor of real dtype. Should be sorted in non decreasing order. First N-1 dimensions represent batching dimensions.

  • y_data: y coordinates. A N-D Tensor of real dtype. Should have the compatible shape as x_data. First N-1 dimensions represent batching dimensions.

  • left_slope: The slope to use for extrapolation with x-coordinate smaller than the min x_data. It’s a 0-D or N-D Tensor. Default value: None, which maps to 0.0 meaning constant extrapolation, i.e. extrapolated value will be the leftmost y_data.

  • right_slope: The slope to use for extrapolation with x-coordinate greater than the max x_data. It’s a 0-D or N-D Tensor. Default value: None which maps to 0.0 meaning constant extrapolation, i.e. extrapolated value will be the rightmost y_data.

  • validate_args: Python bool that indicates whether the function performs the check if the shapes of x_data and y_data are equal and that the elements in x_data are non decreasing. If this value is set to False and the elements in x_data are not increasing, the result of linear interpolation may be wrong. Default value: False.

  • optimize_for_tpu: A Python bool. If True, the algorithm uses one-hot encoding to lookup indices of x in x_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 for x, x_data, y_data, left_slopeandright_slope. Default value: Nonewhich means that thedtypeinferred from  x`.

  • name: Python str. The name prefixed to the ops created by this function. Default value: None which maps to ‘linear_interpolation’.

Returns:#

A N-D Tensor of real dtype corresponding to the x-values in x.