Skip to content

Commit

Permalink
Add alternative formula for verifying geometric mean
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Gaievski <[email protected]>
  • Loading branch information
martin-gaievski committed Aug 3, 2023
1 parent b2aeaae commit f2fdcbe
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ public void testLogic_whenNotAllScoresPresentAndNoWeights_thenCorrectScores() {
testLogic_whenNotAllScoresPresentAndNoWeights_thenCorrectScores(technique);
}

public void testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores() {
List<Float> scores = List.of(1.0f, 0.5f, 0.3f);
List<Double> weights = List.of(0.9, 0.2, 0.7);
ScoreCombinationTechnique technique = new ArithmeticMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
float expectedScore = 0.6722f;
testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, scores, expectedScore);
}

public void testRandomValues_whenAllScoresAndWeightsPresent_thenCorrectScores() {
List<Double> weights = IntStream.range(0, RANDOM_SCORES_SIZE)
.mapToObj(i -> RandomizedTest.randomDouble())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,23 @@ public void testRandomValues_whenNotAllScoresAndWeightsPresent_thenCorrectScores
testRandomValues_whenNotAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
}

/**
* Verify score correctness by using alternative formula for geometric mean as n-th root of product of weighted scores,
* more details in here https://en.wikipedia.org/wiki/Weighted_geometric_mean
*/
private float geometricMean(List<Float> scores, List<Double> weights) {
assertEquals(scores.size(), weights.size());
float sumOfWeights = 0;
float weightedSumOfLn = 0;
for (int i = 0; i < scores.size(); i++) {
float score = scores.get(i), weight = weights.get(i).floatValue();
if (score > 0) {
sumOfWeights += weight;
weightedSumOfLn += weight * Math.log(score);
float product = 1.0f;
float sumOfWeights = 0.0f;
for (int indexOfSubQuery = 0; indexOfSubQuery < scores.size(); indexOfSubQuery++) {
float score = scores.get(indexOfSubQuery);
if (score <= 0) {
// scores 0.0 need to be skipped, ln() of 0 is not defined
continue;
}
float weight = weights.get(indexOfSubQuery).floatValue();
product *= Math.pow(score, weight);
sumOfWeights += weight;
}
return sumOfWeights == 0 ? 0f : (float) Math.exp(weightedSumOfLn / sumOfWeights);
return sumOfWeights == 0 ? 0f : (float) Math.pow(product, (float) 1 / sumOfWeights);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ public void testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores() {
testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, scores, expecteScore);
}

public void testRandomValues_whenAllScoresAndWeightsPresent_thenCorrectScores() {
List<Double> weights = IntStream.range(0, RANDOM_SCORES_SIZE)
.mapToObj(i -> RandomizedTest.randomDouble())
.collect(Collectors.toList());
ScoreCombinationTechnique technique = new HarmonicMeanScoreCombinationTechnique(
Map.of(PARAM_NAME_WEIGHTS, weights),
scoreCombinationUtil
);
testRandomValues_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, weights);
}

public void testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores() {
List<Float> scores = List.of(1.0f, -1.0f, 0.6f);
List<Double> weights = List.of(0.9, 0.2, 0.7);
Expand Down

0 comments on commit f2fdcbe

Please sign in to comment.