SciPy - interpolate.pchip_interpolate() Function



scipy.interpolate.pchip_interpolate() is a function in the SciPy library used for piecewise cubic Hermite interpolating polynomial (PCHIP) interpolation. It performs monotonic interpolation of a set of data points by ensuring smoothness without overshooting.

When the given arrays of x i.e., independent variable and y i.e., dependent variable then this function interpolates the value of y at new points xi. It preserves the shape and monotonicity of the data by making it ideal for applications where data must not exhibit artificial oscillations such as time series or financial data interpolation.

Syntax

Following is the syntax of the function scipy.interpolate.pchip_interpolate() to perform PCHIP interpolation −

scipy.interpolate.pchip_interpolate(xi, yi, x, der=0, axis=0)

Parameters

Here are the parameters of the scipy.interpolate.pchip_interpolate() function −

  • xi(array-like): The independent variable i.e., x-coordinates of the data points which must be sorted in increasing order.
  • yi(array-like): The dependent variable i.e., y-coordinates of the data points corresponding to xi.
  • x(array-like): The new x-coordinates where we want to interpolate the corresponding y-values.
  • axis(int, optional): The axis along which to interpolate if yi is multidimensional and the default value is 0.
  • der(int or list of int, optional): Specifies the order of the derivative to compute. The default value is 0 which means it interpolates the value.

Return Value

The scipy.interpolate.pchip_interpolate() function returns an array of interpolated values based on the input data.

Basic Interpolation

When we have a dataset of points and we want to interpolate additional points between them using the PCHIP method. Following is the example of using the scipy.interpolate.pchip_interpolate() function which is used to apply PCHIP method −

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import pchip_interpolate

# Define known data points
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 1, 4, 1, 0, 1])

# Points where we want to interpolate
xi = np.linspace(0, 5, 100)

# Perform PCHIP interpolation
yi = pchip_interpolate(x, y, xi)

# Plot
plt.plot(x, y, 'o', label='Data points')
plt.plot(xi, yi, '-', label='PCHIP interpolation')
plt.legend()
plt.show()

Here is the output of the scipy.interpolate.pchip_interpolate() function −

Pchip Interpolate basic Example

Interpolation and Derivatives

When we wanted to compute higher-order derivatives such as der=2 for the second derivative then we could do so by adjusting the der parameter. However the smoothness of the derivatives depends on the nature of the original data and the PCHIP method's piecewise construction −

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import pchip_interpolate

# Define known data points
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 1, 4, 1, 0, 1])

# Points where we want to interpolate
xi = np.linspace(0, 5, 100)

# Interpolate function values (der=0)
yi = pchip_interpolate(x, y, xi, der=0)

# Compute first derivative (der=1)
d_yi = pchip_interpolate(x, y, xi, der=1)

# Plot both the function and its derivative
plt.figure(figsize=(10, 6))
plt.plot(xi, yi, label='PCHIP interpolation (der=0)', color='b')
plt.plot(xi, d_yi, label='First derivative (der=1)', color='r', linestyle='--')
plt.scatter(x, y, color='k', zorder=5, label='Data points')
plt.title('PCHIP Interpolation and its First Derivative')
plt.xlabel('x')
plt.ylabel('y and dy/dx')
plt.legend()
plt.grid(True)
plt.show()

Here is the output of the scipy.interpolate.pchip_interpolate() function which is used to find the derivatives −

Pchip Interpolate Derivative Example

Interpolating Non-Monotonic Data

When dealing with non-monotonic data the Piecewise Cubic Hermite Interpolation (PCHIP) is particularly useful because it preserves the shape of the data without introducing unrealistic oscillations or overshooting as can happen with other interpolation methods like cubic splines. Here is an example of it −

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import pchip_interpolate

# Define non-monotonic data points
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 2, 1, 4, 2, 0])

# Points where we want to interpolate
xi = np.linspace(0, 5, 100)

# Interpolate function values (der=0)
yi = pchip_interpolate(x, y, xi, der=0)

# Compute first derivative (der=1)
d_yi = pchip_interpolate(x, y, xi, der=1)

# Plot both the function and its derivative
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'o', label='Data points', color='k')
plt.plot(xi, yi, label='PCHIP interpolation', color='b')
plt.plot(xi, d_yi, '--', label='First derivative', color='r')
plt.title('PCHIP Interpolation and First Derivative for Non-Monotonic Data')
plt.xlabel('x')
plt.ylabel('y and dy/dx')
plt.legend()
plt.grid(True)
plt.show()

Here is the output of the scipy.interpolate.pchip_interpolate() function which is used to apply non-monotonic data −

Pchip Interpolate Non-Monotonic Example
scipy_interpolate.htm
Advertisements