Liking cljdoc? Tell your friends :D

fastmath.kernel.density

Kernel density estimation: kernel functions, bandwidth selectors and the one and two dimensional density estimators.

A kernel density estimate of the sample x_1 ... x_n is f(x) = 1/(n h) * sum K((x - x_i) / h) for a kernel K (a symmetric probability density) and a bandwidth h > 0.

Kernels (standard forms, the compact ones have support [-1, 1]; gaussian has standard deviation 1, which is not the convention of density in R, where the bandwidth is the standard deviation of the kernel): uniform, triangular, epanechnikov, quartic, triweight, tricube, gaussian, cosine, logistic, sigmoid, silverman, laplace, wigner, cauchy. Their constants (roughness, second moment, canonical bandwidth, efficiency, radius) are in kde-data.

Estimators: kernel-density (the function), kernel-density+ (the function with the fitting details), kernel-density-ci (with pointwise confidence intervals); two dimensional, a product kernel with a bandwidth per axis: kernel-density-2d, kernel-density-2d+.

Bandwidth selection: bandwidth with the rules of thumb :nrd, :nrd0, :nrd-adjust, the plug-in selectors :sj, :sj-dpi (as bw.SJ in R) and the cross-validation selectors :lscv, :lcv, :rlcv.

Evaluation is exact: every data point within 1.01 * h * radius of x contributes, no point outside does. Option :binned? replaces the data by a linear binning on a regular grid for large samples.

Kernel density estimation: kernel functions, bandwidth selectors and the one and two dimensional density estimators.

A kernel density estimate of the sample `x_1 ... x_n` is `f(x) = 1/(n h) * sum K((x - x_i) / h)` for a kernel `K` (a symmetric probability density) and a bandwidth `h > 0`.

Kernels (standard forms, the compact ones have support `[-1, 1]`; `gaussian` has standard deviation `1`, which is not the convention of `density` in R, where the bandwidth is the standard deviation of the kernel): [[uniform]], [[triangular]], [[epanechnikov]], [[quartic]], [[triweight]], [[tricube]], [[gaussian]], [[cosine]], [[logistic]], [[sigmoid]], [[silverman]], [[laplace]], [[wigner]], [[cauchy]]. Their constants (roughness, second moment, canonical bandwidth, efficiency, radius) are in [[kde-data]].

Estimators: [[kernel-density]] (the function), [[kernel-density+]] (the function with the fitting details), [[kernel-density-ci]] (with pointwise confidence intervals); two dimensional, a product kernel with a bandwidth per axis: [[kernel-density-2d]], [[kernel-density-2d+]].

Bandwidth selection: [[bandwidth]] with the rules of thumb `:nrd`, `:nrd0`, `:nrd-adjust`, the plug-in selectors `:sj`, `:sj-dpi` (as `bw.SJ` in R) and the cross-validation selectors `:lscv`, `:lcv`, `:rlcv`.

Evaluation is exact: every data point within `1.01 * h * radius` of `x` contributes, no point outside does. Option `:binned?` replaces the data by a linear binning on a regular grid for large samples.
raw docstring

bandwidthclj

(bandwidth kernel data h)
(bandwidth kernel data h {:keys [info?] :as opts})

Estimates the bandwidth h of the kernel density estimate of data.

The bandwidth controls how much each data point is spread out by the kernel: a smaller value follows the data more closely (a noisy estimate), a larger value gives a smoother and more biased estimate.

