tf_quant_finance.math.interpolation.cubic.build_spline

Last updated: 2023-03-16.

tf_quant_finance.math.interpolation.cubic.build_spline#

View source

Builds a SplineParameters interpolation object.

tf_quant_finance.math.interpolation.cubic.build_spline(
    x_data, y_data, boundary_condition_type=None, left_boundary_value=None,
    right_boundary_value=None, validate_args=False, dtype=None, name=None
)

Given a Tensor of state points x_data and corresponding values y_data creates an object that contains interpolation coefficients. The object can be used by the interpolate function to get interpolated values for a set of state points x using the cubic spline interpolation algorithm. It assumes that the second derivative at the first and last spline points are zero. The basic logic is explained in [1] (see also, e.g., [2]).

Repeated entries in x_data are only allowed for the right boundary values of x_data. For example, x_data can be [1., 2, 3. 4., 4., 4.] but not [1., 1., 2., 3.]. The repeated values play no role in interpolation and are useful only for interpolating multiple splines with different numbers of data point. It is user responsibility to verify that the corresponding values of y_data are the same for the repeated values of x_data.

Typical Usage Example:#

import tensorflow as tf
import tf_quant_finance as tff
import numpy as np

x_data = tf.linspace(-5.0, 5.0,  num=11)
y_data = 1.0/(1.0 + x_data**2)
spline = tff.math.interpolation.cubic.build_spline(x_data, y_data)
x_args = [3.3, 3.4, 3.9]

tff.math.interpolation.cubic.interpolate(x_args, spline)
# Expected: [0.0833737 , 0.07881707, 0.06149562]

References:#

[1]: R. Sedgewick, Algorithms in C, 1990, p. 545-550. Link: https://api.semanticscholar.org/CorpusID:10976311 [2]: R. Pienaar, M Choudhry. Fitting the term structure of interest rates: the practical implementation of cubic spline methodology. Link: http://yieldcurve.com/mktresearch/files/PienaarChoudhry_CubicSpline2.pdf

Args:#

  • x_data: A real Tensor of shape [..., num_points] containing X-coordinates of points to fit the splines to. The values have to be monotonically non-decreasing along the last dimension.

  • y_data: A Tensor of the same shape and dtype as x_data containing Y-coordinates of points to fit the splines to.

  • boundary_condition_type: Boundary condition type for current cubic interpolation. Instance of BoundaryConditionType enum. Default value: None which maps to BoundaryConditionType.NATURAL.

  • left_boundary_value: Set to non-empty value IFF boundary_condition_type is FIXED_FIRST_DERIVATIVE, in which case set to cubic spline’s first derivative at x_data[..., 0].

  • right_boundary_value: Set to non-empty value IFF boundary_condition_type is FIXED_FIRST_DERIVATIVE, in which case set to cubic spline’s first derivative at x_data[..., num_points - 1].

  • validate_args: Python bool. When True, verifies if elements of x_data are sorted in the last dimension in non-decreasing order despite possibly degrading runtime performance. Default value: False.

  • dtype: Optional dtype for both x_data and y_data. Default value: None which maps to the default dtype inferred by TensorFlow.

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

Returns:#

An instance of SplineParameters.