View Source Scholar.Interpolation.Linear (Scholar v0.3.1)

Linear interpolation.

This kind of interpolation is calculated by fitting polynomials of the first degree between each pair of given points.

This means that for points $(x_0, y_0), (x_1, y_1)$, the predictive polynomial will be given by: $$ \begin{cases} y = ax + b \newline a = \dfrac{y_1 - y_0}{x_1 - x_0} \newline b = y_1 - ax_1 = y_0 - ax_0 \end{cases} $$

Linear interpolation has $O(N)$ time and space complexity where $N$ is the number of points.

Summary

Functions

Fits a linear interpolation of the given (x, y) points

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

Types

@type t() :: %Scholar.Interpolation.Linear{coefficients: term(), x: term()}

Functions

Fits a linear 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([2.0, 0.0, 1.0])
iex> Scholar.Interpolation.Linear.fit(x, y)
%Scholar.Interpolation.Linear{
  coefficients: Nx.tensor(
    [
      [-2.0, 2.0],
      [1.0, -1.0]
    ]
  ),
  x: Nx.tensor(
    [0, 1, 2]
  )
}
Link to this function

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

View Source

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

Examples

iex> x = Nx.iota({3})
iex> y = Nx.tensor([2.0, 0.0, 1.0])
iex> model = Scholar.Interpolation.Linear.fit(x, y)
iex> Scholar.Interpolation.Linear.predict(model, Nx.tensor([[1.0, 4.0], [3.0, 7.0]]))
Nx.tensor(
  [
    [0.0, 3.0],
    [2.0, 6.0]
  ]
)

iex> x = Nx.iota({5})
iex> y = Nx.tensor([2.0, 0.0, 1.0, 3.0, 4.0])
iex> model = Scholar.Interpolation.Linear.fit(x, y)
iex> target_x = Nx.tensor([-2, -1, 1.25, 3, 3.25, 5.0])
iex> Scholar.Interpolation.Linear.predict(model, target_x, left: 0.0, right: 10.0)
#Nx.Tensor<
  f32[6]
  [0.0, 0.0, 0.25, 3.0, 3.25, 10.0]
>