-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Kajetan Schweighofer <[email protected]>
- Loading branch information
1 parent
fd1c707
commit f3c4c9f
Showing
5 changed files
with
118 additions
and
41 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
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,43 @@ | ||
import numpy as np | ||
|
||
|
||
def get_metric_abs_tolerance(group_size: int) -> float: | ||
"""Reasonable value for metric fulfillment given the inherent randomization | ||
of predictions and the size of the group over which the metric is computed. | ||
""" | ||
return (0.1 * group_size) ** (-1 / 1.7) # tighter for larger groups, less tight for smaller groups | ||
# return group_size ** (-1/2) | ||
|
||
|
||
def check_metric_tolerance( | ||
theory_val: float, | ||
empirical_val: float, | ||
group_size: int, | ||
metric_name: str = "", | ||
less_or_equal: bool = False, | ||
) -> bool: | ||
"""Checks that empirical value approximately matches theoretical value. | ||
Parameters | ||
---------- | ||
theory_val : float | ||
The theoretical value to fulfill for the metrics. | ||
empirical_val : float | ||
The actual realized value for the metric. | ||
group_size : int | ||
The smallest group size over which the metric is evaluated. | ||
metric_name : str, optional | ||
The metric's name, by default "". This is used for debugging purposes. | ||
less_or_equal : bool, optional | ||
Whether a lower empirical value compared to theory is fine, by default | ||
False. | ||
""" | ||
if less_or_equal and empirical_val <= theory_val: | ||
return True | ||
|
||
assert np.isclose( | ||
theory_val, | ||
empirical_val, | ||
atol=get_metric_abs_tolerance(group_size), | ||
rtol=0.01, | ||
), f"> '{metric_name}' mismatch; expected {theory_val:.3}; got {empirical_val:.3};" |