コード例 #1
0
ファイル: gp_clvm.py プロジェクト: senaj/scLVM
    def __init__(self,
                 Y=None,
                 X0=None,
                 k=1,
                 standardize=False,
                 interaction=True):
        """
		Y:		data [NxG]
		X0:		known latent factors [Nxk0]
		k:		number of latent factors to infer
		"""

        assert Y != None, 'gpCLVM:: set Y!'
        assert X0 != None, 'gpCLVM:: set X0!'

        self.cache = {}
        self.interaction = interaction

        # read data
        self._setY(Y, standardize=standardize)
        self._setX0(X0)
        self.k = k

        # covariance for known latex factor
        self.C0 = limix.CFixedCF(self.K0)

        # covariance for unknow latent factor
        self.C = limix.CProductCF()
        self.Ca = limix.CLowRankCF(self.N, self.k)
        self.C.addCovariance(self.Ca)
        if self.interaction == True:
            self.Cb1 = limix.CFixedCF(SP.ones((self.N, self.N)))
            self.Cb1.setParamMask(SP.zeros(1))
            self.Cb2 = limix.CFixedCF(self.K0)
            self.Cb = limix.CSumCF()
            self.Cb.addCovariance(self.Cb1)
            self.Cb.addCovariance(self.Cb2)
            self.C.addCovariance(self.Cb)

        # total covariance
        covar = limix.CSumCF()
        covar.addCovariance(self.C0)
        covar.addCovariance(self.C)

        # likelihood
        self.ll = limix.CLikNormalIso()

        # init GP and hyper params
        self.gp = limix.CGPbase(covar, self.ll)
        self.gp.setY(self.Y)