Parameters:

  • kernel - a keyword naming a predefined kernel of kde-data, or a kernel function (a function of a double returning a double; the selectors which need the canonical bandwidth of the kernel, :nrd-adjust, :sj and :sj-dpi, do not support it).
  • data - a sequence of numbers (finite, at least 2 for the selectors), not modified.
  • h - the number to use, or the keyword of a selector (nil means :nrd):
    • :nrd - the normal reference rule 1.06 * s * n^-0.2 with s = min(sd, IQR / 1.34); the IQR uses the quantile of type 7 as R. When s is 0 the fallback is sd, then abs(x_1), then 1.0 (as bw.nrd0 in R).
    • :nrd0 - Silverman's rule of thumb 0.9 * s * n^-0.2, the same s (bw.nrd0 in R).
    • :nrd-adjust - :nrd multiplied by the canonical bandwidth factor of the kernel (:nrd-scale in kde-data), so the kernels are comparable; throws for :silverman, :cauchy and custom kernels.
    • :sj - the Sheather-Jones solve-the-equation plug-in selector, the gaussian kernel result of bw.SJ(method = 'ste') in R (binned pair counts, the root found by uniroot with the same tolerance) multiplied by the canonical bandwidth factor of the kernel. It is the recommended selector for multimodal or non gaussian data. Throws when the sample is constant or has a zero interquartile range with too sparse data, as R.
    • :sj-dpi - the direct plug-in variant, bw.SJ(method = 'dpi').
    • :lscv - least squares cross validation: minimises the integral of f^2 minus twice the mean of the leave-one-out density.
    • :lcv - likelihood cross validation: maximises the mean log of the leave-one-out density (a data point with a non positive leave-one-out density makes the criterion -Inf; it is -Inf for every bandwidth of a compact kernel when a data point is isolated, then the selector throws ex-info, or with :info? returns the lower bound of the search interval with status :degenerate).
    • :rlcv - robust likelihood cross validation of Wu (Journal of Business and Economic Statistics 37(4), 2019): the likelihood criterion with the logarithm replaced by a linear function below a_n = sqrt(ln n) / (sqrt(2) n) and the bias term b*(h) the integral of f for f >= a_n and of f^2 / (2 a_n) below it.
  • opts (optional map):
    • :info? - when true, return the map described below instead of the number. Default: false.
    • :bounds - the cross validation search interval, a pair [lo hi] of factors of the rule of thumb s. Default: [0.01 5.0].
    • :grid - the number of log spaced points of the first search stage, at least 3. Default: 60, 30 for more than 5000 points.
    • :bins - the number of bins of :sj and :sj-dpi, an integer in [2, 10000]. Default: 1000.
    • :rlcv-form - the bias term of :rlcv: :printed (the formula as printed in Wu 2019) or :continuous. The printed b* jumps by a_n / 2 at f = a_n, the continuous form uses f - a_n / 2 there, which is the one that follows from the definition of the paper. Default: :printed.
    • :radius, :extent - for a custom kernel function: the distance beyond which the kernel is ignored in the sum (default ##Inf: every point) and beyond which the density is treated as zero (default 5.0).

The cross validation selectors standardise the data, so the result is equivariant to a shift and a scale of the data, search the criterion on a deterministic grid, refine the best grid point with Brent's method and cost O(n^2) evaluations of the kernel for each bandwidth tried (the :binned? option is ignored). Measured for the gaussian kernel and n points (default search, one core, including the JIT warm-up): n = 1000 takes 0.5 s (:lscv), 0.6 s (:lcv, :rlcv); n = 5000 takes 3.6 s, 8.3 s and 4.7 s; n = 10000 takes 8 s, 25 s and 16 s. A kernel which sums every point (:cauchy, a custom kernel without :radius) is slower: n = 1000 takes 10 s for :lscv against 0.14 s for :epanechnikov. The integral terms use a grid with a step of at most h / 40 over the range of the data extended by min(extent, 100) h, so for :cauchy the tail beyond 100 h is left out. :sj and :sj-dpi need O(bins^2) operations (below 0.1 s). When the best point is a bound of the search interval the bandwidth may be outside of it: see :at-bound?.

Returns the bandwidth as a double. With :info? returns a map with :h, :value (the value of the criterion at h in standardised units, ##NaN for the closed form selectors), :at-bound? (true when h is within 1.0e-6 relative of an end of the search interval) and :status (:closed-form, :ok or :degenerate).

Throws ex-info (:type :invalid-input) for invalid data, kernel, selector or options.

See also kernel-density, kernel-density+, kernel-density-ci, kde-data.

Estimates the bandwidth `h` of the kernel density estimate of `data`.

The bandwidth controls how much each data point is spread out by the kernel: a smaller value follows the data more closely (a noisy estimate), a larger value gives a smoother and more biased estimate.

Parameters:

- `kernel` - a keyword naming a predefined kernel of [[kde-data]], or a kernel function (a function of a double returning a double; the selectors which need the canonical bandwidth of the kernel, `:nrd-adjust`, `:sj` and `:sj-dpi`, do not support it).
- `data` - a sequence of numbers (finite, at least 2 for the selectors), not modified.
- `h` - the number to use, or the keyword of a selector (`nil` means `:nrd`):
    - `:nrd` - the normal reference rule `1.06 * s * n^-0.2` with `s = min(sd, IQR / 1.34)`; the IQR uses the quantile of type 7 as R. When `s` is `0` the fallback is `sd`, then `abs(x_1)`, then `1.0` (as `bw.nrd0` in R).
    - `:nrd0` - Silverman's rule of thumb `0.9 * s * n^-0.2`, the same `s` (`bw.nrd0` in R).
    - `:nrd-adjust` - `:nrd` multiplied by the canonical bandwidth factor of the kernel (`:nrd-scale` in [[kde-data]]), so the kernels are comparable; throws for `:silverman`, `:cauchy` and custom kernels.
    - `:sj` - the Sheather-Jones solve-the-equation plug-in selector, the gaussian kernel result of `bw.SJ(method = 'ste')` in R (binned pair counts, the root found by `uniroot` with the same tolerance) multiplied by the canonical bandwidth factor of the kernel. It is the recommended selector for multimodal or non gaussian data. Throws when the sample is constant or has a zero interquartile range with too sparse data, as R.
    - `:sj-dpi` - the direct plug-in variant, `bw.SJ(method = 'dpi')`.
    - `:lscv` - least squares cross validation: minimises the integral of `f^2` minus twice the mean of the leave-one-out density.
    - `:lcv` - likelihood cross validation: maximises the mean log of the leave-one-out density (a data point with a non positive leave-one-out density makes the criterion `-Inf`; it is `-Inf` for every bandwidth of a compact kernel when a data point is isolated, then the selector throws `ex-info`, or with `:info?` returns the lower bound of the search interval with status `:degenerate`).
    - `:rlcv` - robust likelihood cross validation of Wu (*Journal of Business and Economic Statistics* 37(4), 2019): the likelihood criterion with the logarithm replaced by a linear function below `a_n = sqrt(ln n) / (sqrt(2) n)` and the bias term `b*(h)` the integral of `f` for `f >= a_n` and of `f^2 / (2 a_n)` below it.
- `opts` (optional map):
    - `:info?` - when `true`, return the map described below instead of the number. Default: `false`.
    - `:bounds` - the cross validation search interval, a pair `[lo hi]` of factors of the rule of thumb `s`. Default: `[0.01 5.0]`.
    - `:grid` - the number of log spaced points of the first search stage, at least 3. Default: `60`, `30` for more than 5000 points.
    - `:bins` - the number of bins of `:sj` and `:sj-dpi`, an integer in `[2, 10000]`. Default: `1000`.
    - `:rlcv-form` - the bias term of `:rlcv`: `:printed` (the formula as printed in Wu 2019) or `:continuous`. The printed `b*` jumps by `a_n / 2` at `f = a_n`, the continuous form uses `f - a_n / 2` there, which is the one that follows from the definition of the paper. Default: `:printed`.
    - `:radius`, `:extent` - for a custom kernel function: the distance beyond which the kernel is ignored in the sum (default `##Inf`: every point) and beyond which the density is treated as zero (default `5.0`).

The cross validation selectors standardise the data, so the result is equivariant to a shift and a scale of the data, search the criterion on a deterministic grid, refine the best grid point with Brent's method and cost `O(n^2)` evaluations of the kernel for each bandwidth tried (the `:binned?` option is ignored). Measured for the gaussian kernel and `n` points (default search, one core, including the JIT warm-up): `n = 1000` takes 0.5 s (`:lscv`), 0.6 s (`:lcv`, `:rlcv`); `n = 5000` takes 3.6 s, 8.3 s and 4.7 s; `n = 10000` takes 8 s, 25 s and 16 s. A kernel which sums every point (`:cauchy`, a custom kernel without `:radius`) is slower: `n = 1000` takes 10 s for `:lscv` against 0.14 s for `:epanechnikov`. The integral terms use a grid with a step of at most `h / 40` over the range of the data extended by `min(extent, 100) h`, so for `:cauchy` the tail beyond `100 h` is left out. `:sj` and `:sj-dpi` need `O(bins^2)` operations (below 0.1 s). When the best point is a bound of the search interval the bandwidth may be outside of it: see `:at-bound?`.

Returns the bandwidth as a double. With `:info?` returns a map with `:h`, `:value` (the value of the criterion at `h` in standardised units, `##NaN` for the closed form selectors), `:at-bound?` (`true` when `h` is within `1.0e-6` relative of an end of the search interval) and `:status` (`:closed-form`, `:ok` or `:degenerate`).

Throws `ex-info` (`:type :invalid-input`) for invalid data, kernel, selector or options.

See also [[kernel-density]], [[kernel-density+]], [[kernel-density-ci]], [[kde-data]].
raw docstring

cauchyclj

(cauchy x)

Cauchy kernel, 1 / (pi/2 * (1 + 4 x^2)), experimental.

The density has no finite variance, so the canonical bandwidth is not defined, the exact evaluation sums every data point and the :nrd-adjust bandwidth is not available.

Parameters: x - a point.

Returns the value of the kernel at x.

See also kde-data, kernel-density.

Cauchy kernel, `1 / (pi/2 * (1 + 4 x^2))`, experimental.

The density has no finite variance, so the canonical bandwidth is not defined, the exact evaluation sums every data point and the `:nrd-adjust` bandwidth is not available.

Parameters: `x` - a point.

Returns the value of the kernel at `x`.

See also [[kde-data]], [[kernel-density]].
raw docstring

cosineclj

(cosine x)

Cosine kernel, pi/4 * cos(pi/2 * x) on [-1, 1], 0.0 outside.

Parameters: x - a point.

Returns the value of the kernel at x.

See also kde-data, kernel-density.

Cosine kernel, `pi/4 * cos(pi/2 * x)` on `[-1, 1]`, `0.0` outside.

Parameters: `x` - a point.

Returns the value of the kernel at `x`.

See also [[kde-data]], [[kernel-density]].
raw docstring

epanechnikovclj

(epanechnikov x)

Epanechnikov kernel, 0.75 * (1 - x^2) on [-1, 1], 0.0 outside.

Parameters: x - a point.

Returns the value of the kernel at x.

See also kde-data, kernel-density.

Epanechnikov kernel, `0.75 * (1 - x^2)` on `[-1, 1]`, `0.0` outside.

Parameters: `x` - a point.

Returns the value of the kernel at `x`.

See also [[kde-data]], [[kernel-density]].
raw docstring

gaussianclj

(gaussian x)

Gaussian kernel, the standard normal density.

Parameters: x - a point.

Returns the value of the kernel at x.

See also kde-data, kernel-density.

Gaussian kernel, the standard normal density.

Parameters: `x` - a point.

Returns the value of the kernel at `x`.

See also [[kde-data]], [[kernel-density]].
raw docstring

kde-dataclj

Constants of the predefined kernels.

Keys - kernel keywords, values - maps with:

  • :kernel - the kernel function
  • :k2 - integral of K(x)^2 (the roughness)
  • :x2k - integral of x^2 K(x) (the second moment); 0.0 for :silverman and ##Inf for :cauchy
  • :delta0 - the canonical bandwidth k2^0.2 * x2k^-0.4; ##Inf for :silverman and 0.0 for :cauchy, which means undefined (the bandwidth adjusted to the kernel is not available)
  • :nrd-scale - delta0 relative to the gaussian kernel, the factor of the :nrd-adjust bandwidth (1.0 for :gaussian); undefined as :delta0
  • :efficiency - the efficiency constant k2 * sqrt(x2k)
  • :radius - the distance from the data point beyond which the kernel is not used in the evaluation: 1.0 for the compact kernels, a distance with abs(K) < 1.0e-10 for the other ones, ##Inf for :cauchy (every data point is summed)
  • :extent - the distance beyond which the density is treated as zero when the support of the estimate (:mn, :mx) is inferred; the same as :radius except :cauchy (1000.0, the mass cut off is about 4.0e-4)

A custom kernel function has none of these constants; :radius (default ##Inf) and :extent (default 5.0) can be passed as options to kernel-density+ and bandwidth.

See also kernel-density, bandwidth.

Constants of the predefined kernels.

Keys - kernel keywords, values - maps with:

* `:kernel` - the kernel function
* `:k2` - integral of `K(x)^2` (the roughness)
* `:x2k` - integral of `x^2 K(x)` (the second moment); `0.0` for `:silverman` and `##Inf` for `:cauchy`
* `:delta0` - the canonical bandwidth `k2^0.2 * x2k^-0.4`; `##Inf` for `:silverman` and `0.0` for `:cauchy`, which means undefined (the bandwidth adjusted to the kernel is not available)
* `:nrd-scale` - `delta0` relative to the gaussian kernel, the factor of the `:nrd-adjust` bandwidth (`1.0` for `:gaussian`); undefined as `:delta0`
* `:efficiency` - the efficiency constant `k2 * sqrt(x2k)`
* `:radius` - the distance from the data point beyond which the kernel is not used in the evaluation: `1.0` for the compact kernels, a distance with `abs(K) < 1.0e-10` for the other ones, `##Inf` for `:cauchy` (every data point is summed)
* `:extent` - the distance beyond which the density is treated as zero when the support of the estimate (`:mn`, `:mx`) is inferred; the same as `:radius` except `:cauchy` (`1000.0`, the mass cut off is about `4.0e-4`)

A custom kernel function has none of these constants; `:radius` (default `##Inf`) and `:extent` (default `5.0`) can be passed as options to [[kernel-density+]] and [[bandwidth]].

See also [[kernel-density]], [[bandwidth]].
raw docstring

kernel-densityclj

(kernel-density kernel data)
(kernel-density kernel data params)

Returns a 1d kernel density estimation (KDE) function for data.

A kernel density estimate is a smoothed histogram: it places a scaled copy of kernel at every data point and sums the contributions, weighted by a bandwidth that controls how much each point spreads out.

Parameters:

  • kernel - a keyword naming one of the predefined kernels of kde-data (:uniform, :triangular, :epanechnikov, :quartic, :triweight, :tricube, :gaussian, :cosine, :logistic, :sigmoid, :silverman, :wigner, :cauchy, :laplace), or a custom kernel function accepting and returning a double.
  • data - a sequence of numbers (finite, not empty).
  • params (optional map): :bandwidth, :bandwidth-options, :binned?, :radius, :extent, with the meaning and the defaults of kernel-density+.

Returns a function of a single double returning the estimated density at that point as a double.

Throws ex-info (:type :invalid-input) for invalid input, see kernel-density+.

See also kernel-density+ (the same function plus the bandwidth, the factor and the support), kernel-density-ci (density with confidence intervals), bandwidth, kde-data.

Returns a 1d kernel density estimation (KDE) function for `data`.

A kernel density estimate is a smoothed histogram: it places a scaled copy of `kernel` at every data point and sums the contributions, weighted by a bandwidth that controls how much each point spreads out.

Parameters:

- `kernel` - a keyword naming one of the predefined kernels of [[kde-data]] (`:uniform`, `:triangular`, `:epanechnikov`, `:quartic`, `:triweight`, `:tricube`, `:gaussian`, `:cosine`, `:logistic`, `:sigmoid`, `:silverman`, `:wigner`, `:cauchy`, `:laplace`), or a custom kernel function accepting and returning a double.
- `data` - a sequence of numbers (finite, not empty).
- `params` (optional map): `:bandwidth`, `:bandwidth-options`, `:binned?`, `:radius`, `:extent`, with the meaning and the defaults of [[kernel-density+]].

Returns a function of a single double returning the estimated density at that point as a double.

Throws `ex-info` (`:type :invalid-input`) for invalid input, see [[kernel-density+]].

See also [[kernel-density+]] (the same function plus the bandwidth, the factor and the support), [[kernel-density-ci]] (density with confidence intervals), [[bandwidth]], [[kde-data]].
raw docstring

kernel-density+clj

(kernel-density+ kernel data)
(kernel-density+ kernel
                 data
                 {:keys [bandwidth binned? bandwidth-options] :as params})

Builds a 1d kernel density estimator (KDE) for data and returns it together with the fitting details.

A kernel density estimate is a smoothed histogram: it places a scaled copy of kernel at every data point and sums the contributions, weighted by a bandwidth h that controls how much each point spreads out.

Parameters:

  • kernel - a keyword naming one of the predefined kernels of kde-data, or a custom kernel function accepting and returning a double (same shape as gaussian, epanechnikov, etc.).
  • data - a sequence of numbers (finite, not empty), not modified.
  • params (optional map):
    • :bandwidth - the bandwidth h, a number greater than 0 or a keyword accepted by bandwidth (:nrd, :nrd0, :nrd-adjust, :sj, :sj-dpi, :rlcv, :lcv, :lscv). Default: :nrd.
    • :bandwidth-options - a map of the options of bandwidth (:bounds, :grid, :bins, :rlcv-form).
    • :binned? - false (default) for the exact evaluation. When true or a number, the data are replaced by a linear binning on a regular grid with the spacing h / divisor (the divisor is 5 for true, the number otherwise) and the density is a sum over the grid nodes; the error is of the second order in the spacing, and the cost does not depend on the number of data points. A grid of more than 2^24 nodes throws.
    • :radius, :extent - for a custom kernel function: the distance beyond which the kernel is ignored (default ##Inf: the sum is over every data point) and beyond which the density is treated as zero (default 5.0). Ignored for the predefined kernels (see kde-data).

The evaluation is exact: the density at x sums the kernel over every data point within 1.01 * h * radius of x, so it is not zero in a gap of the data wider than the kernel radius when the kernel is not compact.

Returns a map:

  • :kde - the density function, a function of a double returning the estimate (##NaN at ##NaN); it is negative at some points for :silverman.
  • :factor - the normalizing factor 1/(n*h), n is the number of data points (also with :binned?).
  • :h - the bandwidth used.
  • :mn and :mx - the support of the estimator: the data range extended by 1.01 * h * extent; useful as integration bounds.

Throws ex-info (:type :invalid-input) for empty data, a non finite value in the data, an unknown kernel or bandwidth selector, a bandwidth which is not finite and positive, or invalid :binned?.

See also kernel-density (returns only the function), kernel-density-ci (adds confidence intervals), bandwidth, kde-data.

Builds a 1d kernel density estimator (KDE) for `data` and returns it together with the fitting details.

A kernel density estimate is a smoothed histogram: it places a scaled copy of `kernel` at every data point and sums the contributions, weighted by a bandwidth `h` that controls how much each point spreads out.

Parameters:

- `kernel` - a keyword naming one of the predefined kernels of [[kde-data]], or a custom kernel function accepting and returning a double (same shape as [[gaussian]], [[epanechnikov]], etc.).
- `data` - a sequence of numbers (finite, not empty), not modified.
- `params` (optional map):
    - `:bandwidth` - the bandwidth `h`, a number greater than 0 or a keyword accepted by [[bandwidth]] (`:nrd`, `:nrd0`, `:nrd-adjust`, `:sj`, `:sj-dpi`, `:rlcv`, `:lcv`, `:lscv`). Default: `:nrd`.
    - `:bandwidth-options` - a map of the options of [[bandwidth]] (`:bounds`, `:grid`, `:bins`, `:rlcv-form`).
    - `:binned?` - `false` (default) for the exact evaluation. When `true` or a number, the data are replaced by a linear binning on a regular grid with the spacing `h / divisor` (the divisor is `5` for `true`, the number otherwise) and the density is a sum over the grid nodes; the error is of the second order in the spacing, and the cost does not depend on the number of data points. A grid of more than `2^24` nodes throws.
    - `:radius`, `:extent` - for a custom kernel function: the distance beyond which the kernel is ignored (default `##Inf`: the sum is over every data point) and beyond which the density is treated as zero (default `5.0`). Ignored for the predefined kernels (see [[kde-data]]).

The evaluation is exact: the density at `x` sums the kernel over every data point within `1.01 * h * radius` of `x`, so it is not zero in a gap of the data wider than the kernel radius when the kernel is not compact.

Returns a map:

- `:kde` - the density function, a function of a double returning the estimate (`##NaN` at `##NaN`); it is negative at some points for `:silverman`.
- `:factor` - the normalizing factor `1/(n*h)`, `n` is the number of data points (also with `:binned?`).
- `:h` - the bandwidth used.
- `:mn` and `:mx` - the support of the estimator: the data range extended by `1.01 * h * extent`; useful as integration bounds.

Throws `ex-info` (`:type :invalid-input`) for empty data, a non finite value in the data, an unknown kernel or bandwidth selector, a bandwidth which is not finite and positive, or invalid `:binned?`.

See also [[kernel-density]] (returns only the function), [[kernel-density-ci]] (adds confidence intervals), [[bandwidth]], [[kde-data]].
raw docstring

kernel-density-2dclj

(kernel-density-2d kernel xs ys)
(kernel-density-2d kernel xs ys params)

Returns a two dimensional kernel density estimation function of the points (xs_i, ys_i): a product kernel with one bandwidth per axis.

Parameters: kernel, xs, ys and the optional params (:bandwidth, :bandwidth-options, :binned?, :radius, :extent) as in kernel-density-2d+.

Returns a function of two doubles x, y returning the estimated density as a double.

Throws ex-info (:type :invalid-input) for invalid input, see kernel-density-2d+.

See also kernel-density-2d+ (the function and the bandwidths, the factor and the support), kernel-density.

Returns a two dimensional kernel density estimation function of the points `(xs_i, ys_i)`: a product kernel with
one bandwidth per axis.

Parameters: `kernel`, `xs`, `ys` and the optional `params` (`:bandwidth`, `:bandwidth-options`, `:binned?`,
`:radius`, `:extent`) as in [[kernel-density-2d+]].

Returns a function of two doubles `x`, `y` returning the estimated density as a double.

Throws `ex-info` (`:type :invalid-input`) for invalid input, see [[kernel-density-2d+]].

See also [[kernel-density-2d+]] (the function and the bandwidths, the factor and the support), [[kernel-density]].
raw docstring

kernel-density-2d+clj

(kernel-density-2d+ kernel xs ys)
(kernel-density-2d+ kernel
                    xs
                    ys
                    {:keys [bandwidth binned? bandwidth-options] :as params})

Builds a two dimensional kernel density estimator of the points (xs_i, ys_i) and returns it together with the fitting details.

The estimate uses the product kernel f(x, y) = 1/(n hx hy) * sum K((x - x_i) / hx) K((y - y_i) / hy) with one bandwidth per axis (no rotation: the bandwidth matrix is diagonal).

Parameters:

  • kernel - a keyword naming one of the predefined kernels of kde-data, or a custom kernel function accepting and returning a double; the same kernel is used on both axes.
  • xs, ys - sequences of numbers of the same length (finite, not empty), the coordinates of the points; not modified.
  • params (optional map):
    • :bandwidth - [hx hy], or one specification used for both axes. A specification is a number greater than 0, a selector of bandwidth applied to the coordinates of that axis alone (:nrd, :nrd0, :nrd-adjust, :sj, :sj-dpi, :rlcv, :lcv, :lscv), or :scott - Scott's rule of a two dimensional sample s * n^(-1/6), s the standard deviation of the axis. Default: :nrd on both axes, which is MASS::kde2d of R with its default bandwidth (bandwidth.nrd there is four times the standard deviation of the gaussian kernel).
    • :bandwidth-options - a map of the options of bandwidth (:bounds, :grid, :bins, :rlcv-form).
    • :binned? - false (default) for the exact evaluation. When true or a number, the points are replaced by a two dimensional linear binning on a regular grid with the spacing hx / divisor, hy / divisor (the divisor is 5 for true, the number otherwise); the cost of an evaluation does not depend on the number of points. A grid of more than 2^24 nodes throws.
    • :radius, :extent - for a custom kernel function: the distance beyond which the kernel is ignored (default ##Inf) and beyond which the density is treated as zero (default 5.0), in bandwidths. Ignored for the predefined kernels.

The evaluation is exact: the density at (x, y) sums the points within 1.01 * radius bandwidths of it on both axes.

Returns a map:

  • :kde - the density function of two doubles x, y (##NaN when either is ##NaN).
  • :factor - the normalizing factor 1/(n hx hy).
  • :h - the bandwidths used, [hx hy].
  • :mn, :mx - the corners [x y] of the support: the range of the points on each axis extended by 1.01 * h * extent.

Throws ex-info (:type :invalid-input) for empty data, coordinates which are not finite or of different lengths, an unknown kernel or bandwidth selector, a bandwidth which is not finite and positive, a selector on fewer than 2 points, or an invalid :binned?.

See also kernel-density-2d, kernel-density+, bandwidth.

Builds a two dimensional kernel density estimator of the points `(xs_i, ys_i)` and returns it together with the
fitting details.

The estimate uses the product kernel `f(x, y) = 1/(n hx hy) * sum K((x - x_i) / hx) K((y - y_i) / hy)` with one
bandwidth per axis (no rotation: the bandwidth matrix is diagonal).

Parameters:

- `kernel` - a keyword naming one of the predefined kernels of [[kde-data]], or a custom kernel function accepting and returning a double; the same kernel is used on both axes.
- `xs`, `ys` - sequences of numbers of the same length (finite, not empty), the coordinates of the points; not modified.
- `params` (optional map):
    - `:bandwidth` - `[hx hy]`, or one specification used for both axes. A specification is a number greater than 0, a selector of [[bandwidth]] applied to the coordinates of that axis alone (`:nrd`, `:nrd0`, `:nrd-adjust`, `:sj`, `:sj-dpi`, `:rlcv`, `:lcv`, `:lscv`), or `:scott` - Scott's rule of a two dimensional sample `s * n^(-1/6)`, `s` the standard deviation of the axis. Default: `:nrd` on both axes, which is `MASS::kde2d` of R with its default bandwidth (`bandwidth.nrd` there is four times the standard deviation of the gaussian kernel).
    - `:bandwidth-options` - a map of the options of [[bandwidth]] (`:bounds`, `:grid`, `:bins`, `:rlcv-form`).
    - `:binned?` - `false` (default) for the exact evaluation. When `true` or a number, the points are replaced by a two dimensional linear binning on a regular grid with the spacing `hx / divisor`, `hy / divisor` (the divisor is `5` for `true`, the number otherwise); the cost of an evaluation does not depend on the number of points. A grid of more than `2^24` nodes throws.
    - `:radius`, `:extent` - for a custom kernel function: the distance beyond which the kernel is ignored (default `##Inf`) and beyond which the density is treated as zero (default `5.0`), in bandwidths. Ignored for the predefined kernels.

The evaluation is exact: the density at `(x, y)` sums the points within `1.01 * radius` bandwidths of it on both axes.

Returns a map:

- `:kde` - the density function of two doubles `x`, `y` (`##NaN` when either is `##NaN`).
- `:factor` - the normalizing factor `1/(n hx hy)`.
- `:h` - the bandwidths used, `[hx hy]`.
- `:mn`, `:mx` - the corners `[x y]` of the support: the range of the points on each axis extended by `1.01 * h * extent`.

Throws `ex-info` (`:type :invalid-input`) for empty data, coordinates which are not finite or of different lengths, an unknown kernel or bandwidth selector, a bandwidth which is not finite and positive, a selector on fewer than 2 points, or an invalid `:binned?`.

See also [[kernel-density-2d]], [[kernel-density+]], [[bandwidth]].
raw docstring

kernel-density-ciclj

(kernel-density-ci kernel data)
(kernel-density-ci kernel data {:keys [alpha] :as params})

Returns a 1d KDE function that also reports pointwise asymptotic confidence intervals.

Uses the asymptotic normal approximation of the estimate (section 6.1.5 of http://sfb649.wiwi.hu-berlin.de/fedc_homepage/xplore/tutorials/xlghtmlnode33.html): the variance of the density estimate f(x) is approximated by k2 * f(x) / (n*h), where k2 is the roughness of the kernel (integral of the squared kernel), and the bounds are f(x) plus and minus the standard normal quantile of 1 - alpha/2 times the standard deviation. The interval is for the expected value of the estimate (the bias is ignored); the variance is computed from max(f(x), 0).

Parameters:

  • kernel - a keyword naming one of the predefined kernels of kde-data; a custom kernel function is not accepted, since its roughness k2 is not known.
  • data - a sequence of numbers (finite, not empty).
  • params (optional map), forwarded to kernel-density+, plus:
    • :alpha - the interval has the coverage 1 - alpha, a number in (0, 1). Default: 0.05.

Returns a function of a single double x returning a 3-element vector [fx lower upper]: the estimate and the confidence bounds. The lower bound is not clipped at zero.

Throws ex-info (:type :invalid-input) for a kernel which is not in kde-data, alpha outside of (0, 1) and the invalid input of kernel-density+.

See also kernel-density, kernel-density+, kde-data.

Returns a 1d KDE function that also reports pointwise asymptotic confidence intervals.

Uses the asymptotic normal approximation of the estimate (section 6.1.5 of http://sfb649.wiwi.hu-berlin.de/fedc_homepage/xplore/tutorials/xlghtmlnode33.html): the variance of the density estimate `f(x)` is approximated by `k2 * f(x) / (n*h)`, where `k2` is the roughness of the kernel (integral of the squared kernel), and the bounds are `f(x)` plus and minus the standard normal quantile of `1 - alpha/2` times the standard deviation. The interval is for the expected value of the estimate (the bias is ignored); the variance is computed from `max(f(x), 0)`.

Parameters:

- `kernel` - a keyword naming one of the predefined kernels of [[kde-data]]; a custom kernel function is not accepted, since its roughness `k2` is not known.
- `data` - a sequence of numbers (finite, not empty).
- `params` (optional map), forwarded to [[kernel-density+]], plus:
    - `:alpha` - the interval has the coverage `1 - alpha`, a number in `(0, 1)`. Default: `0.05`.

Returns a function of a single double `x` returning a 3-element vector `[fx lower upper]`: the estimate and the confidence bounds. The lower bound is not clipped at zero.

Throws `ex-info` (`:type :invalid-input`) for a kernel which is not in [[kde-data]], `alpha` outside of `(0, 1)` and the invalid input of [[kernel-density+]].

See also [[kernel-density]], [[kernel-density+]], [[kde-data]].
raw docstring

laplaceclj

(laplace x)

Laplace kernel, 0.5 * e^-abs(x), experimental.

Parameters: x - a point.

Returns the value of the kernel at x.

See also kde-data, kernel-density.

Laplace kernel, `0.5 * e^-abs(x)`, experimental.

Parameters: `x` - a point.

Returns the value of the kernel at `x`.

See also [[kde-data]], [[kernel-density]].
raw docstring

logisticclj

(logistic x)

Logistic kernel, the density of the standard logistic distribution 1 / (2 + e^x + e^-x).

Parameters: x - a point.

Returns the value of the kernel at x; 0.0 for very large abs(x).

See also kde-data, kernel-density.

Logistic kernel, the density of the standard logistic distribution `1 / (2 + e^x + e^-x)`.

Parameters: `x` - a point.

Returns the value of the kernel at `x`; `0.0` for very large `abs(x)`.

See also [[kde-data]], [[kernel-density]].
raw docstring

quarticclj

(quartic x)

Quartic (biweight) kernel, 15/16 * (1 - x^2)^2 on [-1, 1], 0.0 outside.

Parameters: x - a point.

Returns the value of the kernel at x.

See also kde-data, kernel-density.

Quartic (biweight) kernel, `15/16 * (1 - x^2)^2` on `[-1, 1]`, `0.0` outside.

Parameters: `x` - a point.

Returns the value of the kernel at `x`.

See also [[kde-data]], [[kernel-density]].
raw docstring

sigmoidclj

(sigmoid x)

Sigmoid kernel, 2/pi / (e^x + e^-x).

Parameters: x - a point.

Returns the value of the kernel at x; 0.0 for very large abs(x).

See also kde-data, kernel-density.

Sigmoid kernel, `2/pi / (e^x + e^-x)`.

Parameters: `x` - a point.

Returns the value of the kernel at `x`; `0.0` for very large `abs(x)`.

See also [[kde-data]], [[kernel-density]].
raw docstring

silvermanclj

(silverman x)

Silverman kernel, 0.5 * e^-u * sin(u + pi/4) for u = abs(x) / sqrt(2).

The kernel takes negative values (a higher order kernel), so a density estimate built with it can be negative.

Parameters: x - a point.

Returns the value of the kernel at x; 0.0 at +-Inf.

See also kde-data, kernel-density.

Silverman kernel, `0.5 * e^-u * sin(u + pi/4)` for `u = abs(x) / sqrt(2)`.

The kernel takes negative values (a higher order kernel), so a density estimate built with it can be negative.

Parameters: `x` - a point.

Returns the value of the kernel at `x`; `0.0` at `+-Inf`.

See also [[kde-data]], [[kernel-density]].
raw docstring

triangularclj

(triangular x)

Triangular kernel, 1 - abs(x) on [-1, 1], 0.0 outside.

Parameters: x - a point.

Returns the value of the kernel at x.

See also kde-data, kernel-density.

Triangular kernel, `1 - abs(x)` on `[-1, 1]`, `0.0` outside.

Parameters: `x` - a point.

Returns the value of the kernel at `x`.

See also [[kde-data]], [[kernel-density]].
raw docstring

tricubeclj

(tricube x)

Tricube kernel, 70/81 * (1 - abs(x)^3)^3 on [-1, 1], 0.0 outside.

Parameters: x - a point.

Returns the value of the kernel at x.

See also kde-data, kernel-density.

Tricube kernel, `70/81 * (1 - abs(x)^3)^3` on `[-1, 1]`, `0.0` outside.

Parameters: `x` - a point.

Returns the value of the kernel at `x`.

See also [[kde-data]], [[kernel-density]].
raw docstring

triweightclj

(triweight x)

Triweight kernel, 35/32 * (1 - x^2)^3 on [-1, 1], 0.0 outside.

Parameters: x - a point.

Returns the value of the kernel at x.

See also kde-data, kernel-density.

Triweight kernel, `35/32 * (1 - x^2)^3` on `[-1, 1]`, `0.0` outside.

Parameters: `x` - a point.

Returns the value of the kernel at `x`.

See also [[kde-data]], [[kernel-density]].
raw docstring

uniformclj

(uniform x)

Uniform kernel, 0.5 on [-1, 1], 0.0 outside.

Parameters: x - a point.

Returns the value of the kernel at x.

See also kde-data, kernel-density.

Uniform kernel, `0.5` on `[-1, 1]`, `0.0` outside.

Parameters: `x` - a point.

Returns the value of the kernel at `x`.

See also [[kde-data]], [[kernel-density]].
raw docstring

wignerclj

(wigner x)

Wigner (semicircle) kernel, 2/pi * sqrt(1 - x^2) on [-1, 1], 0.0 outside, experimental.

Parameters: x - a point.

Returns the value of the kernel at x.

See also kde-data, kernel-density.

Wigner (semicircle) kernel, `2/pi * sqrt(1 - x^2)` on `[-1, 1]`, `0.0` outside, experimental.

Parameters: `x` - a point.

Returns the value of the kernel at `x`.

See also [[kde-data]], [[kernel-density]].
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