In the zero-noise limit of probabilistic PCA, EM becomes a fast iterative way to find the principal subspace without forming the covariance matrix. Rows of X (shape (N, D)) are observations; centre them by the sample mean, and let be the (D, N) matrix of centred points as columns.
em_pca(X, L, n_iter, W0): start from W0 (shape (D, L)) and repeat n_iter times:
After the loop, turn the final into an ordered orthonormal basis: take an orthonormal basis of its column span (for example from np.linalg.qr), project the data onto it, rotate by the eigenvectors of the projected data's covariance so that the columns are ordered by decreasing projected variance, then flip the sign of each column so that its entry of largest absolute value is positive (first such entry on ties).
Return (basis, errors): the (D, L) basis and the list of n_iter recorded errors.
principal_angles(A, B): the principal angles in degrees between the column spans of A and B (both (D, L)), ascending, computed from the singular values of (clip them to ).