def assert_double_centering_identities(fs, M): """ Raise a Counterexample if one is found. """ A = M[:fs.nsmall, :fs.nsmall] HMH = double_centered_slow(M) HMH_A = HMH[:fs.nsmall, :fs.nsmall] # check an identity between two ways to compute a centered submatrix HAH_direct = double_centered_slow(A) HAH_indirect = double_centered_slow(HMH_A) assert_named_equation( (HAH_direct, 'double centered submatrix'), (HAH_indirect, 're-centered submatrix of doubly centered matrix'), M) HAH = HAH_indirect # This is not true: # check that HAH <==inverse_in_H==> H A^-1 H HAH_pinv = inverse_in_H(HAH) H_Ainv_H = double_centered_slow(np.linalg.inv(A)) """ assert_named_equation( (HAH_pinv, 'inverse-in-H of HAH'), (H_Ainv_H, 'double centered inverse of A')) """ # check a submatrix-schur-inverse commutativity in double centering HMH_pinv = inverse_in_H(HMH) schur_of_HMH_pinv = schur(HMH_pinv, fs.nsmall) assert_named_equation((HAH_pinv, 'inverse-in-H of HAH'), (schur_of_HMH_pinv, 'schur of pinv of HMH'), M)
def assert_double_centering_identities(fs, M): """ Raise a Counterexample if one is found. """ A = M[:fs.nsmall, :fs.nsmall] HMH = double_centered_slow(M) HMH_A = HMH[:fs.nsmall, :fs.nsmall] # check an identity between two ways to compute a centered submatrix HAH_direct = double_centered_slow(A) HAH_indirect = double_centered_slow(HMH_A) assert_named_equation( (HAH_direct, 'double centered submatrix'), (HAH_indirect, 're-centered submatrix of doubly centered matrix')) HAH = HAH_indirect # This is not true: # check that HAH <==inverse_in_H==> H A^-1 H HAH_pinv = inverse_in_H(HAH) H_Ainv_H = double_centered_slow(np.linalg.inv(A)) assert_named_equation( (HAH_pinv, 'inverse-in-H of HAH'), (H_Ainv_H, 'double centered inverse of A')) # check a submatrix-schur-inverse commutativity in double centering HMH_pinv = inverse_in_H(HMH) schur_of_HMH_pinv = schur(HMH_pinv, fs.nsmall) assert_named_equation( (HAH_pinv, 'inverse-in-H of HAH'), (schur_of_HMH_pinv, 'schur of pinv of HMH'))
def assert_pinvproj(fs, M): """ Raise a Counterexample if one is found. """ M_sub = M[:fs.nsmall, :fs.nsmall] pinvproj_of_sub = pinvproj(M_sub) schur_of_pinvproj = schur(pinvproj(M), fs.nsmall) bottduff_of_sub = numpyutils.bott_duffin_const(M_sub) schur_of_bottduff = schur(numpyutils.bott_duffin_const(M), fs.nsmall) """ assert_named_equation( (np.array([[1]]), 'one'), (np.array([[2]]), 'two'), MatrixUtil.double_centered(M)) """ assert_named_equation( (pinvproj_of_sub, 'pinvproj of sub'), (schur_of_pinvproj, 'schur of pinvproj'), double_centered_slow(M)) assert_named_equation( (pinvproj_of_sub, 'pinvproj of sub'), (bottduff_of_sub, 'bottduff of sub'), double_centered_slow(M)) assert_named_equation( (schur_of_pinvproj, 'schur of pinvproj'), (schur_of_bottduff, 'schur of bottduff'), double_centered_slow(M))
def edm_to_gower_matrix(D): """ @param D: an EDM @return: Gower's double centered matrix """ G = -0.5 * double_centered_slow(D) return G
def get_response_content(fs): nbig = 10 nsmall = 4 e = np.ones(nbig) I = np.eye(nbig) Q = np.outer(e, e) / np.inner(e, e) H = I - Q M = sample_sym(nbig) W = double_centered_slow(M) WpQ = W + Q # get eigendecomposition of schur complement W_SC = schur(W, nsmall) W_SC_w, W_SC_vt = scipy.linalg.eigh(W_SC) # get eigendecomposition of schur complement WpQ_SC = schur(WpQ, nsmall) WpQ_SC_w, WpQ_SC_vt = scipy.linalg.eigh(WpQ_SC) # show the results out = StringIO() np.set_printoptions(linewidth=200) print >> out, 'random symmetric matrix:' print >> out, M print >> out, 'after centering:' print >> out, W print >> out, 'after adding a constant eigenvector with unit eigenvalue:' print >> out, WpQ print >> out print >> out, 'eigendecomposition of Schur complement of centered matrix:' print >> out, W_SC_w print >> out, W_SC_vt print >> out print >> out, 'eigendecomposition of Schur complement of decentered matrix:' print >> out, WpQ_SC_w print >> out, WpQ_SC_vt print >> out return out.getvalue()
def assert_pinvproj(fs, M): """ Raise a Counterexample if one is found. """ M_sub = M[:fs.nsmall, :fs.nsmall] pinvproj_of_sub = pinvproj(M_sub) schur_of_pinvproj = schur(pinvproj(M), fs.nsmall) bottduff_of_sub = numpyutils.bott_duffin_const(M_sub) schur_of_bottduff = schur(numpyutils.bott_duffin_const(M), fs.nsmall) """ assert_named_equation( (np.array([[1]]), 'one'), (np.array([[2]]), 'two'), MatrixUtil.double_centered(M)) """ assert_named_equation((pinvproj_of_sub, 'pinvproj of sub'), (schur_of_pinvproj, 'schur of pinvproj'), double_centered_slow(M)) assert_named_equation((pinvproj_of_sub, 'pinvproj of sub'), (bottduff_of_sub, 'bottduff of sub'), double_centered_slow(M)) assert_named_equation((schur_of_pinvproj, 'schur of pinvproj'), (schur_of_bottduff, 'schur of bottduff'), double_centered_slow(M))