|
| 1 | +package com.gu.mediaservice.lib |
| 2 | + |
| 3 | +import org.scalatest.Inspectors |
| 4 | +import org.scalatest.OptionValues |
| 5 | +import org.scalatest.funspec.AnyFunSpec |
| 6 | +import org.scalatest.matchers.should.Matchers |
| 7 | +import org.scalacheck.Gen |
| 8 | +import VectorUtils.{dotProduct, magnitude, cosineSimilarity, firstBasisVector, vectorWithCosineSimilarity} |
| 9 | + |
| 10 | + |
| 11 | +class VectorUtilsTest extends AnyFunSpec with Matchers with Inspectors with OptionValues { |
| 12 | + val tolerance = 1e-9 |
| 13 | + |
| 14 | + describe("dotProduct") { |
| 15 | + it ("should compute the dot product of two vectors") { |
| 16 | + dotProduct(List(1.0, 2.0, 3.0), List(4.0, 5.0, 6.0)) shouldBe 32.0 |
| 17 | + } |
| 18 | + |
| 19 | + it ("should compute a dot product of zero for orthogonal vectors") { |
| 20 | + dotProduct(List(1.0, 0.0), List(0.0, 1.0)) shouldBe 0.0 |
| 21 | + } |
| 22 | + |
| 23 | + it ("should return zero for the dot product of empty vectors") { |
| 24 | + dotProduct(List.empty, List.empty) shouldBe 0.0 |
| 25 | + } |
| 26 | + |
| 27 | + it ("should reject vectors of differing dimensionality") { |
| 28 | + an [IllegalArgumentException] should be thrownBy dotProduct(List(1.0, 2.0), List(1.0, 2.0, 3.0)) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + describe("magnitude") { |
| 33 | + it ("should compute the magnitude of a vector") { |
| 34 | + magnitude(List(3.0, 4.0)) shouldBe 5.0 |
| 35 | + } |
| 36 | + |
| 37 | + it ("should return a magnitude of zero for a zero vector") { |
| 38 | + magnitude(List(0.0, 0.0, 0.0)) shouldBe 0.0 |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + describe("cosineSimilarity") { |
| 43 | + it ("should return a cosine similarity of 1 for identical vectors") { |
| 44 | + cosineSimilarity(List(1.0, 2.0, 3.0), List(1.0, 2.0, 3.0)).value shouldBe 1.0 +- tolerance |
| 45 | + } |
| 46 | + |
| 47 | + it ("should return a cosine similarity of 1 for parallel vectors") { |
| 48 | + cosineSimilarity(List(1.0, 2.0, 3.0), List(2.0, 4.0, 6.0)).value shouldBe 1.0 +- tolerance |
| 49 | + } |
| 50 | + |
| 51 | + it ("should return a cosine similarity of 0 for orthogonal vectors") { |
| 52 | + cosineSimilarity(List(1.0, 0.0), List(0.0, 1.0)).value shouldBe 0.0 +- tolerance |
| 53 | + } |
| 54 | + |
| 55 | + it ("should return a cosine similarity of -1 for opposite vectors") { |
| 56 | + cosineSimilarity(List(1.0, 2.0, 3.0), List(-1.0, -2.0, -3.0)).value shouldBe -1.0 +- tolerance |
| 57 | + } |
| 58 | + |
| 59 | + it ("should be undefined (None) when the first vector has zero magnitude") { |
| 60 | + cosineSimilarity(List(0.0, 0.0, 0.0), List(1.0, 2.0, 3.0)) shouldBe None |
| 61 | + } |
| 62 | + |
| 63 | + it ("should be undefined (None) when the second vector has zero magnitude") { |
| 64 | + cosineSimilarity(List(1.0, 2.0, 3.0), List(0.0, 0.0, 0.0)) shouldBe None |
| 65 | + } |
| 66 | + |
| 67 | + it ("should be undefined (None) when both vectors have zero magnitude") { |
| 68 | + cosineSimilarity(List(0.0, 0.0, 0.0), List(0.0, 0.0, 0.0)) shouldBe None |
| 69 | + } |
| 70 | + |
| 71 | + it ("should be undefined (None) when the vectors differ in dimensionality") { |
| 72 | + cosineSimilarity(List(1.0, 2.0), List(1.0, 2.0, 3.0)) shouldBe None |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // This function is specifically for use in tests, |
| 77 | + // where we need to create test fixtures with specific semantic scores. |
| 78 | + describe("vectorWithCosineSimilarity") { |
| 79 | + val dims = 256 |
| 80 | + |
| 81 | + it ("should create a vector at the requested cosine similarity across a fine sweep of the valid range") { |
| 82 | + val similarities = (-1000 to 1000).map(_ / 1000.0) |
| 83 | + forAll(similarities) { s => |
| 84 | + cosineSimilarity(firstBasisVector(dims), vectorWithCosineSimilarity(dims, s)).value shouldBe s +- tolerance |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + it ("should create a vector at the requested cosine similarity for randomly generated similarities") { |
| 89 | + val similarityGen = Gen.chooseNum(-1.0, 1.0) |
| 90 | + val samples = Gen.listOfN(500, similarityGen).sample.getOrElse(Nil) |
| 91 | + samples should not be empty |
| 92 | + forAll(samples) { s => |
| 93 | + cosineSimilarity(firstBasisVector(dims), vectorWithCosineSimilarity(dims, s)).value shouldBe s +- tolerance |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + it ("should work across a range of dimensions") { |
| 98 | + forAll(List(2, 3, 8, 16, 128, 256, 1024)) { n => |
| 99 | + cosineSimilarity(firstBasisVector(n), vectorWithCosineSimilarity(n, 0.42)).value shouldBe 0.42 +- tolerance |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + it ("should return a vector identical to the basis vector for a similarity of 1") { |
| 104 | + vectorWithCosineSimilarity(dims, 1.0) shouldBe firstBasisVector(dims) |
| 105 | + cosineSimilarity(firstBasisVector(dims), vectorWithCosineSimilarity(dims, 1.0)).value shouldBe 1.0 +- tolerance |
| 106 | + } |
| 107 | + |
| 108 | + it ("should return a vector opposite to the basis vector for a similarity of -1") { |
| 109 | + cosineSimilarity(firstBasisVector(dims), vectorWithCosineSimilarity(dims, -1.0)).value shouldBe -1.0 +- tolerance |
| 110 | + } |
| 111 | + |
| 112 | + it ("should return a vector orthogonal to the basis vector for a similarity of 0") { |
| 113 | + cosineSimilarity(firstBasisVector(dims), vectorWithCosineSimilarity(dims, 0.0)).value shouldBe 0.0 +- tolerance |
| 114 | + } |
| 115 | + |
| 116 | + it ("should negate the vector when the similarity is negated") { |
| 117 | + val positive = vectorWithCosineSimilarity(dims, 0.25) |
| 118 | + val negative = vectorWithCosineSimilarity(dims, -0.25) |
| 119 | + forAll(positive.zip(negative)) { case (p, n) => n shouldBe (-p) +- tolerance } |
| 120 | + } |
| 121 | + |
| 122 | + it ("should produce only finite components for valid similarities") { |
| 123 | + forAll(List(-1.0, -0.5, -1e-3, 0.0, 1e-3, 0.5, 1.0)) { s => |
| 124 | + forAll(vectorWithCosineSimilarity(dims, s)) { component => |
| 125 | + component.isNaN shouldBe false |
| 126 | + component.isInfinite shouldBe false |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + it ("should reject a similarity greater than 1") { |
| 132 | + an [IllegalArgumentException] should be thrownBy vectorWithCosineSimilarity(dims, 1.0001) |
| 133 | + } |
| 134 | + |
| 135 | + it ("should reject a similarity less than -1") { |
| 136 | + an [IllegalArgumentException] should be thrownBy vectorWithCosineSimilarity(dims, -1.0001) |
| 137 | + } |
| 138 | + |
| 139 | + it ("should reject fewer than 2 dimensions") { |
| 140 | + an [IllegalArgumentException] should be thrownBy vectorWithCosineSimilarity(1, 0.5) |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments