def _covariate_setup(self, M): self._M = M SVD = economic_svd(M) self._svd_U = SVD[0] self._svd_S12 = sqrt(SVD[1]) self._svd_V = SVD[2] self._tM = ddot(self._svd_U, self._svd_S12, left=False) if self.__tbeta is not None: self.__tbeta = resize(self.__tbeta, self._tM.shape[1])
def _get_mvn_restricted(y, X, G): SVD = economic_svd(X) tX = ddot(SVD[0], SVD[1]) def mvn_restricted(beta, v0, v1, y, X, K0): n = K0.shape[0] m = X @ beta K = v0 * K0 + v1 * eye(K0.shape[0]) lml = st.multivariate_normal(m, K).logpdf(y) lml += slogdet(tX.T @ tX)[1] / 2 - slogdet(tX.T @ solve(K, tX))[1] / 2 lml += n * log(2 * pi) / 2 lml -= (n - tX.shape[1]) * log(2 * pi) / 2 return lml return mvn_restricted
def __init__(self, A=None, USVt=None): from numpy_sugar.linalg import ddot, economic_svd if A is None and USVt is None: raise ValueError("Both `A` and `USVt` cannot be `None`.") if A is None: self._US = ddot(USVt[0], USVt[1]) self._Vt = USVt[2] self._A = self._US @ self._Vt else: USVt = economic_svd(A) self._US = ddot(USVt[0], USVt[1]) self._Vt = USVt[2] self._A = A self._rank = len(USVt[1])
def test_economic_svd(): random = RandomState(6) A = random.randn(3, 2) A = dot(A, A.T) S = [ [-0.21740668, 0.56064537], [0.21405445, -0.77127452], [-0.95232086, -0.30135094], ] V = [7.65340901, 0.84916508] D = [[-0.21740668, 0.21405445, -0.95232086], [0.56064537, -0.77127452, -0.30135094]] SVD = economic_svd(A) assert_allclose(SVD[0], S) assert_allclose(SVD[1], V) assert_allclose(SVD[2], D)
def __init__(self, y, K, covariates=None, progress=True): super(NormalVarDec, self).__init__( K, covariates=covariates, progress=progress) self._y = y mean = LinearMean(self._covariates.shape[1]) mean.set_data(self._covariates) covs = [] for Ki in iter(K.items()): c = LinearCov() if Ki[1][1]: G = economic_svd(Ki[1][0]) else: G = Ki[1][0] c.set_data((G, G)) covs.append(c) cov = SumCov(covs) self._lmm = SlowLMM(y, mean, cov)
def test_economic_svd_zero_rank(): A = zeros((3, 2)) SVD = economic_svd(A) assert_(SVD[0].shape == (3, 0)) assert_(SVD[1].shape == (0, )) assert_(SVD[2].shape == (0, 2))
def _covariate_setup(self, M): SVD = economic_svd(M) self._svd_U = SVD[0] self._svd_S12 = sqrt(SVD[1]) self._svd_V = SVD[2] self._tM = ddot(self._svd_U, self._svd_S12, left=False)
def __init__(self, y, X, QS=None, restricted=False): """ Constructor. Parameters ---------- y : array_like Outcome. X : array_like Covariates as a two-dimensional array. QS : tuple Economic eigendecompositon in form of ``((Q0, Q1), S0)`` of a covariance matrix ``K``. restricted : bool ``True`` for restricted maximum likelihood optimization; ``False`` otherwise. Defaults to ``False``. """ from numpy_sugar import is_all_finite from numpy_sugar.linalg import ddot, economic_svd logistic = Scalar(0.0) logistic.listen(self._delta_update) logistic.bounds = (-numbers.logmax, +numbers.logmax) Function.__init__(self, "LMM", logistic=logistic) self._logistic = logistic y = asarray(y, float).ravel() if not is_all_finite(y): raise ValueError("There are non-finite values in the outcome.") if len(y) == 0: raise ValueError("The outcome array is empty.") X = atleast_2d(asarray(X, float).T).T if not is_all_finite(X): raise ValueError( "There are non-finite values in the covariates matrix.") self._optimal = {"beta": False, "scale": False} if QS is None: QS = economic_qs_zeros(len(y)) self.delta = 1.0 logistic.fix() else: self.delta = 0.5 if QS[0][0].shape[0] != len(y): msg = "Sample size differs between outcome and covariance decomposition." raise ValueError(msg) if y.shape[0] != X.shape[0]: msg = "Sample size differs between outcome and covariates." raise ValueError(msg) self._Darr = [] n = y.shape[0] d = self.delta if QS[1].size > 0: self._Darr += [QS[1] * (1 - d) + d] if QS[1].size < n: self._Darr += [full(n - QS[1].size, d)] self._y = y self._QS = QS SVD = economic_svd(X) self._X = {"X": X, "tX": ddot(SVD[0], SVD[1]), "VT": SVD[2]} self._tbeta = zeros(len(SVD[1])) self._scale = 1.0 self._fix = {"beta": False, "scale": False} self._restricted = restricted