def test_gp_gradient2(): random = RandomState(94584) N = 10 X = random.randn(N, 2) K0 = random.randn(N, N + 1) K0 = K0 @ K0.T + eye(N) * 1e-5 K1 = random.randn(N, N + 1) K1 = K1 @ K1.T + eye(N) * 1e-5 mean = LinearMean(X) mean.effsizes = [-1.2, +0.9] cov0 = GivenCov(K0) cov1 = GivenCov(K1) cov2 = EyeCov(N) cov = SumCov([cov0, cov1, cov2]) cov0.scale = 0.1 cov1.scale = 2.3 cov2.scale = 0.3 y = random.randn(N) gp = GP(y, mean, cov) assert_allclose(gp._check_grad(), 0, atol=1e-5)
def _get_data(): random = RandomState(0) N = 10 X = random.randn(N, N + 1) X -= X.mean(0) X /= X.std(0) X /= sqrt(X.shape[1]) offset = 1.0 mean = OffsetMean(N) mean.offset = offset cov_left = LinearCov(X) cov_left.scale = 1.5 cov_right = EyeCov(N) cov_right.scale = 1.5 cov = SumCov([cov_left, cov_right]) lik = BernoulliProdLik(LogitLink()) y = GGPSampler(lik, mean, cov).sample(random) return dict(mean=mean, cov=cov, lik=lik, y=y, cov_left=cov_left, cov_right=cov_right)
def append_iid_noise(self): from glimix_core.cov import EyeCov cov = EyeCov() cov.set_data((self._sample_idx, self._sample_idx)) self._covariance_matrices["impl"].append(cov) self._covariance_matrices["user"].append(user_cov.EyeCov(cov)) self._covariance_matrices["user"][-1].name = "residual" self._cov = None
def _outcome_sample(random, offset, X): n = X.shape[0] mean = OffsetMean(n) mean.offset = offset cov_left = LinearCov(X) cov_left.scale = 1.5 cov_right = EyeCov(n) cov_right.scale = 1.5 cov = SumCov([cov_left, cov_right]) lik = DeltaProdLik() return GGPSampler(lik, mean, cov).sample(random)
def linear_eye_cov(): from numpy import sqrt from numpy.random import RandomState from glimix_core.cov import EyeCov, LinearCov, SumCov random = RandomState(458) X = random.randn(_nsamples(), _nsamples() + 1) X -= X.mean(0) X /= X.std(0) X /= sqrt(X.shape[1]) cov_left = LinearCov(X) cov_left.scale = 1.0 cov_right = EyeCov(_nsamples()) cov_right.scale = 1.0 return SumCov([cov_left, cov_right])
def append_iid(self, name="residual"): from glimix_core.cov import EyeCov if self._multi_trait(): cov = MTEyeCov(self._y.shape[1]) else: cov = EyeCov(self._y.shape[0]) cov.name = name self._covariance.append(cov)
def test_lmm_predict(): random = RandomState(9458) n = 30 X = random.randn(n, n + 1) X -= X.mean(0) X /= X.std(0) X /= sqrt(X.shape[1]) offset = 1.0 mean = OffsetMean(n) mean.offset = offset cov_left = LinearCov(X) cov_left.scale = 1.5 cov_right = EyeCov(n) cov_right.scale = 1.5 cov = SumCov([cov_left, cov_right]) lik = DeltaProdLik() y = GGPSampler(lik, mean, cov).sample(random) QS = economic_qs_linear(X) lmm = LMM(y, ones((n, 1)), QS) lmm.fit(verbose=False) plmm = LMMPredict(y, lmm.beta, lmm.v0, lmm.v1, lmm.mean(), lmm.covariance()) K = dot(X, X.T) pm = plmm.predictive_mean(ones((n, 1)), K, K.diagonal()) assert_allclose(corrcoef(y, pm)[0, 1], 0.8358820971891354)
def test_ggp_expfam_tobi(): random = RandomState(2) n = 30 ntrials = random.randint(30, size=n) K = random.randn(n, n) K = matmul(K, K.T) lik = BinomialProdLik(ntrials=ntrials, link=LogitLink()) mean = OffsetMean(n) cov2 = EyeCov(n) y = GGPSampler(lik, mean, cov2).sample(random) ggp = ExpFamGP(y, ("binomial", ntrials), mean, cov2) assert_allclose(ggp.lml(), -67.84095700542488) ggp.fit(verbose=False) assert_allclose(ggp.lml(), -64.26701904994792)
def test_eyecov(): cov = EyeCov(2) cov.scale = 1.5 assert_allclose(cov.value(), 1.5 * eye(2)) assert_allclose(cov._check_grad(), 0, atol=1e-5)
def append_iid(self, name="residual"): from glimix_core.cov import EyeCov c = EyeCov(self._y.shape[0]) c.name = name self._covariance.append(c)