Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Interpolation

Open in Colab

Interpolation means to fill in a function between known values. The data for interpolation are a set of points x and a set of function values y, and the result is a function f from some function class so that f(x) = y. Typically this function class is something simple, like Polynomials of bounded degree, piecewise constant functions, or splines.

Let’s look at a few examples

Here are a couple of functions we’ll look at:

If we sample a couple of points on each function, we would see something like the following:

An interpolation just uses the sampled points and function values to try to reconstruct the original function.

Scipy provides a high-level interface for doing this with scipy.interpolate.interp1d. You can pass in a variety of arguments, and the output is an interpolation object, which implements the __call__ method, so you can evaluate the interpolation.

If we increase the number of points used for interpolation, then we can get better approximations to the function.

There are a variety of options for the kind keyword.

  • kind='linear': piecewise linear interpolation

  • kind='nearest': nearest-neighbor interpolation (piecewise constant)

  • kind='quadratic': quadratic spline (piecewise quadratic)

  • kind='cubic': cubic spline (piecewise cubic)

and more. See the documentation for details

Barycentric Interpolation

There are also several interpolation methods not covered by interp1d, or which offer more customization. These include:

  • BarycentricInterpolator

  • KroghInterpolator

  • PchipInterpolator

and so on. See the interpolation documentation for more.

Barycentric interpolation can be done using BarycentricInterpolator, which constructs a Lagrange Polynomial of minimum degree which interpolates the points.

See Berrut and Trefethen, 2004 for details.

Lagrange interpolation: given n data points (x0,y0),(x1,y1),…,(xn,yn)(x_0, y_0), (x_1, y_1), \dots, (x_n, y_n), the Lagrange interpolating polynomial P(x)P(x) of degree at most n−1n-1 is defined as:

P(x)=∑j=0nyjPj(x),Pj(x)=∏k=0k≠jnx−xkxj−xk.P(x) = \sum_{j=0}^{n} y_j P_j(x),\quad P_j(x) = \prod_{\substack{k=0 \\ k \neq j}}^{n} \frac{x - x_k}{x_j - x_k}.

You can verify that Pj(xj)=1P_j(x_j) = 1 and Pj(xi)=0P_j(x_i) = 0 for all i≠ji \neq j, and that P(xj)=yjP(x_j) = y_j.

Barycentric interpolation is an improved way of Lagrange interpolation, given by

P(x)=∑j=0nwjx−xjfj∑j=0nwjx−xjP(x) = \frac{\sum_{j=0}^{n} \frac{w_j}{x - x_j} f_j}{\sum_{j=0}^{n} \frac{w_j}{x - x_j}}

with weights

wj=1∏k≠j(xj−xk)w_j = \frac{1}{\prod_{k \neq j} (x_j - x_k)}

a big difference between a Barycentric interpolator and the sorts of interpolants you contruct using interp1d is that it is not defined piecewise - it is a degree k-1 polynomial which fits each of the k interpolation points exactly. If the function being interpolated is not a lower-degree polynomial, this can lead to large fluctuations away from the origin, which is called the Runge phenomenon.

You can minimize the effects of the Runge phenomenon by using Chebyshev interpolation points:

cos⁡(nπ/(k−1)),n=0,...,k−1\cos(n \pi /(k-1)) \quad , n=0,...,k-1

Higher-Dimensional Interpolation

Higher dimensional interpolations can be computed using either scipy.interpolate.griddata (for scattered data), or scipy.interpolate.RegularGridInterpolator (for data sampled on a regular grid). Both work in any number of dimensions. There are also a variety of more specific options which you can find in the documentation.

We consider the following function on R2\mathbb R^2:

f(x,y)=x(1−x)cos⁡(4πx)sin⁡(4πy2)2f(x, y) = x(1-x) \cos (4\pi x) \sin (4\pi y^2)^2

We’ll want to interpolate onto a grid covering the unit square. The following two cells generate equivalent data:

We’ll sample the function at some random points

RegularGridInterpolator is used when the samples lie on a regular grid. It takes the 1-dimensional arrays that define that grid, and can then be evaluated at arbitrary points - here, the points of a finer grid.

Extrapolate

After SciPy version 0.17.0, scipy.interpolate.interp1d allows extrapolation, by setting fill_value='extrapolate'. Please see documentations here for reference.

Barycentric interpolation with BarycentricInterpolator natively supports extrapolation.