Skip to content

Commit

Permalink
Handle cases when denominator is too small
Browse files Browse the repository at this point in the history
  • Loading branch information
szhan committed Feb 26, 2024
1 parent 7840914 commit 40ac7c7
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions python/tests/beagle_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,15 +749,17 @@ def compute_estimated_allelic_r_squared(gt_probs):
:return: Estimated allelic R-squared.
:rtype: float
"""
_MIN_R2_DEN = 1e-8
n = len(gt_probs) # Number of individuals
z = np.argmax(gt_probs, axis=1) # Most likely imputed genotypes
u = gt_probs[:, 1] + 2 * gt_probs[:, 2] # E[X | y_i]
w = gt_probs[:, 1] + 4 * gt_probs[:, 2] # E[X^2 | y_i]
t_1 = (np.sum(z * u) - (1 / n) * (np.sum(u) * np.sum(z))) ** 2
# TODO: Properly handle cases where terms 2 and/or 3 are 0.
t_2 = np.sum(w) - (1 / n) * np.sum(u) ** 2
t_3 = np.sum(z**2) - (1 / n) * np.sum(z) ** 2
est_allelic_rsq = -1 if t_2 * t_3 == 0 else t_1 / (t_2 * t_3)
den = t_2 * t_3
# Minimum of allelic R^2 is zero. See BEAGLE code.
est_allelic_rsq = 0 if den < _MIN_R2_DEN else t_1 / den
return est_allelic_rsq


Expand Down

0 comments on commit 40ac7c7

Please sign in to comment.