コード例 #2
0
 def setUp(self):
     SP.random.seed(1)
     self.n = 4
     self.rank = 1
     d = SP.rand(self.n) + 1
     C0 = dlimix_legacy.CSumCF()
     C0.addCovariance(dlimix_legacy.CLowRankCF(self.n, self.rank))
     C0.addCovariance(dlimix_legacy.CDiagonalCF(self.n))
     self.C = dlimix_legacy.CFixedDiagonalCF(C0, d)
     self.name = 'CFixedDiagonalCF'
     self.n_params = self.C.getNumberParams()
     params = SP.randn(self.n_params)
     self.C.setParams(params)
     """
コード例 #3
0
 def setUp(self):
     SP.random.seed(1)
     self.n = 10
     n_dim1 = 8
     n_dim2 = 12
     self.C = dlimix_legacy.CSumCF()
     self.C.addCovariance(dlimix_legacy.CCovSqexpARD(n_dim1))
     self.C.addCovariance(dlimix_legacy.CCovLinearARD(n_dim2))
     self.n_dim = self.C.getNumberDimensions()
     X = SP.rand(self.n, self.n_dim)
     self.C.setX(X)
     self.name = 'CSumCF'
     self.n_params = self.C.getNumberParams()
     params = SP.exp(SP.randn(self.n_params))
     self.C.setParams(params)
コード例 #4
0
 def setUp(self):
     SP.random.seed(1)
     self.n = 10
     n_dim2 = 12
     K0 = SP.eye(self.n)
     self.C = dlimix_legacy.CSumCF()
     #sum of fixed CF and linearARD
     covar1 = dlimix_legacy.CFixedCF(K0)
     covar2 = dlimix_legacy.CCovLinearARD(n_dim2)
     self.C.addCovariance(covar1)
     self.C.addCovariance(covar2)
     self.n_dim = self.C.getNumberDimensions()
     self.X = SP.rand(self.n, self.n_dim)
     self.C.setX(self.X)
     self.name = 'CSumCF'
     self.n_params = self.C.getNumberParams()
     params = SP.exp(SP.randn(self.n_params))
     self.C.setParams(params)
コード例 #5
0
    def setUp(self):
        SP.random.seed(1)

        #1. simulate
        self.settings = {'K': 5, 'N': 100, 'D': 80}
        self.simulation = self.simulate()

        N = self.settings['N']
        K = self.settings['K']
        D = self.settings['D']

        #2. setup GP
        K0 = SP.dot(self.simulation['S'], self.simulation['S'].T)
        K0[:] = 0

        covar1 = dlimix_legacy.CFixedCF(K0)
        covar2 = dlimix_legacy.CCovLinearISO(K)
        covar = dlimix_legacy.CSumCF()
        covar.addCovariance(covar1)
        covar.addCovariance(covar2)

        ll = dlimix_legacy.CLikNormalIso()
        #create hyperparm
        covar_params = SP.array([0.0, 1.0])
        lik_params = SP.array([0.1])
        hyperparams = dlimix_legacy.CGPHyperParams()
        hyperparams['covar'] = covar_params
        hyperparams['lik'] = lik_params
        hyperparams['X'] = self.simulation['X0']
        #cretae GP
        self.gp = dlimix_legacy.CGPbase(covar, ll)
        #set data
        self.gp.setY(self.simulation['Y'])
        self.gp.setX(self.simulation['X0'])
        self.gp.setParams(hyperparams)
        pass
コード例 #6
0
    def train(self, rank=20, Kpop=True, LinearARD=False):
        """train panama module"""

        if 0:
            covar = limix_legacy.CCovLinearISO(rank)
            ll = limix_legacy.CLikNormalIso()
            X0 = sp.random.randn(self.N, rank)
            X0 = PCA(self.Y, rank)[0]
            X0 /= sp.sqrt(rank)
            covar_params = sp.array([1.0])
            lik_params = sp.array([1.0])

            hyperparams = limix_legacy.CGPHyperParams()
            hyperparams['covar'] = covar_params
            hyperparams['lik'] = lik_params
            hyperparams['X'] = X0

            constrainU = limix_legacy.CGPHyperParams()
            constrainL = limix_legacy.CGPHyperParams()
            constrainU['covar'] = +5 * sp.ones_like(covar_params)
            constrainL['covar'] = 0 * sp.ones_like(covar_params)
            constrainU['lik'] = +5 * sp.ones_like(lik_params)
            constrainL['lik'] = 0 * sp.ones_like(lik_params)

        if 1:
            covar = limix_legacy.CSumCF()
            if LinearARD:
                covar_1 = limix_legacy.CCovLinearARD(rank)
                covar_params = []
                for d in range(rank):
                    covar_params.append(1 / sp.sqrt(d + 2))
            else:
                covar_1 = limix_legacy.CCovLinearISO(rank)
                covar_params = [1.0]
            covar.addCovariance(covar_1)

            for K in self.Ks:
                covar.addCovariance(limix_legacy.CFixedCF(K))
                covar_params.append(1.0)

            ll = limix_legacy.CLikNormalIso()
            X0 = PCA(self.Y, rank)[0]
            X0 /= sp.sqrt(rank)
            covar_params = sp.array(covar_params)
            lik_params = sp.array([1.0])

            hyperparams = limix_legacy.CGPHyperParams()
            hyperparams['covar'] = covar_params
            hyperparams['lik'] = lik_params
            hyperparams['X'] = X0

            constrainU = limix_legacy.CGPHyperParams()
            constrainL = limix_legacy.CGPHyperParams()
            constrainU['covar'] = +5 * sp.ones_like(covar_params)
            constrainL['covar'] = -5 * sp.ones_like(covar_params)
            constrainU['lik'] = +5 * sp.ones_like(lik_params)

        gp = limix_legacy.CGPbase(covar, ll)
        gp.setY(self.Y)
        gp.setX(X0)
        lml0 = gp.LML(hyperparams)
        dlml0 = gp.LMLgrad(hyperparams)
        gpopt = limix_legacy.CGPopt(gp)
        gpopt.setOptBoundLower(constrainL)
        gpopt.setOptBoundUpper(constrainU)

        t1 = time.time()
        gpopt.opt()
        t2 = time.time()

        #Kpanama
        self.Xpanama = covar_1.getX()
        if LinearARD:
            self.Xpanama /= self.Xpanama.std(0)
        self.Kpanama = covar_1.K()
        #        self.Kpanama/= self.Kpanama.diagonal().mean()

        # Ktot
        self.Ktot = covar_1.K()
        for c_i in range(len(self.Ks)):
            self.Ktot += covar.getCovariance(c_i + 1).K()
#       self.Ktot/= self.Ktot.diagonal().mean()

#store variances
        V = {}
        if LinearARD:
            V['LinearARD'] = covar_1.getParams()**2 * covar_1.getX().var(0)
        else:
            V['Kpanama'] = sp.array([covar_1.K().diagonal().mean()])
#        if self.use_Kpop:
#            V['Ks'] = sp.array([covar.getCovariance(c_i+1).K().diagonal().mean() for c_i in range(len(self.Ks))])
        V['Ks'] = sp.array(
            [covar.getCovariance(c_i + 1).K() for c_i in range(len(self.Ks))])
        V['noise'] = gp.getParams()['lik']**2
        self.varianceComps = V

        # predictions
        Ki = la.inv(ll.K() + covar.K())
        self.Ypanama = sp.dot(covar_1.K(), sp.dot(Ki, self.Y))
        self.LL = ll.K()
コード例 #7
0
    def _buildTraitCovar(self,
                         trait_covar_type='lowrank_diag',
                         rank=1,
                         fixed_trait_covar=None,
                         jitter=1e-4,
                         d=None):
        """
        Internal functions that builds the trait covariance matrix using the LIMIX framework

        Args:
            trait_covar_type: type of covaraince to use. Default 'freeform'. possible values are
            rank:       rank of a possible lowrank component (default 1)
            fixed_trait_covar:   PxP matrix for the (predefined) trait-to-trait covariance matrix if fixed type is used
            jitter:        diagonal contribution added to trait-to-trait covariance matrices for regularization
        Returns:
            LIMIX::PCovarianceFunction for Trait covariance matrix
            vector labelling Cholesky parameters for different initializations
        """
        cov = dlimix_legacy.CSumCF()
        if trait_covar_type == 'freeform':
            cov.addCovariance(dlimix_legacy.CFreeFormCF(self.P))
            L = sp.eye(self.P)
            diag = sp.concatenate([L[i, :(i + 1)] for i in range(self.P)])
        elif trait_covar_type == 'fixed':
            assert fixed_trait_covar is not None, 'VarianceDecomposition:: set fixed_trait_covar'
            assert fixed_trait_covar.shape[
                0] == self.N, 'VarianceDecomposition:: Incompatible shape for fixed_trait_covar'
            assert fixed_trait_covar.shape[
                1] == self.N, 'VarianceDecomposition:: Incompatible shape for fixed_trait_covar'
            cov.addCovariance(dlimix_legacy.CFixedCF(fixed_trait_covar))
            diag = sp.zeros(1)
        elif trait_covar_type == 'diag':
            cov.addCovariance(dlimix_legacy.CDiagonalCF(self.P))
            diag = sp.ones(self.P)
        elif trait_covar_type == 'lowrank':
            cov.addCovariance(dlimix_legacy.CLowRankCF(self.P, rank))
            diag = sp.zeros(self.P * rank)
        elif trait_covar_type == 'lowrank_id':
            cov.addCovariance(dlimix_legacy.CLowRankCF(self.P, rank))
            cov.addCovariance(dlimix_legacy.CFixedCF(sp.eye(self.P)))
            diag = sp.concatenate([sp.zeros(self.P * rank), sp.ones(1)])
        elif trait_covar_type == 'lowrank_diag':
            cov.addCovariance(dlimix_legacy.CLowRankCF(self.P, rank))
            cov.addCovariance(dlimix_legacy.CDiagonalCF(self.P))
            diag = sp.concatenate([sp.zeros(self.P * rank), sp.ones(self.P)])
        elif trait_covar_type == 'lowrank_diag1':
            assert d.shape[0] == self.P, 'dimension mismatch for d'
            cov1 = dlimix_legacy.CSumCF()
            cov1.addCovariance(dlimix_legacy.CLowRankCF(self.P, rank))
            cov1.addCovariance(dlimix_legacy.CDiagonalCF(self.P))
            cov.addCovariance(dlimix_legacy.CFixedDiagonalCF(cov1, d))
            diag = sp.concatenate([sp.zeros(self.P * rank), sp.ones(self.P)])
        elif trait_covar_type == 'freeform1':
            assert d.shape[0] == self.P, 'dimension mismatch for d'
            cov.addCovariance(
                dlimix_legacy.CFixedDiagonalCF(
                    dlimix_legacy.CFreeFormCF(self.P), d))
            L = sp.eye(self.P)
            diag = sp.concatenate([L[i, :(i + 1)] for i in range(self.P)])
        elif trait_covar_type == 'block':
            cov.addCovariance(dlimix_legacy.CFixedCF(sp.ones(
                (self.P, self.P))))
            diag = sp.zeros(1)
        elif trait_covar_type == 'block_id':
            cov.addCovariance(dlimix_legacy.CFixedCF(sp.ones(
                (self.P, self.P))))
            cov.addCovariance(dlimix_legacy.CFixedCF(sp.eye(self.P)))
            diag = sp.concatenate([sp.zeros(1), sp.ones(1)])
        elif trait_covar_type == 'block_diag':
            cov.addCovariance(dlimix_legacy.CFixedCF(sp.ones(
                (self.P, self.P))))
            cov.addCovariance(dlimix_legacy.CDiagonalCF(self.P))
            diag = sp.concatenate([sp.zeros(1), sp.ones(self.P)])
        else:
            assert True == False, 'VarianceDecomposition:: trait_covar_type not valid'
        if jitter > 0:
            _cov = dlimix_legacy.CFixedCF(sp.eye(self.P))
            _cov.setParams(sp.array([sp.sqrt(jitter)]))
            _cov.setParamMask(sp.zeros(1))
            cov.addCovariance(_cov)
        return cov, diag