Esempio n. 1
0
 def setUp(self):
     np.random.seed(1)
     self._X1 = np.random.randn(10, 5)
     self._X2 = np.random.randn(10, 8)
     self._cov1 = SQExpCov(self._X1)
     self._cov2 = SQExpCov(self._X2)
     self._cov = SumCov(self._cov1, self._cov2)
Esempio n. 2
0
    def test_input(self):
        with self.assertRaises(ValueError):
            SQExpCov(np.array([[np.inf]]))

        with self.assertRaises(ValueError):
            SQExpCov(np.array([[np.nan]]))

        with self.assertRaises(NotArrayConvertibleError):
            SQExpCov("Ola meu querido.")
Esempio n. 3
0
 def setUp(self):
     # np.random.seed(1)
     self._X1 = np.random.randn(10, 5)
     self._X2 = np.random.randn(10, 8)
     self._X3 = np.random.randn(10, 7)
     self._cov1 = SQExpCov(self._X1)
     self._cov2 = SQExpCov(self._X2)
     self._cov3 = SQExpCov(self._X3)
     self._cov = ProdCov(self._cov1, self._cov2, self._cov3)
Esempio n. 4
0
    def build_environmental(self):
        Xtrain, Xstar = self.X[self.train_set, :], self.X[~self.train_set, :]
        if Xstar.shape == (0, 0): Xstar = None
        environmental_cov = SQExpCov(Xtrain, Xstar=Xstar)
        environmental_cov.act_length = False

        return environmental_cov
Esempio n. 5
0
    def simulate_local(self):
        tmp = SQExpCov(self.X)
        tmp.length = self.l2
        k = tmp.K()
        k *= covar_rescaling_factor_efficient(k)

        self.covar += k
Esempio n. 6
0
 def setUp(self):
     np.random.seed(1)
     self._X = np.random.randn(10, 5)
     self._cov = SQExpCov(self._X)
Esempio n. 7
0
def build_model(Kinship, phenotype, N_cells, X, cell_types, test_set=None, intrinsic =True, environment = True,
                environmental_cell_types=None, affected_cell_type=None, by_effective_type=True):

    if test_set is not None and test_set.dtype == bool:
        X_training = X[~test_set, :]
        X_test = X[test_set, :]
        mean_training = phenotype[~test_set]
        N_cells_training = sum(~test_set)
        N_cells_test = N_cells - N_cells_training
        cell_types_training = cell_types[~test_set]
        cell_types_test = cell_types[test_set]

    else:
        X_training = X
        X_test = None
        mean_training = phenotype
        N_cells_training = N_cells
        N_cells_test = 0
        cell_types_training = cell_types

    rm_diag = True
    cov = None

    # list cell types
    cell_type_list = np.unique(cell_types)

    # local_noise
    local_noise_cov = SQExpCov(X_training, Xstar=X_test)
    local_noise_cov.setPenalty(mu=50., sigma=50.)

    # noise
    noise_covs = [None for i in range(len(cell_type_list))]
    for t in cell_type_list:
        cells_selection = (cell_types_training == t) * np.eye(N_cells_training)
        if N_cells_test == 0:
            Kcross = None
        # TODO: adapt to multiple cell types
        else:
            # Kcross = np.concatenate((np.zeros([N_cells_test, N_cells_training]), np.eye(N_cells_test)), axis=1)
            Kcross = np.zeros([N_cells_test, N_cells_training])
        noise_covs[t] = FixedCov(cells_selection, Kcross)
        if cov is None:
            cov = SumCov(local_noise_cov, noise_covs[t])
        else:
            cov = SumCov(cov, noise_covs[t])

    # environment effect: for each pair of cell types
    # t1 is the receiving type, t2 is the effector
    if environment:
        if by_effective_type:
            # env_covs = np.array([len(cell_type_list), len(cell_type_list)])
            env_covs = [[None for i in range(len(cell_type_list))] for j in range(len(cell_type_list))]
        else:
            env_covs = [None for i in range(len(cell_type_list))]
        # env_covs = [tmp] * len(cell_type_list)
        for t1 in cell_type_list:
            if affected_cell_type is not None and affected_cell_type != t1:
                continue
            if by_effective_type:
                for t2 in cell_type_list:
                    # select only the environmental cell type if not all
                    if environmental_cell_types is not None and environmental_cell_types != t2:
                        continue
                    interaction_matrix = build_interaction_matrix(t1, t2, cell_types)
                    tmp = ZKZCov(X, Kinship, rm_diag, interaction_matrix, test_set)
                    env_covs[t1][t2] = tmp
                    env_covs[t1][t2].setPenalty(mu=200., sigma=50.)
                    cov = SumCov(cov, env_covs[t1][t2])
            else:
                interaction_matrix = build_interaction_matrix(t1, 'all', cell_types)
                tmp = ZKZCov(X, Kinship, rm_diag, interaction_matrix, test_set)
                env_covs[t1] = tmp
                env_covs[t1].setPenalty(mu=200., sigma=50.)
                cov = SumCov(cov, env_covs[t1])
    else:
        env_covs = None

    if intrinsic:
        K = build_cell_type_kinship(cell_types_training)
        if N_cells_test != 0:
            Kcross = build_cell_type_kinship(cell_types_test, cell_types_training)
        intrinsic_cov = FixedCov(K, Kcross)
        cov = SumCov(cov, intrinsic_cov)
    else:
        intrinsic_cov = None

    # mean term
    mean = MeanBase(mean_training)

    # define GP
    gp = limix.core.gp.GP(covar=cov, mean=mean)

    print('GP created ')

    return gp, noise_covs, local_noise_cov, env_covs, intrinsic_cov
Esempio n. 8
0
    # generate data
    N = 400
    X = sp.linspace(0,2,N)[:,sp.newaxis]
    v_noise = 0.01
    Y = sp.sin(X) + sp.sqrt(v_noise) * sp.randn(N, 1)

    # for out-of-sample preditions
    Xstar = sp.linspace(0,2,1000)[:,sp.newaxis]

    # define mean term
    W = 1. * (sp.rand(N, 2) < 0.2)
    mean = lin_mean(Y, W)

    # define covariance matrices
    sqexp = SQExpCov(X, Xstar = Xstar)
    noise = FixedCov(sp.eye(N))
    covar  = SumCov(sqexp, noise)

    # define gp
    gp = GP(covar=covar,mean=mean)
    # initialize params
    sqexp.scale = 1e-4
    sqexp.length = 1
    noise.scale = Y.var()
    # optimize
    gp.optimize(calc_ste=True)
    # predict out-of-sample
    Ystar = gp.predict()

    # print optimized values and standard errors