feat: data-driven low-rank factor initialization for FactorEM#24
Open
jamgochiana wants to merge 2 commits into
Open
feat: data-driven low-rank factor initialization for FactorEM#24jamgochiana wants to merge 2 commits into
jamgochiana wants to merge 2 commits into
Conversation
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Initialize each FactorEM component's low-rank factor F from the leading principal directions of that component's residual covariance, scaled by the square root of the corresponding eigenvalues (F = Q * Diagonal(sqrt(lambda))), mirroring the PCAEM factorization. The diagonal D is set to the per-feature residual variance not explained by F, floored at a small positive epsilon so the LRDMvNormal positive-diagonal constraint always holds. This replaces the previous scheme where initialize_gmm set F = zeros and m_step! injected an arbitrary, data-scale-independent 0.1*I on the first inner iteration (populating only the first rank features). The new init is data-driven and scale-aware. The 0.1*I branch is kept purely as a safety net for degenerate all-zero factors, now scaled by the residual variance. Edge cases handled: empty clusters (zero factor, global variance), clusters smaller than rank / rank == 0 (zero-padded columns), and the :rand method (analogous eigendecomposition-based init from a random data subset). Non-regression: on fixed-seed synthetic low-rank-plus-diagonal cluster data the new init matches the old 0.1*I converged log-likelihood (delta ~ 0) and is strictly better with few EM iterations (e.g. +5 to +7 nats at nIter=1) because it starts near the true structure. Fitted results change by design. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the arbitrary
0.1*Iinitialization of the FactorEM low-rank factorFwith a data-driven scheme built from each component's residual covariance.Closes #17
The old scheme
initialize_gmmsetF = zeros(n_features, rank), andm_step!detected the all-zero factor on the first inner iteration and replaced it with0.1 * I. Problems: the0.1scale is arbitrary and data-scale-independent, it only populated the firstrankfeatures (identity columns), and it was not data-driven.The new scheme
For each cluster/component,
Fis initialized to the leadingrankprincipal directions of that cluster's residual covariance, scaled by the square root of the corresponding eigenvalues:This is exactly the low-rank factorization PCAEM already uses (
F = P * Q * Diagonal(sqrt(lambda))). The diagonalDis set to the per-feature residual variance not explained by F (total_var - diag(F F')), floored atepsilon * mean(global_var)soDstays strictly positive (required by theLRDMvNormalconstructor).Edge cases handled:
rank, orrank == 0-> remainingFcolumns zero-padded.:randmethod -> analogous eigendecomposition-based init from a random data subset (no longer the arbitrary identity).The
all(iszero, F)branch inm_step!is kept as a safety net for degenerate all-zero factors, but now scaled by the residual variance rather than a fixed0.1.Fitted results change by design
This changes fitted results (approved). The public API (
FactorEMstruct,fitsignatures and return types) is unchanged.Non-regression evidence
On fixed-seed synthetic data with genuine low-rank-plus-diagonal cluster structure (
X = F_true*z + cluster_mean + diagonal noise, d=15, rank 3, 3 clusters):At convergence (15 EM iters), new init matches old (delta ~ 0):
With few EM iterations the new init is strictly better (starts near the true structure):
Tests
Added to
test/test_fitting.jl(all deterministic,Random.seed!):initialize_gmm, each component'sFis finite and correctly shaped andDis strictly positive, across both methods and ranks {0, 2, 5}; factor is non-zero on structured data.Full suite passes (191 fitting tests, all suites green). Unrelated GaussianMixtures "Too low occupancy" warnings are expected.
🤖 Generated with Claude Code