Last updated: 2023-03-16.
tf_quant_finance.math.interpolation.interpolation_2d.Interpolation2D#
Performs interpolation in a 2-dimensional space.
tf_quant_finance.math.interpolation.interpolation_2d.Interpolation2D(
x_data, y_data, z_data, dtype=None, name=None
)
For input x_data in x-direction we assume that values in y-direction are
given by y_data and the corresponding function values by z_data.
For given x and y along x- and y- direction respectively,
the interpolated function values are computed on grid [x, y].
The interpolation is first performed along y-direction for every x_data
point and all y using 1-d cubic spline interpolation. Next, for
each interpolated y_value point, the function values are interpolated along
x-direction for x using 1-d cubic spline interpolation.
Constant extrapolation is used for the linear interpolation and natural
boundary conditions are used for the cubic spline.
Example. Volatility surface interpolation#
dtype = np.float64
times = tf.constant([2., 2.5, 3, 4.5], dtype=dtype)
strikes = tf.constant([16, 22, 35], dtype=dtype)
times_data = tf.constant([1.5, 2.5, 3.5, 4.5, 5.5], dtype=dtype)
# Corresponding squared volatility values
sigma_square_data = tf.constant(
[[0.15, 0.25, 0.35, 0.4, 0.45, 0.4],
[0.2, 0.35, 0.55, 0.45, 0.4, 0.6],
[0.3, 0.45, 0.25, 0.4, 0.5, 0.65],
[0.25, 0.25, 0.45, 0.25, 0.5, 0.55],
[0.35, 0.35, 0.25, 0.4, 0.55, 0.65]], dtype=dtype)
# Interpolation is done for the total variance
total_variance = tf.expand_dims(times_data, -1) * sigma_square_data
# Corresponding strike values. Notice we need to broadcast to the shape of
# `sigma_square_data`
strike_data = tf.broadcast_to(
tf.constant([15, 25, 35, 40, 50, 55], dtype=dtype), [5, 6])
# Interpolate total variance on for coordinates `(times, strikes)`
interpolator = Interpolation2D(times_data, strike_data, total_variance,
dtype=dtype)
interpolated_values = interpolator.interpolate(times, strikes)
Args:#
x_data: ATensorof realdtypeand shapebatch_shape + [num_x_data_points]. Defines the x-coordinates of the input data.num_x_data_pointsshould be >= 2. The elements ofx_datashould be in a non-decreasing order.y_data: ATensorof the samedtypeasx_dataand shapebatch_shape + [num_x_data_points, num_y_data_points]. Defines the y-coordinates of the input data.num_y_data_pointsshould be >= 2. The elements ofy_datashould be in a non-decreasing order along last dimension.z_data: ATensorof the same shape anddtypeasy_data. Defines the z-coordinates of the input data (i.e., the function values).dtype: Optional dtype for the inputTensors. Default value:Nonewhich maps to the default dtype inferred by TensorFlow.name: Pythonstrname prefixed to ops created by this class. Default value:Nonewhich is mapped to the default nameinterpolation_2d.
Methods#
interpolate
interpolate(
x, y, name=None
)
Performs 2-D interpolation on a specified set of points.
Args:#
x: Real-valuedTensorof shapebatch_shape + [num_points]. Defines the x-coordinates at which the interpolation should be performed. Note thatbatch_shapeshould be the same as in the underlying data.y: ATensorof the same shape anddtypeasx. Defines the y-coordinates at which the interpolation should be performed.name: Pythonstrname prefixed to ops created by this function. Default value:Nonewhich is mapped to the default nameinterpolate.
Returns:#
A Tensor of the same shape and dtype as x. Represents the
interpolated values of the function on for the coordinates
(x, y).