def test_compute_b_matches_sym(): sigma = 1. X = np.random.randn(10, 2) R = incomplete_cholesky_gaussian(X, sigma, eta=0.1)["R"] x = gaussian_low_rank.compute_b(X, X, R.T, R.T, sigma) y = develop_gaussian_low_rank.compute_b_sym(X, R.T, sigma) assert_allclose(x, y)
def test_fit_sym_time(): sigma = 1. lmbda = 1. N = 20000 Z = np.random.randn(N, 2) R = incomplete_cholesky_gaussian(Z, sigma, eta=0.1)["R"] develop_gaussian_low_rank.fit_sym(Z, sigma, lmbda, L=R.T, cg_tol=1e-1)
def test_compute_b_sym_matches_full(): sigma = 1. Z = np.random.randn(100, 2) low_rank_dim = int(len(Z) * .9) K = gaussian_kernel(Z, sigma=sigma) R = incomplete_cholesky_gaussian(Z, sigma, eta=low_rank_dim)["R"] x = develop_gaussian.compute_b_sym(Z, K, sigma) y = develop_gaussian_low_rank.compute_b_sym(Z, R.T, sigma) assert_allclose(x, y, atol=5e-1)
def test_fit_sym_matches_full(): sigma = 1. lmbda = 1. Z = np.random.randn(100, 2) a = develop_gaussian.fit_sym(Z, sigma, lmbda) R = incomplete_cholesky_gaussian(Z, sigma, eta=0.1)["R"] a_cholesky_cg = develop_gaussian_low_rank.fit_sym(Z, sigma, lmbda, L=R.T) assert_allclose(a, a_cholesky_cg, atol=3)
def fit_wrapper_(self): self.inc_cholesky = incomplete_cholesky_gaussian(self.X, self.sigma, eta=self.eta) L_X = self.inc_cholesky["R"].T logger.debug( "Incomplete Cholesky using rank %d/%d capturing %.3f/1.0 of the variance " % (len(self.inc_cholesky["I"]), len(self.X), self.eta) ) # start optimisation from previous alpha alpha0 = self.alpha if len(self.alpha) == len(self.X) and len(self.alpha) > 0 else np.zeros(len(self.X)) return fit(self.X, self.X, self.sigma, self.lmbda, L_X, L_X, self.cg_tol, self.cg_maxiter, alpha0)
def test_apply_C_left_sym_matches_full(): sigma = 1. N = 10 Z = np.random.randn(N, 2) K = gaussian_kernel(Z, sigma=sigma) R = incomplete_cholesky_gaussian(Z, sigma, eta=0.1)["R"] v = np.random.randn(Z.shape[0]) lmbda = 1. x = (develop_gaussian.compute_C_sym(Z, K, sigma) + lmbda * (K + np.eye(len(K)))).dot(v) y = develop_gaussian_low_rank.apply_left_C_sym(v, Z, R.T, lmbda) assert_allclose(x, y, atol=2e-1, rtol=2e-1)
def test_objective_sym_matches_full(): sigma = 1. lmbda = 1. Z = np.random.randn(100, 2) K = gaussian_kernel(Z, sigma=sigma) a_opt = develop_gaussian.fit_sym(Z, sigma, lmbda, K) J_opt = develop_gaussian.objective_sym(Z, sigma, lmbda, a_opt, K) L = incomplete_cholesky_gaussian(Z, sigma, eta=0.01)["R"].T a_opt_chol = develop_gaussian_low_rank.fit_sym(Z, sigma, lmbda, L) J_opt_chol = develop_gaussian_low_rank.objective_sym(Z, sigma, lmbda, a_opt_chol, L) assert_almost_equal(J_opt, J_opt_chol, delta=2.)
def test_objective_sym_optimum(): sigma = 1. lmbda = 1. Z = np.random.randn(100, 2) L = incomplete_cholesky_gaussian(Z, sigma, eta=0.1)["R"].T a = develop_gaussian_low_rank.fit_sym(Z, sigma, lmbda, L) b = develop_gaussian_low_rank.compute_b_sym(Z, L, sigma) J_opt = develop_gaussian_low_rank.objective_sym(Z, sigma, lmbda, a, L, b) for _ in range(10): a_random = np.random.randn(len(Z)) J = develop_gaussian_low_rank.objective_sym(Z, sigma, lmbda, a_random, L) assert J >= J_opt
def fit_wrapper_(self): self.inc_cholesky = incomplete_cholesky_gaussian(self.X, self.sigma, eta=self.eta) L_X = self.inc_cholesky["R"].T logger.debug("Incomplete Cholesky using rank %d/%d capturing %.3f/1.0 of the variance " % \ (len(self.inc_cholesky['I']), len(self.X), self.eta)) # start optimisation from previous alpha alpha0 = self.alpha if len(self.alpha) == len( self.X) and len(self.alpha) > 0 else np.zeros(len(self.X)) return fit(self.X, self.X, self.sigma, self.lmbda, L_X, L_X, self.cg_tol, self.cg_maxiter, alpha0)
def test_objective_sym_matches_full(): sigma = 1. lmbda = 1. Z = np.random.randn(100, 2) K = gaussian_kernel(Z, sigma=sigma) a_opt = develop_gaussian.fit_sym(Z, sigma, lmbda, K) J_opt = develop_gaussian.objective_sym(Z, sigma, lmbda, a_opt, K) L = incomplete_cholesky_gaussian(Z, sigma, eta=0.01)["R"].T a_opt_chol = develop_gaussian_low_rank.fit_sym(Z, sigma, lmbda, L) J_opt_chol = develop_gaussian_low_rank.objective_sym( Z, sigma, lmbda, a_opt_chol, L) assert_almost_equal(J_opt, J_opt_chol, delta=2.)