| SOCR ≫ | DSPA ≫ | DSPA2 Topics ≫ |
This notebook implements a novel statistical commutator framework for regularization parameter selection in penalized regression models. The approach minimizes the misalignment between data-driven and penalty-driven influence operators, providing a principled alternative to cross-validation.
Let \(X\in\mathbb{R}^{n\times p}\) and \(y\in\mathbb{R}^n\). Define the empirical curvature \(A := \tfrac{1}{n}X^\top X\) and a symmetric positive semidefinite (PSD) penalty \(P\succeq 0\). For \(\lambda>0\) set \[H_\lambda := A + \lambda P,\qquad T_A(\lambda) := H_\lambda^{-1} A,\qquad T_P(\lambda) := H_\lambda^{-1} P.\]
We aim to avoid degeneracy of the commutator, we define \(S_{\mathrm{data}}(\lambda):=H_\lambda^{-1}A\) and \(S_{\mathrm{pen}}(\lambda):=H_\lambda^{-1}(\lambda P)\), then \(S_{\mathrm{data}}(\lambda)+S_{\mathrm{pen}}(\lambda)=I_p\) and \([S_{\mathrm{data}}(\lambda),S_{\mathrm{pen}}(\lambda)]=0\), for all \(\lambda\), which renders the commutator-minimization objective ill-posed.
\[C_A(\lambda) \;:=\; [A,\,T_P(\lambda)] = A\,H_\lambda^{-1}P - H_\lambda^{-1}PA.\]
Define the (squared) misalignment and a balance factor by \[M_A(\lambda) := \|C_A(\lambda)\|_F^2, \qquad B(\lambda) := \mathrm{tr}\big(T_A(\lambda)\big)\Big(p-\mathrm{tr}\big(T_A(\lambda)\big)\Big).\]
The balanced interior-optimum objective is \[\underbrace{J_A(\lambda)}_{loss} := \underbrace{M_A(\lambda)}_{misalignment}\ \underbrace{B(\lambda)}_{balance}, \qquad \lambda_A^\star \;\in\; \arg\max_{\lambda>0}\, J_A(\lambda).\]
\[C_P(\lambda) := [P,\,T_A(\lambda)] \;=\; P\,H_\lambda^{-1}A - H_\lambda^{-1}A P,\]
\[M_P(\lambda) \;:=\; \|C_P(\lambda)\|_F^2,\qquad J_P(\lambda) \;:=\; M_P(\lambda)\,B(\lambda), \qquad \lambda_P^\star \;\in\; \arg\max_{\lambda>0}\, J_P(\lambda).\]
Note: We utilize the balance function
\[B(\lambda) = \mathrm{tr}\big(T_A(\lambda)\big)\Big(p-\mathrm{tr}\big(T_A(\lambda)\big)\Big)\] since as \(\lambda\!\downarrow\!0\) or \(\lambda\!\uparrow\!\infty\), either \(T_A(\lambda)\) or \(I_p-T_A(\lambda)\) dominates, making \(B(\lambda)=\mathrm{tr}(T_A)(p-\mathrm{tr}(T_A))\) vanish at both extremes and promoting an interior maximizer for well-posed problems.
In both commutator cases, \(C_A(\lambda) =\; [A,\,T_P(\lambda)] = A\,H_\lambda^{-1}P - H_\lambda^{-1}PA\) and \(C_P(\lambda) := [P,\,T_A(\lambda)] = P\,H_\lambda^{-1}A - H_\lambda^{-1}A P\), to maximize the balanced commutator score (loss function), \(J_X, X\in\{A,P\}\), we use standard optimizaiton routines to we minimize the reciprocal loss
\[\widetilde{J}_X(\lambda) \;:=\; \frac{1}{J_X(\lambda)+\varepsilon} \quad (\ \forall\ X\in\{A,P\},\;\varepsilon>0 \text{ small}\ ),\] which is equivalent to maximizing \(J_X(\lambda)\).
Traditional ridge regression suffers from degeneracy when using spherical penalties, \(P = I\), as the Fisher information and risk Hessian commute trivially (due to their linear association)
\[[I_\lambda, J_\lambda] = 0, \quad \text{where} \quad I_\lambda = \frac{X^\top X}{\sigma^2 n} + \lambda I, \quad J_\lambda = \frac{X^\top X}{n} + \lambda I.\]
To overcome this commutator degeneracy, \([I_\lambda, J_\lambda] = 0\), we use \(A = X^\top X/n\) and a non‑spherical penalty, \(P\), e.g., a 1‑D difference Laplacian \(P=D^\top D\) if the features are ordered, or alternatively a graph Laplacian, and define influence operators
\[\begin{aligned} (\text{penalized operator})\quad H_\lambda &= \underbrace{A}_{data\\ fidelity} + \lambda \underbrace{P}_{penalty\\ regularizer}, \\ (\text{data-driven influence operator})\quad S_{\text{data}}(\lambda) &= H_\lambda^{-1} A, \\ (\text{penalty-driven influence operator})\quad S_{\text{pen}}(\lambda) &= H_\lambda^{-1} (\lambda P), \\ (\text{commutator})\quad \mathcal{C}(\lambda) &= \left\|\left[S_{\text{data}}(\lambda), S_{\text{pen}}(\lambda)\right]\right\|_F^2 \ . \end{aligned}\]
Because of the two appearances of \(H_\lambda^{-1}\) in the data- and penalty-driven influence operators, the commutator \(\mathcal{C}(\lambda) = \big|[S_{\text{data}}(\lambda),S_{\text{pen}}(\lambda)] \big|_F^2\) is nonlinear in \(\lambda\) and non‑trivial even when \(P=I\) in some problems, and certainly non‑trivial when \(P\neq I\). Minimizing \(\mathcal{C}(\lambda)\) chooses \(\lambda\) where the data‑driven and penalty‑driven influence operators are most aligned; it operationalizes the commutator minimization story in the revised paper while avoiding the ridge degeneracy.
This also has geometric interpretation that mirrors the classical/quantum two‑step optimization of Braunstein-Caves; work with a metric/influence object then optimize the distinguishing power, where \(H_\lambda^{-1}\) plays the role of a measurement‑optimized transport/information operator for the penalized model.
Minimizing \(\mathcal{C}(\lambda)\) selects the regularization parameter that optimally balances data fidelity and structural prior information, operationalizing the information-geometric principle of operator alignment.
# Stable matrix operations
stable_solve_spd <- function(M, b = NULL, jitter = 1e-10) {
Mj <- forceSymmetric(M) + jitter * Diagonal(nrow(M))
cholM <- tryCatch(Cholesky(Mj, LDL = FALSE), error = function(e) NULL)
if (is.null(cholM)) {
return(if (is.null(b)) solve(as.matrix(Mj)) else solve(as.matrix(Mj), b))
}
if (is.null(b)) return(solve(cholM, Diagonal(nrow(Mj))))
solve(cholM, b)
}
frob_sq <- function(M) {
Matrix::norm(M, "F")^2
}
# Non-spherical penalty construction
make_diff_penalty <- function(p, order = 1) {
D <- Matrix(0, nrow = p - 1, ncol = p, sparse = TRUE)
for (i in 1:(p - 1)) {
D[i, i] <- -1; D[i, i + 1] <- 1
}
if (order == 1) {
return(t(D) %*% D)
} else {
P <- t(D) %*% D
for (k in 2:order) P <- t(D) %*% (D %*% P)
return(P)
}
}
# # Proper influence operator commutator
# Sdata(λ)=Hλ−1A and Spen(λ)=Hλ−1(λP) with Hλ=A+λP. Then,
# Sdata+Spen=I, so [Sdata,Spen]≡0 for all λ (trivial objective).
# This revised function instead optimizes ||[A, Hλ−1P]||_F^2,
# or ||[P, Hλ−1A]||_F^2, multiplied by a “balance” term that kills the spurious edge optima.
proper_influence_commutator <- function(lambda, X, Y, P,
mode = c("A_HinvP","P_HinvA"),
use_weights = TRUE,
jitter = 1e-10,
eps = 1e-12) {
mode <- match.arg(mode)
n <- nrow(X); p <- ncol(X)
# (0) Unweighted curvature and ridge fit for residuals
A0 <- crossprod(X) / n
H0 <- A0 + lambda * P
b0 <- crossprod(X, Y) / n
beta_hat <- stable_solve_spd(H0, b0, jitter = jitter)
# (1) Optional residual-based weights (robust curvature)
if (use_weights) {
r <- as.vector(Y - X %*% beta_hat)
sigma2 <- sum(r^2) / max(n - p, 1)
sigma2 <- max(sigma2, 1e-8)
w <- 1 / (1 + abs(r) / sqrt(sigma2))
W <- Diagonal(n = n, x = w)
A <- crossprod(X, W %*% X) / n
} else {
A <- A0
}
# (2) Final Hessian
H <- A + lambda * P
# (3) λ-dependent influence maps (no degeneracy here)
HinvA <- stable_solve_spd(H, A, jitter = jitter) # T_A(λ) = H^{-1}A
HinvP <- stable_solve_spd(H, P, jitter = jitter) # T_P(λ) = H^{-1}P (no λ!)
eff_df <- sum(diag(HinvA)) # tr(T_A)
# (4) Non-trivial commutator
if (mode == "A_HinvP") {
C <- A %*% HinvP - HinvP %*% A # [A, H^{-1}P]
} else {
C <- P %*% HinvA - HinvA %*% P # [P, H^{-1}A]
}
mis_sq <- Matrix::norm(C, "F")^2 # ||[·,·]||_F^2
# (5) Balance factor—0 at extremes, encourages interior λ
balance <- eff_df * (p - eff_df)
# (6) Final score: we want to MAXIMIZE (mis_sq * balance)
# To keep caller logic (which.min) unchanged, return its inverse.
score_balanced <- mis_sq * balance
objective <- 1 / (score_balanced + eps) # MINIMIZE this
list(
commutator = objective, # objective to MINIMIZE (inverse balanced score)
score_balanced = score_balanced, # raw score to MAXIMIZE (for diagnostics if you like)
misalignment_sq = mis_sq,
eff_df = eff_df,
beta_hat = beta_hat,
mode = mode
)
}
# Core bootstrap function
proper_bootstrap <- function(X, Y, lambda_grid, P,
n_boot = 100, train_frac = 0.65,
mode = c("A_HinvP","P_HinvA"),
use_weights = TRUE,
rescale_P_by_A = TRUE,
oob_min = 10,
jitter = 1e-10, eps = 1e-12) {
mode <- match.arg(mode)
n <- nrow(X); p <- ncol(X)
lambda_comm_boot <- rep(NA_real_, n_boot)
lambda_mse_boot <- rep(NA_real_, n_boot)
valid_boots <- logical(n_boot)
pb <- txtProgressBar(min = 0, max = n_boot, style = 3)
on.exit(close(pb), add = TRUE)
for (b in seq_len(n_boot)) {
# Bootstrap training indices
n_tr <- max(1L, round(train_frac * n))
idx_tr <- sample.int(n, n_tr, replace = TRUE)
idx_tr <- sort(idx_tr)
idx_oob <- setdiff(seq_len(n), unique(idx_tr))
Xtr <- X[idx_tr, , drop = FALSE]
Ytr <- Y[idx_tr]
Xoob <- if (length(idx_oob) > 0) X[idx_oob, , drop = FALSE] else NULL
Yoob <- if (length(idx_oob) > 0) Y[idx_oob] else NULL
# Optional: rescale P to Frobenius norm of A_tr
P_b <- P
if (rescale_P_by_A) {
A_tr <- crossprod(Xtr) / nrow(Xtr)
nf <- Matrix::norm(A_tr, "F") / max(Matrix::norm(P_b, "F"), 1e-12)
P_b <- P_b * nf
}
comm_vals <- rep(NA_real_, length(lambda_grid))
oob_errs <- rep(NA_real_, length(lambda_grid))
for (i in seq_along(lambda_grid)) {
lam <- lambda_grid[i]
res <- tryCatch(
# proper_influence_commutator(lam, Xtr, Ytr, P_b,
# mode = mode,
# use_weights = use_weights,
# jitter = jitter, eps = eps),
# For [A, H^{-1}P] ###############################################
proper_influence_commutator(lam, Xtr, Ytr, P, mode = "A_HinvP"),
# # For [P, H^{-1}A] ############################################
# proper_influence_commutator(lam, Xtr, Ytr, P, mode = "P_HinvA"),
# # Bootstrap (OOB MSE now)
# boot <- proper_bootstrap(X, Y, lambda_grid_boot, P, n_boot = 100, mode = "A_HinvP")
error = function(e) NULL
)
if (is.null(res) || any(is.na(res$beta_hat))) next
# Commutator objective (inverse balanced score) -> minimize
comm_vals[i] <- res$commutator
# OOB MSE as the reference criterion (-> minimize)
if (length(idx_oob) >= oob_min) {
pred_oob <- as.vector(Xoob %*% res$beta_hat)
oob_errs[i] <- mean((Yoob - pred_oob)^2)
}
}
ok_comm <- which(is.finite(comm_vals))
ok_oob <- which(is.finite(oob_errs))
if (length(ok_comm) > 0) {
lambda_comm_boot[b] <- lambda_grid[ ok_comm[ which.min(comm_vals[ok_comm]) ] ]
}
if (length(ok_oob) > 0) {
lambda_mse_boot[b] <- lambda_grid[ ok_oob[ which.min(oob_errs[ok_oob]) ] ]
}
valid_boots[b] <- (length(ok_comm) > 0) && (length(ok_oob) > 0)
setTxtProgressBar(pb, b)
}
list(
lambda_comm = lambda_comm_boot, # from commutator objective (minimize inverse score)
lambda_mse = lambda_mse_boot, # from OOB MSE (minimize)
valid_boots = which(valid_boots)
)
}This experiment involves a linear regression model with structured coefficient sparsity and correlated predictors. Let \(n = 150\) and \(p = 60\). The design matrix \(\mathbf{X} \in \mathbb{R}^{n \times p}\) is generated from a multivariate normal distribution \(\mathcal{N}(\mathbf{0}, \boldsymbol{\Sigma}_X)\), where \(\boldsymbol{\Sigma}_X\) has Toeplitz correlation structure \([\boldsymbol{\Sigma}_X]_{ij} = \rho^{|i-j|}\) with \(\rho = 0.4\), and diagonal entries scaled to \(1.2\) to improve conditioning. The true coefficient vector \(\boldsymbol{\beta}_{\text{true}}\) exhibits piecewise-constant structure
\[\beta_j = \begin{cases} \text{linear decay from } 2 \text{ to } 0.5 & j \in \{10,\dots,20\},\\ 1.5 & j \in \{30,\dots,35\},\\ \text{linear increase from } 0 \text{ to } 2 & j \in \{45,\dots,50\},\\ 0 & \text{otherwise}. \end{cases}\]
The responses are generated as \(\mathbf{y} = \mathbf{X} \boldsymbol{\beta}_{\text{true}} + \boldsymbol{\varepsilon}\), where \(\boldsymbol{\varepsilon} \sim \mathcal{N}(\mathbf{0}, \sigma^2 \mathbf{I})\) with \(\sigma = 1.2\).
To break spherical symmetry and enable a non-degenerate commutator, we employ a first-order difference (fused-ridge) penalty matrix \(\mathbf{P} = \mathbf{D}^\top \mathbf{D}\), where \(\mathbf{D} \in \mathbb{R}^{(p-1) \times p}\) is the discrete first-difference operator. \(\mathbf{P}\) is scaled to match the Frobenius norm of the empirical curvature \(\mathbf{A} = \mathbf{X}^\top \mathbf{X}/n\), ensuring comparable magnitudes. Non-commutativity is verified by \(\|[\mathbf{A}, \mathbf{P}]\|_F = 7.104 > 0\).
The regularization path is tuned via the balanced commutator objective \[J_A(\lambda) = \underbrace{\big\| [\mathbf{A},\, \mathbf{H}_\lambda^{-1} \mathbf{P}] \big\|_F^2}_{\text{misalignment}} \times \underbrace{\operatorname{tr}(\mathbf{H}_\lambda^{-1} \mathbf{A}) \big(p - \operatorname{tr}(\mathbf{H}_\lambda^{-1} \mathbf{A})\big)}_{\text{balance}}, \quad \mathbf{H}_\lambda = \mathbf{A} + \lambda \mathbf{P}.\]
The balance term vanishes as \(\lambda \to 0\) or \(\lambda \to \infty\), promoting an interior maximizer. In practice, we minimize the reciprocal objective \(1/(J_A(\lambda) + \varepsilon)\). The commutator-optimal \(\lambda^\star_{\text{comm}}\) is compared against a held-out test MSE optimum \(\lambda^\star_{\text{MSE}}\) on a 30% test set. Bootstrap stability is assessed over 100 resamples using out-of-bag MSE as the reference criterion.
set.seed(123)
n <- 150
p <- 60
rho <- 0.4
# Generate correlated features
Sigma_X <- toeplitz(rho^(0:(p-1)))
diag(Sigma_X) <- 1.2
X <- mvrnorm(n, mu = rep(0, p), Sigma = Sigma_X)
# True coefficient pattern (structured sparsity)
beta_true <- numeric(p)
beta_true[10:20] <- seq(2, 0.5, length.out = 11)
beta_true[30:35] <- 1.5
beta_true[45:50] <- seq(0, 2, length.out = 6)
sigma_noise <- 1.2
Y <- X %*% beta_true + rnorm(n, 0, sigma_noise)
# Create and scale non-spherical penalty matrix
P <- make_diff_penalty(p, order = 1)
A <- crossprod(X) / n
P <- P / max(1e-12, Matrix::norm(P, "F")) * Matrix::norm(A, "F")
# Verify non-commutativity
comm_AP <- A %*% P - P %*% A
frob_AP <- Matrix::norm(comm_AP, "F")
cat("Non-commutativity verification:\n")## Non-commutativity verification:
## ||[X'X/n, P]||_F = 7.104 (should be > 0)
## Penalty matrix rank: 59 / 60
lambda_grid <- exp(seq(-5, 2, length.out = 80))
# Train-test split
idx_train <- sample(1:n, round(0.7 * n))
Xtr <- X[idx_train, , drop = FALSE]
Ytr <- Y[idx_train]
Xte <- X[-idx_train, , drop = FALSE]
Yte <- Y[-idx_train]
A_tr <- crossprod(Xtr) / nrow(Xtr)
# Initialize storage
comm_vals <- numeric(length(lambda_grid))
mse_te_vals <- numeric(length(lambda_grid))
eff_df_vals <- numeric(length(lambda_grid))
# If we need to plot the raw (maximized) score, use
# score_balanced_vals <- numeric(length(lambda_grid))
# score_balanced_vals[i] <- result$score_balanced
cat("Computing commutator landscape...\n")## Computing commutator landscape...
## | | | 0%
for (i in seq_along(lambda_grid)) {
lam <- lambda_grid[i]
# Compute commutator using proper influence formulation
# result <- proper_influence_commutator(lam, Xtr, Ytr, P)
# For [A, H^{-1}P] ###############################################
result <- proper_influence_commutator(lam, Xtr, Ytr, P, mode = "A_HinvP")
# # For [P, H^{-1}A] ############################################
# result <- proper_influence_commutator(lam, Xtr, Ytr, P, mode = "P_HinvA")
# # Bootstrap (OOB MSE now)
# boot <- proper_bootstrap(X, Y, lambda_grid_boot, P, n_boot = 100, mode = "A_HinvP")
comm_vals[i] <- result$commutator
eff_df_vals[i] <- result$eff_df
# If we need to plot the raw (maximized) score, use
# score_balanced_vals <- numeric(length(lambda_grid))
# score_balanced_vals[i] <- result$score_balanced
# Test MSE
pred_te <- as.vector(Xte %*% result$beta_hat)
mse_te_vals[i] <- mean((Yte - pred_te)^2)
setTxtProgressBar(pb, i)
}## | |= | 1% | |== | 2% | |=== | 4% | |==== | 5% | |==== | 6% | |===== | 8% | |====== | 9% | |======= | 10% | |======== | 11% | |========= | 12% | |========== | 14% | |========== | 15% | |=========== | 16% | |============ | 18% | |============= | 19% | |============== | 20% | |=============== | 21% | |================ | 22% | |================= | 24% | |================== | 25% | |================== | 26% | |=================== | 28% | |==================== | 29% | |===================== | 30% | |====================== | 31% | |======================= | 32% | |======================== | 34% | |======================== | 35% | |========================= | 36% | |========================== | 38% | |=========================== | 39% | |============================ | 40% | |============================= | 41% | |============================== | 42% | |=============================== | 44% | |================================ | 45% | |================================ | 46% | |================================= | 48% | |================================== | 49% | |=================================== | 50% | |==================================== | 51% | |===================================== | 52% | |====================================== | 54% | |====================================== | 55% | |======================================= | 56% | |======================================== | 58% | |========================================= | 59% | |========================================== | 60% | |=========================================== | 61% | |============================================ | 62% | |============================================= | 64% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 68% | |================================================ | 69% | |================================================= | 70% | |================================================== | 71% | |=================================================== | 72% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 76% | |====================================================== | 78% | |======================================================= | 79% | |======================================================== | 80% | |========================================================= | 81% | |========================================================== | 82% | |=========================================================== | 84% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 88% | |============================================================== | 89% | |=============================================================== | 90% | |================================================================ | 91% | |================================================================= | 92% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 100%
# Find optimal lambda values
lambda_opt_comm <- lambda_grid[which.min(comm_vals)]
lambda_opt_mse <- lambda_grid[which.min(mse_te_vals)]
# If we need to plot the raw (maximized) score, use
# max_score <- result$score_balanced.
cat("\nOptimal regularization parameters:\n")##
## Optimal regularization parameters:
## Commutator criterion: λ = 0.023296
## Test MSE criterion: λ = 0.051714
## Log ratio: -0.797
results_df <- data.frame(
lambda = lambda_grid,
log_lambda = log(lambda_grid),
log_comm = log(pmax(comm_vals, 1e-300)),
log_mse = log(pmax(mse_te_vals, 1e-300)),
eff_df = eff_df_vals
)
# To visualize the true score we maximized, plot log(score_balanced) instead of log(commutator);
# the latter is the inverse that we minimized only to keep our which.min() code intact
# Main comparison plot
p_main <- plot_ly(results_df) %>%
add_trace(x = ~log_lambda, y = ~log_comm,
type = 'scatter', mode = 'lines+markers',
name = 'log ||[S_data,S_pen]||²',
line = list(width = 3, color = '#1f77b4'),
marker = list(size = 4)) %>%
add_trace(x = ~log_lambda, y = ~log_mse,
type = 'scatter', mode = 'lines',
name = 'log Test MSE', yaxis = 'y2',
line = list(width = 3, color = '#ff7f0e')) %>%
add_trace(x = rep(log(lambda_opt_comm), 2),
y = range(results_df$log_comm, na.rm = TRUE),
type = 'scatter', mode = 'lines',
line = list(dash = 'dash', color = '#1f77b4', width = 2),
name = 'λ* (Commutator)') %>%
add_trace(x = rep(log(lambda_opt_mse), 2),
y = range(results_df$log_mse, na.rm = TRUE),
type = 'scatter', mode = 'lines',
line = list(dash = 'dot', color = '#ff7f0e', width = 2),
name = 'λ* (Test MSE)', yaxis = 'y2') %>%
layout(
title = 'Influence Operator Commutator vs Test Performance',
xaxis = list(title = 'log(λ)'),
yaxis = list(title = 'log Commutator Criterion', side = 'left'),
yaxis2 = list(
title = 'log Test MSE',
side = 'right',
overlaying = 'y',
ticklen = 5, # Increase tick length for better visibility
tickfont = list(size = 10), # Adjust font size if needed
automargin = TRUE # Automatically adjust margin to prevent cutoff
),
legend = list(x = 0.02, y = 0.98),
hovermode = 'x unified',
margin = list(r = 80) # Add right margin to ensure yaxis2 labels are not cut off
)
# Effective degrees of freedom
p_edf <- plot_ly(results_df, x = ~log_lambda, y = ~eff_df,
type = 'scatter', mode = 'lines',
line = list(width = 3, color = '#2ca02c')) %>%
layout(title = 'Effective Degrees of Freedom vs Regularization',
xaxis = list(title = 'log(λ)'),
yaxis = list(title = 'Effective DF'))
p_main# Use coarser grid for bootstrap efficiency
# lambda_grid_boot <- exp(seq(-5, 3, length.out = 120)) # or 60
# or use log-spaced grid with more points near 0
lambda_grid_boot <- c(
exp(seq(-8, -2, length.out = 60)), # fine resolution for small λ
exp(seq(-2, 2, length.out = 40)) # coarser for large λ
)
lambda_grid_boot <- sort(unique(lambda_grid_boot))
# Bootstrap (OOB MSE now)
# boot_results <-
# proper_bootstrap(X, Y, lambda_grid_boot, n_boot = 100, train_frac = 0.65)
boot_results <- proper_bootstrap(X, Y, lambda_grid_boot, P, n_boot = 100, mode = "A_HinvP")## | | | 0% | |= | 1% | |= | 2% | |== | 3% | |=== | 4% | |==== | 5% | |==== | 6% | |===== | 7% | |====== | 8% | |====== | 9% | |======= | 10% | |======== | 11% | |======== | 12% | |========= | 13% | |========== | 14% | |========== | 15% | |=========== | 16% | |============ | 17% | |============= | 18% | |============= | 19% | |============== | 20% | |=============== | 21% | |=============== | 22% | |================ | 23% | |================= | 24% | |================== | 25% | |================== | 26% | |=================== | 27% | |==================== | 28% | |==================== | 29% | |===================== | 30% | |====================== | 31% | |====================== | 32% | |======================= | 33% | |======================== | 34% | |======================== | 35% | |========================= | 36% | |========================== | 37% | |=========================== | 38% | |=========================== | 39% | |============================ | 40% | |============================= | 41% | |============================= | 42% | |============================== | 43% | |=============================== | 44% | |================================ | 45% | |================================ | 46% | |================================= | 47% | |================================== | 48% | |================================== | 49% | |=================================== | 50% | |==================================== | 51% | |==================================== | 52% | |===================================== | 53% | |====================================== | 54% | |====================================== | 55% | |======================================= | 56% | |======================================== | 57% | |========================================= | 58% | |========================================= | 59% | |========================================== | 60% | |=========================================== | 61% | |=========================================== | 62% | |============================================ | 63% | |============================================= | 64% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 67% | |================================================ | 68% | |================================================ | 69% | |================================================= | 70% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 80% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 83% | |=========================================================== | 84% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 87% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 90% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 93% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 97% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 100%
##
## === Bootstrap Summary Statistics ===
## Commutator Method Performance:
cat(" Valid bootstrap samples:", length(boot_results$valid_boots), "of", length(boot_results$lambda_comm), "\n")## Valid bootstrap samples: 100 of 100
if (length(boot_results$valid_boots) >= 10) {
lambda_comm_valid <- boot_results$lambda_comm[boot_results$valid_boots]
lambda_mse_valid <- boot_results$lambda_mse[boot_results$valid_boots]
# Compute log gaps
log_gaps <- abs(log(lambda_comm_valid) - log(lambda_mse_valid))
gap_mean <- mean(log_gaps)
gap_se <- sd(log_gaps) / sqrt(length(log_gaps))
gap_ci <- quantile(log_gaps, c(0.025, 0.975))
# Compute correlation if there's variability
if (sd(log(lambda_comm_valid)) > 1e-8 && sd(log(lambda_mse_valid)) > 1e-8) {
correlation <- cor(log(lambda_comm_valid), log(lambda_mse_valid))
} else {
correlation <- NA
}
cat("\nLog-Lambda Gap (|log(λ_comm) - log(λ_mse)|):\n")
cat(" Mean ± SE: ", signif(gap_mean, 4), " ± ", signif(gap_se, 4), "\n", sep = "")
cat(" 95% CI: [", signif(gap_ci[1], 4), ", ", signif(gap_ci[2], 4), "]\n", sep = "")
cat(" Median gap: ", signif(median(log_gaps), 4), "\n", sep = "")
if (!is.na(correlation)) {
cat("\nBootstrap Correlation:\n")
cat(" cor(log λ_comm, log λ_ref) =", signif(correlation, 4), "\n")
}
# Additional diagnostics
cat("\nλ* (Commutator) across bootstraps:\n")
cat(" Median: ", signif(median(lambda_comm_valid), 4), "\n", sep = "")
cat(" IQR: [", signif(quantile(lambda_comm_valid, 0.25), 4), ", ",
signif(quantile(lambda_comm_valid, 0.75), 4), "]\n", sep = "")
cat(" SD: ", signif(sd(lambda_comm_valid), 4), "\n", sep = "")
cat(" Unique values:", length(unique(round(lambda_comm_valid, 6))), "\n")
cat("\nλ* (Test MSE) across bootstraps:\n")
cat(" Median: ", signif(median(lambda_mse_valid), 4), "\n", sep = "")
cat(" IQR: [", signif(quantile(lambda_mse_valid, 0.25), 4), ", ",
signif(quantile(lambda_mse_valid, 0.75), 4), "]\n", sep = "")
cat(" SD: ", signif(sd(lambda_mse_valid), 4), "\n", sep = "")
cat(" Unique values:", length(unique(round(lambda_mse_valid, 6))), "\n")
# VISUALIZATION
p_boot_scatter <- plot_ly() %>%
add_trace(x = log(lambda_comm_valid),
y = log(lambda_mse_valid),
type = 'scatter', mode = 'markers',
marker = list(size = 8, opacity = 0.6, color = '#2ca02c'),
name = 'Bootstrap samples',
text = paste("Sample", 1:length(lambda_comm_valid)),
hovertemplate = '%{text}<br>log(λ_comm): %{x:.3f}<br>log(λ_ref): %{y:.3f}<extra></extra>') %>%
add_trace(x = c(log(lambda_opt_comm), log(lambda_opt_comm)),
y = range(log(lambda_mse_valid)),
type = 'scatter', mode = 'lines',
line = list(dash = 'dash', color = '#1f77b4', width = 2),
name = 'Main λ* (Comm)') %>%
add_trace(x = range(log(lambda_comm_valid)),
y = c(log(lambda_opt_mse), log(lambda_opt_mse)),
type = 'scatter', mode = 'lines',
line = list(dash = 'dot', color = '#ff7f0e', width = 2),
name = 'Main λ* (MSE)') %>%
add_trace(x = range(log(lambda_comm_valid)),
y = range(log(lambda_comm_valid)),
type = 'scatter', mode = 'lines',
line = list(color = 'black', dash = 'solid', width = 1),
name = 'Identity line') %>%
layout(
title = paste0('Bootstrap Agreement: log(λ*_Comm) vs. log(λ*_MSE)<br>',
'<sub>Correlation: ',
ifelse(!is.na(correlation), signif(correlation, 3), 'NA'),
' | n=', length(lambda_comm_valid), '</sub>'),
xaxis = list(title = 'log(λ*) from Commutator'),
yaxis = list(title = 'log(λ*) from Test MSE'),
hovermode = 'closest'
)
# Distribution of gaps
p_gaps <- plot_ly(x = log_gaps, type = 'histogram',
nbinsx = 20,
marker = list(color = '#8c564b',
line = list(color = 'white', width = 1))) %>%
add_trace(x = c(gap_mean, gap_mean),
y = c(0, max(hist(log_gaps, plot = FALSE)$counts)),
type = 'scatter', mode = 'lines',
line = list(color = 'red', width = 2, dash = 'dash'),
name = 'Mean gap') %>%
layout(title = 'Distribution of |log(λ*_Comm) - log(λ*_MSE)|',
xaxis = list(title = 'Log-Lambda Gap'),
yaxis = list(title = 'Frequency'),
bargap = 0.1,
showlegend = TRUE)
p_boot_scatter
p_gaps
} else {
cat("\n⚠ Insufficient valid bootstrap samples for detailed analysis\n")
cat(" Only", length(boot_results$valid_boots), "valid samples (need ≥10)\n")
cat("\nTroubleshooting suggestions:\n")
cat(" 1. Increase n_boot to 200+\n")
cat(" 2. Widen lambda_grid range\n")
cat(" 3. Increase grid resolution\n")
cat(" 4. Check for numerical instability in commutator computation\n")
}##
## Log-Lambda Gap (|log(λ_comm) - log(λ_mse)|):
## Mean ± SE: 3.27 ± 0.0807
## 95% CI: [1.627, 5.036]
## Median gap: 3.255
##
## Bootstrap Correlation:
## cor(log λ_comm, log λ_ref) = 0.1619
##
## λ* (Commutator) across bootstraps:
## Median: 0.003851
## IQR: [0.002316, 0.005225]
## SD: 0.002007
## Unique values: 27
##
## λ* (Test MSE) across bootstraps:
## Median: 0.09493
## IQR: [0.05999, 0.1353]
## SD: 0.05328
## Unique values: 23
The second demonstration of the commutator framework involves binary classification via logistic regression. In this case, the design matrix \(\mathbf{X} \in \mathbb{R}^{n \times p}\) with \(n = 200\), \(p = 40\) is generated with moderate block-wise correlation; each of three blocks of 10 features shares a common latent factor. The true coefficient vector \(\boldsymbol{\beta}_{\text{true}}\) is structured as
\[\beta_j = \begin{cases} \text{linear increase from } -1.5 \text{ to } 1.5 & j \in \{5,\dots,12\},\\ 1 & j \in \{20,\dots,25\},\\ \text{linear decrease from } 0.5 \text{ to } -0.5 & j \in \{35,\dots,38\},\\ 0 & \text{otherwise}. \end{cases}\]
The binary outcomes are generated via \(\Pr(Y_i = 1) = \sigma(\mathbf{x}_i^\top \boldsymbol{\beta}_{\text{true}})\), where \(\sigma(\cdot)\) is the logistic function, yielding balanced classes \(n_0 = 101\), \(n_1 = 99\).
Again, we adopt the same first-order difference penalty \(\mathbf{P} = \mathbf{D}^\top \mathbf{D}\), scaled to the Frobenius norm of the weighted curvature \(\mathbf{A}(\boldsymbol{\beta}) = \mathbf{X}^\top \mathbf{W}(\boldsymbol{\beta}) \mathbf{X}/n\), where \(\mathbf{W}(\boldsymbol{\beta})\) is the diagonal weight matrix from iteratively reweighted least squares (IRLS). Analogously to the linear case, the commutator objective (loss function) is defined using the local curvature at convergence
\[J_A(\lambda) = \big\| [\mathbf{A}(\hat{\boldsymbol{\beta}}_\lambda),\, \mathbf{H}_\lambda^{-1} \mathbf{P}] \big\|_F^2 \times \operatorname{tr}(\mathbf{H}_\lambda^{-1} \mathbf{A}(\hat{\boldsymbol{\beta}}_\lambda)) \big(p - \operatorname{tr}(\mathbf{H}_\lambda^{-1} \mathbf{A}(\hat{\boldsymbol{\beta}}_\lambda))\big),\]
with \(\mathbf{H}_\lambda = \mathbf{A}(\hat{\boldsymbol{\beta}}_\lambda) + \lambda \mathbf{P}\). Model fitting is performed via penalized IRLS, with safeguards against separation with probability clipping at \(10^{-6}\).
In this experiment, the commutator-optimal \(\lambda^\star_{\text{comm}}\) is benchmarked against an out-of-sample AUC optimum \(\lambda^\star_{\text{AUC}}\) on a 30% test set. Bootstrap analysis over 100 resamples evaluates stability using out-of-bag AUC as the reference. The commutator landscape is visualized against the balanced score (to be maximized) and test AUC.
############### Logistic Experimental Setting
set.seed(456)
n_class <- 200
p_class <- 40
# Generate features with moderate correlation
X_class <- matrix(rnorm(n_class * p_class), n_class, p_class)
for(i in 1:3) {
idx <- ((i-1)*10 + 1):(i*10)
X_class[, idx] <- X_class[, idx] + matrix(rnorm(n_class), n_class, 10) * 0.3
}
# True coefficients with structured pattern
beta_class <- numeric(p_class)
beta_class[5:12] <- seq(-1.5, 1.5, length.out = 8)
beta_class[20:25] <- rep(1, 6)
beta_class[35:38] <- seq(0.5, -0.5, length.out = 4)
# Generate probabilities and outcomes
linear_comb <- X_class %*% beta_class
prob_true <- 1 / (1 + exp(-linear_comb))
Y_class <- rbinom(n_class, 1, prob_true)
cat("Class distribution:\n")## Class distribution:
## Y_class
## 0 1
## 101 99
This uses a non‑trivial commutator \([A, H^{-1}P]\) or \([P, H^{-1}A]\), chosen by
mode, rather than a simple degenerate alternative
\([H^{-1}A, H^{-1}\lambda P]\). The
approach uses a balanced score \(||[·,·]||_F^2\ \text{tr}(H^{-1}A) (p -
\text{tr}(H^{-1}A))\) and minimize its inverse. The reference
metric is OOB AUC inside the bootstrap loop, so it won’t
collapse to a grid edge even when the training fit is very flexible.
## ---------- LOGISTIC HELPERS ----------
# Fast, tie-correct AUC for 0/1 labels
auc_fast <- function(y_true, p_hat) {
r <- rank(p_hat, ties.method = "average")
n_pos <- sum(y_true == 1L)
n_neg <- sum(y_true == 0L)
if (n_pos == 0L || n_neg == 0L) return(NA_real_)
sum_r_pos <- sum(r[y_true == 1L])
(sum_r_pos - n_pos * (n_pos + 1) / 2) / (n_pos * n_neg)
}
# Penalized IRLS for a given lambda
irls_penalized_logistic <- function(X, y, P, lambda,
beta_init = NULL,
max_iters = 50, tol = 1e-6,
clip_p = 1e-6, clip_w = 1e-6,
jitter = 1e-10) {
n <- nrow(X); p <- ncol(X)
beta <- if (is.null(beta_init)) rep(0, p) else beta_init
iters <- 0L
for (it in seq_len(max_iters)) {
iters <- it
eta <- as.vector(X %*% beta)
mu <- plogis(eta)
mu <- pmin(pmax(mu, clip_p), 1 - clip_p)
Wv <- pmax(mu * (1 - mu), clip_w) # ensure positive weights
z <- eta + (y - mu) / Wv
W <- Diagonal(n, x = Wv)
A <- crossprod(X, W %*% X) / n # curvature
b <- crossprod(X, W %*% z) / n # pseudo-response
H <- A + lambda * P
beta_new <- tryCatch(stable_solve_spd(H, b, jitter = jitter),
error = function(e) rep(NA_real_, p))
if (!all(is.finite(beta_new))) {
return(list(beta = beta, converged = FALSE, iters = iters))
}
if (max(abs(beta_new - beta)) < tol * (1 + max(1, sqrt(sum(beta^2))))) {
beta <- beta_new
return(list(beta = beta, converged = TRUE, iters = iters))
}
beta <- beta_new
}
list(beta = beta, converged = FALSE, iters = iters)
}
# Proper logistic commutator objective (same mode logic as linear)
# Returns an objective to MINIMIZE: 1 / ( mis^2 * balance + eps )
proper_influence_commutator_logistic <- function(lambda, X, y, P,
mode = c("A_HinvP","P_HinvA"),
beta_init = NULL,
max_iters = 50, tol = 1e-6,
clip_p = 1e-6, clip_w = 1e-6,
jitter = 1e-10, eps = 1e-12) {
mode <- match.arg(mode)
n <- nrow(X); p <- ncol(X)
fit <- irls_penalized_logistic(X, y, P, lambda,
beta_init = beta_init,
max_iters = max_iters, tol = tol,
clip_p = clip_p, clip_w = clip_w, jitter = jitter)
if (!all(is.finite(fit$beta))) {
return(list(commutator = NA_real_, score_balanced = NA_real_,
misalignment_sq = NA_real_, eff_df = NA_real_,
beta_hat = fit$beta, converged = FALSE, iters = fit$iters,
mode = mode))
}
# Final weights at convergence
eta <- as.vector(X %*% fit$beta)
mu <- plogis(eta)
mu <- pmin(pmax(mu, clip_p), 1 - clip_p)
Wv <- pmax(mu * (1 - mu), clip_w)
W <- Diagonal(n, x = Wv)
A_final <- crossprod(X, W %*% X) / n
H_final <- A_final + lambda * P
HinvA <- tryCatch(stable_solve_spd(H_final, A_final, jitter = jitter), error = function(e) NULL)
HinvP <- tryCatch(stable_solve_spd(H_final, P, jitter = jitter), error = function(e) NULL)
if (is.null(HinvA) || is.null(HinvP)) {
return(list(commutator = NA_real_, score_balanced = NA_real_,
misalignment_sq = NA_real_, eff_df = NA_real_,
beta_hat = fit$beta, converged = fit$converged, iters = fit$iters,
mode = mode))
}
# Non-trivial commutator
if (mode == "A_HinvP") {
C <- A_final %*% HinvP - HinvP %*% A_final # [A, H^{-1}P]
} else {
C <- P %*% HinvA - HinvA %*% P # [P, H^{-1}A]
}
mis_sq <- Matrix::norm(C, "F")^2
eff_df <- sum(diag(HinvA))
balance <- eff_df * (p - eff_df) # 0 at extremes
score <- mis_sq * balance
list(
commutator = 1 / (score + eps), # MINIMIZE this (inverse of balanced score)
score_balanced = score, # raw score to MAXIMIZE (for diagnostics)
misalignment_sq = mis_sq,
eff_df = eff_df,
beta_hat = fit$beta,
converged = fit$converged,
iters = fit$iters,
mode = mode
)
}
# Bootstrap for logistic: commutator (min inverse balanced score) vs OOB AUC (max)
proper_bootstrap_logistic <- function(X, y, lambda_grid, P,
n_boot = 100, train_frac = 0.65,
mode = c("A_HinvP","P_HinvA"),
max_iters = 50, tol = 1e-6,
rescale_P_by_A = TRUE,
oob_min_pos = 5, oob_min_neg = 5,
clip_p = 1e-6, clip_w = 1e-6,
jitter = 1e-10, eps = 1e-12) {
mode <- match.arg(mode)
n <- nrow(X); p <- ncol(X)
lambda_comm_boot <- rep(NA_real_, n_boot)
lambda_auc_boot <- rep(NA_real_, n_boot)
valid_boots <- logical(n_boot)
cat("Running logistic bootstrap (commutator vs OOB AUC)...\n")
pb <- txtProgressBar(min = 0, max = n_boot, style = 3)
on.exit(close(pb), add = TRUE)
for (b in seq_len(n_boot)) {
# Bootstrap training indices + OOB
n_tr <- max(1L, round(train_frac * n))
idx_tr <- sort(sample.int(n, n_tr, replace = TRUE))
idx_oob <- setdiff(seq_len(n), unique(idx_tr))
Xtr <- X[idx_tr, , drop = FALSE]; ytr <- y[idx_tr]
Xoob <- if (length(idx_oob) > 0) X[idx_oob, , drop = FALSE] else NULL
yoob <- if (length(idx_oob) > 0) y[idx_oob] else NULL
# Optional: rescale P to Frobenius norm of A_tr
P_b <- P
if (rescale_P_by_A) {
A_tr <- crossprod(Xtr) / nrow(Xtr)
nf <- Matrix::norm(A_tr, "F") / max(Matrix::norm(P_b, "F"), 1e-12)
P_b <- P_b * nf
}
comm_vals <- rep(NA_real_, length(lambda_grid))
auc_vals <- rep(NA_real_, length(lambda_grid))
for (i in seq_along(lambda_grid)) {
lam <- lambda_grid[i]
res <- tryCatch(
proper_influence_commutator_logistic(lam, Xtr, ytr, P_b,
mode = mode,
max_iters = max_iters, tol = tol,
clip_p = clip_p, clip_w = clip_w,
jitter = jitter, eps = eps),
error = function(e) NULL
)
if (is.null(res) || any(!is.finite(res$beta_hat))) next
# Commutator objective (inverse balanced score) -> minimize
comm_vals[i] <- res$commutator
# OOB AUC as reference (-> maximize)
if (!is.null(Xoob) && length(idx_oob) > 0) {
if (sum(yoob == 1L) >= oob_min_pos && sum(yoob == 0L) >= oob_min_neg) {
p_oob <- plogis(as.vector(Xoob %*% res$beta_hat))
auc_vals[i] <- auc_fast(yoob, p_oob)
} else {
auc_vals[i] <- NA_real_
}
}
}
ok_comm <- which(is.finite(comm_vals))
ok_auc <- which(is.finite(auc_vals))
if (length(ok_comm) > 0) {
lambda_comm_boot[b] <- lambda_grid[ ok_comm[ which.min(comm_vals[ok_comm]) ] ]
}
if (length(ok_auc) > 0) {
lambda_auc_boot[b] <- lambda_grid[ ok_auc[ which.max(auc_vals[ok_auc]) ] ]
}
valid_boots[b] <- (length(ok_comm) > 0 && length(ok_auc) > 0)
setTxtProgressBar(pb, b)
}
list(
lambda_comm = lambda_comm_boot, # from commutator objective (minimize inverse score)
lambda_auc = lambda_auc_boot, # from OOB AUC (maximize)
valid_boots = which(valid_boots)
)
}
# Weight matrix for logistic regression
compute_W <- function(X, beta) {
eta <- as.vector(X %*% beta)
p <- 1 / (1 + exp(-eta))
p <- pmin(pmax(p, 1e-6), 1 - 1e-6)
Diagonal(x = p * (1 - p))
}
# Logistic influence commutator
commutator_influence_logistic <- function(lambda, X, y, beta_init, P, max_iters = 50, tol = 1e-6) {
n <- nrow(X)
beta <- beta_init
# IRLS fitting
for (t in 1:max_iters) {
W <- compute_W(X, beta)
mu <- as.vector(1 / (1 + exp(-(X %*% beta))))
z <- as.vector(X %*% beta) + (y - mu) / diag(W)
A <- crossprod(X, W %*% X) / n
b <- crossprod(X, W %*% z) / n
H <- A + lambda * P
beta_new <- stable_solve_spd(H, b)
if (max(abs(beta_new - beta)) < tol) {
beta <- beta_new
break
}
beta <- beta_new
}
# Compute influence operators at convergence
W_final <- compute_W(X, beta)
A_final <- crossprod(X, W_final %*% X) / n
H_final <- A_final + lambda * P
invH_A <- stable_solve_spd(H_final, A_final)
invH_P <- stable_solve_spd(H_final, lambda * P)
Cmat <- invH_A %*% invH_P - invH_P %*% invH_A
list(
commutator = frob_sq(Cmat),
beta_hat = beta,
converged = (t < max_iters)
)
}## Grid on a single train/test split
lambda_grid_class <- exp(seq(-8, 2, length.out = 120))
# Train/test split
idx_tr <- sample(seq_len(nrow(X_class)), round(0.7 * nrow(X_class)))
Xtr_class <- X_class[idx_tr, , drop = FALSE]
Ytr_class <- Y_class[idx_tr]
Xte_class <- X_class[-idx_tr, , drop = FALSE]
Yte_class <- Y_class[-idx_tr]
# Scale penalty once (or per split)
P_class <- make_diff_penalty(ncol(X_class), order = 1)
A_tr <- crossprod(Xtr_class) / nrow(Xtr_class)
P_class <- P_class / max(1e-12, Matrix::norm(P_class, "F")) * Matrix::norm(A_tr, "F")
comm_vals <- auc_vals <- numeric(length(lambda_grid_class))
for (i in seq_along(lambda_grid_class)) {
lam <- lambda_grid_class[i]
res <- proper_influence_commutator_logistic(lam, Xtr_class, Ytr_class, P_class,
mode = "A_HinvP")
comm_vals[i] <- res$commutator # minimize inverse balanced score
p_te <- plogis(as.vector(Xte_class %*% res$beta_hat))
auc_vals[i] <- auc_fast(Yte_class, p_te) # maximize AUC
}
lambda_opt_comm_class <- lambda_grid_class[which.min(comm_vals)]
lambda_opt_auc_class <- lambda_grid_class[which.max(auc_vals)]
cat("Logistic: λ*_comm =", signif(lambda_opt_comm_class,5),
" | λ*_AUC =", signif(lambda_opt_auc_class,5), "\n")## Logistic: λ*_comm = 0.00033546 | λ*_AUC = 0.093504
##
## Logistic Regression Results:
## Commutator optimum: λ = 0.00033546
## AUC optimum: λ = 0.093504
## Log ratio: -5.63
## Bootstrap comparison (commutator vs OOB AUC)
boot_log <- proper_bootstrap_logistic(
X_class, Y_class, lambda_grid = lambda_grid_class, P = P_class,
n_boot = 100, train_frac = 0.65, mode = "A_HinvP"
)## Running logistic bootstrap (commutator vs OOB AUC)...
## | | | 0% | |= | 1% | |= | 2% | |== | 3% | |=== | 4% | |==== | 5% | |==== | 6% | |===== | 7% | |====== | 8% | |====== | 9% | |======= | 10% | |======== | 11% | |======== | 12% | |========= | 13% | |========== | 14% | |========== | 15% | |=========== | 16% | |============ | 17% | |============= | 18% | |============= | 19% | |============== | 20% | |=============== | 21% | |=============== | 22% | |================ | 23% | |================= | 24% | |================== | 25% | |================== | 26% | |=================== | 27% | |==================== | 28% | |==================== | 29% | |===================== | 30% | |====================== | 31% | |====================== | 32% | |======================= | 33% | |======================== | 34% | |======================== | 35% | |========================= | 36% | |========================== | 37% | |=========================== | 38% | |=========================== | 39% | |============================ | 40% | |============================= | 41% | |============================= | 42% | |============================== | 43% | |=============================== | 44% | |================================ | 45% | |================================ | 46% | |================================= | 47% | |================================== | 48% | |================================== | 49% | |=================================== | 50% | |==================================== | 51% | |==================================== | 52% | |===================================== | 53% | |====================================== | 54% | |====================================== | 55% | |======================================= | 56% | |======================================== | 57% | |========================================= | 58% | |========================================= | 59% | |========================================== | 60% | |=========================================== | 61% | |=========================================== | 62% | |============================================ | 63% | |============================================= | 64% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 67% | |================================================ | 68% | |================================================ | 69% | |================================================= | 70% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 80% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 83% | |=========================================================== | 84% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 87% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 90% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 93% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 97% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 100%
valid <- boot_log$valid_boots
cat("Valid logistic bootstraps:", length(valid), "of", length(boot_log$lambda_comm), "\n")## Valid logistic bootstraps: 100 of 100
if (length(valid) >= 10) {
lc <- boot_log$lambda_comm[valid]
la <- boot_log$lambda_auc[valid]
gaps <- abs(log(lc) - log(la))
cat("Mean |log-gap| =", signif(mean(gaps),4), " SD =", signif(sd(gaps),4), "\n")
}## Mean |log-gap| = 5.964 SD = 1.639
Either of the 2 types of commutators can be set using e.g.,
mode = "P_HinvA" in both helper calls.The AUC
implementation (auc_fast) is rank‑based, using tie‑correct
Mann–Whitney, and returns \(NA\) if the
OOB set has only one class; those \(\lambda\)’s are ignored for selecting \(\lambda^*_{AUC}\). To track the iterative
optimization process (maximization), we can plot and interpret the
convergence of \(\log(score\_balanced)\) and/or plotting
\(\log(commutator)\) if we stick with
which.min. In the numerical algorithm, the
clip_p and clip_w parameters keep IRLS stable
when probabilities saturate. As needed, these parameters may be
increased slightly, e.g., \(1e-5\) if
there are warnings near separation.
## --- COMPACT LOGISTIC VISUALIZATION (commutator vs AUC) ---
## Balanced commutator is the score to MAXIMIZE; we still select λ* by
## minimizing the inverse objective returned as $commutator (for compatibility).
mode_choice <- "A_HinvP" # or "P_HinvA"
comm_inv_vals <- rep(NA_real_, length(lambda_grid_class)) # inverse of balanced score (-> minimize)
score_balanced_vals <- rep(NA_real_, length(lambda_grid_class)) # balanced score (-> maximize)
auc_vals <- rep(NA_real_, length(lambda_grid_class))
for (i in seq_along(lambda_grid_class)) {
lam <- lambda_grid_class[i]
res <- proper_influence_commutator_logistic(
lam, Xtr_class, Ytr_class, P_class,
mode = mode_choice
)
if (!any(is.na(res$beta_hat))) {
comm_inv_vals[i] <- res$commutator
score_balanced_vals[i] <- res$score_balanced
p_te <- plogis(drop(Xte_class %*% res$beta_hat))
auc_vals[i] <- auc_fast(Yte_class, p_te)
}
}
# Optima
lambda_opt_comm_class <- lambda_grid_class[which.min(comm_inv_vals)] # same as which.max(score_balanced_vals)
lambda_opt_auc_class <- lambda_grid_class[which.max(auc_vals)]
# Data for plotting
logistic_results <- data.frame(
log_lambda = log(lambda_grid_class),
log_score_bal = log(pmax(score_balanced_vals, 1e-300)),
auc = auc_vals
)
# Plot: log balanced score (left axis) vs AUC (right axis)
p_logistic <- plot_ly(logistic_results) %>%
add_trace(x = ~log_lambda, y = ~log_score_bal,
type = 'scatter', mode = 'lines+markers',
name = 'log Balanced Score (maximize)',
line = list(width = 3),
marker = list(size = 4)) %>%
add_trace(x = ~log_lambda, y = ~auc,
type = 'scatter', mode = 'lines',
name = 'Test AUC (maximize)', yaxis = 'y2',
line = list(width = 3)) %>%
add_trace(x = rep(log(lambda_opt_comm_class), 2),
y = range(logistic_results$log_score_bal, na.rm = TRUE),
type = 'scatter', mode = 'lines',
line = list(dash = 'dash', width = 2),
name = 'λ* (Commutator)') %>%
add_trace(x = rep(log(lambda_opt_auc_class), 2),
y = range(logistic_results$auc, na.rm = TRUE),
type = 'scatter', mode = 'lines',
line = list(dash = 'dot', width = 2),
name = 'λ* (AUC)', yaxis = 'y2') %>%
layout(
title = paste0('Logistic: Balanced Commutator vs AUC (mode = ', mode_choice, ')'),
xaxis = list(title = 'log(λ)'),
yaxis = list(title = 'log Balanced Score', side = 'left'),
yaxis2 = list(title = 'AUC', side = 'right', overlaying = 'y',
ticklen = 5, # Increase tick length for better visibility
tickfont = list(size = 10), # Adjust font size if needed
automargin = TRUE # Automatically adjust margin to prevent cutoff
),
legend = list(x = 0.02, y = 0.98),
hovermode = 'x unified'
)
# Show the figure and text readout
p_logistic##
## Logistic Regression Results:
cat(" Commutator optimum (", mode_choice, "): λ = ", signif(lambda_opt_comm_class, 6), "\n", sep = "")## Commutator optimum (A_HinvP): λ = 0.000335463
## AUC optimum: λ = 0.0935043
## Log ratio: -5.63
## --- COMPACT LOGISTIC VISUALIZATION (commutator vs AUC) ---
## Balanced commutator is the score to MAXIMIZE; we still select λ* by
## minimizing the inverse objective returned as $commutator (for compatibility).
mode_choice <- "P_HinvA" # or "A_HinvP"
comm_inv_vals <- rep(NA_real_, length(lambda_grid_class)) # inverse of balanced score (-> minimize)
score_balanced_vals <- rep(NA_real_, length(lambda_grid_class)) # balanced score (-> maximize)
auc_vals <- rep(NA_real_, length(lambda_grid_class))
for (i in seq_along(lambda_grid_class)) {
lam <- lambda_grid_class[i]
res <- proper_influence_commutator_logistic(
lam, Xtr_class, Ytr_class, P_class,
mode = mode_choice
)
if (!any(is.na(res$beta_hat))) {
comm_inv_vals[i] <- res$commutator
score_balanced_vals[i] <- res$score_balanced
p_te <- plogis(drop(Xte_class %*% res$beta_hat))
auc_vals[i] <- auc_fast(Yte_class, p_te)
}
}
# Optima
lambda_opt_comm_class <- lambda_grid_class[which.min(comm_inv_vals)] # same as which.max(score_balanced_vals)
lambda_opt_auc_class <- lambda_grid_class[which.max(auc_vals)]
# Data for plotting
logistic_results <- data.frame(
log_lambda = log(lambda_grid_class),
log_score_bal = log(pmax(score_balanced_vals, 1e-300)),
auc = auc_vals
)
# Plot: log balanced score (left axis) vs AUC (right axis)
p_logistic <- plot_ly(logistic_results) %>%
add_trace(x = ~log_lambda, y = ~log_score_bal,
type = 'scatter', mode = 'lines+markers',
name = 'log Balanced Score (maximize)',
line = list(width = 3),
marker = list(size = 4)) %>%
add_trace(x = ~log_lambda, y = ~auc,
type = 'scatter', mode = 'lines',
name = 'Test AUC (maximize)', yaxis = 'y2',
line = list(width = 3)) %>%
add_trace(x = rep(log(lambda_opt_comm_class), 2),
y = range(logistic_results$log_score_bal, na.rm = TRUE),
type = 'scatter', mode = 'lines',
line = list(dash = 'dash', width = 2),
name = 'λ* (Commutator)') %>%
add_trace(x = rep(log(lambda_opt_auc_class), 2),
y = range(logistic_results$auc, na.rm = TRUE),
type = 'scatter', mode = 'lines',
line = list(dash = 'dot', width = 2),
name = 'λ* (AUC)', yaxis = 'y2') %>%
layout(
title = paste0('Logistic: Balanced Commutator vs AUC (mode = ', mode_choice, ')'),
xaxis = list(title = 'log(λ)'),
yaxis = list(title = 'log Balanced Score', side = 'left'),
yaxis2 = list(title = 'AUC', side = 'right', overlaying = 'y',
ticklen = 5, # Increase tick length for better visibility
tickfont = list(size = 10), # Adjust font size if needed
automargin = TRUE # Automatically adjust margin to prevent cutoff
),
legend = list(x = 0.02, y = 0.98),
hovermode = 'x unified'
)
# Show the figure and text readout
p_logistic##
## Logistic Regression Results:
cat(" Commutator optimum (", mode_choice, "): λ = ", signif(lambda_opt_comm_class, 6), "\n", sep = "")## Commutator optimum (P_HinvA): λ = 0.154812
## AUC optimum: λ = 0.0935043
## Log ratio: 0.504
## --- FINAL RESULTS SUMMARY (revised; consistent with balanced commutator) ---
summary_results <- data.frame(
Model = c("Linear Regression", "Logistic Regression"),
Lambda_Commutator = c(
signif(lambda_opt_comm, 6),
signif(lambda_opt_comm_class, 6)
),
Lambda_Reference = c(
signif(lambda_opt_mse, 6), # Linear reference = Test/OOB MSE optimum
signif(lambda_opt_auc_class, 6) # Logistic reference = AUC optimum
)
)
# Log-ratio and alignment label (|log-ratio| < 1 => "Good", else "Divergent")
log_ratio <- log(as.numeric(summary_results$Lambda_Commutator) /
as.numeric(summary_results$Lambda_Reference))
summary_results$Log_Ratio <- signif(log_ratio, 3)
summary_results$Method_Alignment <- ifelse(abs(log_ratio) < 1, "Good", "Divergent")
cat("=== FINAL RESULTS SUMMARY ===\n\n")## === FINAL RESULTS SUMMARY ===
## Model Lambda_Commutator Lambda_Reference Log_Ratio
## Linear Regression 0.0232955 0.0517141 -0.797
## Logistic Regression 0.1548120 0.0935043 0.504
## Method_Alignment
## Good
## Good
Both the linear and logistic experiments demonstrate that the commutator framework naturally extends to generalized linear models, leveraging local curvature information to produce a theoretically grounded, resampling-free tuning rule that remains stable under bootstrap perturbations.
This statistical commutator framework demonstrates that the commutator provides a theoretically grounded and practically effective approach to regularization parameter selection. By minimizing the misalignment between data-driven and penalty-driven influence operators, we obtain regularization parameters that are:
The influence operators method successfully overcomes the degeneracy of spherical ridge penalties and provides an alternative to cross-validation, particularly in settings with structured penalties or computational constraints.
Note that our experimental validation uses proper influence operators with bootstrap-resampled penalty matrices to ensure statistical validity and avoid potential zero-variance issues of over-simplified implementations.
The core theoretical issue is to avoid trivial commutators, as in using influence operators defined in hte linear modeling case as \[S_{\text{data}}(\lambda)=H_\lambda^{-1}A,\] \[S_{\text{pen}}(\lambda)=H_\lambda^{-1}(\lambda P),\] where \(H_\lambda=A+\lambda P\), and similarly in the logistic model with the weighted \(A\) minimizing a commutator loss using the Frobenius norm \[\mathcal{C}(\lambda)=|[S_{\text{data}}(\lambda),S_{\text{pen}}(\lambda)]|_F^2.\] In these situations, \[S_{\text{data}}(\lambda)+S_{\text{pen}}(\lambda)=H_\lambda^{-1}(A+\lambda P)=I\] and thus, \(S_{\text{pen}}(\lambda)=I-S_{\text{data}}(\lambda)\), which is assocaited with a trivial commutator \[[S_{\text{data}},S_{\text{pen}}]=[S_{\text{data}},I-S_{\text{data}}] = [S_{\text{data}},I]-[S_{\text{data}},S_{\text{data}}]=0,\ \forall\ \lambda^* .\]
In our commutator formulation, we define a pair of (data- and penalty-) influence operators with the following properties:
The above implementation correctly follows the revised mathematical formulation. The code properly implements the non-trivial commutators \([A, H_{\lambda}^{-1}P]\) and \([P, H_{\lambda}^{-1}A]\) with the balance term \(B(\lambda)\). However, there is weak empirical validation are valid and should be addressed to strengthen the results.
To avoid Cherry-Picking of results, we need the code tests both Type I and Type II commutators but
The code only compares against oracle test-set optima, not against standard k-fold CV.
Only two synthetic datasets are tested with specific correlation structures.
Below is the revised enhanced code that addresses some of the drawbacks of the initial implementation above.
# 1. Linear Model
# Stable matrix operations
stable_solve_spd <- function(M, b = NULL, jitter = 1e-10) {
Mj <- forceSymmetric(M) + jitter * Diagonal(nrow(M))
cholM <- tryCatch(Cholesky(Mj, LDL = FALSE), error = function(e) NULL)
if (is.null(cholM)) {
return(if (is.null(b)) solve(as.matrix(Mj)) else solve(as.matrix(Mj), b))
}
if (is.null(b)) return(solve(cholM, Diagonal(nrow(Mj))))
solve(cholM, b)
}
frob_sq <- function(M) {
Matrix::norm(M, "F")^2
}
# Non-spherical penalty construction
make_diff_penalty <- function(p, order = 1) {
D <- Matrix(0, nrow = p - 1, ncol = p, sparse = TRUE)
for (i in 1:(p - 1)) {
D[i, i] <- -1; D[i, i + 1] <- 1
}
if (order == 1) {
return(t(D) %*% D)
} else {
P <- t(D) %*% D
for (k in 2:order) P <- t(D) %*% (D %*% P)
return(P)
}
}
# # Proper influence operator commutator
# Sdata(λ)=Hλ−1A and Spen(λ)=Hλ−1(λP) with Hλ=A+λP. Then,
# Sdata+Spen=I, so [Sdata,Spen]≡0 for all λ (trivial objective).
# This revised function instead optimizes ||[A, Hλ−1P]||_F^2,
# or ||[P, Hλ−1A]||_F^2, multiplied by a “balance” term that kills the spurious edge optima.
proper_influence_commutator <- function(lambda, X, Y, P,
mode = c("A_HinvP","P_HinvA"),
use_weights = TRUE,
jitter = 1e-10,
eps = 1e-12) {
mode <- match.arg(mode)
n <- nrow(X); p <- ncol(X)
# (0) Unweighted curvature and ridge fit for residuals
A0 <- crossprod(X) / n
H0 <- A0 + lambda * P
b0 <- crossprod(X, Y) / n
beta_hat <- stable_solve_spd(H0, b0, jitter = jitter)
# (1) Optional residual-based weights (robust curvature)
if (use_weights) {
r <- as.vector(Y - X %*% beta_hat)
sigma2 <- sum(r^2) / max(n - p, 1)
sigma2 <- max(sigma2, 1e-8)
w <- 1 / (1 + abs(r) / sqrt(sigma2))
W <- Diagonal(n = n, x = w)
A <- crossprod(X, W %*% X) / n
} else {
A <- A0
}
# (2) Final Hessian
H <- A + lambda * P
# (3) λ-dependent influence maps (no degeneracy here)
HinvA <- stable_solve_spd(H, A, jitter = jitter) # T_A(λ) = H^{-1}A
HinvP <- stable_solve_spd(H, P, jitter = jitter) # T_P(λ) = H^{-1}P (no λ!)
eff_df <- sum(diag(HinvA)) # tr(T_A)
# (4) Non-trivial commutator
if (mode == "A_HinvP") {
C <- A %*% HinvP - HinvP %*% A # [A, H^{-1}P]
} else {
C <- P %*% HinvA - HinvA %*% P # [P, H^{-1}A]
}
mis_sq <- Matrix::norm(C, "F")^2 # ||[·,·]||_F^2
# (5) Balance factor—0 at extremes, encourages interior λ
balance <- eff_df * (p - eff_df)
# (6) Final score: we want to MAXIMIZE (mis_sq * balance)
# To keep caller logic (which.min) unchanged, return its inverse.
score_balanced <- mis_sq * balance
objective <- 1 / (score_balanced + eps) # MINIMIZE this
list(
commutator = objective, # objective to MINIMIZE (inverse balanced score)
score_balanced = score_balanced, # raw score to MAXIMIZE (for diagnostics if you like)
misalignment_sq = mis_sq,
eff_df = eff_df,
beta_hat = beta_hat,
mode = mode
)
}
# Core bootstrap function
proper_bootstrap <- function(X, Y, lambda_grid, P,
n_boot = 100, train_frac = 0.65,
mode = c("A_HinvP","P_HinvA"),
use_weights = TRUE,
rescale_P_by_A = TRUE,
oob_min = 10,
jitter = 1e-10, eps = 1e-12) {
mode <- match.arg(mode)
n <- nrow(X); p <- ncol(X)
lambda_comm_boot <- rep(NA_real_, n_boot)
lambda_mse_boot <- rep(NA_real_, n_boot)
valid_boots <- logical(n_boot)
pb <- txtProgressBar(min = 0, max = n_boot, style = 3)
on.exit(close(pb), add = TRUE)
for (b in seq_len(n_boot)) {
# Bootstrap training indices
n_tr <- max(1L, round(train_frac * n))
idx_tr <- sample.int(n, n_tr, replace = TRUE)
idx_tr <- sort(idx_tr)
idx_oob <- setdiff(seq_len(n), unique(idx_tr))
Xtr <- X[idx_tr, , drop = FALSE]
Ytr <- Y[idx_tr]
Xoob <- if (length(idx_oob) > 0) X[idx_oob, , drop = FALSE] else NULL
Yoob <- if (length(idx_oob) > 0) Y[idx_oob] else NULL
# Optional: rescale P to Frobenius norm of A_tr
P_b <- P
if (rescale_P_by_A) {
A_tr <- crossprod(Xtr) / nrow(Xtr)
nf <- Matrix::norm(A_tr, "F") / max(Matrix::norm(P_b, "F"), 1e-12)
P_b <- P_b * nf
}
comm_vals <- rep(NA_real_, length(lambda_grid))
oob_errs <- rep(NA_real_, length(lambda_grid))
for (i in seq_along(lambda_grid)) {
lam <- lambda_grid[i]
res <- tryCatch(
# proper_influence_commutator(lam, Xtr, Ytr, P_b,
# mode = mode,
# use_weights = use_weights,
# jitter = jitter, eps = eps),
# For [A, H^{-1}P] ###############################################
proper_influence_commutator(lam, Xtr, Ytr, P, mode = "A_HinvP"),
# # For [P, H^{-1}A] ############################################
# proper_influence_commutator(lam, Xtr, Ytr, P, mode = "P_HinvA"),
# # Bootstrap (OOB MSE now)
# boot <- proper_bootstrap(X, Y, lambda_grid_boot, P, n_boot = 100, mode = "A_HinvP")
error = function(e) NULL
)
if (is.null(res) || any(is.na(res$beta_hat))) next
# Commutator objective (inverse balanced score) -> minimize
comm_vals[i] <- res$commutator
# OOB MSE as the reference criterion (-> minimize)
if (length(idx_oob) >= oob_min) {
pred_oob <- as.vector(Xoob %*% res$beta_hat)
oob_errs[i] <- mean((Yoob - pred_oob)^2)
}
}
ok_comm <- which(is.finite(comm_vals))
ok_oob <- which(is.finite(oob_errs))
if (length(ok_comm) > 0) {
lambda_comm_boot[b] <- lambda_grid[ ok_comm[ which.min(comm_vals[ok_comm]) ] ]
}
if (length(ok_oob) > 0) {
lambda_mse_boot[b] <- lambda_grid[ ok_oob[ which.min(oob_errs[ok_oob]) ] ]
}
valid_boots[b] <- (length(ok_comm) > 0) && (length(ok_oob) > 0)
setTxtProgressBar(pb, b)
}
list(
lambda_comm = lambda_comm_boot, # from commutator objective (minimize inverse score)
lambda_mse = lambda_mse_boot, # from OOB MSE (minimize)
valid_boots = which(valid_boots)
)
}
# 2. Logistic Model
## ---------- LOGISTIC HELPERS ----------
# Fast, tie-correct AUC for 0/1 labels
auc_fast <- function(y_true, p_hat) {
r <- rank(p_hat, ties.method = "average")
n_pos <- sum(y_true == 1L)
n_neg <- sum(y_true == 0L)
if (n_pos == 0L || n_neg == 0L) return(NA_real_)
sum_r_pos <- sum(r[y_true == 1L])
(sum_r_pos - n_pos * (n_pos + 1) / 2) / (n_pos * n_neg)
}
# Penalized IRLS for a given lambda
irls_penalized_logistic <- function(X, y, P, lambda,
beta_init = NULL,
max_iters = 50, tol = 1e-6,
clip_p = 1e-6, clip_w = 1e-6,
jitter = 1e-10) {
n <- nrow(X); p <- ncol(X)
beta <- if (is.null(beta_init)) rep(0, p) else beta_init
iters <- 0L
for (it in seq_len(max_iters)) {
iters <- it
eta <- as.vector(X %*% beta)
mu <- plogis(eta)
mu <- pmin(pmax(mu, clip_p), 1 - clip_p)
Wv <- pmax(mu * (1 - mu), clip_w) # ensure positive weights
z <- eta + (y - mu) / Wv
W <- Diagonal(n, x = Wv)
A <- crossprod(X, W %*% X) / n # curvature
b <- crossprod(X, W %*% z) / n # pseudo-response
H <- A + lambda * P
beta_new <- tryCatch(stable_solve_spd(H, b, jitter = jitter),
error = function(e) rep(NA_real_, p))
if (!all(is.finite(beta_new))) {
return(list(beta = beta, converged = FALSE, iters = iters))
}
if (max(abs(beta_new - beta)) < tol * (1 + max(1, sqrt(sum(beta^2))))) {
beta <- beta_new
return(list(beta = beta, converged = TRUE, iters = iters))
}
beta <- beta_new
}
list(beta = beta, converged = FALSE, iters = iters)
}
# Proper logistic commutator objective (same mode logic as linear)
# Returns an objective to MINIMIZE: 1 / ( mis^2 * balance + eps )
proper_influence_commutator_logistic <- function(lambda, X, y, P,
mode = c("A_HinvP","P_HinvA"),
beta_init = NULL,
max_iters = 50, tol = 1e-6,
clip_p = 1e-6, clip_w = 1e-6,
jitter = 1e-10, eps = 1e-12) {
mode <- match.arg(mode)
n <- nrow(X); p <- ncol(X)
fit <- irls_penalized_logistic(X, y, P, lambda,
beta_init = beta_init,
max_iters = max_iters, tol = tol,
clip_p = clip_p, clip_w = clip_w, jitter = jitter)
if (!all(is.finite(fit$beta))) {
return(list(commutator = NA_real_, score_balanced = NA_real_,
misalignment_sq = NA_real_, eff_df = NA_real_,
beta_hat = fit$beta, converged = FALSE, iters = fit$iters,
mode = mode))
}
# Final weights at convergence
eta <- as.vector(X %*% fit$beta)
mu <- plogis(eta)
mu <- pmin(pmax(mu, clip_p), 1 - clip_p)
Wv <- pmax(mu * (1 - mu), clip_w)
W <- Diagonal(n, x = Wv)
A_final <- crossprod(X, W %*% X) / n
H_final <- A_final + lambda * P
HinvA <- tryCatch(stable_solve_spd(H_final, A_final, jitter = jitter), error = function(e) NULL)
HinvP <- tryCatch(stable_solve_spd(H_final, P, jitter = jitter), error = function(e) NULL)
if (is.null(HinvA) || is.null(HinvP)) {
return(list(commutator = NA_real_, score_balanced = NA_real_,
misalignment_sq = NA_real_, eff_df = NA_real_,
beta_hat = fit$beta, converged = fit$converged, iters = fit$iters,
mode = mode))
}
# Non-trivial commutator
if (mode == "A_HinvP") {
C <- A_final %*% HinvP - HinvP %*% A_final # [A, H^{-1}P]
} else {
C <- P %*% HinvA - HinvA %*% P # [P, H^{-1}A]
}
mis_sq <- Matrix::norm(C, "F")^2
eff_df <- sum(diag(HinvA))
balance <- eff_df * (p - eff_df) # 0 at extremes
score <- mis_sq * balance
list(
commutator = 1 / (score + eps), # MINIMIZE this (inverse of balanced score)
score_balanced = score, # raw score to MAXIMIZE (for diagnostics)
misalignment_sq = mis_sq,
eff_df = eff_df,
beta_hat = fit$beta,
converged = fit$converged,
iters = fit$iters,
mode = mode
)
}
# Bootstrap for logistic: commutator (min inverse balanced score) vs OOB AUC (max)
proper_bootstrap_logistic <- function(X, y, lambda_grid, P,
n_boot = 100, train_frac = 0.65,
mode = c("A_HinvP","P_HinvA"),
max_iters = 50, tol = 1e-6,
rescale_P_by_A = TRUE,
oob_min_pos = 5, oob_min_neg = 5,
clip_p = 1e-6, clip_w = 1e-6,
jitter = 1e-10, eps = 1e-12) {
mode <- match.arg(mode)
n <- nrow(X); p <- ncol(X)
lambda_comm_boot <- rep(NA_real_, n_boot)
lambda_auc_boot <- rep(NA_real_, n_boot)
valid_boots <- logical(n_boot)
cat("Running logistic bootstrap (commutator vs OOB AUC)...\n")
pb <- txtProgressBar(min = 0, max = n_boot, style = 3)
on.exit(close(pb), add = TRUE)
for (b in seq_len(n_boot)) {
# Bootstrap training indices + OOB
n_tr <- max(1L, round(train_frac * n))
idx_tr <- sort(sample.int(n, n_tr, replace = TRUE))
idx_oob <- setdiff(seq_len(n), unique(idx_tr))
Xtr <- X[idx_tr, , drop = FALSE]; ytr <- y[idx_tr]
Xoob <- if (length(idx_oob) > 0) X[idx_oob, , drop = FALSE] else NULL
yoob <- if (length(idx_oob) > 0) y[idx_oob] else NULL
# Optional: rescale P to Frobenius norm of A_tr
P_b <- P
if (rescale_P_by_A) {
A_tr <- crossprod(Xtr) / nrow(Xtr)
nf <- Matrix::norm(A_tr, "F") / max(Matrix::norm(P_b, "F"), 1e-12)
P_b <- P_b * nf
}
comm_vals <- rep(NA_real_, length(lambda_grid))
auc_vals <- rep(NA_real_, length(lambda_grid))
for (i in seq_along(lambda_grid)) {
lam <- lambda_grid[i]
res <- tryCatch(
proper_influence_commutator_logistic(lam, Xtr, ytr, P_b,
mode = mode,
max_iters = max_iters, tol = tol,
clip_p = clip_p, clip_w = clip_w,
jitter = jitter, eps = eps),
error = function(e) NULL
)
if (is.null(res) || any(!is.finite(res$beta_hat))) next
# Commutator objective (inverse balanced score) -> minimize
comm_vals[i] <- res$commutator
# OOB AUC as reference (-> maximize)
if (!is.null(Xoob) && length(idx_oob) > 0) {
if (sum(yoob == 1L) >= oob_min_pos && sum(yoob == 0L) >= oob_min_neg) {
p_oob <- plogis(as.vector(Xoob %*% res$beta_hat))
auc_vals[i] <- auc_fast(yoob, p_oob)
} else {
auc_vals[i] <- NA_real_
}
}
}
ok_comm <- which(is.finite(comm_vals))
ok_auc <- which(is.finite(auc_vals))
if (length(ok_comm) > 0) {
lambda_comm_boot[b] <- lambda_grid[ ok_comm[ which.min(comm_vals[ok_comm]) ] ]
}
if (length(ok_auc) > 0) {
lambda_auc_boot[b] <- lambda_grid[ ok_auc[ which.max(auc_vals[ok_auc]) ] ]
}
valid_boots[b] <- (length(ok_comm) > 0 && length(ok_auc) > 0)
setTxtProgressBar(pb, b)
}
list(
lambda_comm = lambda_comm_boot, # from commutator objective (minimize inverse score)
lambda_auc = lambda_auc_boot, # from OOB AUC (maximize)
valid_boots = which(valid_boots)
)
}
# Weight matrix for logistic regression
compute_W <- function(X, beta) {
eta <- as.vector(X %*% beta)
p <- 1 / (1 + exp(-eta))
p <- pmin(pmax(p, 1e-6), 1 - 1e-6)
Diagonal(x = p * (1 - p))
}
# Logistic influence commutator
commutator_influence_logistic <- function(lambda, X, y, beta_init, P, max_iters = 50, tol = 1e-6) {
n <- nrow(X)
beta <- beta_init
# IRLS fitting
for (t in 1:max_iters) {
W <- compute_W(X, beta)
mu <- as.vector(1 / (1 + exp(-(X %*% beta))))
z <- as.vector(X %*% beta) + (y - mu) / diag(W)
A <- crossprod(X, W %*% X) / n
b <- crossprod(X, W %*% z) / n
H <- A + lambda * P
beta_new <- stable_solve_spd(H, b)
if (max(abs(beta_new - beta)) < tol) {
beta <- beta_new
break
}
beta <- beta_new
}
# Compute influence operators at convergence
W_final <- compute_W(X, beta)
A_final <- crossprod(X, W_final %*% X) / n
H_final <- A_final + lambda * P
invH_A <- stable_solve_spd(H_final, A_final)
invH_P <- stable_solve_spd(H_final, lambda * P)
Cmat <- invH_A %*% invH_P - invH_P %*% invH_A
list(
commutator = frob_sq(Cmat),
beta_hat = beta,
converged = (t < max_iters)
)
}Now, run the complete Statistical-Commutator Simulation and Analytics Verification Pipeline.
# ============================================================================
# ENHANCED SIMULATION FRAMEWORK WITH COMPREHENSIVE BENCHMARKING
# ============================================================================
library(Matrix)
library(plotly)
library(MASS)
library(glmnet)
library(pROC)
library(knitr)
# ----------------------------------------------------------------------------
# SECTION 1: ENHANCED HELPER FUNCTIONS WITH CV COMPARISON
# ----------------------------------------------------------------------------
#' Perform k-fold cross-validation for ridge/elastic net
#' @param X Design matrix
#' @param y Response vector
#' @param P Penalty matrix
#' @param lambda_grid Grid of lambda values
#' @param k Number of folds (default 10)
#' @param type "linear" or "logistic"
#' @return List with optimal lambda and performance metrics
kfold_cv <- function(X, y, P = NULL, lambda_grid, k = 10, type = "linear",
alpha = 1, seed = NULL) {
if (!is.null(seed)) set.seed(seed)
n <- nrow(X)
folds <- sample(rep(1:k, length.out = n))
cv_errors <- matrix(NA, k, length(lambda_grid))
for (fold in 1:k) {
# Split data
idx_train <- which(folds != fold)
idx_val <- which(folds == fold)
X_train <- X[idx_train, , drop = FALSE]
y_train <- y[idx_train]
X_val <- X[idx_val, , drop = FALSE]
y_val <- y[idx_val]
# Fit models for each lambda
for (i in seq_along(lambda_grid)) {
lam <- lambda_grid[i]
if (type == "linear") {
# Standard ridge or custom penalty
if (is.null(P)) {
# Standard ridge
A <- crossprod(X_train) / nrow(X_train)
H <- A + lam * diag(ncol(X))
b <- crossprod(X_train, y_train) / nrow(X_train)
} else {
# Custom penalty matrix
A <- crossprod(X_train) / nrow(X_train)
H <- A + lam * P
b <- crossprod(X_train, y_train) / nrow(X_train)
}
beta_hat <- tryCatch(
solve(H, b),
error = function(e) rep(NA, ncol(X))
)
if (all(is.finite(beta_hat))) {
pred_val <- X_val %*% beta_hat
cv_errors[fold, i] <- mean((y_val - pred_val)^2)
}
} else if (type == "logistic") {
# CORRECTED: Logistic regression with proper error handling
fit <- tryCatch({
irls_penalized_logistic(X_train, y_train,
P = if (is.null(P)) diag(ncol(X)) else P,
lambda = lam)
}, error = function(e) {
list(beta = rep(NA, ncol(X)), converged = FALSE)
})
# Check if we have valid coefficients
if (!is.null(fit$beta) && all(is.finite(fit$beta)) && length(fit$beta) == ncol(X)) {
pred_prob <- tryCatch({
plogis(as.vector(X_val %*% fit$beta))
}, error = function(e) rep(NA, length(y_val)))
if (all(is.finite(pred_prob))) {
# Use deviance for CV error in logistic
cv_errors[fold, i] <- -2 * mean(
y_val * log(pmax(pred_prob, 1e-10)) +
(1 - y_val) * log(pmax(1 - pred_prob, 1e-10))
)
}
}
}
}
}
# Rest of the function remains the same...
mean_cv_error <- colMeans(cv_errors, na.rm = TRUE)
se_cv_error <- apply(cv_errors, 2, sd, na.rm = TRUE) / sqrt(k)
# Find optimal lambda (minimum CV error)
idx_min <- which.min(mean_cv_error)
lambda_opt_cv <- lambda_grid[idx_min]
# One-standard-error rule
threshold <- mean_cv_error[idx_min] + se_cv_error[idx_min]
idx_1se <- min(which(mean_cv_error <= threshold))
lambda_1se <- lambda_grid[idx_1se]
list(
lambda_opt = lambda_opt_cv,
lambda_1se = lambda_1se,
cv_errors = mean_cv_error,
cv_se = se_cv_error,
fold_errors = cv_errors
)
}
#' Comprehensive performance evaluation
#' @param X_test Test design matrix
#' @param y_test Test response
#' @param beta Coefficient vector
#' @param type "linear" or "logistic"
#' @return Named vector of performance metrics
evaluate_performance <- function(X_test, y_test, beta, type = "linear") {
pred <- as.vector(X_test %*% beta)
if (type == "linear") {
mse <- mean((y_test - pred)^2)
rmse <- sqrt(mse)
mae <- mean(abs(y_test - pred))
r2 <- 1 - mse / var(y_test)
return(c(MSE = mse, RMSE = rmse, MAE = mae, R2 = r2))
} else if (type == "logistic") {
prob <- plogis(pred)
# AUC
roc_obj <- roc(y_test, prob, quiet = TRUE)
auc_val <- as.numeric(auc(roc_obj))
# Log-loss
logloss <- -mean(y_test * log(pmax(prob, 1e-10)) +
(1 - y_test) * log(pmax(1 - prob, 1e-10)))
# Accuracy at 0.5 threshold
pred_class <- as.integer(prob > 0.5)
accuracy <- mean(pred_class == y_test)
# Brier score
brier <- mean((prob - y_test)^2)
return(c(AUC = auc_val, LogLoss = logloss,
Accuracy = accuracy, Brier = brier))
}
}
# ----------------------------------------------------------------------------
# SECTION 2: COMPREHENSIVE COMPARISON FRAMEWORK
# ----------------------------------------------------------------------------
#' Run comprehensive comparison of all methods
#' @param X Full design matrix
#' @param y Full response vector
#' @param P Penalty matrix
#' @param test_frac Fraction for test set
#' @param lambda_grid Grid of lambda values
#' @param n_boot Number of bootstrap samples
#' @param type "linear" or "logistic"
#' @return Comprehensive results data frame
comprehensive_comparison <- function(X, y, P, test_frac = 0.3,
lambda_grid = exp(seq(-8, 2, length = 100)),
n_boot = 100, type = "linear",
seed = 123) {
set.seed(seed)
n <- nrow(X)
p <- ncol(X)
# Train-test split
n_test <- round(test_frac * n)
idx_test <- sample(1:n, n_test)
idx_train <- setdiff(1:n, idx_test)
X_train <- X[idx_train, , drop = FALSE]
y_train <- y[idx_train]
X_test <- X[idx_test, , drop = FALSE]
y_test <- y[idx_test]
# Scale penalty matrix
A_train <- crossprod(X_train) / nrow(X_train)
P_scaled <- P / max(1e-12, norm(P, "F")) * norm(A_train, "F")
cat("=== Running Comprehensive Method Comparison ===\n")
cat("Dataset: n =", n, ", p =", p, "\n")
cat("Train size:", length(idx_train), ", Test size:", length(idx_test), "\n\n")
# --------------------------------------------
# 1. ORACLE (Test Set Optimum)
# --------------------------------------------
cat("1. Computing oracle (test set optimum)...\n")
test_performance <- numeric(length(lambda_grid))
for (i in seq_along(lambda_grid)) {
lam <- lambda_grid[i]
if (type == "linear") {
H <- A_train + lam * P_scaled
b <- crossprod(X_train, y_train) / nrow(X_train)
beta_hat <- solve(H, b)
perf <- evaluate_performance(X_test, y_test, beta_hat, type)
test_performance[i] <- perf["MSE"]
} else {
fit <- irls_penalized_logistic(X_train, y_train, P_scaled, lam)
if (all(is.finite(fit$beta))) {
perf <- evaluate_performance(X_test, y_test, fit$beta, type)
test_performance[i] <- perf["AUC"]
} else {
test_performance[i] <- NA
}
}
}
if (type == "linear") {
idx_oracle <- which.min(test_performance)
} else {
idx_oracle <- which.max(test_performance[!is.na(test_performance)])
}
lambda_oracle <- lambda_grid[idx_oracle]
# --------------------------------------------
# 2. STANDARD K-FOLD CV
# --------------------------------------------
cat("2. Running 10-fold cross-validation...\n")
cv_results <- kfold_cv(X_train, y_train, P_scaled, lambda_grid,
k = 10, type = type, seed = seed + 1)
lambda_cv <- cv_results$lambda_opt
lambda_cv_1se <- cv_results$lambda_1se
# --------------------------------------------
# 3. COMMUTATOR METHOD (Type I)
# --------------------------------------------
cat("3. Computing Type I commutator [A, H^{-1}P]...\n")
comm_scores_I <- numeric(length(lambda_grid))
for (i in seq_along(lambda_grid)) {
lam <- lambda_grid[i]
if (type == "linear") {
res <- proper_influence_commutator(lam, X_train, y_train, P_scaled,
mode = "A_HinvP")
} else {
res <- proper_influence_commutator_logistic(lam, X_train, y_train,
P_scaled, mode = "A_HinvP")
}
comm_scores_I[i] <- res$commutator
}
lambda_comm_I <- lambda_grid[which.min(comm_scores_I)]
# --------------------------------------------
# 4. COMMUTATOR METHOD (Type II)
# --------------------------------------------
cat("4. Computing Type II commutator [P, H^{-1}A]...\n")
comm_scores_II <- numeric(length(lambda_grid))
for (i in seq_along(lambda_grid)) {
lam <- lambda_grid[i]
if (type == "linear") {
res <- proper_influence_commutator(lam, X_train, y_train, P_scaled,
mode = "P_HinvA")
} else {
res <- proper_influence_commutator_logistic(lam, X_train, y_train,
P_scaled, mode = "P_HinvA")
}
comm_scores_II[i] <- res$commutator
}
lambda_comm_II <- lambda_grid[which.min(comm_scores_II)]
# --------------------------------------------
# 5. EVALUATE ALL METHODS ON TEST SET
# --------------------------------------------
cat("5. Evaluating all methods on test set...\n")
methods <- c("Oracle", "CV", "CV_1SE", "Comm_I", "Comm_II")
lambdas <- c(lambda_oracle, lambda_cv, lambda_cv_1se,
lambda_comm_I, lambda_comm_II)
results_df <- data.frame(
Method = methods,
Lambda = lambdas,
stringsAsFactors = FALSE
)
# Get test performance for each method
test_metrics <- list()
for (j in 1:length(methods)) {
lam <- lambdas[j]
if (type == "linear") {
H <- A_train + lam * P_scaled
b <- crossprod(X_train, y_train) / nrow(X_train)
beta_hat <- solve(H, b)
} else { # logistic
fit <- irls_penalized_logistic(X_train, y_train, P_scaled, lam)
beta_hat <- fit$beta
}
perf <- evaluate_performance(X_test, y_test, beta_hat, type)
test_metrics[[j]] <- perf
}
# Add performance metrics to results
perf_matrix <- do.call(rbind, test_metrics)
results_df <- cbind(results_df, perf_matrix)
# --------------------------------------------
# 6. BOOTSTRAP STABILITY ANALYSIS
# --------------------------------------------
cat("6. Running bootstrap stability analysis (", n_boot, " samples)...\n")
boot_lambdas <- matrix(NA, n_boot, length(methods))
colnames(boot_lambdas) <- methods
pb <- txtProgressBar(min = 0, max = n_boot, style = 3)
for (b in 1:n_boot) {
# CORRECTED: Sample from training set indices, not original indices
n_train <- nrow(X_train)
idx_boot <- sample(1:n_train, n_train, replace = TRUE) # Use positional indices
X_boot <- X_train[idx_boot, , drop = FALSE]
y_boot <- y_train[idx_boot]
# Skip oracle (uses test set)
boot_lambdas[b, "Oracle"] <- NA
# CV on bootstrap sample
cv_boot <- tryCatch({
kfold_cv(X_boot, y_boot, P_scaled, lambda_grid,
k = 5, type = type, seed = seed + b)
}, error = function(e) NULL)
if (!is.null(cv_boot)) {
boot_lambdas[b, "CV"] <- cv_boot$lambda_opt
boot_lambdas[b, "CV_1SE"] <- cv_boot$lambda_1se
}
# Commutator Type I
comm_I_vals <- sapply(lambda_grid, function(lam) {
tryCatch({
if (type == "linear") {
res <- proper_influence_commutator(lam, X_boot, y_boot, P_scaled,
mode = "A_HinvP")
} else {
res <- proper_influence_commutator_logistic(lam, X_boot, y_boot,
P_scaled, mode = "A_HinvP")
}
res$commutator
}, error = function(e) NA)
})
if (any(!is.na(comm_I_vals))) {
boot_lambdas[b, "Comm_I"] <- lambda_grid[which.min(comm_I_vals)]
}
# Commutator Type II
comm_II_vals <- sapply(lambda_grid, function(lam) {
tryCatch({
if (type == "linear") {
res <- proper_influence_commutator(lam, X_boot, y_boot, P_scaled,
mode = "P_HinvA")
} else {
res <- proper_influence_commutator_logistic(lam, X_boot, y_boot,
P_scaled, mode = "P_HinvA")
}
res$commutator
}, error = function(e) NA)
})
if (any(!is.na(comm_II_vals))) {
boot_lambdas[b, "Comm_II"] <- lambda_grid[which.min(comm_II_vals)]
}
setTxtProgressBar(pb, b)
}
close(pb)
# Compute bootstrap statistics
boot_stats <- data.frame(
Method = methods,
Lambda_Mean = colMeans(boot_lambdas, na.rm = TRUE),
Lambda_SD = apply(boot_lambdas, 2, sd, na.rm = TRUE),
Lambda_CV = apply(boot_lambdas, 2, function(x) sd(x, na.rm = TRUE) /
mean(x, na.rm = TRUE))
)
# --------------------------------------------
# 7. RETURN COMPREHENSIVE RESULTS
# --------------------------------------------
return(list(
summary = results_df,
bootstrap = boot_stats,
lambda_grid = lambda_grid,
test_performance = test_performance,
cv_errors = cv_results$cv_errors,
commutator_I = comm_scores_I,
commutator_II = comm_scores_II,
boot_lambdas = boot_lambdas
))
}
# ----------------------------------------------------------------------------
# SECTION 3: ENHANCED EXPERIMENTS WITH MULTIPLE DATASETS
# ----------------------------------------------------------------------------
#' Generate diverse datasets for testing
#' @param scenario One of "toeplitz", "block", "sparse", "grouped"
#' @param n Sample size
#' @param p Number of features
#' @param type "linear" or "logistic"
#' @return List with X, y, beta_true, P
generate_dataset <- function(scenario = "toeplitz", n = 200, p = 50,
type = "linear", seed = NULL) {
if (!is.null(seed)) set.seed(seed)
# Generate design matrix based on scenario
if (scenario == "toeplitz") {
# Toeplitz correlation structure
rho <- 0.5
Sigma <- toeplitz(rho^(0:(p-1)))
X <- mvrnorm(n, mu = rep(0, p), Sigma = Sigma)
# True coefficients: grouped structure
beta_true <- numeric(p)
beta_true[5:10] <- 2
beta_true[20:25] <- -1.5
beta_true[35:40] <- 1
# Fused penalty
D <- diff(diag(p),