From 3ecb48057afa2dd805f7214166340af987af9b0e Mon Sep 17 00:00:00 2001 From: 0xfdf <0xfdf@implies.com> Date: Tue, 13 Aug 2024 00:01:50 -0400 Subject: [PATCH] fix merge --- toraniko/math.py | 53 ------------------------------------------------ 1 file changed, 53 deletions(-) diff --git a/toraniko/math.py b/toraniko/math.py index fe3868a..dd1ad2d 100644 --- a/toraniko/math.py +++ b/toraniko/math.py @@ -238,56 +238,3 @@ def ledoit_wolf_shrinkage(X: np.ndarray) -> tuple[float | int, np.ndarray]: shrunk_cov = (1 - shrinkage) * sample_cov + shrinkage * mu * np.eye(m) return shrinkage, shrunk_cov - - -# TODO: test -def stfu_shrinkage(X: np.ndarray) -> tuple[tuple[float | int, ...], np.ndarray]: - """Estimate the covariance matrix of `X` via Specializing the Target to Features and Unlabeled shrinkage (SFTU). - - Parameters - ---------- - X : array-like input data matrix for which to estimate covariance, having shape (n_samples, m_features) - - Returns - ------- - shrinkage: float estimated shrinkage parameter. - shrunk_cov: numpy ndarray estimated shrunk covariance matrix having shape (n_features, n_features) - """ - n, m = X.shape - - # Center the data - X = X - X.mean(axis=0) - - # Sample covariance - sample_cov = np.dot(X.T, X) / n - - # Diagonal of sample covariance - sample_var = np.diag(sample_cov) - - # Mean of sample variances - mean_var = np.mean(sample_var) - - # Estimate of tr(sigma^2) - tr_sigma2 = np.sum(sample_cov**2) - - # Estimate of tr(sigma^4) - tr_sigma4 = np.sum((X.T @ X / n) ** 2) - - # Estimate lambda_star (shrinkage towards diagonal matrix) - lambda_star = (tr_sigma2 - np.sum(sample_var**2)) / ( - (n - 1) * (tr_sigma2 - 2 * np.sum(sample_var**2) + m * mean_var**2) - ) - lambda_star = max(0, min(1, lambda_star)) - - # Estimate mu_star (shrinkage towards scalar matrix) - mu_star = (tr_sigma2 - m * mean_var**2) / ((n - 1) * (tr_sigma4 - tr_sigma2**2 / m)) - mu_star = max(0, min(1, mu_star)) - - # Compute shrunk covariance matrix - shrunk_cov = ( - (1 - lambda_star) * sample_cov - + lambda_star * np.diag(sample_var) - + lambda_star * mu_star * (mean_var - np.diag(sample_var)) - ) - - return (lambda_star, mu_star), shrunk_cov