tf_quant_finance.math.interpolation.interpolation_2d.Interpolation2D

Last updated: 2023-03-16.

tf_quant_finance.math.interpolation.interpolation_2d.Interpolation2D#

View source

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: A Tensor of real dtype and shape batch_shape + [num_x_data_points]. Defines the x-coordinates of the input data. num_x_data_points should be >= 2. The elements of x_data should be in a non-decreasing order.

  • y_data: A Tensor of the same dtype as x_data and shape batch_shape + [num_x_data_points, num_y_data_points]. Defines the y-coordinates of the input data. num_y_data_points should be >= 2. The elements of y_data should be in a non-decreasing order along last dimension.

  • z_data: A Tensor of the same shape and dtype as y_data. Defines the z-coordinates of the input data (i.e., the function values).

  • dtype: Optional dtype for the input Tensors. Default value: None which maps to the default dtype inferred by TensorFlow.

  • name: Python str name prefixed to ops created by this class. Default value: None which is mapped to the default name interpolation_2d.

Methods#

interpolate

View source

interpolate(
    x, y, name=None
)

Performs 2-D interpolation on a specified set of points.

Args:#

  • x: Real-valued Tensor of shape batch_shape + [num_points]. Defines the x-coordinates at which the interpolation should be performed. Note that batch_shape should be the same as in the underlying data.

  • y: A Tensor of the same shape and dtype as x. Defines the y-coordinates at which the interpolation should be performed.

  • name: Python str name prefixed to ops created by this function. Default value: None which is mapped to the default name interpolate.

Returns:#

A Tensor of the same shape and dtype as x. Represents the interpolated values of the function on for the coordinates (x, y).