-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More and better correlation coefficients for comparing eval configs t…
…o human scores
- Loading branch information
Showing
8 changed files
with
549 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ dependencies = [ | |
"pillow>=11.0.0", | ||
"pystray>=0.19.5", | ||
"pyinstaller==6.11.1", | ||
"scipy>=1.15.2", | ||
] | ||
|
||
|
||
|
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,110 @@ | ||
import math | ||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
from scipy import stats | ||
|
||
|
||
@dataclass | ||
class CorrelationScore: | ||
measured_score: float | ||
human_score: float | ||
normalized_measured_score: float | ||
normalized_human_score: float | ||
|
||
|
||
@dataclass | ||
class CorrelationResult: | ||
mean_absolute_error: float | ||
mean_normalized_absolute_error: float | ||
mean_squared_error: float | ||
mean_normalized_squared_error: float | ||
spearman_correlation: float | ||
pearson_correlation: float | ||
kendalltau_correlation: float | ||
|
||
|
||
class CorrelationCalculator: | ||
def __init__(self): | ||
self.scores: List[CorrelationScore] = [] | ||
|
||
def add_score(self, score: CorrelationScore): | ||
self.scores.append(score) | ||
|
||
def calculate_correlation(self) -> CorrelationResult: | ||
if len(self.scores) == 0: | ||
raise ValueError("No scores to calculate correlation") | ||
|
||
return CorrelationResult( | ||
mean_absolute_error=self.calculate_mean_absolute_error(), | ||
mean_normalized_absolute_error=self.calculate_mean_normalized_absolute_error(), | ||
mean_squared_error=self.calculate_mean_squared_error(), | ||
mean_normalized_squared_error=self.calculate_mean_normalized_squared_error(), | ||
spearman_correlation=self.calculate_spearman_correlation(), | ||
pearson_correlation=self.calculate_pearson_correlation(), | ||
kendalltau_correlation=self.calculate_kendalltau_correlation(), | ||
) | ||
|
||
def calculate_mean_absolute_error(self) -> float: | ||
total_absolute_error = sum( | ||
abs(score.measured_score - score.human_score) for score in self.scores | ||
) | ||
return total_absolute_error / len(self.scores) | ||
|
||
def calculate_mean_normalized_absolute_error(self) -> float: | ||
total_normalized_absolute_error = sum( | ||
abs(score.normalized_measured_score - score.normalized_human_score) | ||
for score in self.scores | ||
) | ||
return total_normalized_absolute_error / len(self.scores) | ||
|
||
def calculate_mean_squared_error(self) -> float: | ||
total_squared_error = sum( | ||
(score.measured_score - score.human_score) ** 2 for score in self.scores | ||
) | ||
return total_squared_error / len(self.scores) | ||
|
||
def calculate_mean_normalized_squared_error(self) -> float: | ||
total_normalized_squared_error = sum( | ||
(score.normalized_measured_score - score.normalized_human_score) ** 2 | ||
for score in self.scores | ||
) | ||
return total_normalized_squared_error / len(self.scores) | ||
|
||
def calculate_spearman_correlation(self) -> float: | ||
if len(self.scores) < 2: | ||
# If there is only one pair, return 0 = no correlation | ||
return 0 | ||
x = [score.measured_score for score in self.scores] | ||
y = [score.human_score for score in self.scores] | ||
result = stats.spearmanr(x, y) | ||
# library doesn't support proper types | ||
correlation = result.__getattribute__("correlation") | ||
if math.isnan(correlation) or not isinstance(correlation, float): | ||
# Very small samples may have a NaN result (unknown correlation) | ||
return 0 | ||
return correlation | ||
|
||
def calculate_pearson_correlation(self) -> float: | ||
if len(self.scores) < 2: | ||
# If there is only one pair, return 0 = no correlation | ||
return 0 | ||
x = [score.measured_score for score in self.scores] | ||
y = [score.human_score for score in self.scores] | ||
result = stats.pearsonr(x, y) | ||
if math.isnan(result.correlation): | ||
# Very small samples may have a NaN result (unknown correlation) | ||
return 0 | ||
return result.correlation | ||
|
||
def calculate_kendalltau_correlation(self) -> float: | ||
if len(self.scores) < 2: | ||
# If there is only one pair, return 0 = no correlation | ||
return 0 | ||
x = [score.measured_score for score in self.scores] | ||
y = [score.human_score for score in self.scores] | ||
result = stats.kendalltau(x, y) | ||
if math.isnan(result.correlation): | ||
# Very small samples may have a NaN result (unknown correlation) | ||
return 0 | ||
return result.correlation |
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.