Exemplo n.º 1
0
    def inference(self,
                  kern,
                  X,
                  W,
                  likelihood,
                  Y,
                  mean_function=None,
                  Y_metadata=None,
                  K=None,
                  variance=None,
                  Z_tilde=None):
        """
        Returns a Posterior class containing essential quantities of the posterior
        """

        if mean_function is None:
            m = 0
        else:
            m = mean_function.f(X)

        if variance is None:
            variance = likelihood.gaussian_variance(Y_metadata)

        YYT_factor = Y - m

        if K is None:
            K = kern.K(X)

        Ky = K.copy()

        diag.add(Ky, variance + 1e-8)

        Wi, LW, LWi, W_logdet = pdinv(Ky)

        alpha, _ = dpotrs(LW, YYT_factor, lower=1)

        log_marginal = 0.5 * (-Y.size * log_2_pi - Y.shape[1] * W_logdet -
                              np.sum(alpha * YYT_factor))

        if Z_tilde is not None:
            # This is a correction term for the log marginal likelihood
            # In EP this is log Z_tilde, which is the difference between the
            # Gaussian marginal and Z_EP
            log_marginal += Z_tilde

        dL_dK = 0.5 * (tdot(alpha) - Y.shape[1] * Wi)

        dL_dthetaL = likelihood.exact_inference_gradients(
            np.diag(dL_dK), Y_metadata)

        posterior_ = Posterior(woodbury_chol=LW, woodbury_vector=alpha, K=K)

        return posterior_, log_marginal, {
            'dL_dK': dL_dK,
            'dL_dthetaL': dL_dthetaL,
            'dL_dm': alpha
        }, W_logdet
Exemplo n.º 2
0
def my_predictive_gradient(Xnew, GPmodel, test_point, kern=None):
        """
        Returns gradients of predictive mean, variance, and covariance
        """
        if kern is None:
            kern = GPmodel.kern
        mean_jac = np.empty((Xnew.shape[0],Xnew.shape[1],GPmodel.output_dim))

        for i in range(GPmodel.output_dim):
            mean_jac[:,:,i] = kern.gradients_X(GPmodel.posterior.woodbury_vector[:,i:i+1].T, Xnew, GPmodel._predictive_variable)

        # gradients wrt the diagonal part k_{xx}
        dv_dX = kern.gradients_X(np.eye(Xnew.shape[0]), Xnew)
        #grads wrt 'Schur' part K_{xf}K_{ff}^{-1}K_{fx}
        alpha = -2.*np.dot(kern.K(Xnew, GPmodel._predictive_variable), GPmodel.posterior.woodbury_inv)
        dv_dX += kern.gradients_X(alpha, Xnew, GPmodel._predictive_variable)
        
        # Gradient w.r.t. covariance
        dC_dX = kern.gradients_X(np.eye(Xnew.shape[0]), Xnew, test_point.reshape(1,test_point.size))
        alpha2 = -np.dot(kern.K(test_point.reshape(1,test_point.size), GPmodel._predictive_variable), GPmodel.posterior.woodbury_inv)
        dC_dX += kern.gradients_X(alpha2, Xnew, GPmodel._predictive_variable)
        
        return mean_jac, dv_dX - 2.*dC_dX
        # return mean_jac, dv_dX
Exemplo n.º 3
0
def latent_funs_cov(Z, kernel_list):
    """
    Description: Builds the full-covariance cov[u(z),u(z)] of a Multi-output GP for a Sparse approximation
    :param Z: Inducing Points
    :param kernel_list: Kernels of u_q functions priors
    :return: Kuu
    """
    Q = len(kernel_list)
    M,Dz = Z.shape
    Xdim = int(Dz/Q)
    Kuu = np.empty((Q, M, M))
    Luu = np.empty((Q, M, M))
    Kuui = np.empty((Q, M, M))
    for q, kern in enumerate(kernel_list):
        Kuu[q, :, :] = kern.K(Z[:,q*Xdim:q*Xdim+Xdim],Z[:,q*Xdim:q*Xdim+Xdim])
        Luu[q, :, :] = linalg.jitchol(Kuu[q, :, :])
        Kuui[q, :, :], _ = linalg.dpotri(np.asfortranarray(Luu[q, :, :]))
    return Kuu, Luu, Kuui
def latent_funs_cov(Z, kernel_list):
    """
    Builds the full-covariance cov[u(z),u(z)] of a Multi-output GP
    for a Sparse approximation
    :param Z: Inducing Points
    :param kernel_list: Kernels of u_q functions priors
    :return: Kuu
    """
    Q = len(kernel_list)
    M, Dz = Z.shape
    Xdim = int(Dz / Q)
    #Kuu = np.zeros([Q*M,Q*M])
    Kuu = np.empty((Q, M, M))
    Luu = np.empty((Q, M, M))
    Kuui = np.empty((Q, M, M))
    for q, kern in enumerate(kernel_list):
        Kuu[q, :, :] = kern.K(Z[:, q * Xdim:q * Xdim + Xdim],
                              Z[:, q * Xdim:q * Xdim + Xdim])
        Kuu[q, :, :] = Kuu[
            q, :, :]  #+ 1.0e-6*np.eye(*Kuu[q, :, :].shape)    #This line included by Juan for numerical stability
        Luu[q, :, :] = linalg.jitchol(Kuu[q, :, :], maxtries=10)
        Kuui[q, :, :], _ = linalg.dpotri(np.asfortranarray(Luu[q, :, :]))
    return Kuu, Luu, Kuui