nirspipe.pipeline.ar_irls#

AR whitening with iteratively reweighted least squares.

An AR noise model divides out the residual’s autocorrelation but still fits every time point with equal weight, so a few seconds of residual motion move the beta as hard as a clean minute does. AR-IRLS alternates the two: estimate the AR filter from the current residual, whiten both sides, refit with a robust norm that down-weights the outlying samples, repeat.

Three quantities come out of it and all three reach the t value:

  • theta, the robust whitened estimate;

  • the robust scale, which replaces the residual mean square as the dispersion;

  • an effective degrees of freedom, because a fit that gave several hundred samples a weight near zero did not spend the sample count a residual df of n - p claims it did.

The solver returns an ordinary nilearn regression result, so contrasts, the tidy frame and the data-space residual all work exactly as they do under arN.

Module Contents#

Functions#

bic_ar_order

AR coefficients of resid, order chosen by BIC over 0..pmax.

satterthwaite_df

Satterthwaite effective residual df of a weighted fit.

whiten

AR-filter along the first axis, in nilearn’s own shift-and-subtract form.

fit_channel

Fit one channel by AR-IRLS and return nilearn’s regression result.

resolve_pmax

Largest AR order for an ar_irls spec: ar_irls follows auto’s 4x rule.

Data#

API#

nirspipe.pipeline.ar_irls.logger = 'get_logger(...)'#
nirspipe.pipeline.ar_irls.DEFAULT_TUNE = 4.685#
nirspipe.pipeline.ar_irls.DEFAULT_MAX_ITER = 10#
nirspipe.pipeline.ar_irls.BETA_TOL = 0.01#
nirspipe.pipeline.ar_irls.bic_ar_order(resid: numpy.ndarray, pmax: int) → numpy.ndarray#

AR coefficients of resid, order chosen by BIC over 0..pmax.

Levinson-Durbin gives every order’s coefficients and prediction-error variance from one pass over the autocovariance, so the whole order search costs one recursion rather than pmax separate fits.

\[\mathrm{BIC}(p) = n \log \sigma^2_p + p \log n\]

where \(\sigma^2_p\) is the order-\(p\) prediction error variance. The autocovariance is the biased one (divided by \(n\)), which makes the coefficients at a fixed order the Yule-Walker maximum-likelihood ones.

Parameters:
  • resid (ndarray, shape (n,)) -- Residual series to model.

  • pmax (int) -- Largest order considered.

Returns:

Coefficients of the selected order, empty when BIC picks 0.

Return type:

ndarray, shape (p,)

nirspipe.pipeline.ar_irls.satterthwaite_df(design: numpy.ndarray, weights: numpy.ndarray) → float#

Satterthwaite effective residual df of a weighted fit.

With \(H = \mathrm{diag}(w) - AMA^\top\), \(A = \mathrm{diag}(w)X\) and \(M = (A^\top A)^{+}\),

\[\mathrm{df} = \frac{\operatorname{tr}(H^\top H)^2}{\operatorname{tr}((H^\top H)^2)}\]

Building \(H\) costs \(n \times n\), which at fNIRS record lengths is tens of gigabytes, so every trace below is taken through the \(p \times p\) matrices and the per-row leverages instead. \(H\) is symmetric, so \(H^\top H = H^2\) and the two traces are of \(H^2\) and \(H^4\); expanding \((D - P)^k\) and collecting by cyclic equivalence leaves only \(\sum_i w_i^k q_i\), \(\operatorname{tr}((MB)^2)\) and \(\operatorname{tr}(P^k) = p\), where \(q_i\) is row \(i\)’s leverage in \(A\) and \(B = A^\top \mathrm{diag}(w) A\).

Parameters:
  • design (ndarray, shape (n, p)) -- The whitened design the robust fit ran on.

  • weights (ndarray, shape (n,)) -- Robust weights, in [0, 1].

Returns:

Effective residual degrees of freedom.

Return type:

float

nirspipe.pipeline.ar_irls.whiten(X: numpy.ndarray, rho: numpy.ndarray) → numpy.ndarray#

AR-filter along the first axis, in nilearn’s own shift-and-subtract form.

nirspipe.pipeline.ar_irls.fit_channel(y: numpy.ndarray, design: numpy.ndarray, pmax: int, tune: float = DEFAULT_TUNE, max_iter: int = DEFAULT_MAX_ITER)#

Fit one channel by AR-IRLS and return nilearn’s regression result.

Each pass estimates the AR filter from the residual in the data’s own space, whitens both sides with it, and refits with a bisquare robust norm. The loop stops when beta moves less than BETA_TOL in relative norm.

Parameters:
  • y (ndarray, shape (n,) or (n, 1)) -- One channel’s time course.

  • design (ndarray, shape (n, p)) -- Design matrix, unwhitened.

  • pmax (int) -- Largest AR order BIC may choose.

  • tune (float) -- Bisquare tuning constant; smaller is less sensitive to outliers and less efficient when there are none.

  • max_iter (int) -- Cap on the alternation.

Returns:

Whose theta is the AR-IRLS estimate, whose Y and model.design are unwhitened, and whose dispersion and df_residuals are the robust scale and the effective df rather than the residual mean square and n - p.

Return type:

nilearn RegressionResults

nirspipe.pipeline.ar_irls.resolve_pmax(spec: str, sfreq: float) → int#

Largest AR order for an ar_irls spec: ar_irls follows auto’s 4x rule.

resolve_pmax(“ar_irls”, 7.81) -> 31; resolve_pmax(“ar_irls40”, 7.81) -> 40