def test_fullrank(self): X = R.standard_normal((40,10)) X[:,0] = X[:,1] + X[:,2] Y = utils.fullrank(X) self.assertEquals(Y.shape, (40,9)) self.assertEquals(utils.rank(Y), 9) X[:,5] = X[:,3] + X[:,4] Y = utils.fullrank(X) self.assertEquals(Y.shape, (40,8)) self.assertEquals(utils.rank(Y), 8)
def isestimable(C, D): """ From an q x p contrast matrix C and an n x p design matrix D, checks if the contrast C is estimable by looking at the rank of vstack([C,D]) and verifying it is the same as the rank of D. """ if C.ndim == 1: C.shape = (C.shape[0], 1) new = N.vstack([C, D]) if utils.rank(new) != utils.rank(D): return False return True
def initialize(self, design): """ Set design for model, prewhitening design matrix and precomputing covariance of coefficients (up to scale factor in front). :Parameters: design : TODO TODO """ self.design = design self.wdesign = self.whiten(design) self.calc_beta = L.pinv(self.wdesign) self.normalized_cov_beta = N.dot(self.calc_beta, N.transpose(self.calc_beta)) self.df_resid = self.wdesign.shape[0] - utils.rank(self.design)
def contrastfromcols(T, D, pseudo=None): """ From an n x p design matrix D and a matrix T, tries to determine a p x q contrast matrix C which determines a contrast of full rank, i.e. the n x q matrix dot(transpose(C), pinv(D)) is full rank. T must satisfy either T.shape[0] == n or T.shape[1] == p. Note that this always produces a meaningful contrast, not always with the intended properties because q is always non-zero unless T is identically 0. That is, it produces a contrast that spans the column space of T (after projection onto the column space of D). """ n, p = D.shape if T.shape[0] != n and T.shape[1] != p: raise ValueError, "shape of T and D mismatched" if pseudo is None: pseudo = pinv(D) if T.shape[0] == n: C = N.transpose(N.dot(pseudo, T)) else: C = T Tp = N.dot(D, N.transpose(C)) if utils.rank(Tp) != Tp.shape[1]: Tp = utils.fullrank(Tp) C = N.transpose(N.dot(pseudo, Tp)) return N.squeeze(C)
def test_rank(self): X = R.standard_normal((40,10)) self.assertEquals(utils.rank(X), 10) X[:,0] = X[:,1] + X[:,2] self.assertEquals(utils.rank(X), 9)