def test_vardec_multitrait(): random = RandomState(0) nsamples = 20 X = random.randn(nsamples, 2) X = (X - X.mean(0)) / X.std(0) X = concatenate((ones((nsamples, 1)), X), axis=1) lik = "normal" K = random.randn(nsamples, 10) K = K @ K.T K /= K.diagonal().mean() K += eye(nsamples) * 1e-4 y0 = X @ random.randn(3) + mvn(random, zeros(nsamples), K) y1 = X @ random.randn(3) + mvn(random, zeros(nsamples), K) Y = stack((y0, y1), axis=1) vardec = VarDec(Y, lik, X) vardec.append(K) vardec.append_iid() vardec.fit(verbose=False) assert_allclose( vardec.covariance[0].scale, [ [0.7051133960584494, 0.18484625482083472], [0.18484625482083472, 0.048457649666404354], ], rtol=1e-4, ) assert_allclose( vardec.covariance[1].scale, [ [1.4901383238452581e-05, 1.78191074575448e-05], [1.78191074575448e-05, 1.4592715995489043], ], atol=1e-6, rtol=1e-4, ) assert_allclose(vardec.lml(), -17.39609607331073) assert_allclose( vardec.effsizes, [ [-0.6356567160957514, 0.06692298344869516], [-0.3888579153976617, -1.5350512471600464], [-0.13499034585739983, 0.08961009904531193], ], rtol=1e-4, )
def test_qtl_scan_three_hypotheses_mt(): random = RandomState(0) n = 30 ntraits = 2 ncovariates = 3 A = random.randn(ntraits, ntraits) A = A @ A.T M = random.randn(n, ncovariates) C0 = random.randn(ntraits, ntraits) C0 = C0 @ C0.T C1 = random.randn(ntraits, ntraits) C1 = C1 @ C1.T G = random.randn(n, 4) A0 = random.randn(ntraits, 1) A1 = random.randn(ntraits, 2) A01 = concatenate((A0, A1), axis=1) K = random.randn(n, n + 1) K = normalise_covariance(K @ K.T) beta = vec(random.randn(ntraits, ncovariates)) alpha = vec(random.randn(A01.shape[1], G.shape[1])) m = kron(A, M) @ beta + kron(A01, G) @ alpha Y = unvec(mvn(random, m, kron(C0, K) + kron(C1, eye(n))), (n, -1)) idx = [[0, 1], 2, [3]] r = scan(G, Y, idx=idx, K=K, M=M, A=A, A0=A0, A1=A1, verbose=False) str(r)
def test_vardec_2_matrices(): random = RandomState(0) nsamples = 20 X = random.randn(nsamples, 2) X = (X - X.mean(0)) / X.std(0) X = concatenate((ones((nsamples, 1)), X), axis=1) lik = "normal" K = random.randn(nsamples, 10) K = K @ K.T K /= K.diagonal().mean() K += eye(nsamples) * 1e-4 y = X @ random.randn(3) + mvn(random, zeros(nsamples), K) + random.randn(nsamples) vardec = VarDec(y, lik, X) vardec.append(K) vardec.append_iid() vardec.fit(verbose=False) assert_allclose(vardec.covariance[0].scale, 0.32199728815536727, rtol=1e-5) assert_allclose(vardec.covariance[1].scale, 1.4182987383374532, rtol=1e-5) assert_allclose(vardec.lml(), -33.63946372828994, rtol=1e-5)
def test_vardec_poisson_2_matrices(): random = RandomState(0) nsamples = 20 X = random.randn(nsamples, 2) X = (X - X.mean(0)) / X.std(0) X = concatenate((ones((nsamples, 1)), X), axis=1) lik = "poisson" K = random.randn(nsamples, 10) K = K @ K.T K /= K.diagonal().mean() K += eye(nsamples) * 1e-4 y = X @ random.randn(3) + mvn(random, zeros(nsamples), K) y = exp((y - y.mean()) / y.std()) vardec = VarDec(y, lik, X) vardec.append(K) vardec.append_iid() vardec.fit(verbose=False) assert_allclose(vardec.covariance[0].scale, 0.10726852397002325, atol=1e-5) assert_allclose(vardec.covariance[1].scale, 4.168569936272955e-11, atol=1e-5) assert_allclose(vardec.lml(), -26.36419072811823)
def test_vardec(): random = RandomState(0) nsamples = 20 X = random.randn(nsamples, 2) X = (X - X.mean(0)) / X.std(0) X = concatenate((ones((nsamples, 1)), X), axis=1) lik = "normal" K0 = random.randn(nsamples, 10) K0 = K0 @ K0.T K0 /= K0.diagonal().mean() K0 += eye(nsamples) * 1e-4 K1 = random.randn(nsamples, 10) K1 = K1 @ K1.T K1 /= K1.diagonal().mean() K1 += eye(nsamples) * 1e-4 y = X @ random.randn(3) + mvn(random, zeros(nsamples), K0) y += mvn(random, zeros(nsamples), K1) vardec = VarDec(y, lik, X) vardec.append(K0) vardec.append(K1) vardec.append_iid() vardec.fit(verbose=False) assert_allclose(vardec.covariance[0].scale, 0.38473522809891697) assert_allclose(vardec.covariance[1].scale, 1.1839796169221422) assert_allclose(vardec.covariance[2].scale, 2.061153622438558e-09, atol=1e-5) assert_allclose(vardec.lml(), -21.91827344966165) assert_allclose( vardec.effsizes, [-0.5008873352111712, -1.193536406235688, -0.28254298530554534])
def test_vardec_poisson(): random = RandomState(0) nsamples = 20 X = random.randn(nsamples, 2) X = (X - X.mean(0)) / X.std(0) X = concatenate((ones((nsamples, 1)), X), axis=1) lik = "poisson" K0 = random.randn(nsamples, 10) K0 = K0 @ K0.T K0 /= K0.diagonal().mean() K0 += eye(nsamples) * 1e-4 K1 = random.randn(nsamples, 10) K1 = K1 @ K1.T K1 /= K1.diagonal().mean() K1 += eye(nsamples) * 1e-4 y = X @ random.randn(3) + mvn(random, zeros(nsamples), K0) y += mvn(random, zeros(nsamples), K1) y = exp((y - y.mean()) / y.std()) vardec = VarDec(y, lik, X) vardec.append(K0) vardec.append(K1) vardec.append_iid() vardec.fit(verbose=False) assert_allclose(vardec.covariance[0].scale, 2.808478303826397e-09, atol=1e-5) assert_allclose(vardec.covariance[1].scale, 0.3503800007985209) assert_allclose(vardec.covariance[2].scale, 2.061153622438558e-09, atol=1e-5) assert_allclose(vardec.lml(), -28.887285796984564)
def _test_qtl_scan_st(lik): random = RandomState(0) n = 30 ncovariates = 3 M = random.randn(n, ncovariates) v0 = random.rand() v1 = random.rand() G = random.randn(n, 4) K = random.randn(n, n + 1) K = normalise_covariance(K @ K.T) beta = random.randn(ncovariates) alpha = random.randn(G.shape[1]) m = M @ beta + G @ alpha y = mvn(random, m, v0 * K + v1 * eye(n)) idx = [[0, 1], 2, [3]] if lik == "poisson": y = random.poisson(exp(y)) elif lik == "bernoulli": y = random.binomial(1, 1 / (1 + exp(-y))) elif lik == "probit": y = random.binomial(1, st.norm.cdf(y)) elif lik == "binomial": ntrials = random.randint(0, 30, len(y)) y = random.binomial(ntrials, 1 / (1 + exp(-y))) lik = (lik, ntrials) r = scan(G, y, lik=lik, idx=idx, K=K, M=M, verbose=False) str(r) str(r.stats.head()) str(r.effsizes["h2"].head()) str(r.h0.trait) str(r.h0.likelihood) str(r.h0.lml) str(r.h0.effsizes) str(r.h0.variances)
def test_qtl_scan_two_hypotheses_mt_A0A1_none(): random = RandomState(0) n = 30 ntraits = 2 ncovariates = 3 A = random.randn(ntraits, ntraits) A = A @ A.T M = random.randn(n, ncovariates) C0 = random.randn(ntraits, ntraits) C0 = C0 @ C0.T C1 = random.randn(ntraits, ntraits) C1 = C1 @ C1.T G = random.randn(n, 4) A1 = eye(ntraits) K = random.randn(n, n + 1) K = normalise_covariance(K @ K.T) beta = vec(random.randn(ntraits, ncovariates)) alpha = vec(random.randn(A1.shape[1], G.shape[1])) m = kron(A, M) @ beta + kron(A1, G) @ alpha Y = unvec(mvn(random, m, kron(C0, K) + kron(C1, eye(n))), (n, -1)) Y = DataArray(Y, dims=["sample", "trait"], coords={"trait": ["WA", "Cx"]}) idx = [[0, 1], 2, [3]] r = scan(G, Y, idx=idx, K=K, M=M, A=A, verbose=False) df = r.effsizes["h2"] df = df[df["test"] == 0] assert_array_equal(df["trait"], ["WA"] * 3 + ["Cx"] * 3 + [None] * 4) assert_array_equal( df["env"], [None] * 6 + ["env1_WA", "env1_WA", "env1_Cx", "env1_Cx"] ) str(r)