Probabilistic PCA is the latent-variable model z∼N(0,IL), x∣z∼N(Wz+μ,σ2ID), so x∼N(μ,C) with C=WW⊤+σ2I. Its maximum-likelihood fit has a closed form. Rows of X (shape (N, D)) are observations.
ppca_fit(X, L): return (mu, W, sigma2). Use the sample mean and the MLE covariance S=N1∑n(xn−xˉ)(xn−xˉ)⊤ with eigenvalues λ1≥⋯≥λD and unit eigenvectors U. Set σ2=D−L1∑i>Lλi and W=UL(ΛL−σ2I)1/2 with R=I. Before forming W, flip the sign of each column so that its entry of largest absolute value is positive (first such entry on ties) of UL.
ppca_loglik(X, mu, W, sigma2): the total log-likelihood ∑nlogN(xn∣μ,WW⊤+σ2I) as a Python float.
ppca_posterior(X, mu, W, sigma2): the posterior p(z∣x)=N(M−1W⊤(x−μ),σ2M−1) with M=W⊤W+σ2IL. Return (means, cov): the (N, L) posterior means and the shared (L, L) covariance.
Assume 1≤L<D.