Compute Subspace Similarity Between Two Basis Matrices
Source:R/evaluation_wrappers.R
compute_subspace_similarity.RdComputes the similarity between two subspaces spanned by basis matrices using QR decomposition and SVD. This metric measures how well the estimated subspace recovers the true subspace, independent of rotation and scaling.
Value
Numeric scalar in [0, 1], where 1 indicates identical subspaces. Returns NA if inputs are invalid (NULL, empty, or contain NA/NaN/Inf).
Details
The subspace similarity is computed as follows:
Orthonormalize both bases using QR decomposition
Compute the SVD of Q_true' * Q_hat
Return the mean of squared singular values (up to min_dim)
This metric is invariant to rotation and scaling of the basis vectors, making it suitable for comparing factor analysis solutions where rotation indeterminacy exists.
When r_true != r_hat, the metric uses min(r_true, r_hat) dimensions, which allows comparison even when the estimated rank differs from truth.
Examples
if (FALSE) { # \dontrun{
# Same subspace (rotated)
L_true <- matrix(c(1, 0, 0, 1), 2, 2)
R <- matrix(c(cos(pi/4), -sin(pi/4), sin(pi/4), cos(pi/4)), 2, 2)
L_hat <- L_true %*% R
compute_subspace_similarity(L_true, L_hat) # Should be ~1
# Orthogonal subspaces
L_true <- matrix(c(1, 0, 0, 0), 2, 2)
L_hat <- matrix(c(0, 0, 0, 1), 2, 2)
compute_subspace_similarity(L_true, L_hat) # Should be ~0
} # }