Skip to content

feat: convergence criterion overhaul — relative ELBO, composite stop, patience#95

Merged
jc-macdonald merged 5 commits into
mainfrom
feature/issue-54-convergence-overhaul
Apr 12, 2026
Merged

feat: convergence criterion overhaul — relative ELBO, composite stop, patience#95
jc-macdonald merged 5 commits into
mainfrom
feature/issue-54-convergence-overhaul

Conversation

@jc-macdonald
Copy link
Copy Markdown
Collaborator

Closes #54

Summary

Adds three new convergence mechanisms and a patience window to give users
fine-grained control over when VB-PCA iteration terminates.

New options

Option Type Default Description
cfstop_rel float | None None Stop when
cfstop_curv float | None None Stop when
composite_stop dict | None None AND-gate multiple sub-criteria (keys: angle, rms, elbo_rel, elbo_curv)
patience int 1 Require N consecutive triggering iterations before reporting convergence

Changes

  • src/vbpca_py/_converge.py (+276/−32) — Added _relative_elbo_stop(),
    _elbo_curvature_stop(), _cost_criteria(), _composite_stop(),
    _apply_patience() helpers; refactored convergence_check() to a
    list-of-checks pattern to stay within C901 complexity limits.
  • src/vbpca_py/_pca_full.py (+4) — Register new option defaults.
  • src/vbpca_py/_monitoring.py (+1) — Initialize _patience counter in lc dict.
  • justfile (+1/−1) — Fix typecheck recipe to include --extra dev --extra plot
    so matplotlib stubs resolve correctly.
  • tests/test_converge.py (+480) — 23 new unit tests covering all criteria
    and patience behaviour.
  • tests/test_integration.py (+46) — 3 integration tests verifying
    cfstop_rel, composite_stop, and patience in real pca_full fits.

CI

431 tests pass, 90.05% coverage (threshold 89%).

The typecheck recipe called `uv run mypy` without `--extra plot`,
so matplotlib stubs were missing when the venv was created by `uv run`
rather than `just uv-sync`.  Add `--extra dev --extra plot` to match
the sync recipe.
Add two new ELBO-based convergence criteria:

- cfstop_rel: stops when |ELBO[t] - ELBO[t-1]| / |ELBO[t]| < threshold
  (scale-invariant relative decrease).
- cfstop_curv: stops when |ΔELBO[t] - ΔELBO[t-1]| < threshold
  (2nd difference / curvature, detects asymptotic regime).

Both are evaluated after the existing cfstop plateau check, preserving
priority order.  Disabled by default (None).

Refactors cost-related criteria into _cost_criteria() helper to keep
convergence_check() complexity under the C901 limit.

Refs: #54
Add composite_stop option: a dict of sub-criteria that must ALL be
satisfied simultaneously before declaring convergence.  Supported
sub-criteria: angle, rms (relative change), elbo_rel.

Example: composite_stop={'angle': 1e-8, 'rms': 1e-4, 'elbo_rel': 1e-6}

Refactors convergence_check() to use a list-of-checks pattern,
eliminating excessive return statements and complexity.

Refs: #54
Gate all convergence signals through a configurable patience counter
that requires N consecutive triggering iterations before reporting
convergence. Default patience=1 preserves existing behavior.

- Add _apply_patience() helper to _converge.py
- Initialize _patience counter in lc dict (_monitoring.py)
- Add patience option with default 1 to _pca_full.py
- Add 4 tests covering suppression, consecutive trigger, reset, default

Part of #54
Verify cfstop_rel, composite_stop, and patience options work in
end-to-end pca_full fits on a low-rank synthetic matrix.

Part of #54
@jc-macdonald jc-macdonald marked this pull request as ready for review April 12, 2026 23:09
@jc-macdonald jc-macdonald merged commit d8c4141 into main Apr 12, 2026
7 checks passed
@jc-macdonald jc-macdonald deleted the feature/issue-54-convergence-overhaul branch April 12, 2026 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convergence criterion overhaul: relative ELBO, composite criterion, patience window

1 participant