def test_pca_score2(self): # Test that probabilistic PCA correctly separated different datasets n, p = 100, 3 rng = np.random.RandomState(0) X = mt.tensor(rng.randn(n, p) * .1) + mt.array([3, 4, 5]) for solver in self.solver_list: pca = PCA(n_components=2, svd_solver=solver) pca.fit(X) ll1 = pca.score(X) ll2 = pca.score(mt.tensor(rng.randn(n, p) * .2) + mt.array([3, 4, 5])) self.assertGreater(ll1.fetch(), ll2.fetch()) # Test that it gives different scores if whiten=True pca = PCA(n_components=2, whiten=True, svd_solver=solver) pca.fit(X) ll2 = pca.score(X) assert ll1.fetch() > ll2.fetch()
def test_pca_score(self): # Test that probabilistic PCA scoring yields a reasonable score n, p = 1000, 3 rng = np.random.RandomState(0) X = mt.tensor(rng.randn(n, p) * .1) + mt.array([3, 4, 5]) for solver in self.solver_list: pca = PCA(n_components=2, svd_solver=solver) pca.fit(X) ll1 = pca.score(X) h = -0.5 * mt.log(2 * mt.pi * mt.exp(1) * 0.1**2) * p np.testing.assert_almost_equal((ll1 / h).execute(), 1, 0)
def test_pca_score3(self): # Check that probabilistic PCA selects the right model n, p = 200, 3 rng = np.random.RandomState(0) Xl = mt.tensor(rng.randn(n, p) + rng.randn(n, 1) * np.array([3, 4, 5]) + np.array([1, 0, 7])) Xt = mt.tensor(rng.randn(n, p) + rng.randn(n, 1) * np.array([3, 4, 5]) + np.array([1, 0, 7])) ll = mt.zeros(p) for k in range(p): pca = PCA(n_components=k, svd_solver='full') pca.fit(Xl) ll[k] = pca.score(Xt) assert ll.argmax().execute() == 1