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 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)