def test_inverse_and_logdet(): # Test `Dense`. a = np.random.randn(3, 3) a = Dense(a.dot(a.T)) yield assert_allclose, B.matmul(a, B.inverse(a)), np.eye(3) yield assert_allclose, B.matmul(B.inverse(a), a), np.eye(3) yield assert_allclose, B.logdet(a), np.log(np.linalg.det(dense(a))) # Test `Diagonal`. d = Diagonal([1, 2, 3]) yield assert_allclose, B.matmul(d, B.inverse(d)), np.eye(3) yield assert_allclose, B.matmul(B.inverse(d), d), np.eye(3) yield assert_allclose, B.logdet(d), np.log(np.linalg.det(dense(d))) yield eq, B.shape(B.inverse(Diagonal([1, 2], rows=2, cols=4))), (4, 2) # Test `Woodbury`. a = np.random.randn(3, 2) b = np.random.randn(2, 2) + 1e-2 * np.eye(2) wb = d + LowRank(left=a, middle=b.dot(b.T)) for _ in range(4): yield assert_allclose, B.matmul(wb, B.inverse(wb)), np.eye(3) yield assert_allclose, B.matmul(B.inverse(wb), wb), np.eye(3) yield assert_allclose, B.logdet(wb), np.log(np.linalg.det(dense(wb))) wb = B.inverse(wb) # Test `LowRank`. yield raises, RuntimeError, lambda: B.inverse(wb.lr) yield raises, RuntimeError, lambda: B.logdet(wb.lr)
def test_qf(): # Generate some test inputs. b, c = np.random.randn(5, 3), np.random.randn(5, 3) # Generate some matrices to test. a = np.random.randn(5, 5) a = Dense(a.dot(a.T)) d = Diagonal(B.diag(a)) e = np.random.randn(2, 2) wb = d + LowRank(left=np.random.randn(5, 2), middle=e.dot(e.T)) for x in [a, d, wb]: allclose(B.qf(x, b), np.linalg.solve(to_np(x), b).T.dot(b)) allclose(B.qf(x, b, b), B.qf(x, b)) allclose(B.qf(x, b, c), np.linalg.solve(to_np(x), b).T.dot(c)) allclose(B.qf_diag(x, b), np.diag(np.linalg.solve(to_np(x), b).T.dot(b))) allclose(B.qf_diag(x, b, b), B.qf_diag(x, b, b)) allclose(B.qf_diag(x, b, c), np.diag(np.linalg.solve(to_np(x), b).T.dot(c))) # Test `LowRank`. lr = LowRank(np.random.randn(5, 3)) with pytest.raises(RuntimeError): B.qf(lr, b) with pytest.raises(RuntimeError): B.qf(lr, b, c) with pytest.raises(RuntimeError): B.qf_diag(lr, b) with pytest.raises(RuntimeError): B.qf_diag(lr, b, c)
def test_inverse_and_logdet(): # Test `Dense`. a = np.random.randn(3, 3) a = Dense(a.dot(a.T)) allclose(B.matmul(a, B.inverse(a)), np.eye(3)) allclose(B.matmul(B.inverse(a), a), np.eye(3)) allclose(B.logdet(a), np.log(np.linalg.det(to_np(a)))) # Test `Diagonal`. d = Diagonal(np.array([1, 2, 3])) allclose(B.matmul(d, B.inverse(d)), np.eye(3)) allclose(B.matmul(B.inverse(d), d), np.eye(3)) allclose(B.logdet(d), np.log(np.linalg.det(to_np(d)))) assert B.shape(B.inverse(Diagonal(np.array([1, 2]), rows=2, cols=4))) == (4, 2) # Test `Woodbury`. a = np.random.randn(3, 2) b = np.random.randn(2, 2) + 1e-2 * np.eye(2) wb = d + LowRank(left=a, middle=b.dot(b.T)) for _ in range(4): allclose(B.matmul(wb, B.inverse(wb)), np.eye(3)) allclose(B.matmul(B.inverse(wb), wb), np.eye(3)) allclose(B.logdet(wb), np.log(np.linalg.det(to_np(wb)))) wb = B.inverse(wb) # Test `LowRank`. with pytest.raises(RuntimeError): B.inverse(wb.lr) with pytest.raises(RuntimeError): B.logdet(wb.lr)
def test_root(): # Test `Dense`. a = np.random.randn(5, 5) a = Dense(a.dot(a.T)) yield assert_allclose, a, B.matmul(B.root(a), B.root(a)) # Test `Diagonal`. d = Diagonal(np.array([1, 2, 3, 4, 5])) yield assert_allclose, d, B.matmul(B.root(d), B.root(d))
def test_ratio(): a, b = np.random.randn(4, 4), np.random.randn(4, 4) a, b = Dense(a.dot(a.T)), Dense(b.dot(b.T)) d, e = Diagonal(B.diag(a)), Diagonal(B.diag(b)) c = np.random.randn(3, 3) lr = LowRank(left=np.random.randn(4, 3), middle=c.dot(c.T)) allclose(B.ratio(a, b), np.trace(np.linalg.solve(to_np(b), to_np(a)))) allclose(B.ratio(lr, b), np.trace(np.linalg.solve(to_np(b), to_np(lr)))) allclose(B.ratio(d, e), np.trace(np.linalg.solve(to_np(e), to_np(d))))
def test_sample(): a = np.random.randn(3, 3) a = Dense(a.dot(a.T)) b = np.random.randn(2, 2) wb = Diagonal(B.diag(a)) + \ LowRank(left=np.random.randn(3, 2), middle=b.dot(b.T)) # Test `Dense` and `Woodbury`. num_samps = 500000 for cov in [a, wb]: samps = B.sample(cov, num_samps) cov_emp = B.matmul(samps, samps, tr_b=True) / num_samps yield le, np.mean(np.abs(dense(cov_emp) - dense(cov))), 5e-2