-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#179] Create a new core.model_metrics module and move _calc_mcc() there
- Loading branch information
1 parent
c1f0d8c
commit c166ace
Showing
2 changed files
with
24 additions
and
16 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# This file is part of the ISRDI's hlink. | ||
# For copyright and licensing information, see the NOTICE and LICENSE files | ||
# in this project's top-level directory, and also on-line at: | ||
# https://github.com/ipums/hlink | ||
import math | ||
|
||
|
||
def mcc(tp: int, tn: int, fp: int, fn: int) -> float: | ||
""" | ||
Given the counts of true positives (tp), true negatives (tn), false | ||
positives (fp), and false negatives (fn) for a model run, compute the | ||
Matthews Correlation Coefficient (MCC). | ||
""" | ||
if (math.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn))) != 0: | ||
mcc = ((tp * tn) - (fp * fn)) / ( | ||
math.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)) | ||
) | ||
else: | ||
mcc = 0 | ||
return mcc |
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