Skip to content

Fix Random Projector magnitudes - #481

Merged
andrewdalpino merged 3 commits into
masterfrom
fix-random-projector-magnitudes
Sep 8, 2026
Merged

Fix Random Projector magnitudes#481
andrewdalpino merged 3 commits into
masterfrom
fix-random-projector-magnitudes

Conversation

@andrewdalpino

Copy link
Copy Markdown
Member

Transformers/GaussianRandomProjector.php:120, SparseRandomProjector.php:82 — [upstream-check] missing the JL 1/sqrt(k) normalization (preserves ratios, not absolute magnitude).

  • GaussianRandomProjector — matrix scaled by ÷ √k at fit (src/Transformers/GaussianRandomProjector.php:122)
  • SparseRandomProjector — dHat changed to 1/√(density · k) (src/Transformers/SparseRandomProjector.php:82)

@andrewdalpino
andrewdalpino requested review from a team and a lite review from Copilot September 8, 2026 01:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

SparseRandomProjector::fit() can still deterministically divide by zero when sparsity is 1.0 (density = 0), producing invalid projection matrices instead of failing fast.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR adjusts the random projection matrix scaling in both GaussianRandomProjector and SparseRandomProjector to include the Johnson–Lindenstrauss (1/\sqrt{k}) normalization so that projected vectors better preserve absolute magnitude (not just relative ratios).

Changes:

  • Scale GaussianRandomProjector’s random matrix by 1 / sqrt(dimensions) during fit().
  • Update SparseRandomProjector’s distribution magnitude (dHat) to 1 / sqrt(density * dimensions).
  • Revise both projector tests to validate approximate magnitude preservation instead of asserting exact projected coordinates.
File summaries
File Description
src/Transformers/GaussianRandomProjector.php Applies JL normalization by dividing the Gaussian matrix by sqrt(k) at fit time.
src/Transformers/SparseRandomProjector.php Updates sparse projection scaling to include dimensions in the normalization factor.
tests/Transformers/GaussianRandomProjectorTest.php Replaces exact-vector assertion with a magnitude-preservation check over a dataset.
tests/Transformers/SparseRandomProjectorTest.php Replaces exact-vector assertion with a magnitude-preservation check over a dataset.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/Transformers/SparseRandomProjector.php
@andrewdalpino
andrewdalpino merged commit 8377484 into master Sep 8, 2026
0 of 12 checks passed
@andrewdalpino
andrewdalpino deleted the fix-random-projector-magnitudes branch September 8, 2026 02:23
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.

2 participants