Liking cljdoc? Tell your friends :D
Clojure only.

fastmath.interpolation.loess

LOESS (locally weighted linear or quadratic regression) smoothing followed by cubic spline interpolation of the smoothed values.

LOESS (locally weighted linear or quadratic regression) smoothing followed by cubic spline interpolation of the
smoothed values.
raw docstring

loessclj

(loess xs ys)
(loess xs ys {:keys [bandwidth degree iters accuracy weights]})

Creates a one dimensional LOESS (locally weighted linear or quadratic regression) smoother returning an interpolating function.

Every node is smoothed by a weighted linear (or quadratic, :degree 2) regression over its k nearest neighbours with tricube weights, optionally repeated with robustness (bisquare) weights that downweight outliers. A natural cubic spline is then fitted through the smoothed values, so the returned function is continuous and smooth and can be evaluated anywhere. With :degree 1 the smoothed values equal statsmodels.nonparametric.lowess(frac=bandwidth, it=iters, delta=0); with :degree 2 and :iters 0 R loess(span = bandwidth, degree = 2, family = "gaussian", surface = "direct") at the nodes.

Parameters:

  • xs (sequence of numbers): nodes, finite and strictly increasing, at least 2 points
  • ys (sequence of numbers): values at the nodes, the same count as xs
  • opts (map, optional):
    • :bandwidth (number in (0, 1], default: 0.3): fraction of nodes used in each local fit. The window holds k = floor(bandwidth * n + 1e-10) true nearest neighbours of the evaluated node (the node itself included). k has to be at least 2, which means bandwidth * n >= 2; for the default 0.3 at least 7 points are needed, for 1.0 at least 2 points. k = 2 returns the data unchanged, because the farthest neighbour gets the zero weight.
    • :degree (1 or 2, default: 1): degree of the local polynomials - local linear (statsmodels lowess) or local quadratic (R loess and ggplot2 geom_smooth default, degree = 2). A quadratic fit whose window has fewer than three distinct weighted points is linear.
    • :iters (non-negative integer, default: 2): number of robustness iterations. 0 is the plain LOESS. In every iteration the residuals are divided by 6 times their true median (the mean of the two central values for an even count) and the bisquare function gives the weights; points with residual of at least 6 medians have zero weight.
    • :accuracy (non-negative number, default: 1.0e-12): the iterations stop when the median of absolute residuals falls below this value (for example when the data lie exactly on a line). With 0.0 the iterations always run, as statsmodels does.
    • :weights (sequence of non-negative numbers, default: nil): weights of the observations, the same count as xs. They are multiplied into the tricube weights (and into the robustness weights) of every local fit; an observation with the weight zero is ignored. nil means equal weights.

