structboost.compute_covariance_cache

structboost.compute_covariance_cache(sourcemat, *, out=None)[source]

Compute the predictor covariance matrix used by allboost.

This function computes X.T @ X (the Gram matrix), which allboost uses internally to update regression coefficients. Pre-computing this matrix can provide significant speedups when calling allboost multiple times on the same sourcemat (e.g., during cross-validation or hyperparameter tuning).

Note: The output matrix is O(p²) in memory, which can be substantial for high-dimensional data.

Parameters:
  • sourcemat (ndarray of shape (n_samples, n_features)) – Predictor matrix. Standardization is recommended but not required.

  • out (ndarray of shape (n_features, n_features), optional) – Pre-allocated output array. If provided, the result is written in-place. Must have dtype float64.

Returns:

covcache (ndarray of shape (n_features, n_features)) – The covariance/Gram matrix X.T @ X.

Return type:

ndarray[tuple[Any, …], dtype[floating]]

Examples

>>> import numpy as np
>>> from structboost import allboost, compute_covariance_cache
>>> rng = np.random.default_rng(42)
>>> X = rng.standard_normal((100, 50))
>>> X = (X - X.mean(axis=0)) / X.std(axis=0)
>>> covcache = compute_covariance_cache(X)
>>> # Reuse one cache across calls. Targets are (n_samples, n_targets).
>>> targets = rng.standard_normal((100, 3))
>>> beta1 = allboost(X, targets, covcache=covcache)
>>> beta2 = allboost(X, targets[:, :2], covcache=covcache)
>>> beta1.shape
(3, 50)