-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add geometric mean normalization for scores
Signed-off-by: Martin Gaievski <[email protected]>
- Loading branch information
1 parent
1f67b94
commit 6e3b996
Showing
7 changed files
with
197 additions
and
9 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...opensearch/neuralsearch/processor/combination/GeometricMeanScoreCombinationTechnique.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.neuralsearch.processor.combination; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Abstracts combination of scores based on geometrical mean method | ||
*/ | ||
public class GeometricMeanScoreCombinationTechnique implements ScoreCombinationTechnique { | ||
|
||
public static final String TECHNIQUE_NAME = "geometric_mean"; | ||
public static final String PARAM_NAME_WEIGHTS = "weights"; | ||
private static final Set<String> SUPPORTED_PARAMS = Set.of(PARAM_NAME_WEIGHTS); | ||
private static final Float ZERO_SCORE = 0.0f; | ||
private final List<Float> weights; | ||
private final ScoreCombinationUtil scoreCombinationUtil; | ||
|
||
public GeometricMeanScoreCombinationTechnique(final Map<String, Object> params, final ScoreCombinationUtil combinationUtil) { | ||
scoreCombinationUtil = combinationUtil; | ||
scoreCombinationUtil.validateParams(params, SUPPORTED_PARAMS); | ||
weights = scoreCombinationUtil.getWeights(params); | ||
} | ||
|
||
/** | ||
* Weighted geometric mean method for combining scores. | ||
* | ||
* We use formula below to calculate mean. It's based on fact that logarithm of geometric mean is the | ||
* weighted arithmetic mean of the logarithms of individual scores. | ||
* | ||
* geometric_mean = exp(sum(weight_1*ln(score_1) + .... + weight_n*ln(score_n))/sum(weight_1 + ... + weight_n)) | ||
*/ | ||
@Override | ||
public float combine(final float[] scores) { | ||
float weightedLnSum = 0; | ||
float sumOfWeights = 0; | ||
for (int indexOfSubQuery = 0; indexOfSubQuery < scores.length; indexOfSubQuery++) { | ||
float score = scores[indexOfSubQuery]; | ||
if (score <= 0) { | ||
// scores 0.0 need to be skipped, ln() of 0 is not defined | ||
continue; | ||
} | ||
float weight = scoreCombinationUtil.getWeightForSubQuery(weights, indexOfSubQuery); | ||
sumOfWeights += weight; | ||
weightedLnSum += weight * Math.log(score); | ||
} | ||
return sumOfWeights == 0 ? ZERO_SCORE : (float) Math.exp(weightedLnSum / sumOfWeights); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
62 changes: 62 additions & 0 deletions
62
...earch/neuralsearch/processor/combination/GeometricMeanScoreCombinationTechniqueTests.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.neuralsearch.processor.combination; | ||
|
||
import static org.opensearch.neuralsearch.processor.combination.ArithmeticMeanScoreCombinationTechnique.PARAM_NAME_WEIGHTS; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class GeometricMeanScoreCombinationTechniqueTests extends BaseScoreCombinationTechniqueTests { | ||
|
||
private ScoreCombinationUtil scoreCombinationUtil = new ScoreCombinationUtil(); | ||
|
||
public GeometricMeanScoreCombinationTechniqueTests() { | ||
this.expectedScoreFunction = this::geometricMean; | ||
} | ||
|
||
public void testLogic_whenAllScoresPresentAndNoWeights_thenCorrectScores() { | ||
ScoreCombinationTechnique technique = new GeometricMeanScoreCombinationTechnique(Map.of(), scoreCombinationUtil); | ||
testLogic_whenAllScoresPresentAndNoWeights_thenCorrectScores(technique); | ||
} | ||
|
||
public void testLogic_whenNotAllScoresPresentAndNoWeights_thenCorrectScores() { | ||
ScoreCombinationTechnique technique = new GeometricMeanScoreCombinationTechnique(Map.of(), scoreCombinationUtil); | ||
testLogic_whenNotAllScoresPresentAndNoWeights_thenCorrectScores(technique); | ||
} | ||
|
||
public void testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores() { | ||
List<Double> weights = List.of(0.9, 0.2, 0.7); | ||
ScoreCombinationTechnique technique = new GeometricMeanScoreCombinationTechnique( | ||
Map.of(PARAM_NAME_WEIGHTS, weights), | ||
scoreCombinationUtil | ||
); | ||
testLogic_whenAllScoresAndWeightsPresent_thenCorrectScores(technique, weights); | ||
} | ||
|
||
public void testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores() { | ||
List<Double> weights = List.of(0.9, 0.2, 0.7); | ||
ScoreCombinationTechnique technique = new GeometricMeanScoreCombinationTechnique( | ||
Map.of(PARAM_NAME_WEIGHTS, weights), | ||
scoreCombinationUtil | ||
); | ||
testLogic_whenNotAllScoresAndWeightsPresent_thenCorrectScores(technique, weights); | ||
} | ||
|
||
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); | ||
} | ||
} | ||
return sumOfWeights == 0 ? 0f : (float) Math.exp(weightedSumOfLn / sumOfWeights); | ||
} | ||
} |
This file contains 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
This file contains 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