Liking cljdoc? Tell your friends :D

fastmath.interpolation.gp

Gaussian process regression (interpolation) of scattered data.

A zero mean Gaussian process with a covariance kernel, kscale times a kernel of fastmath.kernel, and an independent noise (a variance added to the diagonal). Training data can be normalised to the zero mean and the unit variance. The model is fixed (hyperparameters are not optimised).

Functions:

See more here

Gaussian process regression (interpolation) of scattered data.

A zero mean Gaussian process with a covariance kernel, `kscale` times a kernel of `fastmath.kernel`, and an independent noise (a variance added to the diagonal). Training data can be normalised to the zero mean and the unit variance. The model is fixed (hyperparameters are not optimised).

Functions:

- [[gaussian-process]] creates the model, [[gp]] returns the posterior mean as a function.
- [[predict]] and [[predict-all]] give the posterior mean and optionally the standard deviation at one or many points.
- [[L]] is the log marginal likelihood of the model.
- [[prior-samples]] and [[posterior-samples]] draw random functions at the given points.

See more [here](https://nextjournal.com/generateme/gaussian-processes#gp%2B)
raw docstring

gaussian-processclj

(gaussian-process xss ys)
(gaussian-process xss ys opts)

Creates a Gaussian process model from the training data.

The posterior is the one of a zero mean Gaussian process with the covariance kscale * kernel(x, x'), observed with an independent noise of the variance noise.

Parameters:

  • xss (sequence): the training points, at least one; numbers for one dimensional data or sequences of numbers (vectors, Vec2, ...) of an equal dimension
  • ys (sequence of numbers): the values at the points, as many as points
  • opts (map, optional; a nil value is the same as a missing key):
    • :kernel (function, default: (fastmath.kernel/kernel :gaussian), sigma 1.0): a covariance kernel (fn [x1 x2]) of two points given as vectors, see fastmath.kernel/kernel; its own parameters (for example :sigma) set the length scale
    • :kscale (double, default: 1.0): a positive multiplier of the kernel, the signal variance
    • :noise (double, default: 1.0e-8): a variance added to the diagonal of the covariance matrix of the training values, in the units of the normalised values (so relative to the signal variance kscale when :normalize? is true), finite and not negative. It is the alpha of the scikit-learn GaussianProcessRegressor. A small value (1.0e-8) keeps the matrix numerically positive definite; a larger one smooths the data (the model then does not pass through the points)
    • :normalize? (boolean, default: false): shift the values to the zero mean and scale them to the unit sample standard deviation (with n - 1, scikit-learn normalize_y uses n) before the fit, predictions are mapped back. A constant ys (or a single point) uses the standard deviation 1: the predictions equal the constant, the standard deviation comes from the kernel only
    • :L? (boolean, default: true): compute the log marginal likelihood L now and store it in the model; false skips it (NaN is stored, L computes it on demand). gp uses false

Returns a GaussianProcess record, which is also a function: (gp-object x) or (gp-object x true) calls predict for one point and predict-all for a sequence of points.

Throws ex-info (with :type :invalid-input, :method :gp and the key :property) when xss and ys have different lengths (:length-mismatch), there are no points (:too-few-points), the training points have different dimensions (:dimension-mismatch), :kscale or :noise is out of range (:kscale, :noise), or the covariance matrix is not positive definite (:not-positive-definite: repeated points with a tiny :noise; increase it).

See also gp, predict, predict-all, L, posterior-samples, fastmath.interpolation/gp.

Creates a Gaussian process model from the training data.

The posterior is the one of a zero mean Gaussian process with the covariance `kscale * kernel(x, x')`, observed with an independent noise of the variance `noise`.

Parameters:

- `xss` (sequence): the training points, at least one; numbers for one dimensional data or sequences of numbers (vectors, `Vec2`, ...) of an equal dimension
- `ys` (sequence of numbers): the values at the points, as many as points
- `opts` (map, optional; a `nil` value is the same as a missing key):
    - `:kernel` (function, default: `(fastmath.kernel/kernel :gaussian)`, sigma `1.0`): a covariance kernel `(fn [x1 x2])` of two points given as vectors, see [[fastmath.kernel/kernel]]; its own parameters (for example `:sigma`) set the length scale
    - `:kscale` (double, default: `1.0`): a positive multiplier of the kernel, the signal variance
    - `:noise` (double, default: `1.0e-8`): a variance added to the diagonal of the covariance matrix of the training values, in the units of the normalised values (so relative to the signal variance `kscale` when `:normalize?` is true), finite and not negative. It is the `alpha` of the scikit-learn `GaussianProcessRegressor`. A small value (`1.0e-8`) keeps the matrix numerically positive definite; a larger one smooths the data (the model then does not pass through the points)
    - `:normalize?` (boolean, default: `false`): shift the values to the zero mean and scale them to the unit sample standard deviation (with `n - 1`, scikit-learn `normalize_y` uses `n`) before the fit, predictions are mapped back. A constant `ys` (or a single point) uses the standard deviation `1`: the predictions equal the constant, the standard deviation comes from the kernel only
    - `:L?` (boolean, default: `true`): compute the log marginal likelihood [[L]] now and store it in the model; `false` skips it (`NaN` is stored, [[L]] computes it on demand). [[gp]] uses `false`

Returns a `GaussianProcess` record, which is also a function: `(gp-object x)` or `(gp-object x true)` calls [[predict]] for one point and [[predict-all]] for a sequence of points.

Throws `ex-info` (with `:type :invalid-input`, `:method :gp` and the key `:property`) when `xss` and `ys` have different lengths (`:length-mismatch`), there are no points (`:too-few-points`), the training points have different dimensions (`:dimension-mismatch`), `:kscale` or `:noise` is out of range (`:kscale`, `:noise`), or the covariance matrix is not positive definite (`:not-positive-definite`: repeated points with a tiny `:noise`; increase it).

See also [[gp]], [[predict]], [[predict-all]], [[L]], [[posterior-samples]], [[fastmath.interpolation/gp]].
raw docstring

gpclj

(gp xss ys)
(gp xss ys kernel)
(gp xss ys kernel opts)

Creates the Gaussian process interpolator, a function returning the posterior mean.

Parameters:

  • xss, ys: the training points and values, see gaussian-process
  • kernel (function, optional, default: (fastmath.kernel/kernel :gaussian)): the covariance kernel
  • opts (map, optional): the options of gaussian-process (:kscale, :noise, :normalize?); :kernel is taken from the argument and :L? is always false

Returns a function of a query point (a number for one dimensional data, a sequence of numbers otherwise) returning the posterior mean as a double. Throws the ex-info of gaussian-process and of predict.

See also gaussian-process, predict, fastmath.interpolation/gp.

Creates the Gaussian process interpolator, a function returning the posterior mean.

Parameters:

- `xss`, `ys`: the training points and values, see [[gaussian-process]]
- `kernel` (function, optional, default: `(fastmath.kernel/kernel :gaussian)`): the covariance kernel
- `opts` (map, optional): the options of [[gaussian-process]] (`:kscale`, `:noise`, `:normalize?`); `:kernel` is taken from the argument and `:L?` is always `false`

Returns a function of a query point (a number for one dimensional data, a sequence of numbers otherwise) returning the posterior mean as a double. Throws the `ex-info` of [[gaussian-process]] and of [[predict]].

See also [[gaussian-process]], [[predict]], [[fastmath.interpolation/gp]].
raw docstring

Lclj

(L gp-object)
(L ys w chol n)

Computes the log marginal likelihood of a Gaussian process.

L = -1/2 (y^T K^-1 y + log det K + n log 2 pi), where K is the covariance matrix of the training points (the kernel times kscale plus the noise on the diagonal) and y are the training values (normalised when the model was created with :normalize? true, so L is the likelihood of the normalised values). The determinant is computed stably as twice the sum of the logarithms of the diagonal of the Cholesky factor: it does not underflow or overflow for hundreds of points or an extreme kscale.

Parameters:

  • gp-object (GaussianProcess): a model created by gaussian-process. The value computed at the creation is returned when it exists (the option :L? was true), otherwise it is computed
  • ys, w, chol, n: (long form) the training values (RealVector), the weights K^-1 y (RealVector), the Cholesky decomposition of K and the number of points

Returns the log marginal likelihood as a double. It is NaN for a model whose ys are not finite.

See also gaussian-process, predict.

Computes the log marginal likelihood of a Gaussian process.

`L = -1/2 (y^T K^-1 y + log det K + n log 2 pi)`, where `K` is the covariance matrix of the training points (the kernel times `kscale` plus the noise on the diagonal) and `y` are the training values (normalised when the model was created with `:normalize? true`, so `L` is the likelihood of the normalised values). The determinant is computed stably as twice the sum of the logarithms of the diagonal of the Cholesky factor: it does not underflow or overflow for hundreds of points or an extreme `kscale`.

Parameters:

- `gp-object` (`GaussianProcess`): a model created by [[gaussian-process]]. The value computed at the creation is returned when it exists (the option `:L?` was true), otherwise it is computed
- `ys`, `w`, `chol`, `n`: (long form) the training values (`RealVector`), the weights `K^-1 y` (`RealVector`), the Cholesky decomposition of `K` and the number of points

Returns the log marginal likelihood as a double. It is `NaN` for a model whose `ys` are not finite.

See also [[gaussian-process]], [[predict]].
raw docstring

posterior-samplesclj

(posterior-samples gp-object xvals)
(posterior-samples gp-object xvals stddev?)
(posterior-samples gp-object xvals stddev? rng)

Draws a random sample of the posterior of a Gaussian process at the given points.

The sample is drawn from the joint normal distribution of the posterior at xvals: the mean of predict-all and the full posterior covariance of the latent function (without :noise), in the original units of ys (the covariance is multiplied by ystddev^2, so the standard deviation of the samples at a point equals the one of predict, also with :normalize? true).

Parameters:

  • gp-object (GaussianProcess): a model created by gaussian-process
  • xvals (sequence): the points (numbers for one dimensional data, sequences of numbers otherwise); their dimension has to be the dimension of the training points
  • stddev? (boolean, default: false): return pairs [value standard-deviation] where the second element is the posterior standard deviation at the point
  • rng (random number generator, optional, default: nil): the generator of the random numbers, see fastmath.random/ensure-rng; when missing or nil a new unseeded generator is created for every call (the shared fastmath.random/default-rng is never used), so give a seeded one to reproduce the samples, for example (fastmath.random/rng :isaac 42); to give it, give stddev? as well

Returns a sequence of values, one for each point (or pairs). The covariance is symmetrised and decomposed into the eigenvalues, negative eigenvalues (rounding) are clipped to zero: a dense grid (hundreds of points) or points at the training points do not fail. The sample is one random function evaluated at the points, the values at different points are correlated.

Throws ex-info with :property :dimension-mismatch for a point of a wrong dimension.

See also prior-samples, predict-all, gaussian-process.

Draws a random sample of the posterior of a Gaussian process at the given points.

The sample is drawn from the joint normal distribution of the posterior at `xvals`: the mean of [[predict-all]] and the full posterior covariance of the latent function (without `:noise`), in the original units of `ys` (the covariance is multiplied by `ystddev^2`, so the standard deviation of the samples at a point equals the one of [[predict]], also with `:normalize? true`).

Parameters:

- `gp-object` (`GaussianProcess`): a model created by [[gaussian-process]]
- `xvals` (sequence): the points (numbers for one dimensional data, sequences of numbers otherwise); their dimension has to be the dimension of the training points
- `stddev?` (boolean, default: `false`): return pairs `[value standard-deviation]` where the second element is the posterior standard deviation at the point
- `rng` (random number generator, optional, default: `nil`): the generator of the random numbers, see [[fastmath.random/ensure-rng]]; when missing or `nil` a new unseeded generator is created for every call (the shared `fastmath.random/default-rng` is never used), so give a seeded one to reproduce the samples, for example `(fastmath.random/rng :isaac 42)`; to give it, give `stddev?` as well

Returns a sequence of values, one for each point (or pairs). The covariance is symmetrised and decomposed into the eigenvalues, negative eigenvalues (rounding) are clipped to zero: a dense grid (hundreds of points) or points at the training points do not fail. The sample is one random function evaluated at the points, the values at different points are correlated.

Throws `ex-info` with `:property :dimension-mismatch` for a point of a wrong dimension.

See also [[prior-samples]], [[predict-all]], [[gaussian-process]].
raw docstring

predictclj

(predict gp-object xval)
(predict gp-object xval stddev?)

Computes the posterior mean (and optionally the standard deviation) of a Gaussian process at one point.

Parameters:

  • gp-object (GaussianProcess): a model created by gaussian-process
  • xval (number or sequence of numbers): the query point, a number for one dimensional data; its dimension has to be the dimension of the training points
  • stddev? (boolean, default: false): also return the standard deviation

Returns the mean as a double, or, when stddev? is true, a Vec2 of the mean and the standard deviation of the latent function (it does not include :noise; it is 0.0 up to the rounding at a training point for a small noise, the prior sqrt(kscale) times the standard deviation of ys far from the data). Both are in the original units of ys (also with :normalize? true). A tiny negative variance, a rounding artefact, gives the standard deviation 0.0.

Throws ex-info with :property :dimension-mismatch when the dimension of xval differs from the training dimension.

See also predict-all, gaussian-process, posterior-samples.

Computes the posterior mean (and optionally the standard deviation) of a Gaussian process at one point.

Parameters:

- `gp-object` (`GaussianProcess`): a model created by [[gaussian-process]]
- `xval` (number or sequence of numbers): the query point, a number for one dimensional data; its dimension has to be the dimension of the training points
- `stddev?` (boolean, default: `false`): also return the standard deviation

Returns the mean as a double, or, when `stddev?` is true, a `Vec2` of the mean and the standard deviation of the latent function (it does not include `:noise`; it is `0.0` up to the rounding at a training point for a small noise, the prior `sqrt(kscale)` times the standard deviation of `ys` far from the data). Both are in the original units of `ys` (also with `:normalize? true`). A tiny negative variance, a rounding artefact, gives the standard deviation `0.0`.

Throws `ex-info` with `:property :dimension-mismatch` when the dimension of `xval` differs from the training dimension.

See also [[predict-all]], [[gaussian-process]], [[posterior-samples]].
raw docstring

predict-allclj

(predict-all gp-object xvals)
(predict-all gp-object xvals stddev?)

Computes the posterior mean (and optionally the standard deviation) of a Gaussian process at many points.

Parameters:

  • gp-object (GaussianProcess): a model created by gaussian-process
  • xvals (sequence): the query points (numbers for one dimensional data, sequences of numbers otherwise); their dimension has to be the dimension of the training points
  • stddev? (boolean, default: false): also return the standard deviations

Returns a sequence of the means (doubles), or, when stddev? is true, a sequence of vectors [mean standard-deviation] in the original units of ys, the same values as predict gives for each point (a tiny negative variance, a rounding artefact, gives the standard deviation 0.0, never NaN).

Throws ex-info with :property :dimension-mismatch when a point has a different dimension than the training points.

See also predict, gaussian-process, posterior-samples.

Computes the posterior mean (and optionally the standard deviation) of a Gaussian process at many points.

Parameters:

- `gp-object` (`GaussianProcess`): a model created by [[gaussian-process]]
- `xvals` (sequence): the query points (numbers for one dimensional data, sequences of numbers otherwise); their dimension has to be the dimension of the training points
- `stddev?` (boolean, default: `false`): also return the standard deviations

Returns a sequence of the means (doubles), or, when `stddev?` is true, a sequence of vectors `[mean standard-deviation]` in the original units of `ys`, the same values as [[predict]] gives for each point (a tiny negative variance, a rounding artefact, gives the standard deviation `0.0`, never `NaN`).

Throws `ex-info` with `:property :dimension-mismatch` when a point has a different dimension than the training points.

See also [[predict]], [[gaussian-process]], [[posterior-samples]].
raw docstring

prior-samplesclj

(prior-samples gp-object xvals)
(prior-samples gp-object xvals rng)

Draws a random sample of the prior of a Gaussian process at the given points.

The sample is drawn from the normal distribution with the mean ymean and the covariance ystddev^2 * (kscale * kernel + noise I) at xvals (for a model created with :normalize? false this is the zero mean and the covariance kscale * kernel + noise I). The noise is part of the covariance, so the sample is a noisy observation of the prior. The training data do not take part (only the kernel, kscale, noise and the normalisation).

Parameters:

  • gp-object (GaussianProcess): a model created by gaussian-process
  • xvals (sequence): the points (numbers for one dimensional data, sequences of numbers otherwise); their dimension has to be the dimension of the training points
  • rng (random number generator, optional, default: nil): the generator of the random numbers, see fastmath.random/ensure-rng; when missing or nil a new unseeded generator is created for every call (the shared fastmath.random/default-rng is never used), so give a seeded one to reproduce the samples, for example (fastmath.random/rng :isaac 42)

Returns a sequence of values, one for each point. The covariance is decomposed into the eigenvalues with the negative ones clipped to zero, so a dense grid does not fail.

Throws ex-info with :property :dimension-mismatch for a point of a wrong dimension.

See also posterior-samples, gaussian-process.

Draws a random sample of the prior of a Gaussian process at the given points.

The sample is drawn from the normal distribution with the mean `ymean` and the covariance `ystddev^2 * (kscale * kernel + noise I)` at `xvals` (for a model created with `:normalize? false` this is the zero mean and the covariance `kscale * kernel + noise I`). The noise is part of the covariance, so the sample is a noisy observation of the prior. The training data do not take part (only the kernel, `kscale`, `noise` and the normalisation).

Parameters:

- `gp-object` (`GaussianProcess`): a model created by [[gaussian-process]]
- `xvals` (sequence): the points (numbers for one dimensional data, sequences of numbers otherwise); their dimension has to be the dimension of the training points
- `rng` (random number generator, optional, default: `nil`): the generator of the random numbers, see [[fastmath.random/ensure-rng]]; when missing or `nil` a new unseeded generator is created for every call (the shared `fastmath.random/default-rng` is never used), so give a seeded one to reproduce the samples, for example `(fastmath.random/rng :isaac 42)`

Returns a sequence of values, one for each point. The covariance is decomposed into the eigenvalues with the negative ones clipped to zero, so a dense grid does not fail.

Throws `ex-info` with `:property :dimension-mismatch` for a point of a wrong dimension.

See also [[posterior-samples]], [[gaussian-process]].
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