Scholar.Interpolation.MonotonicCubicSpline (Scholar v0.5.0)

Copy Markdown View Source

Monotonic cubic spline interpolation (also known as PCHIP, from Piecewise Cubic Hermite Interpolating Polynomial).

Like Scholar.Interpolation.CubicSpline, this fits a piecewise cubic polynomial to the given points. The derivatives at each point, however, are chosen with the Fritsch-Carlson method so that the resulting curve is monotonic wherever the data is monotonic and has no overshoot around local extrema. In exchange, only the first derivative of the curve is guaranteed to be continuous (the second one is not).

This is a good choice when the shape of the data matters more than its smoothness, for example to avoid interpolated values that fall outside the range of the surrounding samples.

Monotonic cubic spline interpolation is $O(N)$ where $N$ is the number of points.

References:

Summary

Functions

Fits a monotonic cubic spline interpolation of the given (x, y) points.

Returns the value fit by fit/2 corresponding to the target_x input.

Functions

fit(x, y)

Fits a monotonic cubic spline interpolation of the given (x, y) points.

Inputs are expected to be rank-1 tensors with the same shape and at least 2 entries.

Examples

iex> x = Nx.iota({3})
iex> y = Nx.tensor([0.0, 1.0, 0.0])
iex> Scholar.Interpolation.MonotonicCubicSpline.fit(x, y)
%Scholar.Interpolation.MonotonicCubicSpline{
  coefficients: Nx.tensor(
    [
      [0.0, -1.0, 2.0, 0.0],
      [0.0, -1.0, 0.0, 1.0]
    ]
  ),
  x: Nx.tensor(
    [0, 1, 2]
  )
}

predict(model, target_x, opts \\ [])

Returns the value fit by fit/2 corresponding to the target_x input.

Options

  • :extrapolate (boolean/0) - if false, out-of-bounds x return NaN. The default value is true.

Examples

iex> x = Nx.iota({3})
iex> y = Nx.tensor([0.0, 1.0, 0.0])
iex> model = Scholar.Interpolation.MonotonicCubicSpline.fit(x, y)
iex> Scholar.Interpolation.MonotonicCubicSpline.predict(model, Nx.tensor([0.5, 1.5]))
Nx.tensor(
  [0.75, 0.75]
)