Skip to content

src/core/gp.js ​

text
Gaussian-process regression with a Gaussian (squared-exponential) kernel, used to remove the part of a light
curve's residuals that follows the star's position on the detector (the way Spitzer intrapixel systematics
are handled: flux residual as a smooth function of the x and y centroid, optionally of time too).

Model.  r_i = g(x_i) + e_i, with x_i the d inputs of point i (x centroid, y centroid[, time]), g a zero-mean
Gaussian process with covariance
    k(x, x') = A^2 exp( -1/2 sum_d (x_d - x'_d)^2 / l_d^2 )
and e_i independent Gaussian noise of standard deviation s. The d + 2 hyperparameters are the amplitude A,
one length scale l_d per input (anisotropic, "ARD") and the white noise s.

How the hyperparameters are chosen.  By maximising the log marginal likelihood
    ln L = -1/2 r^T K^-1 r - 1/2 ln det K - n/2 ln 2 pi,     K = k(X, X) + s^2 I
with Nelder-Mead in the logarithms of (A, l_1..l_d, s), started from A = std(r), l = the scatter of each
input, s = std(r) / 2, restarted once from the optimum, and with the logs clamped to a box (below).
Nothing is marginalised: these are point estimates, and the prediction's variance does not include their
uncertainty.

Cost.  One likelihood evaluation is one Cholesky factorisation: n^3 / 6 multiply-adds and n^2 doubles of
memory. Measured in this implementation (plain JS, one thread, Node 22 on an Apple M-series laptop,
test/gp.test.mjs prints it): 2 ms at n = 200, 27 ms at n = 500, 0.19 s at n = 1000; by the n^3 law about 1.5 s
at n = 2000 and 32 MB of memory. The optimisation needs 100-250 evaluations, so the hyperparameters are
fitted on at most `maxHyper` points (default 300, every k-th point), and the prediction conditions on at most
`maxCond` points (default 1200), costing one factorisation plus n_cond operations per predicted point. Beyond
those sizes the result is the "subset of data" approximation: exact for the subset, and good for smooth
trends in position as long as the subset samples the range of centroids.

No DOM. Checked against scikit-learn's GaussianProcessRegressor in test/gp.test.mjs.

Import: import { … } from './src/core/gp.js' — no DOM, runs in Node and the browser.

cholesky ​

js
function cholesky(A, n)

In-place Cholesky of the lower triangle of the n x n matrix A (row-major). Returns false if not positive definite.

gpLogLike ​

js
function gpLogLike(X, y, n, d, hyper)
text
Log marginal likelihood of the residuals y (length n) at inputs X (n x d, row-major) for hyper = { amp, scales, noise }.

gpPredict ​

js
function gpPredict(X, y, n, d, hyper, Xs, ns, withStd = false, withNoise = false)

Predictive mean (and, with withStd, standard deviation) at inputs Xs (ns x d), given the n training points. The standard deviation is that of the latent function g; withNoise adds the white noise s^2 to the variance, which is what scikit-learn returns when the kernel contains a WhiteKernel.

nelderMead ​

js
function nelderMead(f, x0, step = 0.7, maxEvals = 300, tol = 1e-5)

Nelder-Mead minimiser of f over R^m from x0 with initial step step. Returns { x, f, evals }.

gpFit ​

js
function gpFit(X, y, n, d, opts = {})

Choose the hyperparameters by maximum marginal likelihood (see the top of this file). Bounds on the logs: A in [1e-3, 1e3] std(y); l_d in [0.05, 100] times the scatter of input d; s in [1e-3, 10] std(y).

  • returns { hyper: { amp, scales, noise }, lnL, evals }

detrendWithCentroids ​

js
function detrendWithCentroids(inputs, resid, keep = null, opts = {})

Detrend residuals against the centroid.

  • param inputs array of d arrays (x centroid, y centroid[, time]), each of length N
  • param resid residual flux (data / model - 1 or data - model), length N
  • param keep optional Uint8Array: points to use (others are predicted but not fitted)
  • param opts { maxHyper = 300, maxCond = 1200, maxEvals = 250 }
  • returns { trend (length N: the GP mean at every point, plus the residual mean), hyper, lnL, nHyper, nCond, rmsBefore, rmsAfter (of the kept points), evals }

MIT licensed · Kyle A. Pearson