Radial basis function interpolation of scattered data.
Radial basis function interpolation of scattered data.
(rbf xss ys)(rbf xss ys kernel)(rbf xss ys kernel opts)Creates the radial basis function (RBF) interpolator of scattered data.
The interpolant is f(x) = sum_i w_i kscale kernel(d(x, xs_i)) (plus a polynomial sum_j c_j p_j(x) when :polynomial-terms are given), where the weights w (and the coefficients c) are found from the linear system built on the nodes. Without regularization the interpolant passes through all the nodes.
Parameters:
xss (sequence): the nodes, at least one; numbers for one dimensional data or sequences of numbersys (sequence of numbers): the values at the nodes, as many as nodeskernel (function, optional, default: (fastmath.kernel/rbf :gaussian)): a radial function of the distance (see fastmath.kernel/rbf); its own :scale sets the length scale of the interpolationopts (map, optional; a nil value is the same as a missing key):
:kscale (double, default: 1.0): a multiplier applied to all the kernel evaluations:lambda (double, default: 0.0): regularization strength, finite and not negative; 0.0 is the exact interpolation:regularization (keyword, default: :normal): how :lambda enters the system
:normal - ridge on the normal equations: (A^T A + lambda I) w = A^T y, where A is the whole system matrix (the polynomial block included, which is regularized too); it squares the condition number. This is the original behaviour:ridge - kernel ridge regression: (K + lambda I) w = y, lambda is added to the kernel matrix only (the polynomial block is not regularized). This is the smoothing of the scipy RBFInterpolator:distance (function, default: Euclidean distance, fastmath.distance/euclidean-1d for numbers and fastmath.distance/euclidean for sequences): a binary distance function (fn [x1 x2]):polynomial-terms (function, default: nil, no polynomial): a function of a point returning a sequence of the values of the polynomial basis (for example (fn [[x y]] [1.0 x y]) for a linear polynomial in two dimensions); the system is augmented with the polynomial constraintsReturns a function of a query point (a number for one dimensional data, a sequence of numbers otherwise) returning a double.
The relation of :kscale and :lambda: without regularization :kscale cancels (the weights scale by its inverse) and has no effect on the values. With :regularization :normal and no polynomial terms :lambda acts as lambda / kscale^2, with :ridge (also with polynomial terms) as lambda / kscale, and with :normal and polynomial terms the effect is mixed (the polynomial block is not scaled by :kscale). Tune :lambda for a fixed :kscale (the default 1.0 is the simplest).
Conditionally positive definite kernels (thin plate splines, cubic and other radial powers, linear, generalized multiquadratic of a positive order, shifted surface splines) need :polynomial-terms of the matching degree (at least a constant for the linear kernel and the multiquadratic, a linear polynomial for the thin plate splines and the cubic kernel) to have a guaranteed unique solution and to reproduce the polynomials. Without them the system is often still solvable but the result differs from the correct one. Positive definite kernels (gaussian, inverse multiquadratic, matern, compactly supported kernels) need no polynomial.
The linear system is solved by the LU decomposition. When the matrix is (numerically) singular, for example for repeated nodes, the minimum norm least squares solution of the SVD is used silently, so repeated nodes with different values give the mean of the values. The system of a smooth kernel (gaussian, multiquadratic) is ill-conditioned for dense nodes and a large length scale: the result is accurate only up to the conditioning (often 1e-8 or worse). A small :lambda with :ridge stabilises it.
Throws ex-info (with :type :invalid-input, :method :rbf and the key :property) when xss and ys have different lengths (:length-mismatch), there are no nodes (:too-few-points), :lambda is negative, infinite or NaN (:lambda) or :regularization is not one of :normal, :ridge (:regularization).
See also fastmath.interpolation/rbf, fastmath.kernel/rbf, fastmath.interpolation.kriging/kriging, fastmath.interpolation.shepard/shepard.
Creates the radial basis function (RBF) interpolator of scattered data.
The interpolant is `f(x) = sum_i w_i kscale kernel(d(x, xs_i))` (plus a polynomial `sum_j c_j p_j(x)` when `:polynomial-terms` are given), where the weights `w` (and the coefficients `c`) are found from the linear system built on the nodes. Without regularization the interpolant passes through all the nodes.
Parameters:
- `xss` (sequence): the nodes, at least one; numbers for one dimensional data or sequences of numbers
- `ys` (sequence of numbers): the values at the nodes, as many as nodes
- `kernel` (function, optional, default: `(fastmath.kernel/rbf :gaussian)`): a radial function of the distance (see [[fastmath.kernel/rbf]]); its own `:scale` sets the length scale of the interpolation
- `opts` (map, optional; a `nil` value is the same as a missing key):
- `:kscale` (double, default: `1.0`): a multiplier applied to all the kernel evaluations
- `:lambda` (double, default: `0.0`): regularization strength, finite and not negative; `0.0` is the exact interpolation
- `:regularization` (keyword, default: `:normal`): how `:lambda` enters the system
- `:normal` - ridge on the normal equations: `(A^T A + lambda I) w = A^T y`, where `A` is the whole system matrix (the polynomial block included, which is regularized too); it squares the condition number. This is the original behaviour
- `:ridge` - kernel ridge regression: `(K + lambda I) w = y`, `lambda` is added to the kernel matrix only (the polynomial block is not regularized). This is the `smoothing` of the scipy `RBFInterpolator`
- `:distance` (function, default: Euclidean distance, `fastmath.distance/euclidean-1d` for numbers and `fastmath.distance/euclidean` for sequences): a binary distance function `(fn [x1 x2])`
- `:polynomial-terms` (function, default: `nil`, no polynomial): a function of a point returning a sequence of the values of the polynomial basis (for example `(fn [[x y]] [1.0 x y])` for a linear polynomial in two dimensions); the system is augmented with the polynomial constraints
Returns a function of a query point (a number for one dimensional data, a sequence of numbers otherwise) returning a double.
The relation of `:kscale` and `:lambda`: without regularization `:kscale` cancels (the weights scale by its inverse) and has no effect on the values. With `:regularization :normal` and no polynomial terms `:lambda` acts as `lambda / kscale^2`, with `:ridge` (also with polynomial terms) as `lambda / kscale`, and with `:normal` and polynomial terms the effect is mixed (the polynomial block is not scaled by `:kscale`). Tune `:lambda` for a fixed `:kscale` (the default `1.0` is the simplest).
Conditionally positive definite kernels (thin plate splines, cubic and other radial powers, linear, generalized multiquadratic of a positive order, shifted surface splines) need `:polynomial-terms` of the matching degree (at least a constant for the linear kernel and the multiquadratic, a linear polynomial for the thin plate splines and the cubic kernel) to have a guaranteed unique solution and to reproduce the polynomials. Without them the system is often still solvable but the result differs from the correct one. Positive definite kernels (gaussian, inverse multiquadratic, matern, compactly supported kernels) need no polynomial.
The linear system is solved by the LU decomposition. When the matrix is (numerically) singular, for example for repeated nodes, the minimum norm least squares solution of the SVD is used silently, so repeated nodes with different values give the mean of the values. The system of a smooth kernel (gaussian, multiquadratic) is ill-conditioned for dense nodes and a large length scale: the result is accurate only up to the conditioning (often `1e-8` or worse). A small `:lambda` with `:ridge` stabilises it.
Throws `ex-info` (with `:type :invalid-input`, `:method :rbf` and the key `:property`) when `xss` and `ys` have different lengths (`:length-mismatch`), there are no nodes (`:too-few-points`), `:lambda` is negative, infinite or `NaN` (`:lambda`) or `:regularization` is not one of `:normal`, `:ridge` (`:regularization`).
See also [[fastmath.interpolation/rbf]], [[fastmath.kernel/rbf]], [[fastmath.interpolation.kriging/kriging]], [[fastmath.interpolation.shepard/shepard]].cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |