def test_lmm_scan_interface(): y = array([-1.0449132, 1.15229426, 0.79595129]) low_rank_K = array([[5.0, 14.0, 23.0], [14.0, 50.0, 86.0], [23.0, 86.0, 149.0]]) QS = economic_qs(low_rank_K) X = ones((3, 1)) y[0] = nan with pytest.raises(ValueError): FastScanner(y, X, QS, 0.5) y[0] = inf with pytest.raises(ValueError): FastScanner(y, X, QS, 0.5) y[0] = 1 X[0, 0] = nan with pytest.raises(ValueError): FastScanner(y, X, QS, 0.5) y[0] = 1 X[0, 0] = 1 with pytest.raises(ValueError): FastScanner(y, X, QS, -1) with pytest.raises(ValueError): FastScanner(y, X, QS, nan)
def test_lmm_kron_scan_with_lmm(): random = RandomState(0) n = 15 Y = random.randn(n, 3) A = random.randn(3, 3) A = A @ A.T F = random.randn(n, 2) G = random.randn(n, 6) klmm = Kron2Sum(Y, A, F, G, restricted=True) klmm.fit(verbose=False) kscan = klmm.get_fast_scanner() K = klmm.covariance() X = kron(A, F) QS = economic_qs(K) scan = FastScanner(vec(Y), X, QS, 0.0) assert_allclose(klmm.covariance(), K) assert_allclose(kscan.null_scale, scan.null_scale) assert_allclose(kscan.null_beta, scan.null_beta) assert_allclose(kscan.null_lml(), scan.null_lml()) assert_allclose(kscan.null_beta_covariance, scan.null_beta_covariance) A1 = random.randn(3, 2) F1 = random.randn(n, 2) M = kron(A1, F1) kr = kscan.scan(A1, F1) r = scan.scan(M) assert_allclose(kr["lml"], r["lml"]) assert_allclose(kr["scale"], r["scale"]) assert_allclose(vec(kr["effsizes0"]), r["effsizes0"]) assert_allclose(vec(kr["effsizes1"]), r["effsizes1"]) assert_allclose(vec(kr["effsizes0_se"]), r["effsizes0_se"]) assert_allclose(vec(kr["effsizes1_se"]), r["effsizes1_se"])
def test_lmm_scan_very_low_rank(): y = array([-1.0449132, 1.15229426, 0.79595129, 2.1]) X = array([ [-0.40592765, 1.04348945], [0.92275415, -0.32394197], [-0.98197991, 1.22912219], [-1.0007991, 2.22912219], ]) G = array([ [-0.14505449, -1.1000817], [0.45714984, 1.82214436], [-1.23763742, 1.38771103], [-2.27377329, 0.9577192], ]) K = G @ G.T low_rank_QS = economic_qs(K) M = array([ [0.88766985, -1.80940339], [0.00822629, -0.4488265], [0.55807272, -2.00868376], [3.2, 2.1], ]) scanner = FastScanner(y, X, low_rank_QS, 0) r = scanner.fast_scan(M, verbose=False) assert_allclose(r["lml"], [46.512791016862764, 46.512791016862764], atol=1e-6, rtol=1e-6) assert_allclose( r["effsizes0"], [ [3.8616635463341358, 0.43233789455471455], [4.534162667593971, 3.573393734139044], ], ) assert_allclose(r["effsizes1"], [2.1553245206596263, -0.684698367443129], atol=1e-6, rtol=1e-6) assert_allclose(r["scale"], [5.551115123125783e-17, 2.5326962749261384e-16], atol=1e-6) X = ones((4, 1)) M = array([ [0.88766985, -1.80940339], [0.00822629, -0.4488265], [0.55807272, -2.00868376], [3.2, 2.1], ]) scanner = FastScanner(y, X, low_rank_QS, 0.75) r = scanner.fast_scan(M, verbose=False) assert_allclose(r["lml"], [-3.988506684733393, -2.852200552237104]) assert_allclose(r["effsizes0"], [[-0.4955288599792398], [0.36297469139979893]]) assert_allclose(r["effsizes1"], [0.5929013274071214, 0.36216887594630626]) assert_allclose(r["scale"], [0.18324637118292808, 0.10382205995195082], atol=1e-6)
def test_lmm_scan_difficult_settings_multicovariates(): y = array([-1.0449132, 1.15229426, 0.79595129]) low_rank_K = array([[5.0, 14.0, 23.0], [14.0, 50.0, 86.0], [23.0, 86.0, 149.0]]) full_rank_K = array([[6.0, 14.0, 23.0], [14.0, 51.0, 86.0], [23.0, 86.0, 150.0]]) low_rank_QS = economic_qs(low_rank_K) full_rank_QS = economic_qs(full_rank_K) X = array( [ [-0.40592765, 1.04348945], [0.92275415, -0.32394197], [-0.98197991, 1.22912219], ] ) M = array( [[0.88766985, -1.80940339], [0.00822629, -0.4488265], [0.55807272, -2.00868376]] ) scanner = FastScanner(y, X, low_rank_QS, 0) r = scanner.fast_scan(M, verbose=False) assert_allclose(r["lml"], [28.896466, 28.896466], atol=1e-6, rtol=1e-6) assert_allclose( r["effsizes0"], [ [-2.247703714834354, 0.8190282920748556], [-3.1760541039273127, 0.40265319454969883], ], ) assert_allclose(r["effsizes1"], [-1.195493, 0.318417], atol=1e-6, rtol=1e-6) assert_allclose( r["scale"], [1.850371707708594e-17, 1.3877787807814457e-17], atol=1e-6 ) X = ones((3, 1)) M = array( [[0.88766985, -1.80940339], [0.00822629, -0.4488265], [0.55807272, -2.00868376]] ) scanner = FastScanner(y, X, low_rank_QS, 0.75) r = scanner.fast_scan(M, verbose=False) assert_allclose(r["lml"], [0.823505300549, -0.607251148058]) assert_allclose(r["effsizes0"], [[0.3495610486128161], [0.21723441738440089]]) assert_allclose(r["effsizes1"], [-1.79486991632, 0.872439001906]) assert_allclose(r["scale"], [0.00476137752581085, 0.012358877799161524], atol=1e-6) M = array([[0.0, -1.80940339], [0.0, -0.4488265], [0.0, -2.00868376]]) scanner = FastScanner(y, X, low_rank_QS, 0) r = scanner.fast_scan(M, verbose=False) assert_allclose(r["lml"], [8.704135, 28.896466]) assert_allclose(r["effsizes0"], [[-0.9261322100000001], [-12.837513658497771]]) assert_allclose(r["effsizes1"][1], -9.23756057859) assert_allclose(r["scale"], [0.010459203921428906, 0.0], atol=1e-6) M = array([[0.0, -1.80940339], [0.0, -0.4488265], [0.0, -2.00868376]]) scanner = FastScanner(y, X, low_rank_QS, 0.75) r = scanner.fast_scan(M, verbose=False) assert_allclose(r["lml"], [-6.137441, -0.607251], atol=1e-6, rtol=1e-6) assert_allclose(r["effsizes0"], [[-0.9093206621461191], [0.21723441738440089]]) assert_allclose(r["effsizes1"][1], 0.872439001906) assert_allclose(r["scale"], [0.49332480112228455, 0.012358877799161524], atol=1e-6) M = array( [[0.88766985, -1.80940339], [0.00822629, -0.4488265], [0.55807272, -2.00868376]] ) scanner = FastScanner(y, X, full_rank_QS, 0) r = scanner.fast_scan(M, verbose=False) assert_allclose(r["lml"], [0.636421389625, -0.795311239268]) assert_allclose(r["effsizes1"], [-1.79757295968, 0.871806031702]) assert_allclose(r["scale"], [0.004736354793149378, 0.012301930830306581], atol=1e-6) M = array( [[0.88766985, -1.80940339], [0.00822629, -0.4488265], [0.55807272, -2.00868376]] ) scanner = FastScanner(y, X, full_rank_QS, 0.75) r = scanner.fast_scan(M, verbose=False) assert_allclose(r["lml"], [0.251547678451, -1.18305656511]) assert_allclose(r["effsizes0"], [[0.3717198167493795], [0.2377661184233066]]) assert_allclose(r["effsizes1"], [-1.80551427016, 0.86994164292]) assert_allclose(r["scale"], [0.004662840142176661, 0.012134195778553977], atol=1e-6) M = array([[0.0, -1.80940339], [0.0, -0.4488265], [0.0, -2.00868376]]) scanner = FastScanner(y, X, full_rank_QS, 0) r = scanner.fast_scan(M, verbose=False) assert_allclose(r["lml"], [-5.911136, -0.795311], atol=1e-6, rtol=1e-6) assert_allclose(r["effsizes0"], [[-0.9038187010303032], [0.2224382967889082]]) assert_allclose(r["effsizes1"][1], 0.871806031702) assert_allclose(r["scale"], [0.3725256920525814, 0.012301930830306581], atol=1e-6) M = array([[0.0, -1.80940339], [0.0, -0.4488265], [0.0, -2.00868376]]) scanner = FastScanner(y, X, full_rank_QS, 0.75) r = scanner.fast_scan(M, verbose=False) assert_allclose(r["lml"], [-5.509792, -1.183057], atol=1e-6, rtol=1e-6) assert_allclose(r["effsizes0"], [[-0.8876088873393126], [0.2377661184233066]]) assert_allclose(r["effsizes1"], [0.0, 0.8699416429204303]) assert_allclose(r["scale"], [0.21713469123194049, 0.012134195778553977], atol=1e-6)
def _test_fast_scanner_effsizes_se(K0, QS, v): X = ones((3, 1)) y = array([-1.0449132, 1.15229426, 0.79595129]) M = array([[0.887669, -1.809403], [0.008226, -0.44882], [0.558072, -2.008683]]) scanner = FastScanner(y, X, QS, v) r = scanner.fast_scan(M, verbose=False) for i in range(2): K = r["scale"][i] * (K0 + v * eye(3)) XM = concatenate((X, M[:, i : (i + 1)]), axis=1) effsizes_se = sqrt(abs(pinv(XM.T @ solve(K, XM)).diagonal())) se = concatenate((r["effsizes0_se"][i], r["effsizes1_se"][i : (i + 1)])) assert_allclose(se, effsizes_se, atol=1e-5) # Redundant covariates M = array([[0, 1], [0, 1], [0, 1]]) scanner = FastScanner(y, X, QS, v) r = scanner.fast_scan(M, verbose=False) for i in range(2): K = r["scale"][i] * (K0 + v * eye(3)) XM = concatenate((X, M[:, i : (i + 1)]), axis=1) effsizes_se = sqrt(abs(pinv(XM.T @ solve(K, XM)).diagonal())) se = concatenate((r["effsizes0_se"][i], r["effsizes1_se"][i : (i + 1)])) assert_allclose(se, effsizes_se, atol=1e-5) M = array([[0.887669, -1.809403], [0.008226, -0.44882], [0.558072, -2.008683]]) scanner = FastScanner(y, X, QS, v) r = scanner.scan(M) K = r["scale"] * (K0 + v * eye(3)) XM = concatenate((X, M), axis=1) effsizes_se = sqrt(abs(pinv(XM.T @ solve(K, XM)).diagonal())) se = concatenate((r["effsizes0_se"], r["effsizes1_se"])) assert_allclose(se, effsizes_se, atol=1e-5) # Redundant covariates M = array([[0, 1], [0, 1], [0, 1]]) scanner = FastScanner(y, X, QS, v) r = scanner.scan(M) K = r["scale"] * (K0 + v * eye(3)) XM = concatenate((X, M), axis=1) effsizes_se = sqrt(abs(pinv(XM.T @ solve(K, XM)).diagonal())) se = concatenate((r["effsizes0_se"], r["effsizes1_se"])) assert_allclose(se, effsizes_se, atol=1e-5)