betting_combat.ml.importance
Feature importance the López way (MLAM ch. 2, 4 and 6; AFML ch. 8): one model on every candidate, judged by what its out-of-sample loss loses when a CLUSTER of features is scrambled.
denoise_corr the correlation matrix with the eigenvalues the Marcenko-Pastur law explains
as noise replaced by their mean (the constant residual eigenvalue method,
MLAM 2.5-2.6): the clusters are found on signal, not on sampling noise
onc optimal number of clusters (MLAM 4.1-4.2): k-means on the distance
sqrt((1 - rho) / 2), k chosen by the silhouette’s mean / std, clusters of
below-average quality re-clustered while that improves them (one such
cluster too: the book stops at one, see onc)
clustered_mda mean decrease accuracy per cluster (MLAM 6.5): per fold, the fitted model’s
test log loss with every column of one cluster shuffled against the loss as
is, (L_shuffled - L) / L_shuffled; mean and standard error over the folds.
A substitute feature cannot hide its cluster’s information: the whole
cluster goes at once
The model, its folds (purged by the caller) and its prior are the caller’s: clustered_mda
only fits through fit and scores through predict.
Functions
clustered_mda
clustered_mda(X: pd.DataFrame, y: np.ndarray, folds: Sequence[tuple[np.ndarray, np.ndarray]], clusters: dict[int, list[str]], fit: Callable[[np.ndarray], Any], predict: Callable[[Any, pd.DataFrame, np.ndarray], np.ndarray], labels: Sequence[int] | None = None, weights: np.ndarray | None = None, seed: int = 0) -> pd.DataFramePer cluster: mean and standard error over folds of (L_shuffled - L) / L_shuffled, L the
fold’s test log loss (MLAM 6.5). folds: positional (train, test) arrays into X;
fit(train) returns the fitted model; predict(model, X_test, test) its probabilities
(test lets the caller attach a prior); weights: each row’s weight in the loss (López’s
uniqueness, AFML 4.4). Every column of a cluster is shuffled, each on
its own permutation of the test rows; the model is never refitted. Also per fold: the base
loss, for the record.
denoise_corr
denoise_corr(corr: pd.DataFrame, n_obs: int) -> tuple[pd.DataFrame, int](the denoised correlation, the number of signal factors): eigenvalues above the Marcenko-Pastur edge kept, the rest set to their mean (trace kept), unit diagonal.
mp_max_eigenvalue
mp_max_eigenvalue(evals: np.ndarray, q: float, bandwidth: float = 0.01) -> tuple[float, float](the largest eigenvalue noise explains, the fitted noise variance): the Marcenko-Pastur variance whose density fits the eigenvalues’ kernel density best (MLAM 2.4).
onc
onc(corr: pd.DataFrame, max_k: int | None = None, n_init: int = 10, seed: int = 0, depth: int = 0) -> tuple[dict[int, list[str]], pd.Series]MLAM 4.2 (clusterKMeansTop): the base partition, then the clusters whose quality (silhouette mean / std inside the cluster) is below the clusters’ average re-clustered on their own, the result kept when its mean quality is better. Returns (cluster -> features, silhouette per feature).