mecfs_bio.build_system.task.annotation_weights.chromosome_blocked_ridge
Weighted ridge regression accumulated per chromosome block.
Pure numpy: no polars, parquet, or Task imports. A ChromRidgeBlock holds the weighted cross-product sufficient statistics of one chromosome; combining blocks lets any leave-one-chromosome-out (or odd/even) fit be formed as a sum, so the full design matrix is never held in memory. Ridge is fit on centered, per-column standardized features and unstandardized back; weights of 1 recover ordinary ridge.
With features x_i (length p), target y_i and weights w_i, the fitted model is y_i ~ intercept + x_i . beta_raw, where beta minimizes
sum_i w_i (y_i - mean_y - z_i . beta_std)^2 + alpha ||beta_std||^2
over the standardized features z_ij = (x_ij - mean_j) / sd_j.
Classes:
-
AlphaSelection–The ridge penalty chosen by leave-one-chromosome-out cross-validation,
-
ChromRidgeBlock–Weighted cross-product sufficient statistics for one chromosome.
-
RidgeFit–A fitted ridge model: y ~ intercept + x . beta_raw.
-
StandardizedSystem–The weighted ridge system after centering and per-column standardization.
Functions:
-
accumulate_block–Compute one block's weighted sufficient statistics from an (n, p) feature
-
combine–Sum blocks into the block of their union (all fields are additive).
-
fit–Fit ridge with penalty alpha on the block and return raw-scale coefficients.
-
heldout_r2–Weighted R^2 on a held-out block of a model fit on a training set.
-
select_alpha_loco–Choose alpha maximizing the mean held-out weighted R^2 over
-
solve–Solve (g_std + alpha I) beta_std = b_std for beta_std.
-
standardized_system–Center and standardize a block's normal equations (see StandardizedSystem).
AlphaSelection
The ridge penalty chosen by leave-one-chromosome-out cross-validation, with its mean held-out weighted R^2, the per-held-out-chromosome R^2, and the mean held-out R^2 of every candidate penalty (the cross-validation curve).
Attributes:
ChromRidgeBlock
Weighted cross-product sufficient statistics for one chromosome.
For the SNPs i in one chromosome, let x_i be the length-p feature row, y_i the target, and w_i >= 0 the regression weight. Every field is a weighted sum over those SNPs:
sw = sum_i w_i scalar (total weight; = n when w == 1)
swx = sum_i w_i x_i length p (weighted feature sum)
swxx = sum_i w_i x_i x_i^T p x p (weighted Gram, X^T W X)
swxy = sum_i w_i x_i y_i length p (weighted feature-target, X^T W y)
swy = sum_i w_i y_i scalar
swyy = sum_i w_i y_i^2 scalar
These are exactly the statistics the weighted-ridge normal equations need, and every one is additive across chromosomes, so any leave-one-out or odd/even training set is formed by summing the relevant blocks (see combine).
Attributes:
RidgeFit
A fitted ridge model: y ~ intercept + x . beta_raw.
beta_std is the same coefficient vector on the standardized feature scale (beta_std = beta_raw * sd), comparable across features.
Attributes:
StandardizedSystem
The weighted ridge system after centering and per-column standardization.
Built from a ChromRidgeBlock. With the standardized feature z_ij = (x_ij - mean_j) / sd_j, the fields are:
mean length p weighted per-feature mean:
mean_j = (sum_i w_i x_ij) / (sum_i w_i) = swx_j / sw
sd length p weighted per-feature standard deviation (zeros replaced by 1):
sd_j = sqrt(swxx_jj / sw - mean_j^2)
mean_y scalar weighted mean of the target: mean_y = swy / sw
g_std p x p weighted Gram of the standardized features:
g_std[j,k] = sum_i w_i z_ij z_ik
= (swxx - sw * outer(mean, mean))[j,k] / (sd_j sd_k)
b_std length p weighted standardized feature-target cross term:
b_std[j] = sum_i w_i z_ij (y_i - mean_y)
= (swxy - mean * swy)[j] / sd_j
(the y-centering term vanishes because sum_i w_i z_ij = 0)
solve returns beta_std from (g_std + alpha I) beta_std = b_std; fit then unstandardizes: beta_raw = beta_std / sd, intercept = mean_y - beta_raw . mean.
Attributes:
accumulate_block
Compute one block's weighted sufficient statistics from an (n, p) feature matrix x, a length-n target y, and optional length-n weights w (default 1).
Source code in mecfs_bio/build_system/task/annotation_weights/chromosome_blocked_ridge.py
combine
Sum blocks into the block of their union (all fields are additive).
Source code in mecfs_bio/build_system/task/annotation_weights/chromosome_blocked_ridge.py
fit
Fit ridge with penalty alpha on the block and return raw-scale coefficients.
Source code in mecfs_bio/build_system/task/annotation_weights/chromosome_blocked_ridge.py
heldout_r2
heldout_r2(
held: ChromRidgeBlock,
beta_std: ndarray,
train_mean: ndarray,
train_sd: ndarray,
train_mean_y: float,
) -> float
Weighted R^2 on a held-out block of a model fit on a training set.
The training model predicts pred_i = c + z_i . beta_std, with c = train_mean_y and z_ij = (x_ij - m_j) / s_j using the TRAINING mean m and sd s. Over the held-out SNPs, from the held-out block's sufficient statistics alone:
SS_res = sum_i w_i (y_i - c - z_i . beta_std)^2
= sum_i w_i (y_i - c)^2 - 2 beta_std . r + beta_std^T Z beta_std
sum_i w_i (y_i - c)^2 = swyy - 2 c swy + sw c^2
r_j = sum_i w_i z_ij (y_i - c)
= (swxy_j - c swx_j - m_j swy + sw c m_j) / s_j
Z_jk = sum_i w_i z_ij z_ik
= (swxx - outer(m, swx) - outer(swx, m) + sw outer(m, m))[j,k] / (s_j s_k)
SS_tot = sum_i w_i (y_i - ybar)^2 = swyy - sw ybar^2, with ybar = swy / sw
and R^2 = 1 - SS_res / SS_tot. SS_res is taken about the training intercept c, while SS_tot uses the held-out block's own mean ybar: the R^2 null model is "predict the held-out mean".
Source code in mecfs_bio/build_system/task/annotation_weights/chromosome_blocked_ridge.py
select_alpha_loco
select_alpha_loco(
blocks_by_chrom: Mapping[int, ChromRidgeBlock],
alphas: Sequence[float],
) -> AlphaSelection
Choose alpha maximizing the mean held-out weighted R^2 over leave-one-chromosome-out folds of the given blocks.
Source code in mecfs_bio/build_system/task/annotation_weights/chromosome_blocked_ridge.py
solve
Solve (g_std + alpha I) beta_std = b_std for beta_std.
Source code in mecfs_bio/build_system/task/annotation_weights/chromosome_blocked_ridge.py
standardized_system
Center and standardize a block's normal equations (see StandardizedSystem).
The centered Gram follows from expanding the outer product: sum_i w_i (x_i - mean)(x_i - mean)^T = swxx - 2 sw outer(mean, mean) + sw outer(mean, mean) = swxx - sw outer(mean, mean).