Returns a Loess record, a function: (f x) returns the interpolated double value - the natural cubic spline through the smoothed values (beyond the domain the end cubic polynomials are continued); the curve does not pass through the data points unless k is 2 or the data is exactly linear. (f x true) returns [fit stderr], the standard error of the fit (as R predict.loess(se = TRUE), with the hat matrix L of the final pass, robustness weights included): sigma * sqrt(sum_j L[i,j]^2 / w_j) at the nodes (w - the user weights, 1 without them) and a natural cubic spline through these values between them (clamped at 0); outside the range of the nodes the standard error is NaN (no information there, as R). The record holds :sigma - the residual scale sqrt(sum w_i r_i^2 / delta1), :df - delta1 = n - 2 tr(L) + tr(L^T L) (R's one.delta, the degrees of freedom of a t interval) and :trace - tr(L); observations with the weight zero are left out of the sums. With robustness iterations (:iters > 0) the hat matrix includes the robustness weights but :sigma uses the plain residuals, so outliers the robust fit downweights still widen the standard errors: the bands are conservative. With k = 2 (or no residual degrees of freedom) :df is 0 and the standard errors are NaN; exactly linear data gives the standard error 0.

A local fit with less than two positive weights keeps the original value. The variance of the weighted regression abscissas is bounded from below by 1e-12 times the squared window radius, so the result does not depend on the scale of xs. Each pass costs O(n k).

Throws ex-info (with :type :invalid-input, :method :loess and a :property) when the lengths differ (:length-mismatch), there are fewer than 2 points (:too-few-points), xs is not finite (:not-finite) or not strictly increasing (:not-increasing), :bandwidth is outside (0, 1] or gives k < 2 (:bandwidth), :degree isn't 1 or 2 (:degree), :iters or :accuracy is negative (:iters, :accuracy) or :weights have a wrong length (:length-mismatch), a negative or a non-finite value (:invalid-weights).

See also [[cubic-smoothing]], [[cubic]], [[interpolation]].

Creates a one dimensional LOESS (locally weighted linear or quadratic regression) smoother returning an interpolating function.

Every node is smoothed by a weighted linear (or quadratic, `:degree 2`) regression over its `k` nearest neighbours with tricube weights, optionally repeated with robustness (bisquare) weights that downweight outliers. A natural cubic spline is then fitted through the smoothed values, so the returned function is continuous and smooth and can be evaluated anywhere. With `:degree 1` the smoothed values equal `statsmodels.nonparametric.lowess(frac=bandwidth, it=iters, delta=0)`; with `:degree 2` and `:iters 0` R `loess(span = bandwidth, degree = 2, family = "gaussian", surface = "direct")` at the nodes.

Parameters:

- `xs` (sequence of numbers): nodes, finite and strictly increasing, at least 2 points
- `ys` (sequence of numbers): values at the nodes, the same count as `xs`
- `opts` (map, optional):
    - `:bandwidth` (number in (0, 1], default: `0.3`): fraction of nodes used in each local fit. The window holds `k = floor(bandwidth * n + 1e-10)` true nearest neighbours of the evaluated node (the node itself included). `k` has to be at least 2, which means `bandwidth * n >= 2`; for the default `0.3` at least 7 points are needed, for `1.0` at least 2 points. `k = 2` returns the data unchanged, because the farthest neighbour gets the zero weight.
    - `:degree` (`1` or `2`, default: `1`): degree of the local polynomials - local linear (statsmodels `lowess`) or local quadratic (R `loess` and ggplot2 `geom_smooth` default, `degree = 2`). A quadratic fit whose window has fewer than three distinct weighted points is linear.
    - `:iters` (non-negative integer, default: `2`): number of robustness iterations. `0` is the plain LOESS. In every iteration the residuals are divided by 6 times their true median (the mean of the two central values for an even count) and the bisquare function gives the weights; points with residual of at least 6 medians have zero weight.
    - `:accuracy` (non-negative number, default: `1.0e-12`): the iterations stop when the median of absolute residuals falls below this value (for example when the data lie exactly on a line). With `0.0` the iterations always run, as statsmodels does.
    - `:weights` (sequence of non-negative numbers, default: `nil`): weights of the observations, the same count as `xs`. They are multiplied into the tricube weights (and into the robustness weights) of every local fit; an observation with the weight zero is ignored. `nil` means equal weights.

Returns a `Loess` record, a function: `(f x)` returns the interpolated `double` value - the natural cubic spline through the smoothed values (beyond the domain the end cubic polynomials are continued); the curve does not pass through the data points unless `k` is 2 or the data is exactly linear. `(f x true)` returns `[fit stderr]`, the standard error of the fit (as R `predict.loess(se = TRUE)`, with the hat matrix `L` of the final pass, robustness weights included): `sigma * sqrt(sum_j L[i,j]^2 / w_j)` at the nodes (`w` - the user weights, 1 without them) and a natural cubic spline through these values between them (clamped at 0); outside the range of the nodes the standard error is `NaN` (no information there, as R). The record holds `:sigma` - the residual scale `sqrt(sum w_i r_i^2 / delta1)`, `:df` - `delta1 = n - 2 tr(L) + tr(L^T L)` (R's `one.delta`, the degrees of freedom of a t interval) and `:trace` - `tr(L)`; observations with the weight zero are left out of the sums. With robustness iterations (`:iters` > 0) the hat matrix includes the robustness weights but `:sigma` uses the plain residuals, so outliers the robust fit downweights still widen the standard errors: the bands are conservative. With `k = 2` (or no residual degrees of freedom) `:df` is 0 and the standard errors are `NaN`; exactly linear data gives the standard error 0.

A local fit with less than two positive weights keeps the original value. The variance of the weighted regression abscissas is bounded from below by 1e-12 times the squared window radius, so the result does not depend on the scale of `xs`. Each pass costs `O(n k)`.

Throws `ex-info` (with `:type` `:invalid-input`, `:method` `:loess` and a `:property`) when the lengths differ (`:length-mismatch`), there are fewer than 2 points (`:too-few-points`), `xs` is not finite (`:not-finite`) or not strictly increasing (`:not-increasing`), `:bandwidth` is outside (0, 1] or gives `k < 2` (`:bandwidth`), `:degree` isn't 1 or 2 (`:degree`), `:iters` or `:accuracy` is negative (`:iters`, `:accuracy`) or `:weights` have a wrong length (`:length-mismatch`), a negative or a non-finite value (`:invalid-weights`).

See also [[cubic-smoothing]], [[cubic]], [[interpolation]].
raw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close