coco_pipe.diagnostics.variance#
Model-free subject and label variance diagnostics for embeddings.
Functions#
|
Return separate marginal label and subject eta-squared diagnostics. |
|
Return the additive subject-within-label ANOVA decomposition. |
Compute design-appropriate omega squared for a nested decomposition. |
|
|
Estimate hierarchy-preserving permutation nulls for marginal effects. |
|
Cross-validate subject identity, optionally holding out whole blocks. |
|
How much of each subject direction of a fitted eraser each trait explains. |
|
Return design-aware subject and label diagnostics for an embedding. |
Module Contents#
- coco_pipe.diagnostics.variance.crossed_ss_fractions(features, subject, label)#
Return separate marginal label and subject eta-squared diagnostics.
These effects may overlap and must not be added or subtracted to obtain a residual. Use
variance_decomposition_report()for an additive design-aware partition.
- coco_pipe.diagnostics.variance.nested_ss(features, subject, label)#
Return the additive subject-within-label ANOVA decomposition.
- Return type:
dict[str, numpy.ndarray | int]
- coco_pipe.diagnostics.variance.omega_squared_from_ss(ss)#
Compute design-appropriate omega squared for a nested decomposition.
The label effect is estimated from subject means so recordings from the same subject are not treated as independent replicates. Subject-within-label is reported as partial omega squared against the recording-level residual.
- coco_pipe.diagnostics.variance.null_control(features, subject, label, *, n_null_permutations=200, rng=None)#
Estimate hierarchy-preserving permutation nulls for marginal effects.
- Parameters:
n_null_permutations (int)
rng (numpy.random.Generator | None)
- Return type:
- coco_pipe.diagnostics.variance.subject_probe(features, subject, *, kind='linear', cap=100, n_splits=5, seed=42, blocks=None)#
Cross-validate subject identity, optionally holding out whole blocks.
- coco_pipe.diagnostics.variance.direction_traits(eraser, features, subject, traits, *, n_directions=None, condition=None)#
How much of each subject direction of a fitted eraser each trait explains.
Each row of
featuresis scored on the directions oferaser(a fittedLeaceEraser, largest subject eta^2 first), and the scores are averaged per subject. For each direction and trait,r2is the share of the variance of these subject means that the trait explains: the squared correlation for a numeric trait, eta^2 across its levels for any other. A trait that explains much of an erased direction is erased with it. Subjects missing a trait are left out of its rows.- Parameters:
eraser (coco_pipe.transforms.LeaceEraser) – The fitted eraser.
features (array-like of shape (n_samples, n_features)) – The vectors, e.g. the ones the eraser was fitted on.
subject (array-like of shape (n_samples,)) – The subject of each row, as in the index of
traits.traits (DataFrame) – One row per subject, one column per trait, e.g. age, sex or diagnosis.
n_directions (int, optional) – Number of directions, largest eta^2 first. Defaults to the erased ones.
condition (array-like of shape (n_samples,), optional) – The condition of each row. The scores are then centered on their condition’s mean before averaging, as in a fit with
condition.
- Returns:
One row per direction and trait:
direction(0 has the largest eta^2),eta2,erased,trait,r2andn_subjects.- Return type:
DataFrame
- coco_pipe.diagnostics.variance.variance_decomposition_report(features, subject=None, label=None, *, feature_scaling='zscore', n_null_permutations=200, rng=None, probe_blocks=None, probe_cap=100, probe_n_splits=5, probe_seed=42)#
Return design-aware subject and label diagnostics for an embedding.
Pure-label subjects use an additive subject-within-label decomposition. When labels vary within subject, an adjusted additive fixed-effects model reports unique label, unique subject, shared/confounded, and residual SS.
- Parameters:
features (array-like or DataContainer) – Embedding observations. Containers are flattened with
obsas rows.subject (array-like or str, optional) – Observation-aligned vectors, or explicit coordinate names for a
DataContainer.container.yis the only implicit label source.label (array-like or str, optional) – Observation-aligned vectors, or explicit coordinate names for a
DataContainer.container.yis the only implicit label source.feature_scaling ({"none", "zscore"}, default="zscore") – Z-scoring makes the pooled SS partition invariant to feature units. Constant features are retained as zeros and counted in the output.
n_null_permutations (int, default=200) – Number of hierarchy-preserving permutations used for marginal nulls.
rng (numpy.random.Generator, optional) – Permutation generator. A deterministic generator is used by default.
probe_blocks (array-like or str, optional) – Acquisition/session block identifier. When supplied, subject-probe CV holds out complete subject-block combinations.
probe_cap (int) – Subject-probe sampling and cross-validation settings.
probe_n_splits (int) – Subject-probe sampling and cross-validation settings.
probe_seed (int) – Subject-probe sampling and cross-validation settings.
- Returns:
One row per metric with design, scaling, null, and probe metadata. Interpretive caveats are stored in
report.attrs["notes"].- Return type: