-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ issue #38 ] F-Measure (F1, F0.5 and F2)
- Loading branch information
agazzarini
committed
Sep 16, 2018
1 parent
dd6b1f6
commit cc10d29
Showing
19 changed files
with
550 additions
and
34 deletions.
There are no files selected for viewing
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
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
16 changes: 16 additions & 0 deletions
16
rre-core/src/main/java/io/sease/rre/core/domain/metrics/impl/F0_5.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,16 @@ | ||
package io.sease.rre.core.domain.metrics.impl; | ||
|
||
/** | ||
* A F-measure which weighs recall lower than precision (by attenuating the influence of false negatives). | ||
* | ||
* @author agazzarini | ||
* @since 1.0 | ||
*/ | ||
public class F0_5 extends FMeasure { | ||
/** | ||
* Builds a new F1 metric instance. | ||
*/ | ||
public F0_5() { | ||
super("F0.5", 0.5f); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
rre-core/src/main/java/io/sease/rre/core/domain/metrics/impl/F1.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,16 @@ | ||
package io.sease.rre.core.domain.metrics.impl; | ||
|
||
/** | ||
* The most popular F-Measure, which balances between precisin and recall using 1 as beta factor. | ||
* | ||
* @author agazzarini | ||
* @since 1.0 | ||
*/ | ||
public class F1 extends FMeasure { | ||
/** | ||
* Builds a new F1 metric instance. | ||
*/ | ||
public F1() { | ||
super("F1", 1); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
rre-core/src/main/java/io/sease/rre/core/domain/metrics/impl/F2.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,16 @@ | ||
package io.sease.rre.core.domain.metrics.impl; | ||
|
||
/** | ||
* A F-measure which weighs recall higher than precision (by placing more emphasis on false negatives). | ||
* | ||
* @author agazzarini | ||
* @since 1.0 | ||
*/ | ||
public class F2 extends FMeasure { | ||
/** | ||
* Builds a new F1 metric instance. | ||
*/ | ||
public F2() { | ||
super("F2", 2); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
rre-core/src/main/java/io/sease/rre/core/domain/metrics/impl/FMeasure.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,102 @@ | ||
package io.sease.rre.core.domain.metrics.impl; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import io.sease.rre.Calculator; | ||
import io.sease.rre.core.domain.metrics.Metric; | ||
import io.sease.rre.core.domain.metrics.ValueFactory; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* The F-measure measures the effectiveness of retrieval with respect to a user who attaches β times as much importance to recall as precision. | ||
* In statistical analysis of binary classification, the F1 score (also F-score or F-measure) is a measure of a test's accuracy. | ||
* It considers both the precision p and the recall r of the test to compute the score: p is the number of correct positive results | ||
* divided by the number of all positive results returned by the classifier, and r is the number of correct positive results | ||
* divided by the number of all relevant samples (all samples that should have been identified as positive). | ||
* | ||
* The F1 score is the harmonic average of the precision and recall, where an F1 score reaches its best value at 1 | ||
* (perfect precision and recall) and worst at 0. | ||
* | ||
* (Wikipedia) | ||
* | ||
* @author agazzarini | ||
* @since 1.1 | ||
*/ | ||
public abstract class FMeasure extends Metric { | ||
|
||
private final BigDecimal beta; | ||
final Metric precision = new Precision(); | ||
final Metric recall = new Recall(); | ||
|
||
/** | ||
* Builds a new F-Measure/F-Score metric with the given beta factor. | ||
* | ||
* @param beta the balance factor between precision and recall. | ||
*/ | ||
public FMeasure(final String name, final float beta) { | ||
super(name); | ||
this.beta = BigDecimal.valueOf(beta).pow(2); | ||
} | ||
|
||
@Override | ||
public ValueFactory createValueFactory(final String version) { | ||
return new ValueFactory(this, version) { | ||
@Override | ||
public BigDecimal value() { | ||
final BigDecimal betaPlusOne = Calculator.sum(BigDecimal.ONE, beta); | ||
|
||
final BigDecimal p = precision.valueFactory(version).value(); | ||
final BigDecimal r = recall.valueFactory(version).value(); | ||
|
||
if (p.doubleValue() == 0 || r.doubleValue() == 0) return BigDecimal.ZERO; | ||
|
||
final BigDecimal precisionTimesBeta = Calculator.multiply(p, beta); | ||
|
||
final BigDecimal dividend = Calculator.multiply(p,r); | ||
final BigDecimal divisor = Calculator.sum(precisionTimesBeta,r); | ||
|
||
return Calculator.multiply(betaPlusOne, Calculator.divide(dividend, divisor)); | ||
} | ||
|
||
@Override | ||
public void setTotalHits(long totalHits, String version) { | ||
precision.setTotalHits(totalHits, version); | ||
recall.setTotalHits(totalHits, version); | ||
} | ||
|
||
@Override | ||
public void collect(final Map<String, Object> hit, final int rank, final String version) { | ||
precision.collect(hit, rank, version); | ||
recall.collect(hit, rank, version); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public void setTotalHits(long totalHits, String version) { | ||
super.setTotalHits(totalHits, version); | ||
} | ||
|
||
@Override | ||
public void setRelevantDocuments(JsonNode relevantDocuments) { | ||
super.setRelevantDocuments(relevantDocuments); | ||
precision.setRelevantDocuments(relevantDocuments); | ||
recall.setRelevantDocuments(relevantDocuments); | ||
} | ||
|
||
@Override | ||
public void setVersions(List<String> versions) { | ||
super.setVersions(versions); | ||
precision.setVersions(versions); | ||
recall.setVersions(versions); | ||
} | ||
|
||
@Override | ||
public void setIdFieldName(String idFieldName) { | ||
super.setIdFieldName(idFieldName); | ||
precision.setIdFieldName(idFieldName); | ||
recall.setIdFieldName(idFieldName); | ||
} | ||
} |
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
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
Oops, something went wrong.