Ensemble Stability#
stability
#
| CLASS | DESCRIPTION |
|---|---|
EstimatorResult |
Results from a single estimator fit. |
SpectralStabilityEnsemble |
Ensemble-based stability assessment via perturbation analysis. |
Attributes#
Classes#
EstimatorResult
#
Bases: NamedTuple
Results from a single estimator fit.
| PARAMETER | DESCRIPTION |
|---|---|
eigenvalues
|
Eigenvalues from the estimator, shape
TYPE:
|
eigenvectors
|
Eigenvectors from the estimator, shape
TYPE:
|
kernel
|
Kernel matrix from the estimator, shape
TYPE:
|
SpectralStabilityEnsemble(estimator: ModelType, perturbation_type: Literal['noise', 'subsample', 'bootstrap'] = 'noise', n_trials: int = 10, noise_scale: float = 0.05, subsample_fraction: float = 0.8, metric: Literal['eigenvector_alignment', 'eigenvalue', 'kernel_frobenius', 'kernel_alignment'] = 'eigenvector_alignment', show_progress: bool = True, fit_manager: FitManager | None = None)
#
Bases: Ensemble
Ensemble-based stability assessment via perturbation analysis.
Evaluates robustness of spectral methods by fitting multiple perturbed versions of an estimator and comparing results. Useful for determining optimal number of components and validating embedding quality.
| PARAMETER | DESCRIPTION |
|---|---|
estimator
|
Estimator to analyze.
TYPE:
|
perturbation_type
|
Type of perturbation for stability testing.
TYPE:
|
n_trials
|
Number of perturbation trials to run.
TYPE:
|
noise_scale
|
Standard deviation of Gaussian noise (for
TYPE:
|
subsample_fraction
|
Fraction of data to retain in subsampling (for "subsample" and "bootstrap").
TYPE:
|
metric
|
Stability metric to compute.
TYPE:
|
show_progress
|
Whether to display a progress bar during stability analysis.
TYPE:
|
fit_manager
|
Manager for unified training of both Estimator and Parametric models. Required for using Parametric (PyTorch Lightning) models in stability analysis. See FitManager for configuration options.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
scores |
Stability scores from all trials, shape
TYPE:
|
results |
Results from each perturbed trial.
TYPE:
|
reference_result |
Result from original unperturbed data.
TYPE:
|
Examples:
Analyze stability of diffusion map under noise:
>>> from spectre.decomposition import DiffusionMap
>>> from spectre.kernel import GaussianKernel
>>> from spectre.pairwise_distance import PairwiseDistanceEuclidean
>>> from spectre.ensemble import SpectralStabilityEnsemble
>>>
>>> kernel_fn = GaussianKernel(bw_method="median")
>>> dm = DiffusionMap(
... kernel_fn=kernel_fn,
... distance_fn=PairwiseDistanceEuclidean(),
... out_features=10,
... )
>>>
>>> stability = SpectralStabilityEnsemble(
... estimator=dm,
... perturbation_type="noise",
... n_trials=20,
... noise_scale=0.05,
... )
Analyze stability with parametric model:
>>> from spectre.parametric import SpectralMap
>>> sm = SpectralMap(model=..., kernel_fn=..., ...)
>>> stability = SpectralStabilityEnsemble(
... estimator=sm,
... perturbation_type="bootstrap",
... n_trials=50,
... )
>>> stability.fit(X)
Compare stability across methods:
>>> dm_stability = SpectralStabilityEnsemble(dm, n_trials=30)
>>> pca_stability = SpectralStabilityEnsemble(pca, n_trials=30)
>>>
>>> dm_stability.fit(X)
>>> pca_stability.fit(X)
>>>
>>> print(f"DM stability: {dm_stability.score():.3f}")
>>> print(f"PCA stability: {pca_stability.score():.3f}")
| METHOD | DESCRIPTION |
|---|---|
fit |
Run stability analysis on input data. |
predict |
Return stability scores as prediction. |
score |
Return mean stability score. |
compute |
Compute stability metric between reference and perturbed results. |
Source code in spectre/ensemble/stability.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
Attributes#
scores: torch.Tensor
property
#
Stability scores from all trials.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Stability scores of shape |
results: list[EstimatorResult]
property
#
Results from each trial.
estimator_reference: ModelType
property
#
Result from original data.
Functions#
fit(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> SpectralStabilityEnsemble
#
Run stability analysis on input data.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape
TYPE:
|
weights
|
Sample weights of shape
TYPE:
|
target
|
Target values for supervised methods.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SpectralStabilityEnsemble
|
Returns self for method chaining. |
Source code in spectre/ensemble/stability.py
predict(X: torch.Tensor) -> torch.Tensor
#
Return stability scores as prediction.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data (not used, stability scores are independent of new data).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Stability scores of shape |
Source code in spectre/ensemble/stability.py
score(X: torch.Tensor | None = None, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> float
#
Return mean stability score.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Not used (stability is independent of new data).
TYPE:
|
weights
|
Not used.
TYPE:
|
target
|
Not used.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Mean stability score across all trials. Higher is more stable. |
Source code in spectre/ensemble/stability.py
compute(estimator_reference: ModelType, estimator_p: ModelType) -> EstimatorResult
#
Compute stability metric between reference and perturbed results.
| RETURNS | DESCRIPTION |
|---|---|
EstimatorResult
|
Results including stability score and estimator outputs if applicable. |