everpolate— JavaScript Numerical Interpolation library

Synopsys

Everpolate provides a set of common interpolation algorithms implementations. Implementations given for interpolation are also implementations for extrapolation. It is written in JavaScript, so it could be used in web browser, Node and so forth.

Go to GitHub repo

Installation & Usage

If you want to use everpolate with module bundler such as webpack or in Node environment just install it via npm or yarn:

$ npm install everpolate

Or you can just download browserified UMD version and then just plop it into a single script tag in some html:

<html>
    <body>
        <script src="everpolate.browserified.min.js"></script>
    </body>
</html>

If a module system is detected in the host environment, it will be used (e.g RequireJS). Otherwise a window global named everpolate will be exported.

Documentation

Contents

  1. Value Interpolation
  2. Step Function Interpolation
  3. Linear Interpolation
  4. Polynomial Interpolation
  5. Linear Regression

Considerations

For this doc we consider following denotes:

Value Interpolation

value($x$ {Array|Number}, $X$ {Array}, $Y$ {Array}, $c$ {Any}) → {Array}

Interpolates missing points for the function $f(x)$ with a constant value $c$.

var value = require('everpolate').value
value([-12, 3.3, 9], [-12, -2, 0, 3.3, 6, 8], [10, 4, 0, 1, 3, -3], null)
// → [10, 1, null]
value([-1e-6, 0, 1e-6], [0], [Infinity], 0)
// → [0, Infinity, 0]

Step Function Interpolation

step($x$ {Array|Number}, $X$ {Array}, $Y$ {Array}) → {Array}

Evaluates interpolating step function on the set of distinct numbers $X$ at the point $x$ (or set of points) for the function $f(x)$.

var step = require('everpolate').step
//interpolation
step([2, 0, 8], [-2, 0, 6, 8], [4, 0, 3, -3])
// → [0, 0, -3]
//extrapolation works as well
step(3, [0, 1, 2], [1, 3, 2])
// → [2]

Linear Interpolation

linear($x$ {Array|Number}, $X$ {Array}, $Y$ {Array}) → {Array}

Evaluates interpolating line (or lines) on the set of distinct numbers $X$ at the point $x$ (or set of points) for the function $f(x)$.

var linear = require('everpolate').linear
//interpolation
linear([2, 0, 8], [-2, 0, 6, 8], [4, 0, 3, -3])
// → [1, 0, -3]
//extrapolation works as well
linear(3, [0, 1, 2], [1, 3, 2])
// → [1]

Polynomial Interpolation

polynomial($x$ {Array|Number}, $X$ {Array}, $Y$ {Array}) → {Array}

Implements Neville’s Iterative Interpolation algorithm. Evaluates the Lagrange polynomial $P(x)$ of degree $N\,-\,1$ on the set of distinct numbers \( X \) at the arbitrary point $x$ (or set of points) for the function $f(x)$:

$$ P(x) = \frac{(x-x_{2})(x-x_{3})...(x-x_{N})}{(x_{1}-x_{2})(x_{1}-x_{3})...(x_{1}-x_{N})}y_{1} + \frac{(x-x_{1})(x-x_{3})...(x-x_{N})}{(x_{2}-x_{1})(x_{2}-x_{3})...(x_{2}-x_{N})}y_{2} \\ + ... + \frac{(x-x_{1})(x-x_{2})...(x-x_{N-1})}{(x_{N}-x_{1})(x_{N}-x_{2})...(x_{N}-x_{N-1})}y_{N}$$


var polynomial = require('everpolate').polynomial
//interpolation
polynomial([0.021, 0.022], [0, 1.1, 2.4], [12, 431, 39])
// → [ 26.442248863636365, 27.123719230769233 ]
//for extrapolation use same syntax
polynomial([3], [0, 1, 2], [1, 3, 2])
// → [-2]

Linear Regression

linearRegression($X$ {Array}, $Y$ {Array}) → {Object}

Estimates linear regression $f(x) = ax + b$ for the set of regressors $X$ and the set of regressands $Y$, returns an Object which has slope, intercept, rSquared and evaluate properties. slope property represents $a$ parameter of $f(x)$ equation, intercept property represents $b$ parameter of $f(x)$ equation. rSquared property represents the square of the Pearson product moment correlation coefficient through data points in $X$ and $Y$:

$$ r^{2} = \left(\frac{N \sum x_{i} y_{i} - \sum x_{i} \sum y_{i}}{\sqrt{N \sum x_{i}^{2} - \left(\sum x_{i}\right)^{2}} \sqrt{N \sum y_{i}^{2} - \left(\sum y_{i}\right)^{2}} }\right)^2 $$

evaluate property is a pointer to function which can be used to evaluate regression at an arbitrary number $x$.

var linearRegression = require('everpolate').linearRegression
  , xValues = [0, 1, 2, 3, 4, 5]
  , yValues = [2, 3, 4, 5, 6, 7]
  , regression = linearRegression(xValues, yValues)

regression.evaluate([2, 3.5])
// → [4, 5.5]
regression.slope
// → 1
regression.intercept
// → 2
regression.rSquared
// → 1