From 4b2dc972e6c882756edc394c54adefc1d54fcbb5 Mon Sep 17 00:00:00 2001 From: "david.seb.fischer" Date: Mon, 24 Feb 2020 15:29:07 +0100 Subject: [PATCH 1/3] added singular matrix cchecck into wald test --- diffxpy/stats/stats.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/diffxpy/stats/stats.py b/diffxpy/stats/stats.py index 74c9beb..4f86444 100644 --- a/diffxpy/stats/stats.py +++ b/diffxpy/stats/stats.py @@ -2,6 +2,7 @@ import numpy.linalg import scipy.sparse import scipy.stats +import sys from typing import Union @@ -263,6 +264,7 @@ def wald_test_chisq( raise ValueError('stats.wald_test(): theta_mle and theta0 have to contain the same number of entries') theta_diff = theta_mle - theta0 + invertible = np.where(np.linalg.cond(theta_covar, p=None) < 1 / sys.float_info.epsilon)[0] wald_statistic = np.array([ np.matmul( np.matmul( @@ -271,8 +273,10 @@ def wald_test_chisq( ), theta_diff[:, [i]] ) - for i in range(theta_diff.shape[1]) + for i in invertible ]).flatten() + if invertible.shape[0] < theta_covar.shape[0]: + print("caught %i linalg singular matrix errors" % theta_covar.shape[0] - invertible.shape[0]) pvals = 1 - scipy.stats.chi2(theta_mle.shape[0]).cdf(wald_statistic) return pvals From 93c89718ba1544933aba8278148ec231a8248af5 Mon Sep 17 00:00:00 2001 From: "david.seb.fischer" Date: Mon, 24 Feb 2020 15:32:12 +0100 Subject: [PATCH 2/3] set pval of non invertible matrices to nan --- diffxpy/stats/stats.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/diffxpy/stats/stats.py b/diffxpy/stats/stats.py index 4f86444..86f008a 100644 --- a/diffxpy/stats/stats.py +++ b/diffxpy/stats/stats.py @@ -265,7 +265,8 @@ def wald_test_chisq( theta_diff = theta_mle - theta0 invertible = np.where(np.linalg.cond(theta_covar, p=None) < 1 / sys.float_info.epsilon)[0] - wald_statistic = np.array([ + wald_statistic = np.zeros([theta_covar.shape[0]]) + np.nan + wald_statistic[invertible] = np.array([ np.matmul( np.matmul( theta_diff[:, [i]].T, From 6345cb4b7acbf62871867faa702eb8483d6590df Mon Sep 17 00:00:00 2001 From: "david.seb.fischer" Date: Mon, 24 Feb 2020 15:42:46 +0100 Subject: [PATCH 3/3] fixed reporting of singularity capture bug --- diffxpy/stats/stats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diffxpy/stats/stats.py b/diffxpy/stats/stats.py index 86f008a..236a209 100644 --- a/diffxpy/stats/stats.py +++ b/diffxpy/stats/stats.py @@ -277,7 +277,7 @@ def wald_test_chisq( for i in invertible ]).flatten() if invertible.shape[0] < theta_covar.shape[0]: - print("caught %i linalg singular matrix errors" % theta_covar.shape[0] - invertible.shape[0]) + print("caught %i linalg singular matrix errors" % (theta_covar.shape[0] - invertible.shape[0])) pvals = 1 - scipy.stats.chi2(theta_mle.shape[0]).cdf(wald_statistic) return pvals