Ejemplo n.º 1
0
    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))
Ejemplo n.º 2
0
    def test_gaussian_random_field(self):
        """
        Reproduce statistics of Gaussian random field
        """
        #
        # Define Gaussian Field with degenerate support
        #
        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, 1], dtype=float)
        Lmd = np.diag(d)

        # Covariance matrix
        K = V.dot(Lmd.dot(V.T))

        mu = np.array([1, 2, 3, 4])[:, None]

        # Zero mean Gaussian field
        dim = 4
        eta = GaussianField(dim, mean=mu, K=K, mode='covariance')
        n_vars = eta.covariance().size()
        level = 1

        # Define Gauss-Hermite physicist's rule exp(-x**2)
        grid = Tasmanian.makeGlobalGrid(n_vars, 4, level, "level",
                                        "gauss-hermite-odd")

        # Evaluate the Gaussian random field at the Gauss points
        z = grid.getPoints()
        y = np.sqrt(2) * z

        const_norm = np.sqrt(np.pi)**n_vars

        # Evaluate the random field at the Gauss points
        w = grid.getQuadratureWeights()
        etay = eta.sample(z=y.T)
        n = grid.getNumPoints()
        I = np.zeros(4)
        II = np.zeros((4, 4))
        for i in range(n):
            II += w[i] * np.outer(etay[:, i] - mu.ravel(),
                                  etay[:, i] - mu.ravel())
            I += w[i] * etay[:, i]
        I /= const_norm
        II /= const_norm

        self.assertTrue(np.allclose(II, K))
        self.assertTrue(np.allclose(I, mu.ravel()))
Ejemplo n.º 3
0
    def test_constructor(self):
        #
        # Initialize Gaussian Random Field
        #
        n = 21  # size
        H = 0.5  # Hurst parameter in [0.5,1]

        # Form covariance and precision matrices
        x = np.arange(1, n)
        X, Y = np.meshgrid(x, x)
        K = fbm_cov(X, Y, H)

        # Compute the precision matrix
        I = np.identity(n - 1)
        Q = linalg.solve(K, I)

        # Define SPD matrices
        KK = SPDMatrix(K)
        QQ = SPDMatrix(Q)

        # Define mean
        mean = np.random.rand(n - 1, 1)

        # Define Gaussian field
        u_cov = GaussianField(n - 1, mean=mean, K=K)
        u_prc = GaussianField(n - 1, mean=mean, K=Q, mode='precision')

        # Check vitals
        self.assertTrue(np.allclose(u_cov.covariance().get_matrix(),\
                                    KK.get_matrix()))
        self.assertEqual(u_cov.size(), n - 1)

        self.assertTrue(np.allclose(u_prc.precision().get_matrix(), Q))
        self.assertTrue(np.allclose(u_cov.mean(), mean))
        self.assertEqual(u_prc.mean(n_copies=2).shape[1], 2)
        self.assertTrue(np.allclose(u_prc.b(), QQ.dot(u_prc.mean())))
Ejemplo n.º 4
0
    def test_condition_pointswise(self):
        """
        Generate samples and random field  by conditioning on pointwise data
        """
        #
        # Initialize Gaussian Random Field
        #
        # Resolution
        max_res = 10
        n = 2**max_res + 1  # size

        # Hurst parameter
        H = 0.5  # Hurst parameter in [0.5,1]

        # Form covariance and precision matrices
        x = np.arange(1, n)
        X, Y = np.meshgrid(x, x)
        K = fbm_cov(X, Y, H)

        # Compute the precision matrix
        I = np.identity(n - 1)
        Q = linalg.solve(K, I)

        # Define mean
        mean = np.random.rand(n - 1, 1)

        # Define Gaussian field
        u_cov = GaussianField(n - 1, mean=mean, K=K, mode='covariance')
        u_prc = GaussianField(n - 1, mean=mean, K=Q, mode='precision')

        # Define generating white noise
        z = u_cov.iid_gauss(n_samples=10)

        u_obs = u_cov.sample(z=z)

        # Index of measured observations
        A = np.arange(0, n - 1, 2)

        # observed quantities
        e = u_obs[A, 0][:, None]
        #print('e shape', e.shape)

        # change A into matrix
        k = len(A)
        rows = np.arange(k)
        cols = A
        vals = np.ones(k)
        AA = sp.coo_matrix((vals, (rows, cols)), shape=(k, n - 1)).toarray()

        AKAt = AA.dot(K.dot(AA.T))
        KAt = K.dot(AA.T)

        U, S, Vt = linalg.svd(AA)
        #print(U)
        #print(S)
        #print(Vt)

        #print(AA.dot(u_obs)-e)

        k = e.shape[0]
        Ko = 0.01 * np.identity(k)

        # Debug
        K = u_cov.covariance()
        #U_spp = u_cov.support()
        #A_cmp = A.dot(U_spp)

        u_cond = u_cov.condition(A, e, Ko=Ko, n_samples=100)
        """