def test_rsolve(): random = RandomState(0) A = random.randn(1, 1) b = random.randn(1) assert_allclose(solve(A, b), npy_solve(A, b)) A = random.randn(2, 2) b = random.randn(2) assert_allclose(solve(A, b), npy_solve(A, b)) A = random.randn(3, 3) b = random.randn(3) assert_allclose(rsolve(A, b), npy_solve(A, b)) A[:] = 1e-10 assert_allclose(rsolve(A, b), zeros(A.shape[1])) A = zeros((0, 0)) b = zeros((0, )) assert_(rsolve(A, b).ndim == 1) assert_(rsolve(A, b).shape[0] == 0) A = zeros((0, 1)) b = zeros((0, )) assert_(rsolve(A, b).ndim == 1) assert_(rsolve(A, b).shape[0] == 1)
def test_solve_raise(): A = array([[2.05036632, 2.05036632], [2.05036632, 2.05036632]]) b = array([0.11260227, 0.11260227]) with pytest.raises(LinAlgError): solve(A, b) A = array([[0.0]]) b = array([1.0]) with pytest.raises(LinAlgError): solve(A, b)
def test_solve(): random = RandomState(0) A = random.randn(1, 1) b = random.randn(1) assert_allclose(solve(A, b), npy_solve(A, b)) A = random.randn(2, 2) b = random.randn(2) assert_allclose(solve(A, b), npy_solve(A, b)) A = random.randn(3, 3) b = random.randn(3) assert_allclose(solve(A, b), npy_solve(A, b))
def _optimal_tbeta(self): self._update() if all(abs(self._M) < 1e-15): return zeros_like(self._tbeta) u = dot(self._tM.T, self._optimal_beta_nom()) Z = self._optimal_tbeta_denom() try: with errstate(all='raise'): self._tbeta = solve(Z, u) except (LinAlgError, FloatingPointError): self._logger.warning('Failed to compute the optimal beta.' + ' Zeroing it.') self.__tbeta[:] = 0. return self.__tbeta
def _update_fixed_effects(self): nominator = self._b1 - self._b0 denominator = self._c1 - self._c0 self._tbeta = solve(denominator, nominator)
def beta(self): r"""Returns :math:`\boldsymbol\beta`.""" return solve(self._svd_V.T, self._tbeta / self._svd_S12)
def fast_scan(self, markers): r"""LMLs of markers by fitting scale and fixed-effect sizes parameters. The likelihood is given by .. math:: \mathcal N\big(~\mathbf y ~|~ \boldsymbol\beta^{\intercal} [\mathrm M ~~ \tilde{\mathrm M}], s \mathrm K~\big), where :math:`s` is the scale parameter and :math:`\boldsymbol\beta` is the fixed-effect sizes; :math:`\tilde{\mathrm M}` is a marker to be scanned. """ assert markers.ndim == 2 nc = self.M.shape[1] M = self.M MTQ0 = dot(M.T, self.Q0) mTQ0 = dot(markers.T, self.Q0) MTQ1 = dot(M.T, self.Q1) mTQ1 = dot(markers.T, self.Q1) yTQ0diag0 = self.yTQ0 / self.diag0 yTQ1diag1 = self.yTQ1 / self.diag1 b0 = yTQ0diag0.dot(MTQ0.T) b0m = yTQ0diag0.dot(mTQ0.T) MTQ0diag0 = MTQ0 / self.diag0 MTQ1diag1 = MTQ1 / self.diag1 c0_00 = MTQ0diag0.dot(MTQ0.T) c0_01 = MTQ0diag0.dot(mTQ0.T) c0_11 = sum((mTQ0 / self.diag0) * mTQ0, axis=1) b1 = yTQ1diag1.dot(MTQ1.T) b1m = yTQ1diag1.dot(mTQ1.T) c1_00 = MTQ1diag1.dot(MTQ1.T) c1_01 = MTQ1diag1.dot(mTQ1.T) c1_11 = sum((mTQ1 / self.diag1) * mTQ1, axis=1) C0 = empty((nc + 1, nc + 1)) C0[:-1, :-1] = c0_00 C1 = empty((nc + 1, nc + 1)) C1[:-1, :-1] = c1_00 n = markers.shape[0] lmls = empty(markers.shape[1]) effect_sizes = empty(markers.shape[1]) LOG2PI = 1.837877066409345339081937709124758839607238769531250 lmls[:] = -n * LOG2PI - n lmls[:] += -sum(log(self.diag0)) - (n - len(self.diag0)) * log( self.diag1) for i in range(markers.shape[1]): b11m = append(b1, b1m[i]) b00m = append(b0, b0m[i]) nominator = b11m - b00m C0[:-1, -1] = c0_01[:, i] C1[:-1, -1] = c1_01[:, i] C0[-1, :-1] = C0[:-1, -1] C1[-1, :-1] = C1[:-1, -1] C0[-1, -1] = c0_11[i] C1[-1, -1] = c1_11[i] denominator = C1 - C0 beta = solve(denominator, nominator) effect_sizes[i] = beta[-1] p0 = self.a1 - 2 * b11m.dot(beta) + beta.dot(C1.dot(beta)) p1 = self.a0 - 2 * b00m.dot(beta) + beta.dot(C0).dot(beta) scale = (p0 + p1) / n lmls[i] -= n * log(scale) lmls /= 2 return lmls, effect_sizes