<!--
This file is generated by a tool. Do not edit directly.
For open-source contributions the docs will be updated automatically.
-->

*Last updated: 2023-03-16.*

<div itemscope itemtype="http://developers.google.com/ReferenceObject">
<meta itemprop="name" content="tf_quant_finance.math.interpolation.cubic.build_spline" />
<meta itemprop="path" content="Stable" />
</div>

# tf_quant_finance.math.interpolation.cubic.build_spline

<!-- Insert buttons and diff -->

<table class="tfo-notebook-buttons tfo-api" align="left">
</table>

<a target="_blank" href="https://github.com/paolodelia99/tf-quant-finance/blob/main/tf_quant_finance/math/interpolation/cubic/cubic_interpolation.py">View source</a>



Builds a SplineParameters interpolation object.

```python
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
)
```



<!-- Placeholder for "Used in" -->

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:



```python
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:


* <b>`x_data`</b>: 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.
* <b>`y_data`</b>: A `Tensor` of the same shape and `dtype` as `x_data` containing
  Y-coordinates of points to fit the splines to.
* <b>`boundary_condition_type`</b>: Boundary condition type for current cubic
  interpolation. Instance of BoundaryConditionType enum.
  Default value: `None` which maps to <a href="../../../../tf_quant_finance/math/interpolation/cubic/BoundaryConditionType.md#NATURAL"><code>BoundaryConditionType.NATURAL</code></a>.
* <b>`left_boundary_value`</b>: 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]`.
* <b>`right_boundary_value`</b>: 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]`.
* <b>`validate_args`</b>: 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.
* <b>`dtype`</b>: Optional dtype for both `x_data` and `y_data`.
  Default value: `None` which maps to the default dtype inferred by
    TensorFlow.
* <b>`name`</b>: 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`.
