<!--
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.interpolation_2d.Interpolation2D" />
<meta itemprop="path" content="Stable" />
<meta itemprop="property" content="__init__"/>
<meta itemprop="property" content="interpolate"/>
</div>

# tf_quant_finance.math.interpolation.interpolation_2d.Interpolation2D

<!-- 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/interpolation_2d/interpolation_2d.py">View source</a>



Performs interpolation in a 2-dimensional space.

```python
tf_quant_finance.math.interpolation.interpolation_2d.Interpolation2D(
    x_data, y_data, z_data, dtype=None, name=None
)
```



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

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

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


* <b>`x_data`</b>: 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.
* <b>`y_data`</b>: 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.
* <b>`z_data`</b>: A `Tensor` of the same shape and `dtype` as `y_data`. Defines the
  z-coordinates of the input data (i.e., the function values).
* <b>`dtype`</b>: Optional dtype for the input `Tensor`s.
  Default value: `None` which maps to the default dtype inferred by
  TensorFlow.
* <b>`name`</b>: Python `str` name prefixed to ops created by this class.
  Default value: `None` which is mapped to the default name
  `interpolation_2d`.

## Methods

<h3 id="interpolate"><code>interpolate</code></h3>

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

```python
interpolate(
    x, y, name=None
)
```

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


#### Args:


* <b>`x`</b>: 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.
* <b>`y`</b>: A `Tensor` of the same shape and `dtype` as `x`.
  Defines the y-coordinates at which the interpolation should be
  performed.
* <b>`name`</b>: 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)`.




