Repository navigation
Fix CosineSimilarity precision so identical inputs give 1.0 and distance 0.0 - #789
Open
atharv1205 wants to merge 1 commit into
Open
atharv1205 wants to merge 1 commit into
atharv1205 wants to merge 1 commit into
Conversation
CosineSimilarity divided the dot product by Math.sqrt(d1) * Math.sqrt(d2). Rounding each square root separately can leave the product one ulp off, so identical vectors gave 1.0000000000000002 or 0.9999999999999998 and CosineDistance returned small non-zero, even negative, values such as -2.220446049250313E-16 for "a b c". Compute dotProduct / Math.sqrt(d1 * d2) instead. For identical vectors d1 * d2 is an exact square, so the result is exactly 1.0. Clamp with Math.min(1.0, ...) so rounding on other inputs cannot exceed 1.0. Add tests that identical inputs give similarity 1.0 and distance 0.0, and that both stay within [0.0, 1.0] over generated inputs.
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.
What
CosineSimilarity.cosineSimilaritycan return values slightly outside [0.0, 1.0] for identical inputs, soCosineDistancereturns tiny non-zero, even negative, distances:CosineDistance.apply("a b", "a b")CosineDistance.apply("a b c", "a b c")CosineDistance.apply("a b c d e f", "a b c d e f")Over 10,000 random identical token maps, 2093 returned a similarity above 1.0 before this change.
Why
The denominator was
Math.sqrt(d1) * Math.sqrt(d2). Each square root is rounded separately and their product can be off by one ulp in either direction, sodot / normmisses 1.0 for identical vectors.How
Compute
dotProduct / Math.sqrt(d1 * d2). For identical vectorsd1 * d2is an exact square, andMath.sqrtis correctly rounded, so the result is exactly 1.0. The result is also clamped withMath.min(1.0, ...)so rounding on other inputs cannot exceed 1.0. Thed1 <= 0 || d2 <= 0branch is unchanged.Tests
CosineSimilarityTest: identical vectors of several sizes give exactly 1.0; similarity stays within [0.0, 1.0] over generated identical and overlapping vectors.CosineDistanceTest: identical inputs ("a", "a b", "a b c", "a b c d e f", ...) give exactly 0.0; distance stays within [0.0, 1.0] over generated inputs.mvn; that'smvnon the command line by itself.