def test_specify_support_twice(self): # # TODO: Haven't done it for precision yet # oort = 1 / np.sqrt(2) V = np.array([[0.5, oort, 0, 0.5], [0.5, 0, -oort, -0.5], [0.5, -oort, 0, 0.5], [0.5, 0, oort, -0.5]]) # Eigenvalues d = np.array([4, 0, 2, 0], dtype=float) Lmd = np.diag(d) # Covariance matrix K = V.dot(Lmd.dot(V.T)) # # Restrict subspace in two steps # # Field with predefined subspace u = GaussianField(4, K=K, mode='covariance', support=V[:, 0:3]) # Further restrict subspace (automatically) u.update_support() # # Reduce subspace at once # v = GaussianField(4, K=K, mode='covariance', support=V[:, [0, 2]]) # Check that the supports are the same U = u.support() V = v.support() self.assertTrue(np.allclose(U - V.dot(V.T.dot(U)), np.zeros(U.shape))) self.assertTrue(np.allclose(V - U.dot(U.T.dot(V)), np.zeros(V.shape)))
def test_degenerate_sample(self): """ Test support and reduced covariance """ oort = 1 / np.sqrt(2) V = np.array([[0.5, oort, 0, 0.5], [0.5, 0, -oort, -0.5], [0.5, -oort, 0, 0.5], [0.5, 0, oort, -0.5]]) # Eigenvalues d = np.array([4, 3, 2, 0], dtype=float) Lmd = np.diag(d) # Covariance matrix K = V.dot(Lmd.dot(V.T)) # Zero mean Gaussian field u_ex = GaussianField(4, K=K, mode='covariance', support=V[:, 0:3]) u_im = GaussianField(4, K=K, mode='covariance') u_im.update_support() # Check reduced covariances self.assertTrue( np.allclose(u_ex.covariance().get_matrix(), u_im.covariance().get_matrix().toarray())) # Check supports V_ex = u_ex.support() V_im = u_im.support() # Ensure they have the same sign for i in range(V_ex.shape[1]): if V_ex[0, i] < 0: V_ex[:, i] = -V_ex[:, i] if V_im[0, i] < 0: V_im[:, i] = -V_im[:, i] self.assertTrue(np.allclose(V_ex, V_im)) u_ex.set_support(V_ex) u_im.set_support(V_im) # Compare samples z = u_ex.iid_gauss(n_samples=1) u_ex_smp = u_ex.sample(z=z, decomposition='chol') u_im_smp = u_im.sample(z=z, decomposition='chol') self.assertTrue(np.allclose(u_ex_smp, u_im_smp))